[buildout]
eggs-directory = ${buildout:eggs-directory}
develop-eggs-directory = ${buildout:develop-eggs-directory}

extends =
  ${nxdtest-instance.cfg:output}

parts =
  .nxdtest

[slap-configuration]
recipe = slapos.cookbook:slapconfiguration.serialised
computer = $${slap-connection:computer-id}
partition = $${slap-connection:partition-id}
url = $${slap-connection:server-url}
key = $${slap-connection:key-file}
cert = $${slap-connection:cert-file}


[directory]
recipe = slapos.cookbook:mkdirectory
home = $${buildout:directory}
bin = $${buildout:directory}/bin
etc = $${buildout:directory}/etc
var = $${buildout:directory}/var
cfg = $${buildout:directory}/.slapos
nxdtestdir = $${:var}/nxdtest
log = $${:var}/log


[client.crt]
recipe = slapos.recipe.template
output = $${directory:cfg}/client.crt
inline = $${slap-configuration:configuration.client.crt}


[client.key]
recipe = slapos.recipe.template
output = $${directory:cfg}/client.key
inline = $${slap-configuration:configuration.client.key}


[slapos-client.cfg]
recipe = slapos.recipe.template
output = $${directory:cfg}/slapos-client.cfg
inline =
  [slapos]
  master_url = $${slap-configuration:configuration.master-url}

  [slapconsole]
  cert_file = $${client.crt:output}
  key_file = $${client.key:output}


[env.sh]
recipe = slapos.recipe.template:jinja2
output = $${directory:cfg}/env.sh
inline =
  export HOME=$${directory:home}


[runTestSuite]
# extended from stack/nxdtest
env.sh = $${env.sh:output}
workdir = $${directory:nxdtestdir}


[.nxdtest]
recipe = slapos.recipe.template:jinja2
output = $${runTestSuite:workdir}/.nxdtest
python_for_test = ${python_for_test:executable}
testdir = $${tests:location}
log-output = $${directory:log}/e2e-testing.log
context =
  key python_for_test :python_for_test
  key testdir :testdir
  key slapos_cfg slapos-client.cfg:output
  key log_file :log-output
inline =
  import os
  directory = "{{ testdir }}"
  slapos_cfg = {{ repr(slapos_cfg) }}
  log_file = {{ repr(log_file) }}
  dir_list = os.listdir(directory)
  for filename in dir_list:
    name, ext = os.path.splitext(filename)
    if name.startswith('test') and ext == '.py':
      TestCase(
        name,
        [{{ repr(python_for_test) }} , '-m', 'unittest',  '-v', name],
        cwd=directory,
        env={'SLAPOS_E2E_TEST_CLIENT_CFG': slapos_cfg,
             'SLAPOS_E2E_TEST_LOG_FILE': log_file},
        summaryf=UnitTest.summary,
      )

[tests]
recipe = slapos.recipe.build
tests = $${slap-configuration:configuration.tests}
location = $${directory:var}/tests
install =
  import os
  os.mkdir(location)
  buildout_offline = self.buildout['buildout']['offline']
  try:
    # Allow to do self.download() which can only be used in "online" mode
    self.buildout['buildout']['offline'] = 'false'
    for i, test in enumerate(options['tests']):
      tmp = self.download(test['url'], test['md5sum'])
      path = os.path.join(location, 'test%s.py' % i)
      os.rename(tmp, path)
  finally:
    # reset the parameter
    self.buildout['buildout']['offline'] = buildout_offline
