Changeset - 4ee589937d60
[Not reviewed]
5 0 8
Branko Majic (branko) - 6 years ago 2017-11-26 22:36:20
branko@majic.rs
MAR-128: Upgraded tests for preseed role:

- Switch to new Molecule configuration.
- Updated set-up playbook to use become: yes.
- Moved some preparatory steps outside of the main playbook (eases
idempotence tests).
- Updated tests to reference the yml inventory file.
- Updated tests to use new fixture (host instead of individual ones).
- Switched to extracting hostname instead of hard-coding it in a
couple of tests.
- Introduced additional hosts for testing on different Debian
versions.
9 files changed with 228 insertions and 66 deletions:
0 comments (0 inline, 0 general)
roles/preseed/molecule.yml
Show inline comments
 
deleted file
roles/preseed/molecule/default/create.yml
Show inline comments
 
new file 100644
 
---
 
- 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
roles/preseed/molecule/default/destroy.yml
Show inline comments
 
new file 100644
 
---
 

	
 
- 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
roles/preseed/molecule/default/molecule.yml
Show inline comments
 
new file 100644
 
---
 

	
 
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-optional-with-overrides-jessie64
 
    groups:
 
      - parameters-optional-with-overrides
 
    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
 

	
 
  - name: parameters-optional-with-overrides-stretch64
 
    groups:
 
      - parameters-optional-with-overrides
 
    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
roles/preseed/molecule/default/playbook.yml
Show inline comments
 
file renamed from roles/preseed/playbook.yml to roles/preseed/molecule/default/playbook.yml
 
@@ -33,7 +33,7 @@
 
      preseed_directory: /tmp/custom_preseed_files_location
 
      ansible_key: CUSTOMKEY
 
      preseed_server_overrides:
 
        parameters-optional-with-overrides:
 
        parameters-optional-with-overrides-jessie64:
 
          country: RS
 
          dns: 1.1.1.1
 
          domain: example.com
roles/preseed/molecule/default/prepare.yml
Show inline comments
 
new file 100644
 
---
 

	
 
- 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: parameters-mandatory
 
  become: yes
 
  tasks:
 

	
 
    # @TODO: This is a hack because inventory_dir used for
 
    # preseed_directory comes from _host_.
 
    - name: Set-up inventory directory matching location where molecule is running itself
 
      file:
 
        path: "{{ inventory_dir }}"
 
        state: directory
 
        owner: vagrant
 
        group: vagrant
 
        mode: 0700
roles/preseed/molecule/default/tests/test_parameters_mandatory.py
Show inline comments
 
file renamed from roles/preseed/tests/test_parameters_mandatory.py to roles/preseed/molecule/default/tests/test_parameters_mandatory.py
 
@@ -4,49 +4,51 @@ 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_preseed_directory(File, Sudo):
 
def test_preseed_directory(host):
 
    """
 
    Tests presence and permissions on created preseed directory.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        # Preseed directory created at same level as inventory.
 
        preseed_directory = File(os.path.join(os.getcwd(), ".molecule", 'preseed_files'))
 
        preseed_directory = host.file(os.path.join(os.getcwd(), ".molecule", 'preseed_files'))
 

	
 
        assert preseed_directory.is_directory
 
        assert preseed_directory.mode == 0o750
 

	
 

	
 
def test_preseed_configuration_files(File, Sudo):
 
def test_preseed_configuration_files(host):
 
    """
 
    Tests presence and permissions on created preseed configuration files.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        # Preseed directory created at same level as inventory.
 
        preseed_directory_path = os.path.join(os.getcwd(), ".molecule", 'preseed_files')
 

	
 
        # Verify that preseed configuration files are created for all hosts.
 
        for host in testinfra_hosts:
 
        for testinfra_host in testinfra_hosts:
 

	
 
            preseed_file = File(os.path.join(preseed_directory_path, "%s.cfg" % host))
 
            preseed_file = host.file(os.path.join(preseed_directory_path, "%s.cfg" % testinfra_host))
 

	
 
            assert preseed_file.is_file
 
            assert preseed_file.mode == 0o640
 

	
 

	
 
def test_preseed_configuration_file_content(File, Sudo):
 
def test_preseed_configuration_file_content(host):
 
    """
 
    Tests content of generated preseed configuration file.
 
    """
 

	
 
    with Sudo():
 
        preseed_file = File(os.path.join(os.getcwd(), ".molecule", "preseed_files", "parameters-mandatory.cfg"))
 
    hostname = host.run('hostname').stdout
 

	
 
    with host.sudo():
 
        preseed_file = host.file(os.path.join(os.getcwd(), ".molecule", "preseed_files", "%s.cfg" % hostname))
 
        preseed_file_content = preseed_file.content_string
 
        ssh_public_key = open(os.path.join(os.path.expanduser("~"), ".ssh", "id_rsa.pub")).read().strip()
 

	
roles/preseed/molecule/default/tests/test_parameters_optional.py
Show inline comments
 
file renamed from roles/preseed/tests/test_parameters_optional.py to roles/preseed/molecule/default/tests/test_parameters_optional.py
 
@@ -4,48 +4,50 @@ 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')
 

	
 

	
 
PRESEED_DIRECTORY = '/tmp/custom_preseed_files_location'
 

	
 

	
 
def test_preseed_directory(File, Sudo):
 
def test_preseed_directory(host):
 
    """
 
    Test presence and permissions of preseed directory.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        preseed_directory = File(PRESEED_DIRECTORY)
 
        preseed_directory = host.file(PRESEED_DIRECTORY)
 

	
 
        assert preseed_directory.is_directory
 
        assert preseed_directory.mode == 0o750
 

	
 

	
 
def test_preseed_configuration_files(File, Sudo):
 
def test_preseed_configuration_files(host):
 
    """
 
    Tests presence and permissions on created preseed configuration files.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        # Verify that preseed configuration files are created for all hosts.
 
        for host in testinfra_hosts:
 
        for testinfra_host in testinfra_hosts:
 

	
 
            preseed_file = File(os.path.join(PRESEED_DIRECTORY, "%s.cfg" % host))
 
            preseed_file = host.file(os.path.join(PRESEED_DIRECTORY, "%s.cfg" % testinfra_host))
 

	
 
            assert preseed_file.is_file
 
            assert preseed_file.mode == 0o640
 

	
 

	
 
def test_preseed_configuration_file_content(File, Sudo):
 
def test_preseed_configuration_file_content(host):
 
    """
 
    Tests content of generated preseed configuration file.
 
    """
 

	
 
    with Sudo():
 
        preseed_file = File(os.path.join(PRESEED_DIRECTORY, "%s.cfg" % 'parameters-optional'))
 
    hostname = host.run('hostname').stdout
 

	
 
    with host.sudo():
 
        preseed_file = host.file(os.path.join(PRESEED_DIRECTORY, "%s.cfg" % hostname))
 
        preseed_file_content = preseed_file.content_string
 
        ssh_public_key = "CUSTOMKEY"
 

	
roles/preseed/molecule/default/tests/test_parameters_optional_with_overrides.py
Show inline comments
 
file renamed from roles/preseed/tests/test_parameters_optional_with_overrides.py to roles/preseed/molecule/default/tests/test_parameters_optional_with_overrides.py
 
@@ -4,32 +4,34 @@ import testinfra.utils.ansible_runner
 

	
 

	
 
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
    '.molecule/ansible_inventory').get_hosts('parameters-optional-with-overrides')
 
    '.molecule/ansible_inventory.yml').get_hosts('parameters-optional-with-overrides')
 

	
 

	
 
PRESEED_DIRECTORY = '/tmp/custom_preseed_files_location'
 

	
 

	
 
def test_preseed_directory(File, Sudo, Ansible):
 
def test_preseed_directory(host):
 
    """
 
    Test presence and permissions of preseed directory.
 
    """
 

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        preseed_directory = File(PRESEED_DIRECTORY)
 
        preseed_directory = host.file(PRESEED_DIRECTORY)
 

	
 
        assert preseed_directory.is_directory
 
        assert preseed_directory.mode == 0o750
 

	
 

	
 
def test_preseed_configuration_file_content_host_without_overrides(File, Sudo):
 
def test_preseed_configuration_file_content_host_without_overrides(host):
 
    """
 
    Tests content of generated preseed configuration file.
 
    """
 

	
 
    with Sudo():
 
        preseed_file = File(os.path.join(PRESEED_DIRECTORY, "parameters-mandatory.cfg"))
 
    hostname = 'parameters-mandatory-jessie64'
 

	
 
    with host.sudo():
 
        preseed_file = host.file(os.path.join(PRESEED_DIRECTORY, "%s.cfg" % hostname))
 
        preseed_file_content = preseed_file.content_string
 
        ssh_public_key = "CUSTOMKEY"
 

	
 
@@ -53,13 +55,15 @@ d-i netcfg/get_domain string ignored-value""" in preseed_file_content
 
    assert ssh_public_key in preseed_file_content
 

	
 

	
 
def test_preseed_configuration_file_content_host_with_overrides(File, Sudo):
 
def test_preseed_configuration_file_content_host_with_overrides(host):
 
    """
 
    Tests content of generated preseed configuration file.
 
    """
 

	
 
    with Sudo():
 
        preseed_file = File(os.path.join(PRESEED_DIRECTORY, "parameters-optional-with-overrides.cfg"))
 
    hostname = 'parameters-optional-with-overrides-jessie64'
 

	
 
    with host.sudo():
 
        preseed_file = host.file(os.path.join(PRESEED_DIRECTORY, "%s.cfg" % hostname))
 
        preseed_file_content = preseed_file.content_string
 
        ssh_public_key = "CUSTOMKEY"
 

	
0 comments (0 inline, 0 general)