diff --git a/roles/ldap_client/defaults/main.yml b/roles/ldap_client/defaults/main.yml index 6c006919b84d114d5992a54bf6e8fa19036bf2c8..d64e0ce549d461f6fe3b35c4a17f0974faea1ff6 100644 --- a/roles/ldap_client/defaults/main.yml +++ b/roles/ldap_client/defaults/main.yml @@ -1,3 +1,3 @@ --- -ldap_client_config: [] \ No newline at end of file +ldap_client_config: [] diff --git a/roles/ldap_client/handlers/main.yml b/roles/ldap_client/handlers/main.yml index 73b314ff7c704c18889cf90fdc024716c634adb6..ed97d539c095cf1413af30cc23dea272095b97dd 100644 --- a/roles/ldap_client/handlers/main.yml +++ b/roles/ldap_client/handlers/main.yml @@ -1 +1 @@ ---- \ No newline at end of file +--- diff --git a/roles/ldap_client/molecule.yml b/roles/ldap_client/molecule.yml deleted file mode 100644 index 2f09364f5cab48a5e129c9fa9bdd48c2a560cb74..0000000000000000000000000000000000000000 --- a/roles/ldap_client/molecule.yml +++ /dev/null @@ -1,31 +0,0 @@ ---- - -ansible: - ansiblecfg_ssh_connection: - pipelining: True - -dependency: {} - -driver: - name: vagrant - -vagrant: - - platforms: - - name: debian-jessie64 - box: debian/contrib-jessie64 - - providers: - - name: virtualbox - type: virtualbox - options: - memory: 512 - cpus: 1 - - instances: - - - name: parameters-mandatory - - name: parameters-optional - -verifier: - name: testinfra diff --git a/roles/ldap_client/molecule/default/create.yml b/roles/ldap_client/molecule/default/create.yml new file mode 100644 index 0000000000000000000000000000000000000000..f8eb37cd4df02c540216c02791d0c50870986202 --- /dev/null +++ b/roles/ldap_client/molecule/default/create.yml @@ -0,0 +1,56 @@ +--- +- name: Create + hosts: localhost + connection: local + gather_facts: False + no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}" + vars: + molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}" + molecule_instance_config: "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}" + molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}" + tasks: + - name: Create molecule instance(s) + molecule_vagrant: + instance_name: "{{ item.name }}" + instance_interfaces: "{{ item.interfaces | default(omit) }}" + instance_raw_config_args: "{{ item.instance_raw_config_args | default(omit) }}" + + platform_box: "{{ item.box }}" + platform_box_version: "{{ item.box_version | default(omit) }}" + platform_box_url: "{{ item.box_url | default(omit) }}" + + provider_name: "{{ molecule_yml.driver.provider.name }}" + provider_memory: "{{ item.memory | default(omit) }}" + provider_cpus: "{{ item.cpus | default(omit) }}" + provider_raw_config_args: "{{ item.raw_config_args | default(omit) }}" + + state: up + register: server + with_items: "{{ molecule_yml.platforms }}" + + # Mandatory configuration for Molecule to function. + + - name: Populate instance config dict + set_fact: + instance_conf_dict: { + 'instance': "{{ item.Host }}", + 'address': "{{ item.HostName }}", + 'user': "{{ item.User }}", + 'port': "{{ item.Port }}", + 'identity_file': "{{ item.IdentityFile }}", } + with_items: "{{ server.results }}" + register: instance_config_dict + when: server.changed | bool + + - name: Convert instance config dict to a list + set_fact: + instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}" + when: server.changed | bool + + - name: Dump instance config + copy: + # NOTE(retr0h): Workaround for Ansible 2.2. + # https://github.com/ansible/ansible/issues/20885 + content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}" + dest: "{{ molecule_instance_config }}" + when: server.changed | bool diff --git a/roles/ldap_client/molecule/default/destroy.yml b/roles/ldap_client/molecule/default/destroy.yml new file mode 100644 index 0000000000000000000000000000000000000000..3972a2df8fafe515b30a74f951499b83aae8449c --- /dev/null +++ b/roles/ldap_client/molecule/default/destroy.yml @@ -0,0 +1,36 @@ +--- + +- name: Destroy + hosts: localhost + connection: local + gather_facts: False + no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}" + vars: + molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}" + molecule_instance_config: "{{ lookup('env',' MOLECULE_INSTANCE_CONFIG') }}" + molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}" + tasks: + - name: Destroy molecule instance(s) + molecule_vagrant: + instance_name: "{{ item.name }}" + platform_box: "{{ item.box }}" + provider_name: "{{ molecule_yml.driver.provider.name }}" + force_stop: "{{ item.force_stop | default(True) }}" + + state: destroy + register: server + with_items: "{{ molecule_yml.platforms }}" + + # Mandatory configuration for Molecule to function. + + - name: Populate instance config + set_fact: + instance_conf: {} + + - name: Dump instance config + copy: + # NOTE(retr0h): Workaround for Ansible 2.2. + # https://github.com/ansible/ansible/issues/20885 + content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}" + dest: "{{ molecule_instance_config }}" + when: server.changed | bool diff --git a/roles/ldap_client/molecule/default/molecule.yml b/roles/ldap_client/molecule/default/molecule.yml new file mode 100644 index 0000000000000000000000000000000000000000..cab3cf35b8c91b686e4dff2cb5f903b45ded876a --- /dev/null +++ b/roles/ldap_client/molecule/default/molecule.yml @@ -0,0 +1,57 @@ +--- + +dependency: {} + +driver: + name: vagrant + provider: + name: virtualbox + +lint: + name: yamllint + +platforms: + + - name: parameters-mandatory-jessie64 + groups: + - parameters-mandatory + box: debian/contrib-jessie64 + memory: 256 + cpus: 1 + + - name: parameters-optional-jessie64 + groups: + - parameters-optional + box: debian/contrib-jessie64 + memory: 256 + cpus: 1 + + - name: parameters-mandatory-stretch64 + groups: + - parameters-mandatory + box: debian/contrib-stretch64 + memory: 256 + cpus: 1 + + - name: parameters-optional-stretch64 + groups: + - parameters-optional + box: debian/contrib-stretch64 + memory: 256 + cpus: 1 + +provisioner: + name: ansible + config_options: + ssh_connection: + pipelining: "True" + lint: + name: ansible-lint + +scenario: + name: default + +verifier: + name: testinfra + lint: + name: flake8 diff --git a/roles/ldap_client/playbook.yml b/roles/ldap_client/molecule/default/playbook.yml similarity index 76% rename from roles/ldap_client/playbook.yml rename to roles/ldap_client/molecule/default/playbook.yml index 17a4755c04f05a16b1f1749f73a8f8c145468703..c110bb100dec50bca3f82f8c8cbcd5263a51e24f 100644 --- a/roles/ldap_client/playbook.yml +++ b/roles/ldap_client/molecule/default/playbook.yml @@ -1,18 +1,12 @@ --- -- hosts: all - tasks: - - - name: Update all caches to avoid errors due to missing remote archives - apt: - update_cache: yes - changed_when: False - - hosts: parameters-mandatory + become: yes roles: - ldap_client - hosts: parameters-optional + become: yes roles: - role: ldap_client ldap_client_config: diff --git a/roles/ldap_client/molecule/default/prepare.yml b/roles/ldap_client/molecule/default/prepare.yml new file mode 100644 index 0000000000000000000000000000000000000000..2ca5369027ee16118496dcf8548f8d803a0c9699 --- /dev/null +++ b/roles/ldap_client/molecule/default/prepare.yml @@ -0,0 +1,19 @@ +--- + +- name: Prepare + hosts: all + gather_facts: False + tasks: + - name: Install python for Ansible + raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal) + become: True + changed_when: False + +- hosts: all + become: yes + tasks: + + - name: Update all caches to avoid errors due to missing remote archives + apt: + update_cache: yes + changed_when: False diff --git a/roles/ldap_client/tests/test_default.py b/roles/ldap_client/molecule/default/tests/test_default.py similarity index 63% rename from roles/ldap_client/tests/test_default.py rename to roles/ldap_client/molecule/default/tests/test_default.py index 6fda904b91e7608bd21a12d52b3f46da59174ac8..c1db640b9f866cc39e4a2af85a77e5f7b1525111 100644 --- a/roles/ldap_client/tests/test_default.py +++ b/roles/ldap_client/molecule/default/tests/test_default.py @@ -1,24 +1,24 @@ import testinfra.utils.ansible_runner testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( - '.molecule/ansible_inventory').get_hosts('all') + '.molecule/ansible_inventory.yml').get_hosts('all') -def test_installed_packages(Package): +def test_installed_packages(host): """ Tests if correct packages are installed. """ - assert Package('ldap-utils').is_installed + assert host.package('ldap-utils').is_installed -def test_ldap_configuration_file(File): +def test_ldap_configuration_file(host): """ Tests if LDAP configuration files has been deployed with correct permissions. """ - config = File('/etc/ldap/ldap.conf') + config = host.file('/etc/ldap/ldap.conf') assert config.is_file assert config.user == 'root' diff --git a/roles/ldap_client/tests/test_mandatory.py b/roles/ldap_client/molecule/default/tests/test_mandatory.py similarity index 55% rename from roles/ldap_client/tests/test_mandatory.py rename to roles/ldap_client/molecule/default/tests/test_mandatory.py index db66c0630512316eb16f0422f0f78a7d74d851ae..cfd14699498c8b90ee4e4a1b3b7a7e047a141f04 100644 --- a/roles/ldap_client/tests/test_mandatory.py +++ b/roles/ldap_client/molecule/default/tests/test_mandatory.py @@ -1,14 +1,14 @@ import testinfra.utils.ansible_runner testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( - '.molecule/ansible_inventory').get_hosts('parameters-mandatory') + '.molecule/ansible_inventory.yml').get_hosts('parameters-mandatory') -def test_ldap_configuration_file_content(File): +def test_ldap_configuration_file_content(host): """ Tests if LDAP configuration file has correct content """ - config = File('/etc/ldap/ldap.conf') + config = host.file('/etc/ldap/ldap.conf') assert config.content == "" diff --git a/roles/ldap_client/tests/test_optional.py b/roles/ldap_client/molecule/default/tests/test_optional.py similarity index 72% rename from roles/ldap_client/tests/test_optional.py rename to roles/ldap_client/molecule/default/tests/test_optional.py index b883ad8781c30f924623e50e4f32abe73ef81989..04060c9c56a5efdcb35a2a2f6cbd1f2e582d4501 100644 --- a/roles/ldap_client/tests/test_optional.py +++ b/roles/ldap_client/molecule/default/tests/test_optional.py @@ -1,10 +1,10 @@ import testinfra.utils.ansible_runner testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( - '.molecule/ansible_inventory').get_hosts('parameters-optional') + '.molecule/ansible_inventory.yml').get_hosts('parameters-optional') -def test_ldap_configuration_file_content(File): +def test_ldap_configuration_file_content(host): """ Tests if LDAP configuration file has correct content """ @@ -18,6 +18,6 @@ URI ldaps://ldap-server/ # Base entry BASE dc=local""" - config = File('/etc/ldap/ldap.conf') + config = host.file('/etc/ldap/ldap.conf') assert config.content == expected_content