From 675f4650c9f66ee66b2515d29fce89719b88fe82 Mon Sep 17 00:00:00 2001 From: Bertrand Lanson Date: Thu, 30 Nov 2023 22:24:37 +0100 Subject: [PATCH] feat: add vagrant tests for later, add become:true --- molecule/default_vagrant/converge.yml | 7 +++ molecule/default_vagrant/molecule.yml | 35 ++++++++++++++ molecule/default_vagrant/requirements.yml | 4 ++ molecule/default_vagrant/verify.yml | 46 +++++++++++++++++++ .../with_custom_ascii_vagrant/converge.yml | 7 +++ .../group_vars/all.yml | 12 +++++ .../with_custom_ascii_vagrant/molecule.yml | 35 ++++++++++++++ .../requirements.yml | 4 ++ molecule/with_custom_ascii_vagrant/verify.yml | 46 +++++++++++++++++++ tasks/cleanup.yml | 2 + tasks/configure.yml | 2 + 11 files changed, 200 insertions(+) create mode 100644 molecule/default_vagrant/converge.yml create mode 100644 molecule/default_vagrant/molecule.yml create mode 100644 molecule/default_vagrant/requirements.yml create mode 100644 molecule/default_vagrant/verify.yml create mode 100644 molecule/with_custom_ascii_vagrant/converge.yml create mode 100644 molecule/with_custom_ascii_vagrant/group_vars/all.yml create mode 100644 molecule/with_custom_ascii_vagrant/molecule.yml create mode 100644 molecule/with_custom_ascii_vagrant/requirements.yml create mode 100644 molecule/with_custom_ascii_vagrant/verify.yml diff --git a/molecule/default_vagrant/converge.yml b/molecule/default_vagrant/converge.yml new file mode 100644 index 0000000..8c00c25 --- /dev/null +++ b/molecule/default_vagrant/converge.yml @@ -0,0 +1,7 @@ +--- +- name: Converge + hosts: all + tasks: + - name: "Include ednxzu.update_motd" + ansible.builtin.include_role: + name: "ednxzu.update_motd" diff --git a/molecule/default_vagrant/molecule.yml b/molecule/default_vagrant/molecule.yml new file mode 100644 index 0000000..2b02360 --- /dev/null +++ b/molecule/default_vagrant/molecule.yml @@ -0,0 +1,35 @@ +--- +dependency: + name: galaxy + options: + requirements-file: ./requirements.yml +driver: + name: vagrant + provider: + name: libvirt +platforms: + - name: instance + box: generic/${MOLECULE_TEST_OS} + cpus: 4 + memory: 4096 +provisioner: + name: ansible + config_options: + defaults: + remote_tmp: /tmp/.ansible +verifier: + name: ansible +scenario: + name: default_vagrant + test_sequence: + - dependency + - cleanup + - destroy + - syntax + - create + - prepare + - converge + - idempotence + - verify + - cleanup + - destroy diff --git a/molecule/default_vagrant/requirements.yml b/molecule/default_vagrant/requirements.yml new file mode 100644 index 0000000..ca250b7 --- /dev/null +++ b/molecule/default_vagrant/requirements.yml @@ -0,0 +1,4 @@ +--- +# requirements file for molecule +roles: + - name: ednxzu.manage_apt_packages diff --git a/molecule/default_vagrant/verify.yml b/molecule/default_vagrant/verify.yml new file mode 100644 index 0000000..ac10650 --- /dev/null +++ b/molecule/default_vagrant/verify.yml @@ -0,0 +1,46 @@ +--- +- name: Verify + hosts: all + gather_facts: true + tasks: + - name: "Test: file /etc/hosts" + block: + - name: "Stat file /etc/hosts" + ansible.builtin.stat: + path: "/etc/hosts" + register: stat_etc_hosts + + - name: "Verify file /etc/hosts" + vars: + etc_hosts_group: + ubuntu: "adm" + debian: "root" + ansible.builtin.assert: + that: + - stat_etc_hosts.stat.exists + - stat_etc_hosts.stat.isreg + - stat_etc_hosts.stat.pw_name == 'root' + - stat_etc_hosts.stat.gr_name == etc_hosts_group[(ansible_distribution|lower)] + + - name: "Test: file /etc/profile.d/motd.cfg" + block: + - name: "Stat file /etc/profile.d/motd.cfg" + ansible.builtin.stat: + path: "/etc/profile.d/motd.cfg" + register: stat_etc_profile_d_motd_cfg + + - name: "Slurp file /etc/profile.d/motd.cfg" + ansible.builtin.slurp: + src: "/etc/profile.d/motd.cfg" + register: slurp_etc_profile_d_motd_cfg + + - name: "Verify file /etc/profile.d/motd.cfg" + ansible.builtin.assert: + that: + - stat_etc_profile_d_motd_cfg.stat.exists + - stat_etc_profile_d_motd_cfg.stat.isreg + - stat_etc_profile_d_motd_cfg.stat.pw_name == 'root' + - stat_etc_profile_d_motd_cfg.stat.gr_name == 'root' + - stat_etc_profile_d_motd_cfg.stat.mode == '0644' + - "'print_info()' in (slurp_etc_profile_d_motd_cfg.content|b64decode)" + - "'ascii_distro=\"auto\"' in (slurp_etc_profile_d_motd_cfg.content|b64decode)" diff --git a/molecule/with_custom_ascii_vagrant/converge.yml b/molecule/with_custom_ascii_vagrant/converge.yml new file mode 100644 index 0000000..8c00c25 --- /dev/null +++ b/molecule/with_custom_ascii_vagrant/converge.yml @@ -0,0 +1,7 @@ +--- +- name: Converge + hosts: all + tasks: + - name: "Include ednxzu.update_motd" + ansible.builtin.include_role: + name: "ednxzu.update_motd" diff --git a/molecule/with_custom_ascii_vagrant/group_vars/all.yml b/molecule/with_custom_ascii_vagrant/group_vars/all.yml new file mode 100644 index 0000000..2f8d634 --- /dev/null +++ b/molecule/with_custom_ascii_vagrant/group_vars/all.yml @@ -0,0 +1,12 @@ +--- +update_motd_filename: "00-motd-neofetch" +update_motd_print_info: + - name: "CPU" + module: "cpu" + - name: "Memory" + module: "memory" + - name: "Local IP" + module: local_ip + - name: "Disk" + module: "disk" +update_motd_ascii_distro: "{{ ansible_distribution|lower }}_small" diff --git a/molecule/with_custom_ascii_vagrant/molecule.yml b/molecule/with_custom_ascii_vagrant/molecule.yml new file mode 100644 index 0000000..ed30bfd --- /dev/null +++ b/molecule/with_custom_ascii_vagrant/molecule.yml @@ -0,0 +1,35 @@ +--- +dependency: + name: galaxy + options: + requirements-file: ./requirements.yml +driver: + name: vagrant + provider: + name: libvirt +platforms: + - name: instance + box: generic/${MOLECULE_TEST_OS} + cpus: 4 + memory: 4096 +provisioner: + name: ansible + config_options: + defaults: + remote_tmp: /tmp/.ansible +verifier: + name: ansible +scenario: + name: with_custom_ascii_vagrant + test_sequence: + - dependency + - cleanup + - destroy + - syntax + - create + - prepare + - converge + - idempotence + - verify + - cleanup + - destroy diff --git a/molecule/with_custom_ascii_vagrant/requirements.yml b/molecule/with_custom_ascii_vagrant/requirements.yml new file mode 100644 index 0000000..ca250b7 --- /dev/null +++ b/molecule/with_custom_ascii_vagrant/requirements.yml @@ -0,0 +1,4 @@ +--- +# requirements file for molecule +roles: + - name: ednxzu.manage_apt_packages diff --git a/molecule/with_custom_ascii_vagrant/verify.yml b/molecule/with_custom_ascii_vagrant/verify.yml new file mode 100644 index 0000000..8494668 --- /dev/null +++ b/molecule/with_custom_ascii_vagrant/verify.yml @@ -0,0 +1,46 @@ +--- +- name: Verify + hosts: all + gather_facts: true + tasks: + - name: "Test: file /etc/hosts" + block: + - name: "Stat file /etc/hosts" + ansible.builtin.stat: + path: "/etc/hosts" + register: stat_etc_hosts + + - name: "Verify file /etc/hosts" + vars: + etc_hosts_group: + ubuntu: "adm" + debian: "root" + ansible.builtin.assert: + that: + - stat_etc_hosts.stat.exists + - stat_etc_hosts.stat.isreg + - stat_etc_hosts.stat.pw_name == 'root' + - stat_etc_hosts.stat.gr_name == etc_hosts_group[(ansible_distribution|lower)] + + - name: "Test: file /etc/profile.d/motd.cfg" + block: + - name: "Stat file /etc/profile.d/motd.cfg" + ansible.builtin.stat: + path: "/etc/profile.d/motd.cfg" + register: stat_etc_profile_d_motd_cfg + + - name: "Slurp file /etc/profile.d/motd.cfg" + ansible.builtin.slurp: + src: "/etc/profile.d/motd.cfg" + register: slurp_etc_profile_d_motd_cfg + + - name: "Verify file /etc/profile.d/motd.cfg" + ansible.builtin.assert: + that: + - stat_etc_profile_d_motd_cfg.stat.exists + - stat_etc_profile_d_motd_cfg.stat.isreg + - stat_etc_profile_d_motd_cfg.stat.pw_name == 'root' + - stat_etc_profile_d_motd_cfg.stat.gr_name == 'root' + - stat_etc_profile_d_motd_cfg.stat.mode == '0644' + - "'print_info()' in (slurp_etc_profile_d_motd_cfg.content|b64decode)" + - "'ascii_distro=\"{{ ansible_distribution|lower|string }}_small\"' in (slurp_etc_profile_d_motd_cfg.content|b64decode)" diff --git a/tasks/cleanup.yml b/tasks/cleanup.yml index 3e32039..ec7b8a8 100644 --- a/tasks/cleanup.yml +++ b/tasks/cleanup.yml @@ -24,8 +24,10 @@ state: absent loop: "{{ collected_files.files + collected_directories.files }}" when: (item.path|basename) != update_motd_filename + become: true - name: "Remove /etc/motd file" ansible.builtin.file: path: "/etc/motd" state: absent + become: true diff --git a/tasks/configure.yml b/tasks/configure.yml index ddbe222..2e0616c 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -7,6 +7,7 @@ owner: root group: root mode: '0644' + become: true - name: "Copy motd script file" ansible.builtin.copy: @@ -15,3 +16,4 @@ mode: '0744' owner: root group: root + become: true