[buildout]
extends = {{ nxdtest_instance }}
parts +=
  publish

[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}

[download-source]
recipe = slapos.recipe.build:gitclone
git-executable = {{ git_location }}/bin/git

[slapos]
<= download-source
repository = {{ slapos_location }}

[directory]
recipe = slapos.cookbook:mkdirectory
bin = ${buildout:directory}/bin
etc = ${buildout:directory}/etc
var = ${buildout:directory}/var
test-working-dir = ${buildout:directory}/tmp
nxdtest-working-dir = ${:var}/nxdtest

[slapos-test-runner-nxdtest-environment.sh]
recipe = slapos.recipe.template:jinja2
output = ${directory:etc}/${:_buildout_section_name_}
PATH={{ buildout['bin-directory'] }}:{{ curl_location }}/bin:{{ faketime_location }}/bin:{{ openssl_location }}/bin:/usr/bin:/bin
context =
  section slap slap-configuration
  key ipv6_random slap-configuration:ipv6-random
{%- raw %}
inline =
  export PATH=${:PATH}
  export SLAPOS_TEST_IPV4=${slap-configuration:ipv4-random}
  export SLAPOS_TEST_IPV6={{ slap.get('ipv6-range-network') or ipv6_random }}
  export SLAPOS_TEST_WORKING_DIR=${directory:test-working-dir}
{%- endraw %}


[slapos-test-runner-dot-nxdtest]
recipe = slapos.recipe.template:jinja2
output = ${:workdir}/.nxdtest
workdir = ${directory:nxdtest-working-dir}
tests = {{ tests | indent(2) }}
inline =
  {% raw %}
  {%- set only_sr = slapparameter_dict.get('only-sr') %}
  {%- if only_sr and not isinstance(only_sr, list) %}
  {%-   set only_sr = [only_sr] %}
  {%- endif %}
  {%- set unittest_args = slapparameter_dict.get('unittest-args', ['discover', '-v']) %}
  {%- if not isinstance(unittest_args, list) %}
  {%-   set unittest_args = [unittest_args] %}
  {%- endif %}
  command = {{ repr([interpreter, '-m', 'unittest'] + unittest_args) }}
  {%- for test in tests.splitlines() %}
  {%-   set name, folder = test.split() %}
  {%-   if not only_sr or name in only_sr %}
  TestCase(
    {{ repr(name) }},
    command,
    cwd={{ repr(folder) }},
    summaryf=UnitTest.summary,
  )
  {%-   endif %}
  {%- endfor %}
  {%- endraw %}
context =
  key slapparameter_dict slap-configuration:configuration
  key tests :tests
  raw interpreter {{ interpreter }}

[runTestSuite]
env.sh = ${slapos-test-runner-nxdtest-environment.sh:output}
workdir = ${slapos-test-runner-dot-nxdtest:workdir}

[slapos-local-development-environment.sh]
recipe = slapos.recipe.template
output = ${directory:etc}/${:_buildout_section_name_}
inline =
  source ${slapos-test-runner-nxdtest-environment.sh:output}
  {% set shared_part_list = [] %}
  {% for shared_part in buildout['shared-part-list'].splitlines() -%}
    {% do shared_part_list.append(shared_part) %}
  {%- endfor %}
  export SLAPOS_TEST_SHARED_PART_LIST={{ os.pathsep.join(shared_part_list) }}
  echo "Environment loaded."
  echo "The embedded SlapOS is in $SLAPOS_TEST_WORKING_DIR"
  echo "To work on a test, execute:"
  echo "   SLAPOS_TEST_DEBUG=1 {{ interpreter }} -m unittest discover -v"
  echo "from a folder containing software release test."
  echo "Tip: you may want to also add"
  echo "   SLAPOS_TEST_SKIP_SOFTWARE_REBUILD=1"
  echo "   SLAPOS_TEST_SKIP_SOFTWARE_CHECK=1"
  echo "   SLAPOS_TEST_LOG_DIRECTORY=<some-empty-directory>"
  echo "See https://lab.nexedi.com/nexedi/slapos/tree/master/software/slapos-sr-testing"
  echo

[publish]
recipe = slapos.cookbook:publish
environment-script = ${slapos-local-development-environment.sh:output}
