From 9ffa4ea6c414d8e57981e673b1423c07f3d51c83 Mon Sep 17 00:00:00 2001 From: Norbert Varzariu Date: Tue, 9 Apr 2019 23:31:22 +0200 Subject: [PATCH] update goss verify.yml to allow to pass host to run on (#1646) * update goss verify.yml to allow to pass host to run on Signed-off-by: Norbert Varzariu * update goss version Signed-off-by: Norbert Varzariu * fix sha sum for goss binary Signed-off-by: Norbert Varzariu * fix test verify.yml Signed-off-by: Norbert Varzariu * do not require root for local find Signed-off-by: Norbert Varzariu * use failed_when instead of ignore_errors Signed-off-by: Norbert Varzariu * add docs, fix travis tests, add test scenario to test our changes Signed-off-by: Norbert Varzariu * remove trailing dot from goss sha256sum Signed-off-by: Norbert Varzariu * - fix documentation - add trailing dot to goss bin shasum to follow current style Signed-off-by: Norbert Varzariu * fix doc (add missing empty line to code block) Signed-off-by: Norbert Varzariu * make linter happy Signed-off-by: Norbert Varzariu --- .../{{cookiecutter.scenario_name}}/verify.yml | 49 +++++++++++++------ molecule/verifier/goss.py | 11 +++++ .../verifier/molecule/goss/molecule.yml | 4 +- .../molecule/goss/tests/test_default.yml | 7 --- .../goss/tests/test_host_instance1.yml | 11 +++++ .../goss/tests/test_host_instance2.yml | 11 +++++ .../verifier/molecule/goss/verify.yml | 31 +++++++++--- 7 files changed, 96 insertions(+), 28 deletions(-) create mode 100644 test/scenarios/verifier/molecule/goss/tests/test_host_instance1.yml create mode 100644 test/scenarios/verifier/molecule/goss/tests/test_host_instance2.yml diff --git a/molecule/cookiecutter/scenario/verifier/goss/{{cookiecutter.molecule_directory}}/{{cookiecutter.scenario_name}}/verify.yml b/molecule/cookiecutter/scenario/verifier/goss/{{cookiecutter.molecule_directory}}/{{cookiecutter.scenario_name}}/verify.yml index d9c59713cc..001a306696 100644 --- a/molecule/cookiecutter/scenario/verifier/goss/{{cookiecutter.molecule_directory}}/{{cookiecutter.scenario_name}}/verify.yml +++ b/molecule/cookiecutter/scenario/verifier/goss/{{cookiecutter.molecule_directory}}/{{cookiecutter.scenario_name}}/verify.yml @@ -10,37 +10,58 @@ vars: goss_version: v0.3.6 goss_arch: amd64 - goss_dst: /usr/local/bin/goss - goss_sha256sum: 53dd1156ab66f2c4275fd847372e6329d895cfb2f0bcbec5f86c1c4df7236dde - goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}" - goss_test_directory: /tmp + goss_bin: /usr/local/bin/goss + goss_sha256sum: 53dd1156ab66f2c4275fd847372e6329d895cfb2f0bcbec5f86c1c4df7236dde. + goss_test_directory: /tmp/molecule/goss goss_format: documentation tasks: - name: Download and install Goss get_url: - url: "{{ goss_url }}" - dest: "{{ goss_dst }}" + url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}" + dest: "{{ goss_bin }}" sha256sum: "{{ goss_sha256sum }}" mode: 0755 - register: download_goss - until: download_goss is succeeded - retries: 3 + + - name: Create Molecule directory for test files + file: + path: "{{ goss_test_directory }}" + state: directory + + - name: Find Goss tests on localhost + find: + paths: "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}" + patterns: + - "test[-.\\w]*.yml" + - "test_host_{{ ansible_hostname }}[-.\\w]*.yml" + excludes: + - "test_host_(?!{{ ansible_hostname }})[-.\\w]*.yml" + use_regex: true + delegate_to: localhost + register: test_files + changed_when: false + become: false + + - name: debug + debug: + msg: "{{ test_files.files }}" + verbosity: 3 - name: Copy Goss tests to remote copy: - src: "{{ item }}" - dest: "{{ goss_test_directory }}/{{ item | basename }}" - with_fileglob: - - "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_*.yml" + src: "{{ item.path }}" + dest: "{{ goss_test_directory }}/{{ item.path | basename }}" + with_items: + - "{{ test_files.files }}" - name: Register test files shell: "ls {{ goss_test_directory }}/test_*.yml" register: test_files - name: Execute Goss tests - command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}" + command: "{{ goss_bin }} -g {{ item }} validate --format {{ goss_format }}" register: test_results with_items: "{{ test_files.stdout_lines }}" + failed_when: false - name: Display details about the Goss results debug: diff --git a/molecule/verifier/goss.py b/molecule/verifier/goss.py index 1cbacb3fa4..bfed71ee5e 100644 --- a/molecule/verifier/goss.py +++ b/molecule/verifier/goss.py @@ -74,6 +74,17 @@ class Goss(base.Base): name: goss directory: /foo/bar/ + All files starting with test_* will be copied to all molecule hosts. + Files matching the regular expression `test_host_$instance_name[-.\\w].yml` + will only run on $instance_name. If you have 2 molecule instances, + instance1 and instance2, your test files could look like this: + + .. code-block:: bash + + test_default.yml (will run on all hosts) + test_host_instance1.yml (will run only on instance1) + test_host_instance2.yml (will run only on instance2) + .. important:: Due to the nature of this verifier. Molecule does not perform options diff --git a/test/scenarios/verifier/molecule/goss/molecule.yml b/test/scenarios/verifier/molecule/goss/molecule.yml index b4b9cec87b..ca5494d9fb 100644 --- a/test/scenarios/verifier/molecule/goss/molecule.yml +++ b/test/scenarios/verifier/molecule/goss/molecule.yml @@ -8,7 +8,9 @@ lint: options: config-file: ../../resources/.yamllint platforms: - - name: instance + - name: instance1 + image: centos:latest + - name: instance2 image: centos:latest provisioner: name: ansible diff --git a/test/scenarios/verifier/molecule/goss/tests/test_default.yml b/test/scenarios/verifier/molecule/goss/tests/test_default.yml index da507c4a80..faf0df55cf 100644 --- a/test/scenarios/verifier/molecule/goss/tests/test_default.yml +++ b/test/scenarios/verifier/molecule/goss/tests/test_default.yml @@ -8,10 +8,3 @@ file: group: root mode: "0755" filetype: directory - /etc/molecule/instance: - exists: true - owner: root - group: root - mode: "0644" - filetype: file - contains: ['instance'] diff --git a/test/scenarios/verifier/molecule/goss/tests/test_host_instance1.yml b/test/scenarios/verifier/molecule/goss/tests/test_host_instance1.yml new file mode 100644 index 0000000000..3c8b8c4b94 --- /dev/null +++ b/test/scenarios/verifier/molecule/goss/tests/test_host_instance1.yml @@ -0,0 +1,11 @@ +# Molecule managed + +--- +file: + /etc/molecule/instance1: + exists: true + owner: root + group: root + mode: "0644" + filetype: file + contains: ['instance1'] diff --git a/test/scenarios/verifier/molecule/goss/tests/test_host_instance2.yml b/test/scenarios/verifier/molecule/goss/tests/test_host_instance2.yml new file mode 100644 index 0000000000..231687f708 --- /dev/null +++ b/test/scenarios/verifier/molecule/goss/tests/test_host_instance2.yml @@ -0,0 +1,11 @@ +# Molecule managed + +--- +file: + /etc/molecule/instance2: + exists: true + owner: root + group: root + mode: "0644" + filetype: file + contains: ['instance2'] diff --git a/test/scenarios/verifier/molecule/goss/verify.yml b/test/scenarios/verifier/molecule/goss/verify.yml index 85c54cc4ae..135c0c7e6d 100644 --- a/test/scenarios/verifier/molecule/goss/verify.yml +++ b/test/scenarios/verifier/molecule/goss/verify.yml @@ -4,10 +4,10 @@ gather_facts: false become: true vars: - goss_version: v0.2.5 + goss_version: v0.3.6 goss_arch: amd64 goss_bin: /usr/local/bin/goss - goss_sha256sum: 31d04f98444ded26e2478fbdb3f5949cc9318bed13b5937598facc1818e1fce1. + goss_sha256sum: 53dd1156ab66f2c4275fd847372e6329d895cfb2f0bcbec5f86c1c4df7236dde. goss_test_directory: /tmp/molecule/goss goss_format: documentation tasks: @@ -23,12 +23,31 @@ path: "{{ goss_test_directory }}" state: directory + - name: Find Goss tests on localhost + find: + paths: "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}" + patterns: + - "test[-.\\w]*.yml" + - "test_host_{{ ansible_hostname }}[-.\\w]*.yml" + excludes: + - "test_host_(?!{{ ansible_hostname }})[-.\\w]*.yml" + use_regex: true + delegate_to: localhost + register: test_files + changed_when: false + become: false + + - name: debug + debug: + msg: "{{ test_files.files }}" + verbosity: 3 + - name: Copy Goss tests to remote copy: - src: "{{ item }}" - dest: "{{ goss_test_directory }}/{{ item | basename }}" - with_fileglob: - - "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_*.yml" + src: "{{ item.path }}" + dest: "{{ goss_test_directory }}/{{ item.path | basename }}" + with_items: + - "{{ test_files.files }}" - name: Register test files shell: "ls {{ goss_test_directory }}/test_*.yml"