Changeset - d0293eece376
[Not reviewed]
13 1 16
Branko Majic (branko) - 6 years ago 2017-11-25 23:55:05
branko@majic.rs
MAR-128: Upgraded tests for ROLE_NAME 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).
18 files changed with 192 insertions and 63 deletions:
0 comments (0 inline, 0 general)
roles/backup/meta/main.yml
Show inline comments
 
@@ -3,4 +3,4 @@
 
allow_duplicates: yes
 

	
 
dependencies:
 
  - backup_client
 
\ No newline at end of file
 
  - backup_client
roles/backup/molecule.yml
Show inline comments
 
deleted file
roles/backup/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/backup/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/backup/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-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
roles/backup/molecule/default/playbook.yml
Show inline comments
 
file renamed from roles/backup/playbook.yml to roles/backup/molecule/default/playbook.yml
 
---
 

	
 
- 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:
 
    - role: backup
 
      backup_patterns_filename: "test"
 
@@ -25,6 +18,7 @@
 

	
 

	
 
- hosts: parameters-optional
 
  become: yes
 
  roles:
 
    - role: backup
 
      backup_patterns_filename: "test"
roles/backup/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: all
 
  become: yes
 
  tasks:
 

	
 
    - name: Update all caches to avoid errors due to missing remote archives
 
      apt:
 
        update_cache: yes
 
      changed_when: False
roles/backup/molecule/default/tests/data/gnupg/parameters-mandatory.asc
Show inline comments
 
file renamed from roles/backup/tests/data/gnupg/parameters-mandatory.asc to roles/backup/molecule/default/tests/data/gnupg/parameters-mandatory.asc
roles/backup/molecule/default/tests/data/gnupg/parameters-optional.asc
Show inline comments
 
file renamed from roles/backup/tests/data/gnupg/parameters-optional.asc to roles/backup/molecule/default/tests/data/gnupg/parameters-optional.asc
roles/backup/molecule/default/tests/data/ssh/parameters-mandatory
Show inline comments
 
file renamed from roles/backup/tests/data/ssh/parameters-mandatory to roles/backup/molecule/default/tests/data/ssh/parameters-mandatory
roles/backup/molecule/default/tests/data/ssh/parameters-optional
Show inline comments
 
file renamed from roles/backup/tests/data/ssh/parameters-optional to roles/backup/molecule/default/tests/data/ssh/parameters-optional
roles/backup/molecule/default/tests/data/ssh/server_dsa.pub
Show inline comments
 
file renamed from roles/backup/tests/data/ssh/server_dsa.pub to roles/backup/molecule/default/tests/data/ssh/server_dsa.pub
roles/backup/molecule/default/tests/data/ssh/server_ecdsa.pub
Show inline comments
 
file renamed from roles/backup/tests/data/ssh/server_ecdsa.pub to roles/backup/molecule/default/tests/data/ssh/server_ecdsa.pub
roles/backup/molecule/default/tests/data/ssh/server_ed25519.pub
Show inline comments
 
file renamed from roles/backup/tests/data/ssh/server_ed25519.pub to roles/backup/molecule/default/tests/data/ssh/server_ed25519.pub
roles/backup/molecule/default/tests/data/ssh/server_rsa.pub
Show inline comments
 
file renamed from roles/backup/tests/data/ssh/server_rsa.pub to roles/backup/molecule/default/tests/data/ssh/server_rsa.pub
roles/backup/molecule/default/tests/test_default.py
Show inline comments
 
file renamed from roles/backup/tests/test_default.py to roles/backup/molecule/default/tests/test_default.py
 
@@ -2,17 +2,17 @@ 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_backup_patterns_file(File, Sudo):
 
def test_backup_patterns_file(host):
 
    """
 
    Tests if deployed file with backup patterns has correct permissions.
 
    """
 

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

	
 
        backup_pattern = File('/etc/duply/main/patterns/test')
 
        backup_pattern = host.file('/etc/duply/main/patterns/test')
 

	
 
        assert backup_pattern.is_file
 
        assert backup_pattern.user == 'root'
 
@@ -20,14 +20,14 @@ def test_backup_patterns_file(File, Sudo):
 
        assert backup_pattern.mode == 0o600
 

	
 

	
 
def test_include_file(File, Sudo):
 
def test_include_file(host):
 
    """
 
    Tests if assembled include file has correct permissions.
 
    """
 

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

	
 
        include = File('/etc/duply/main/include')
 
        include = host.file('/etc/duply/main/include')
 

	
 
        assert include.is_file
 
        assert include.user == 'root'
roles/backup/molecule/default/tests/test_parameters_mandatory.py
Show inline comments
 
file renamed from roles/backup/tests/test_parameters_mandatory.py to roles/backup/molecule/default/tests/test_parameters_mandatory.py
 
@@ -2,29 +2,29 @@ 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_backup_patterns_content(File, Sudo):
 
def test_backup_patterns_content(host):
 
    """
 
    Tests if content of file containing backup patterns is correct.
 
    """
 

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

	
 
        backup_pattern = File('/etc/duply/main/patterns/test')
 
        backup_pattern = host.file('/etc/duply/main/patterns/test')
 

	
 
        assert backup_pattern.content == ''
 

	
 

	
 
def test_include_content(File, Sudo):
 
def test_include_content(host):
 
    """
 
    Tests if content of assembled include file containing backup patterns is
 
    correct.
 
    """
 

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

	
 
        include = File('/etc/duply/main/include')
 
        include = host.file('/etc/duply/main/include')
 

	
 
        assert include.content == ''
roles/backup/molecule/default/tests/test_parameters_optional.py
Show inline comments
 
file renamed from roles/backup/tests/test_parameters_optional.py to roles/backup/molecule/default/tests/test_parameters_optional.py
 
@@ -2,29 +2,29 @@ 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_backup_patterns_content(File, Sudo):
 
def test_backup_patterns_content(host):
 
    """
 
    Tests if content of file containing backup patterns is correct.
 
    """
 

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

	
 
        backup_pattern = File('/etc/duply/main/patterns/test')
 
        backup_pattern = host.file('/etc/duply/main/patterns/test')
 

	
 
        assert backup_pattern.content == "/etc/hosts\n/etc/ethers\n/var/log"
 

	
 

	
 
def test_include_content(File, Sudo):
 
def test_include_content(host):
 
    """
 
    Tests if content of assembled include file containing backup patterns is
 
    correct.
 
    """
 

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

	
 
        include = File('/etc/duply/main/include')
 
        include = host.file('/etc/duply/main/include')
 

	
 
        assert include.content == "/etc/hosts\n/etc/ethers\n/var/log"
0 comments (0 inline, 0 general)