From 8b3481f5116b770125f6c6efe28ad8f39c8022d9 Mon Sep 17 00:00:00 2001 From: efrikin Date: Fri, 15 Oct 2021 04:46:54 +0300 Subject: [PATCH] Add molecule tests for roles (#8080) * Add molecule tests for bastion-ssh-config * Add molecule tests for adduser * Update .gitignore --- .gitignore | 4 ++ roles/adduser/molecule/default/converge.yml | 10 +++++ roles/adduser/molecule/default/molecule.yml | 23 ++++++++++++ .../molecule/default/tests/test_default.py | 37 +++++++++++++++++++ roles/bastion-ssh-config/defaults/main.yml | 2 + .../molecule/default/converge.yml | 15 ++++++++ .../molecule/default/molecule.yml | 31 ++++++++++++++++ .../molecule/default/tests/test_default.py | 34 +++++++++++++++++ roles/bastion-ssh-config/tasks/main.yml | 4 +- .../{ssh-bastion.conf => ssh-bastion.conf.j2} | 0 10 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 roles/adduser/molecule/default/converge.yml create mode 100644 roles/adduser/molecule/default/molecule.yml create mode 100644 roles/adduser/molecule/default/tests/test_default.py create mode 100644 roles/bastion-ssh-config/defaults/main.yml create mode 100644 roles/bastion-ssh-config/molecule/default/converge.yml create mode 100644 roles/bastion-ssh-config/molecule/default/molecule.yml create mode 100644 roles/bastion-ssh-config/molecule/default/tests/test_default.py rename roles/bastion-ssh-config/templates/{ssh-bastion.conf => ssh-bastion.conf.j2} (100%) diff --git a/.gitignore b/.gitignore index b09ca9d3b..c75c9981d 100644 --- a/.gitignore +++ b/.gitignore @@ -99,3 +99,7 @@ target/ # virtualenv venv/ ENV/ + +# molecule +roles/**/molecule/**/__pycache__/ +roles/**/molecule/**/*.conf diff --git a/roles/adduser/molecule/default/converge.yml b/roles/adduser/molecule/default/converge.yml new file mode 100644 index 000000000..47ff6c7e0 --- /dev/null +++ b/roles/adduser/molecule/default/converge.yml @@ -0,0 +1,10 @@ +--- +- name: Converge + hosts: all + become: true + gather_facts: false + roles: + - role: adduser + vars: + user: + name: foo diff --git a/roles/adduser/molecule/default/molecule.yml b/roles/adduser/molecule/default/molecule.yml new file mode 100644 index 000000000..4bb5dce30 --- /dev/null +++ b/roles/adduser/molecule/default/molecule.yml @@ -0,0 +1,23 @@ +--- +dependency: + name: galaxy +lint: | + set -e + yamllint -c ../../.yamllint . +driver: + name: vagrant + provider: + name: libvirt +platforms: + - name: adduser-01 + box: generic/ubuntu2004 + cpus: 1 + memory: 512 +provisioner: + name: ansible + lint: + name: ansible-lint +verifier: + name: testinfra + lint: + name: flake8 diff --git a/roles/adduser/molecule/default/tests/test_default.py b/roles/adduser/molecule/default/tests/test_default.py new file mode 100644 index 000000000..4c81047e2 --- /dev/null +++ b/roles/adduser/molecule/default/tests/test_default.py @@ -0,0 +1,37 @@ +import os +import yaml +import glob +import testinfra.utils.ansible_runner +from ansible.playbook import Playbook +from ansible.cli.playbook import PlaybookCLI + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + +def read_playbook(playbook): + cli_args = [os.path.realpath(playbook), testinfra_hosts] + cli = PlaybookCLI(cli_args) + cli.parse() + loader, inventory, variable_manager = cli._play_prereqs() + + pb = Playbook.load(cli.args[0], variable_manager, loader) + + for play in pb.get_plays(): + yield variable_manager.get_vars(play) + +def get_playbook(): + with open(os.path.realpath(' '.join(map(str,glob.glob('molecule.*')))), 'r') as yamlfile: + data = yaml.load(yamlfile, Loader=yaml.FullLoader) + if 'playbooks' in data['provisioner'].keys(): + if 'converge' in data['provisioner']['playbooks'].keys(): + return data['provisioner']['playbooks']['converge'] + else: + return ' '.join(map(str,glob.glob('converge.*'))) + +def test_user(host): + for vars in read_playbook(get_playbook()): + assert host.user(vars['user']['name']).exists + if 'group' in vars['user'].keys(): + assert host.group(vars['user']['group']).exists + else: + assert host.group(vars['user']['name']).exists diff --git a/roles/bastion-ssh-config/defaults/main.yml b/roles/bastion-ssh-config/defaults/main.yml new file mode 100644 index 000000000..d322814e9 --- /dev/null +++ b/roles/bastion-ssh-config/defaults/main.yml @@ -0,0 +1,2 @@ +--- +ssh_bastion_confing__name: ssh-bastion.conf \ No newline at end of file diff --git a/roles/bastion-ssh-config/molecule/default/converge.yml b/roles/bastion-ssh-config/molecule/default/converge.yml new file mode 100644 index 000000000..54a624705 --- /dev/null +++ b/roles/bastion-ssh-config/molecule/default/converge.yml @@ -0,0 +1,15 @@ +--- +- name: Converge + hosts: all + become: true + gather_facts: false + roles: + - role: bastion-ssh-config + tasks: + - name: Copy config to remote host + copy: + src: "{{ playbook_dir }}/{{ ssh_bastion_confing__name }}" + dest: "{{ ssh_bastion_confing__name }}" + owner: "{{ ansible_user }}" + group: "{{ ansible_user }}" + mode: 0644 diff --git a/roles/bastion-ssh-config/molecule/default/molecule.yml b/roles/bastion-ssh-config/molecule/default/molecule.yml new file mode 100644 index 000000000..1d84db76c --- /dev/null +++ b/roles/bastion-ssh-config/molecule/default/molecule.yml @@ -0,0 +1,31 @@ +--- +dependency: + name: galaxy +lint: | + set -e + yamllint -c ../../.yamllint . +driver: + name: vagrant + provider: + name: libvirt +platforms: + - name: bastion-01 + box: generic/ubuntu2004 + cpus: 1 + memory: 512 +provisioner: + name: ansible + lint: + name: ansible-lint + inventory: + hosts: + all: + hosts: + children: + bastion: + hosts: + bastion-01: +verifier: + name: testinfra + lint: + name: flake8 diff --git a/roles/bastion-ssh-config/molecule/default/tests/test_default.py b/roles/bastion-ssh-config/molecule/default/tests/test_default.py new file mode 100644 index 000000000..f98faa409 --- /dev/null +++ b/roles/bastion-ssh-config/molecule/default/tests/test_default.py @@ -0,0 +1,34 @@ +import os +import yaml +import glob +import testinfra.utils.ansible_runner +from ansible.playbook import Playbook +from ansible.cli.playbook import PlaybookCLI + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + +def read_playbook(playbook): + cli_args = [os.path.realpath(playbook), testinfra_hosts] + cli = PlaybookCLI(cli_args) + cli.parse() + loader, inventory, variable_manager = cli._play_prereqs() + + pb = Playbook.load(cli.args[0], variable_manager, loader) + + for play in pb.get_plays(): + yield variable_manager.get_vars(play) + +def get_playbook(): + with open(os.path.realpath(' '.join(map(str,glob.glob('molecule.*')))), 'r') as yamlfile: + data = yaml.load(yamlfile, Loader=yaml.FullLoader) + if 'playbooks' in data['provisioner'].keys(): + if 'converge' in data['provisioner']['playbooks'].keys(): + return data['provisioner']['playbooks']['converge'] + else: + return ' '.join(map(str,glob.glob('converge.*'))) + +def test_ssh_config(host): + for vars in read_playbook(get_playbook()): + assert host.file(vars['ssh_bastion_confing__name']).exists + assert host.file(vars['ssh_bastion_confing__name']).is_file diff --git a/roles/bastion-ssh-config/tasks/main.yml b/roles/bastion-ssh-config/tasks/main.yml index d638e539e..a18291b3b 100644 --- a/roles/bastion-ssh-config/tasks/main.yml +++ b/roles/bastion-ssh-config/tasks/main.yml @@ -17,6 +17,6 @@ delegate_to: localhost connection: local template: - src: ssh-bastion.conf - dest: "{{ playbook_dir }}/ssh-bastion.conf" + src: "{{ ssh_bastion_confing__name }}.j2" + dest: "{{ playbook_dir }}/{{ ssh_bastion_confing__name }}" mode: 0640 diff --git a/roles/bastion-ssh-config/templates/ssh-bastion.conf b/roles/bastion-ssh-config/templates/ssh-bastion.conf.j2 similarity index 100% rename from roles/bastion-ssh-config/templates/ssh-bastion.conf rename to roles/bastion-ssh-config/templates/ssh-bastion.conf.j2