From dbf762f1f74ecaa70c713233422a8553cf08e144 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Fri, 24 Jul 2020 07:21:06 +0200
Subject: [PATCH] software/slapos-sr-testing: generate an environment script

Using an script to set the environment is easier to use, but the main
reason is that if we want to be able to use shared part from outer
slapos (like when using in theia or slaprunner), the working directory
for test must be contained in the instance directory of the outer
slapos, so that when we run slapos node prune in the outer slapos this
command can see that shared parts are used.
---
 software/slapos-sr-testing/README.md         | 33 ++++++++---------
 software/slapos-sr-testing/buildout.hash.cfg |  2 +-
 software/slapos-sr-testing/instance.cfg      | 39 ++++++++++++++++++--
 software/slapos-sr-testing/software.cfg      |  4 ++
 4 files changed, 56 insertions(+), 22 deletions(-)

diff --git a/software/slapos-sr-testing/README.md b/software/slapos-sr-testing/README.md
index d82b41516..bffd5226b 100644
--- a/software/slapos-sr-testing/README.md
+++ b/software/slapos-sr-testing/README.md
@@ -16,40 +16,36 @@ slaprunner to develop a slapos profile, in the example `helloworld`, make
 changes to the code, run tests and publish changes.
 
 ```bash
-# install this software release
+# install this software release and request an instance
 SR=https://lab.nexedi.com/nexedi/slapos/raw/1.0/software/slapos-sr-testing/software.cfg
-COMP=slaprunner
+COMP=slaprunner # or "local" if using theia
 INSTANCE_NAME=$COMP
 
 slapos supply $SR $COMP
 slapos node software
-slapos request --node=node=$COMP $INSTANCE_NAME $SR
+slapos request --node=computer_guid=$COMP $INSTANCE_NAME $SR
 slapos node instance
 
+# note the `environment-script` published value
+slapos request --node=computer_guid=$COMP $INSTANCE_NAME $SR
+# and load this script to set environment variables
+source ( environment-script from step above )
+
 # Clone a working copy somewhere
 cd ~/srv/runner/project/
 git clone https://lab.nexedi.com/nexedi/slapos.git slapos_work
 
-# Create a directory to hold the slapos test environment
-SLAPOS_TEST_WORKING_DIR=~/tmp/slapos/
-mkdir -p $SLAPOS_TEST_WORKING_DIR
-
 # change directory to the directory containing test for this software
 cd ~/srv/runner/project/slapos_work/software/helloworld/test/
 
-# run tests, using bundled python intepreter with pre-installed eggs dependencies.
-SLAPOS_TEST_IPV6=::1 \
-SLAPOS_TEST_IPV4=127.0.0.1 \
-SLAPOS_TEST_WORKING_DIR=$SLAPOS_TEST_WORKING_DIR \
-SLAPOS_TEST_SHARED_PART_LIST=/opt/slapos/shared/:~/srv/runner/shared \
-SLAPOS_TEST_VERBOSE=1 \
-SLAPOS_TEST_DEBUG=1 \
-~/srv/runner/instance/slappart0/software_release/bin/python_for_test setup.py test
-
+# run test (with debugging features activated)
+SLAPOS_TEST_DEBUG= 1 python_for_test setup.py test
 ```
 
 ## Environment variables
 
+The `environment-script` set all variabels except `SLAPOS_TEST_DEBUG` and `SLAPOS_TEST_VERBOSE` for you, but for reference, here is the list of variables which control the test runner:
+
 | Variable | Description |
 | --- | --- |
 | `SLAPOS_TEST_IPV6` | ipv6 used by this instance. Usually you want to use a global address here to be able to connect to this instance. |
@@ -82,4 +78,7 @@ To use a development version of `slapos.cookbook` on test nodes, you can try usi
 
 ### Test pass locally but fail on test nodes, what can I do ?
 
-At the end of the test, a snapshot of the slapos instances is created. Sometimes examining the log files can help understanding what went wrong.
\ No newline at end of file
+At the end of the test, a snapshot of the slapos instances is created. Sometimes examining the log files can help understanding what went wrong.
+
+Most of the time, problems are because on test nodes paths are very long. One advanced technique to reproduce the problem in your development environment is to set `SLAPOS_TEST_WORKING_DIR` environment variable to a path with the same length as the ones on test nodes.
+One way to make instances uses a slightly shorter path is to define `__partition_reference__` class attribute, so that the instances uses this as prefix instead of the class name.
diff --git a/software/slapos-sr-testing/buildout.hash.cfg b/software/slapos-sr-testing/buildout.hash.cfg
index e65935529..53fc5d3d2 100644
--- a/software/slapos-sr-testing/buildout.hash.cfg
+++ b/software/slapos-sr-testing/buildout.hash.cfg
@@ -15,4 +15,4 @@
 
 [template]
 filename = instance.cfg
-md5sum = 298bac4a631de3b30593b9a1dcf63e1c
+md5sum = 25a4d7e438402d992edadf9339faf557
diff --git a/software/slapos-sr-testing/instance.cfg b/software/slapos-sr-testing/instance.cfg
index a452c4eab..d6400cea0 100644
--- a/software/slapos-sr-testing/instance.cfg
+++ b/software/slapos-sr-testing/instance.cfg
@@ -1,6 +1,7 @@
 [buildout]
 parts =
   slapos-test-runner
+  publish
 
 eggs-directory = {{ buildout['eggs-directory'] }}
 develop-eggs-directory = {{ buildout['develop-eggs-directory'] }}
@@ -26,6 +27,7 @@ repository = {{ slapos_location }}
 recipe = slapos.cookbook:mkdirectory
 bin = ${buildout:directory}/bin
 working-dir = ${buildout:directory}/tmp
+etc = ${buildout:directory}/etc
 
 [slapos-test-runner]
 recipe = slapos.cookbook:wrapper
@@ -36,7 +38,36 @@ command-line =
   --source_code_path_list={{ ','.join(tests.splitlines()) }}
 
 environment =
-  PATH={{ buildout['bin-directory'] }}:{{ curl_location }}/bin/:/usr/bin/:/bin
-  SLAPOS_TEST_IPV4=${slap-configuration:ipv4-random}
-  SLAPOS_TEST_IPV6=${slap-configuration:ipv6-random}
-  SLAPOS_TEST_WORKING_DIR=${directory:working-dir}
+  PATH=${slapos-test-runner-environment:PATH}
+  SLAPOS_TEST_IPV4=${slapos-test-runner-environment:SLAPOS_TEST_IPV4}
+  SLAPOS_TEST_IPV6=${slapos-test-runner-environment:SLAPOS_TEST_IPV6}
+  SLAPOS_TEST_WORKING_DIR=${slapos-test-runner-environment:SLAPOS_TEST_WORKING_DIR}
+
+[slapos-test-runner-environment]
+PATH = {{ buildout['bin-directory'] }}:{{ curl_location }}/bin/:/usr/bin/:/bin
+SLAPOS_TEST_IPV4 = ${slap-configuration:ipv4-random}
+SLAPOS_TEST_IPV6 = ${slap-configuration:ipv6-random}
+SLAPOS_TEST_WORKING_DIR = ${directory:working-dir}
+
+[slapos-local-development-environment.sh]
+recipe = slapos.recipe.template:jinja2
+rendered = ${directory:etc}/${:_buildout_section_name_}
+template = inline:
+  export PATH=${slapos-test-runner-environment:PATH}:$PATH
+  export SLAPOS_TEST_IPV4=${slapos-test-runner-environment:SLAPOS_TEST_IPV4}
+  export SLAPOS_TEST_IPV6=${slapos-test-runner-environment:SLAPOS_TEST_IPV6}
+  export SLAPOS_TEST_WORKING_DIR=${slapos-test-runner-environment:SLAPOS_TEST_WORKING_DIR}
+  {% 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 "To work on a test, execute:"
+  echo "   SLAPOS_TEST_DEBUG=1 {{ interpreter }} setup.py test"
+  echo "from test folder"
+  echo
+
+[publish]
+recipe = slapos.cookbook:publish
+environment-script = ${slapos-local-development-environment.sh:rendered}
diff --git a/software/slapos-sr-testing/software.cfg b/software/slapos-sr-testing/software.cfg
index a27200bb3..97cf9ec31 100644
--- a/software/slapos-sr-testing/software.cfg
+++ b/software/slapos-sr-testing/software.cfg
@@ -19,6 +19,8 @@ parts =
   slapos-cookbook
   template
 
+shared-part-list =
+
 [setup-develop-egg]
 recipe = zc.recipe.egg:develop
 
@@ -227,10 +229,12 @@ branch = master
 [template]
 recipe = slapos.recipe.template:jinja2
 template = ${:_profile_base_location_}/${:filename}
+extensions = jinja2.ext.do
 rendered = ${buildout:directory}/template.cfg
 mode = 640
 context =
   section buildout buildout
+  import os os
   key git_location git:location
   key slapos_location slapos-repository:location
   key interpreter eggs:interpreter
-- 
2.30.9