From d40f72c5dfee558923544edcc9f49610daed90ba Mon Sep 17 00:00:00 2001
From: Maarten de Waard <maarten@greenhost.nl>
Date: Thu, 29 Aug 2019 16:40:57 +0200
Subject: [PATCH] process some feedback, by default run all tests with test
 command

---
 .gitignore                                  |  2 --
 .gitlab-ci.yml                              | 20 ++++++++---------
 ansible/group_vars/all/settings.yml.example |  2 +-
 openappstack/__main__.py                    | 25 +++++++++++++++------
 test/requirements.txt => requirements.txt   |  0
 test/Dockerfile                             |  2 +-
 6 files changed, 29 insertions(+), 22 deletions(-)
 rename test/requirements.txt => requirements.txt (100%)

diff --git a/.gitignore b/.gitignore
index 3d7bff39e..88b8e1aba 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,7 +6,6 @@
 
 # Ignore files created by ansible-playbook
 *.retry
-/ansible/cluster_data/
 
 # Virtualenvs for the project
 /env
@@ -18,7 +17,6 @@
 /test/inventory*
 /test/behave/behave.ini
 /test/behave/rerun_failing.features
-/test/cluster_data/
 /clusters
 
 # Ignore files created during tests
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index d1ab2ac02..0736a5afd 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -24,7 +24,7 @@ ci_test_image:
   only:
     changes:
       - test/Dockerfile
-      - test/requirements.txt
+      - requirements.txt
 
 bootstrap:
   stage: setup-cluster
@@ -36,8 +36,8 @@ bootstrap:
     - chmod 755 ansible/
     - eval $(ssh-agent -s)
     - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
-    - python3 -m openappstack $CI_PIPELINE_ID create --create-droplet openappstack.net --hostname "ci-$CI_PIPELINE_ID" --ssh-key-id 411 --create-domain-records --subdomain "ci-$CI_PIPELINE_ID.ci" 
-    - python3 -m openappstack $CI_PIPELINE_ID install --ansible-param skip-tags=helmfile
+    - python3 -m openappstack ci-$CI_PIPELINE_ID create --create-droplet openappstack.net --hostname "ci-$CI_PIPELINE_ID" --ssh-key-id 411 --create-domain-records --subdomain "ci-$CI_PIPELINE_ID.ci" 
+    - python3 -m openappstack ci-$CI_PIPELINE_ID install --ansible-param skip-tags=helmfile
   artifacts:
     paths:
     - ./clusters
@@ -61,7 +61,7 @@ install:
     - chmod 755 ansible/
     - eval $(ssh-agent -s)
     - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
-    - python3 -m openappstack $CI_PIPELINE_ID install --ansible-param tags=helmfile 
+    - python3 -m openappstack ci-$CI_PIPELINE_ID install --ansible-param tags=helmfile 
     # Show versions of installed apps/binaries
     - ansible master -m shell -a 'oas-version-info.sh 2>&1'
   artifacts:
@@ -82,7 +82,7 @@ testinfra:
     - eval $(ssh-agent -s)
     - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
     - cd test/
-    - py.test -v -m 'testinfra' --connection=ansible --ansible-inventory=./clusters/$CI_PIPELINE_ID/inventory.yml --hosts='ansible://*'
+    - py.test -v -m 'testinfra' --connection=ansible --ansible-inventory=../clusters/ci-$CI_PIPELINE_ID/inventory.yml --hosts='ansible://*'
   only:
     changes:
       - ansible/**/*
@@ -100,7 +100,7 @@ certs:
     - eval $(ssh-agent -s)
     - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
     - cd test/
-    - py.test -s -m 'certs' --connection=ansible --ansible-inventory=./clusters/$CI_PIPELINE_ID/inventory.yml --hosts='ansible://*'
+    - py.test -s -m 'certs' --connection=ansible --ansible-inventory=../clusters/ci-$CI_PIPELINE_ID/inventory.yml --hosts='ansible://*'
   only:
     changes:
       - ansible/**/*
@@ -112,8 +112,7 @@ certs:
 behave-nextcloud:
   stage: integration-test
   script:
-    # Debug failing CI caches which happened in the past
-    - python -m openappstack $CI_PIPELINE_ID test --behave --behave-headless --behave-tags nextcloud || python -m openappstack $CI_PIPELINE_ID test --behave --behave-headless --behave-rerun-failing --behave-tags nextcloud
+    - python3 -m openappstack ci-$CI_PIPELINE_ID test --behave --behave-headless --behave-tags nextcloud || python3 -m openappstack ci-$CI_PIPELINE_ID test --behave --behave-headless --behave-rerun-failing --behave-tags nextcloud
   artifacts:
     paths:
     - test/behave/screenshots/
@@ -131,8 +130,7 @@ behave-nextcloud:
 behave-grafana:
   stage: integration-test
   script:
-    # Debug failing CI caches which happened in the past
-    - python -m openappstack $CI_PIPELINE_ID test --behave --behave-headless --behave-tags grafana || python -m openappstack $CI_PIPELINE_ID test --behave --behave-headless --behave-rerun-failing --behave-tags grafana
+    - python3 -m openappstack ci-$CI_PIPELINE_ID test --behave --behave-headless --behave-tags grafana || python3 -m openappstack ci-$CI_PIPELINE_ID test --behave --behave-headless --behave-rerun-failing --behave-tags grafana
   artifacts:
     paths:
     - test/behave/screenshots/
@@ -150,7 +148,7 @@ terminate:
   stage: cleanup
   script:
     # Remove droplet after successful tests
-    - echo "$CI_COMMIT_MESSAGE" | grep '!ci_dont_terminate' && echo 'Termination of droplet disabled in commit message.' || python3 -m openappstack $CI_PIPELINE_ID --terminate
+    - echo "$CI_COMMIT_MESSAGE" | grep '!ci_dont_terminate' && echo 'Termination of droplet disabled in commit message.' || python3 -m openappstack ci-$CI_PIPELINE_ID --terminate
     # Remove droplet older than 2 days
     - python3 -c "import openappstack.cosmos; openappstack.cosmos.terminate_droplets_by_name(\"^ci-\", 2)"
   only:
diff --git a/ansible/group_vars/all/settings.yml.example b/ansible/group_vars/all/settings.yml.example
index 882b14267..2f77f497e 100644
--- a/ansible/group_vars/all/settings.yml.example
+++ b/ansible/group_vars/all/settings.yml.example
@@ -13,7 +13,7 @@ release_name: "test"
 # Important: Don't quote this variable !
 acme_staging: false
 # Directory where we save all the passwords etc.
-cluster_dir: "./clusters/example-clusterrname/"
+cluster_dir: "./clusters/example-clustername/"
 # Which apps to install from the helmfile.d/ dir
 helmfiles:
   - 00-storage
diff --git a/openappstack/__main__.py b/openappstack/__main__.py
index d31744941..f17154814 100755
--- a/openappstack/__main__.py
+++ b/openappstack/__main__.py
@@ -17,7 +17,7 @@ Install requirements:
       openssl-dev openssh-client
     pip3 install -r requirements.txt
 
-- Apline using packages (much faster):
+- Alpine using packages (much faster):
 
     apk --no-cache add ansible musl-dev linux-headers gcc py3-psutil \
       openssh-client
@@ -38,6 +38,7 @@ import sys
 from behave.__main__ import main as behave_main
 from openappstack import name, cluster, cosmos, ansible
 
+ALL_TESTS = ['behave']
 
 
 def main():  # pylint: disable=too-many-statements,too-many-branches,too-many-locals
@@ -88,7 +89,8 @@ def main():  # pylint: disable=too-many-statements,too-many-branches,too-many-lo
         '--ssh-key-id',
         metavar='ID',
         type=int,
-        help='ID of ssh key to deploy with (default: 411)')
+        help=('Greenhost-specific database ID of ssh key to deploy with'
+              '(default: 411)'))
 
     droplet_creation_group.add_argument(
         '--create-domain-records',
@@ -140,9 +142,12 @@ def main():  # pylint: disable=too-many-statements,too-many-branches,too-many-lo
     test_parser.set_defaults(func=test)
 
     test_parser.add_argument(
-        '--behave',
-        action='store_true',
-        help='Runs "behave" tests. NOTE: This overrides behave.ini!')
+        '--run-test',
+        action='append',
+        help=('Run only a specific kind of test. If not provided, all tests '
+              'are run. The currently available tests are: ') \
+            + ','.join(ALL_TESTS))
+
     test_parser.add_argument(
         '--behave-rerun-failing',
         action='store_true',
@@ -237,7 +242,14 @@ def install(clus, args):
 
 def test(clus, args):
     """Runs behave or testinfra test. Overwrites behave_path/behave.ini!"""
-    if args.behave:
+
+    # At the moment we only have one type if test, but more tests will be added
+    # to this in the future. If args.run_test is empty, we run all the tests
+    run_test = args.run_test
+    if not run_test:
+        run_test = ALL_TESTS
+
+    if 'behave' in run_test:
         behave_path = os.path.join(os.path.dirname(__file__), '..', 'test',
                                    'behave')
         # Run from the behave directory so behave automatically loads all the
@@ -248,7 +260,6 @@ def test(clus, args):
         command = []
         if args.behave_rerun_failing:
             command.append("@rerun_failing.features")
-        log.info(args.behave_tags)
         if args.behave_headless:
             command.append("-D headless=True")
         for tag in args.behave_tags:
diff --git a/test/requirements.txt b/requirements.txt
similarity index 100%
rename from test/requirements.txt
rename to requirements.txt
diff --git a/test/Dockerfile b/test/Dockerfile
index db990ab4f..c4572df58 100644
--- a/test/Dockerfile
+++ b/test/Dockerfile
@@ -20,6 +20,6 @@ RUN apk --no-cache add \
   python3-dev \
   rsync
 
-COPY ./requirements.txt /requirements.txt
+COPY ../requirements.txt /requirements.txt
 RUN pip3 install -r /requirements.txt
 RUN ln -s /usr/bin/python3 /usr/bin/python
-- 
GitLab