From 9367c59e3cfda457fce7494a1ec065c7aa80853b Mon Sep 17 00:00:00 2001 From: Samuli Silvius Date: Fri, 22 Mar 2019 09:33:05 +0200 Subject: Molecule tests for resource-data role Initial molecule tests. Tests now ssh transfer but test files fully supports also testing nfs transfer. Creating nfs transfer as separate scenario later when find out better ways to share common code. Issue-ID: OOM-1754 Change-Id: Ia728ecfed4378c01f7e305d5a4446b0172fe42d9 Signed-off-by: Samuli Silvius --- ansible/roles/resource-data/.yamllint | 11 ++++ ansible/roles/resource-data/defaults/main.yml | 2 +- .../molecule/default/group_vars/all.yml | 7 +++ .../resource-data/molecule/default/molecule.yml | 47 ++++++++++++++ .../resource-data/molecule/default/playbook.yml | 6 ++ .../resource-data/molecule/default/prepare.yml | 5 ++ .../molecule/default/tests/test_default.py | 56 +++++++++++++++++ .../resource-data/tasks/unarchive-nfs-resource.yml | 2 +- .../roles/prepare-resource-data/defaults/main.yml | 2 + .../roles/prepare-resource-data/tasks/main.yml | 8 +++ .../tasks/prepare-infra-server.yml | 16 +++++ .../tasks/prepare-resource-server.yml | 72 ++++++++++++++++++++++ .../prepare-resource-data/templates/exports.j2 | 3 + 13 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 ansible/roles/resource-data/.yamllint create mode 100644 ansible/roles/resource-data/molecule/default/group_vars/all.yml create mode 100644 ansible/roles/resource-data/molecule/default/molecule.yml create mode 100644 ansible/roles/resource-data/molecule/default/playbook.yml create mode 100644 ansible/roles/resource-data/molecule/default/prepare.yml create mode 100644 ansible/roles/resource-data/molecule/default/tests/test_default.py create mode 100644 ansible/test/roles/prepare-resource-data/defaults/main.yml create mode 100644 ansible/test/roles/prepare-resource-data/tasks/main.yml create mode 100644 ansible/test/roles/prepare-resource-data/tasks/prepare-infra-server.yml create mode 100644 ansible/test/roles/prepare-resource-data/tasks/prepare-resource-server.yml create mode 100644 ansible/test/roles/prepare-resource-data/templates/exports.j2 diff --git a/ansible/roles/resource-data/.yamllint b/ansible/roles/resource-data/.yamllint new file mode 100644 index 00000000..e8e79a57 --- /dev/null +++ b/ansible/roles/resource-data/.yamllint @@ -0,0 +1,11 @@ +extends: default + +rules: + braces: + max-spaces-inside: 1 + level: error + brackets: + max-spaces-inside: 1 + level: error + line-length: disable +# truthy: disable diff --git a/ansible/roles/resource-data/defaults/main.yml b/ansible/roles/resource-data/defaults/main.yml index 8a1adfa1..5905ad42 100644 --- a/ansible/roles/resource-data/defaults/main.yml +++ b/ansible/roles/resource-data/defaults/main.yml @@ -1,2 +1,2 @@ --- -resources_on_nfs: no +resources_on_nfs: false diff --git a/ansible/roles/resource-data/molecule/default/group_vars/all.yml b/ansible/roles/resource-data/molecule/default/group_vars/all.yml new file mode 100644 index 00000000..558eacb2 --- /dev/null +++ b/ansible/roles/resource-data/molecule/default/group_vars/all.yml @@ -0,0 +1,7 @@ +--- +app_data_path: /opt/myleculeapp +aux_data_path: "{{ app_data_path }}/runtime_images_source_dir" +resources_dir: /data +resources_filename: resources_package.tar +aux_resources_filename: aux_resources_package.tar +ansible_ssh_private_key_file: ~/.ssh/offline_ssh_key diff --git a/ansible/roles/resource-data/molecule/default/molecule.yml b/ansible/roles/resource-data/molecule/default/molecule.yml new file mode 100644 index 00000000..03f40cb6 --- /dev/null +++ b/ansible/roles/resource-data/molecule/default/molecule.yml @@ -0,0 +1,47 @@ +--- +dependency: + name: galaxy +driver: + name: docker +lint: + name: yamllint +platforms: + + - name: resource-host + image: molecule-${PREBUILD_PLATFORM_DISTRO:-centos}:${PREBUILD_DISTRO_VERSION:-centos7.6} + pre_build_image: true + privileged: true + command: ${MOLECULE_DOCKER_COMMAND:-""} + groups: + - resources + networks: + - name: resource-data + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + # - ${HOME}/resource-data:/data:rw # mount fs from host to get nfs exportfs task working + + - name: infrastructure-server + image: molecule-${PREBUILD_PLATFORM_DISTRO:-centos}:${PREBUILD_DISTRO_VERSION:-centos7.6} + pre_build_image: true + privileged: true + command: ${MOLECULE_DOCKER_COMMAND:-""} + groups: + - infrastructure + networks: + - name: resource-data + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + +provisioner: + name: ansible + log: true + lint: + name: ansible-lint + env: + ANSIBLE_ROLES_PATH: ../../../../test/roles/ +scenario: + name: default +verifier: + name: testinfra + lint: + name: flake8 diff --git a/ansible/roles/resource-data/molecule/default/playbook.yml b/ansible/roles/resource-data/molecule/default/playbook.yml new file mode 100644 index 00000000..cdd9820f --- /dev/null +++ b/ansible/roles/resource-data/molecule/default/playbook.yml @@ -0,0 +1,6 @@ +--- +- name: Converge + hosts: all + roles: + - setup + - resource-data diff --git a/ansible/roles/resource-data/molecule/default/prepare.yml b/ansible/roles/resource-data/molecule/default/prepare.yml new file mode 100644 index 00000000..a4436001 --- /dev/null +++ b/ansible/roles/resource-data/molecule/default/prepare.yml @@ -0,0 +1,5 @@ +--- +- name: Prepare resource-data + hosts: all + roles: + - prepare-resource-data diff --git a/ansible/roles/resource-data/molecule/default/tests/test_default.py b/ansible/roles/resource-data/molecule/default/tests/test_default.py new file mode 100644 index 00000000..7cff76a7 --- /dev/null +++ b/ansible/roles/resource-data/molecule/default/tests/test_default.py @@ -0,0 +1,56 @@ +import os +import pytest + +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('infrastructure-server') + + +@pytest.fixture +def group_vars(host): + all_file = "file=group_vars/all.yml name=all" + return host.ansible("include_vars", all_file)["ansible_facts"]["all"] + + +@pytest.mark.parametrize('dir', [ + 'app_data_path', + 'aux_data_path' +]) +def test_data_target_dirs(host, dir, group_vars): + tested_dir = host.file(group_vars[dir]) + assert tested_dir.exists + assert tested_dir.is_directory + + +@pytest.mark.parametrize('target_file', [ + 'resource1.txt', + 'resource2.txt', + 'resource3.txt', + 'somedir/resource4.txt' +]) +def test_transferred_resources_files(host, target_file, group_vars): + tested_file = host.file(group_vars["app_data_path"] + "/" + target_file) + assert tested_file.exists + assert tested_file.is_file + + flag_file = \ + host.file(group_vars["app_data_path"] + "/" + + group_vars["resources_filename"] + "-uploaded") + assert flag_file.exists + assert flag_file.is_file + + +@pytest.mark.parametrize('target_file', [ + 'auxdata' +]) +def test_transferred_aux_resources_files(host, target_file, group_vars): + tested_file = host.file(group_vars["aux_data_path"] + "/" + target_file) + assert tested_file.exists + assert tested_file.is_file + + flag_file = \ + host.file(group_vars["aux_data_path"] + "/" + + group_vars["aux_resources_filename"] + "-uploaded") + assert flag_file.exists + assert flag_file.is_file diff --git a/ansible/roles/resource-data/tasks/unarchive-nfs-resource.yml b/ansible/roles/resource-data/tasks/unarchive-nfs-resource.yml index bbf99321..6087c81c 100644 --- a/ansible/roles/resource-data/tasks/unarchive-nfs-resource.yml +++ b/ansible/roles/resource-data/tasks/unarchive-nfs-resource.yml @@ -21,7 +21,7 @@ unarchive: src: "/tmp/resource_data/{{ resource_source_filename }}" dest: "{{ resource_destination_directory }}" - remote_src: yes + remote_src: true always: - name: Unmount resource dir mount: diff --git a/ansible/test/roles/prepare-resource-data/defaults/main.yml b/ansible/test/roles/prepare-resource-data/defaults/main.yml new file mode 100644 index 00000000..6cc5701b --- /dev/null +++ b/ansible/test/roles/prepare-resource-data/defaults/main.yml @@ -0,0 +1,2 @@ +--- +resources_on_nfs: false \ No newline at end of file diff --git a/ansible/test/roles/prepare-resource-data/tasks/main.yml b/ansible/test/roles/prepare-resource-data/tasks/main.yml new file mode 100644 index 00000000..5a020882 --- /dev/null +++ b/ansible/test/roles/prepare-resource-data/tasks/main.yml @@ -0,0 +1,8 @@ +--- +- include_tasks: prepare-resource-server.yml + vars: + subdir: somedir + when: inventory_hostname in groups.resources + +- include_tasks: prepare-infra-server.yml + when: inventory_hostname in groups.infrastructure diff --git a/ansible/test/roles/prepare-resource-data/tasks/prepare-infra-server.yml b/ansible/test/roles/prepare-resource-data/tasks/prepare-infra-server.yml new file mode 100644 index 00000000..b55842ac --- /dev/null +++ b/ansible/test/roles/prepare-resource-data/tasks/prepare-infra-server.yml @@ -0,0 +1,16 @@ +--- +- name: Make sure the target dirs (where data is put i.e. what the whole resource-data role is testing) are empty at first + file: + path: "{{ item }}" + state: absent + loop: + - aux_data_path + - app_data_path + +- name: Install nfs-utils + package: + name: nfs-utils + state: present + when: + - resources_on_nfs is defined + - resources_on_nfs diff --git a/ansible/test/roles/prepare-resource-data/tasks/prepare-resource-server.yml b/ansible/test/roles/prepare-resource-data/tasks/prepare-resource-server.yml new file mode 100644 index 00000000..4057ba14 --- /dev/null +++ b/ansible/test/roles/prepare-resource-data/tasks/prepare-resource-server.yml @@ -0,0 +1,72 @@ +--- +- name: Install file exacutable if not there for archive compression checking + package: + name: file + state: present + +- name: "Create resource dir {{ resources_dir }}" + file: + path: "{{ resources_dir }}/{{ subdir }}" + state: directory + +- name: Create test files for the dummy packages + file: + path: "{{ item }}" + state: touch + loop: + - "{{ resources_dir }}/resource1.txt" + - "{{ resources_dir }}/resource2.txt" + - "{{ resources_dir }}/resource3.txt" + - "{{ resources_dir }}/{{ subdir }}/resource4.txt" + - "{{ resources_dir }}/auxdata" + +- name: Create resources tar archive for testing + archive: + path: + - "{{ resources_dir }}/resource*" + - "{{ resources_dir }}/{{ subdir }}/resource*" + dest: "{{ resources_dir }}/{{ resources_filename }}" + when: + - resources_filename is defined + - resources_filename is not none + +- name: Create aux tar archive for testing + archive: + path: "{{ resources_dir }}/aux*" + dest: "{{ resources_dir }}/{{ aux_resources_filename }}" + when: + - aux_resources_filename is defined + - aux_resources_filename is not none + +- block: + - name: Install nfs-utils + package: + name: nfs-utils + state: present + + - name: Start services + systemd: + name: "{{ item }}" + state: started + loop: + - rpcbind + - nfs + + - name: Create data dir to host machine for nfs mount. Must match with volume mount in molecule.yml + file: + path: ~{{ resources_dir }} + state: directory + delegate_to: localhost + + - name: Add hosts to exports + template: + src: exports.j2 + dest: /etc/exports + vars: + nfs_mount_path: "{{ resources_dir }}" + + - name: Export nfs + command: exportfs -ar + when: + - resources_on_nfs is defined + - resources_on_nfs diff --git a/ansible/test/roles/prepare-resource-data/templates/exports.j2 b/ansible/test/roles/prepare-resource-data/templates/exports.j2 new file mode 100644 index 00000000..958dc00b --- /dev/null +++ b/ansible/test/roles/prepare-resource-data/templates/exports.j2 @@ -0,0 +1,3 @@ +{% for host in groups.infrastructure -%} + {{ nfs_mount_path }} {{ hostvars[host].inventory_hostname }}(ro,sync,no_root_squash,no_subtree_check) +{% endfor %} -- cgit 1.2.3-korg