Changeset - 62014a66e35f
[Not reviewed]
0 10 2
Branko Majic (branko) - 7 years ago 2018-05-28 22:34:14
branko@majic.rs
MAR-129: Updated LDAP client role and its tests for new Molecule and Ansible:

- Fixed linting issues.
- Moved test playbook variables into group_vars.
- Include linting configuration for tests from top-level directory.
- Updated how the target hosts are refenced in tests.
- Fixed Ansible warnings.
12 files changed with 44 insertions and 32 deletions:
0 comments (0 inline, 0 general)
roles/ldap_client/handlers/main.yml
Show inline comments
 
---
 

	
 
- debug:
 
    msg: "No handlers are available for ldap_client role."
roles/ldap_client/molecule/default/create.yml
Show inline comments
 
---
 
- name: Create
 
  hosts: localhost
 
  connection: local
 
  gather_facts: False
 
  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) }}"
 

	
roles/ldap_client/molecule/default/destroy.yml
Show inline comments
 
---
 

	
 
- name: Destroy
 
  hosts: localhost
 
  connection: local
 
  gather_facts: False
 
  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) }}"
roles/ldap_client/molecule/default/group_vars/parameters-mandatory.yml
Show inline comments
 
new file 100644
 
---
roles/ldap_client/molecule/default/group_vars/parameters-optional.yml
Show inline comments
 
new file 100644
 
---
 

	
 
ldap_client_config:
 
  - comment: CA truststore
 
    option: TLS_CACERT
 
    value: /etc/ssl/certs/testca.cert.pem
 
  - comment: Ensure TLS is enforced
 
    option: TLS_REQCERT
 
    value: demand
 
  - comment: Default URI to connect to
 
    option: URI
 
    value: ldaps://ldap-server/
 
  - comment: Base entry
 
    option: BASE
 
    value: dc=local
roles/ldap_client/molecule/default/molecule.yml
Show inline comments
 
---
 

	
 
dependency: {}
 

	
 
driver:
 
  name: vagrant
 
  provider:
 
    name: virtualbox
 

	
 
lint:
 
  name: yamllint
 
  options:
 
    config-file: ../../.yamllint.yml
 

	
 
platforms:
 

	
 
  - name: parameters-mandatory-jessie64
 
    groups:
 
      - parameters-mandatory
 
    box: debian/contrib-jessie64
 
    memory: 256
 
    cpus: 1
 

	
 
  - name: parameters-optional-jessie64
 
    groups:
roles/ldap_client/molecule/default/playbook.yml
Show inline comments
 
---
 

	
 
- hosts: parameters-mandatory
 
  become: yes
 
- hosts: parameters-mandatory,parameters-optional
 
  become: true
 
  roles:
 
    - ldap_client
 

	
 
- hosts: parameters-optional
 
  become: yes
 
  roles:
 
    - role: ldap_client
 
      ldap_client_config:
 
        - comment: CA truststore
 
          option: TLS_CACERT
 
          value: /etc/ssl/certs/testca.cert.pem
 
        - comment: Ensure TLS is enforced
 
          option: TLS_REQCERT
 
          value: demand
 
        - comment: Default URI to connect to
 
          option: URI
 
          value: ldaps://ldap-server/
 
        - comment: Base entry
 
          option: BASE
 
          value: dc=local
roles/ldap_client/molecule/default/prepare.yml
Show inline comments
 
---
 

	
 
- name: Prepare
 
  hosts: all
 
  gather_facts: False
 
  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
 
      become: true
 
      changed_when: false
 

	
 
- hosts: all
 
  become: yes
 
  become: true
 
  tasks:
 

	
 
    - name: Update all caches to avoid errors due to missing remote archives
 
      apt:
 
        update_cache: yes
 
      changed_when: False
 
        update_cache: true
 
      changed_when: false
roles/ldap_client/molecule/default/tests/test_default.py
Show inline comments
 
import os
 

	
 
import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    '.molecule/ansible_inventory.yml').get_hosts('all')
 
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts(['parameters-mandatory', 'parameters-optional'])
 

	
 

	
 
def test_installed_packages(host):
 
    """
 
    Tests if correct packages are installed.
 
    """
 

	
 
    assert host.package('ldap-utils').is_installed
 

	
 

	
 
def test_ldap_configuration_file(host):
 
    """
roles/ldap_client/molecule/default/tests/test_mandatory.py
Show inline comments
 
import os
 

	
 
import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    '.molecule/ansible_inventory.yml').get_hosts('parameters-mandatory')
 
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts(['parameters-mandatory'])
 

	
 

	
 
def test_ldap_configuration_file_content(host):
 
    """
 
    Tests if LDAP configuration file has correct content
 
    """
 

	
 
    config = host.file('/etc/ldap/ldap.conf')
 

	
 
    assert config.content == ""
roles/ldap_client/molecule/default/tests/test_optional.py
Show inline comments
 
import os
 

	
 
import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    '.molecule/ansible_inventory.yml').get_hosts('parameters-optional')
 
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts(['parameters-optional'])
 

	
 

	
 
def test_ldap_configuration_file_content(host):
 
    """
 
    Tests if LDAP configuration file has correct content
 
    """
 

	
 
    expected_content = """# CA truststore
 
TLS_CACERT /etc/ssl/certs/testca.cert.pem
 
# Ensure TLS is enforced
 
TLS_REQCERT demand
 
# Default URI to connect to
roles/ldap_client/tasks/main.yml
Show inline comments
 
---
 

	
 
- name: Install OpenLDAP client tools
 
  apt:
 
    name: ldap-utils
 
    state: installed
 
    state: present
 

	
 
- name: Deploy LDAP client configuration file
 
  template:
 
    src: ldap.conf.j2
 
    dest: /etc/ldap/ldap.conf
 
    owner: root
 
    group: root
 
    mode: 0644
 

	
 
- name: Explicitly run all handlers
 
  include: ../handlers/main.yml
 
  when: "handlers | default(False) | bool() == True"
0 comments (0 inline, 0 general)