From d9672a9b19b706041362a3dec212be2658c97f1b Mon Sep 17 00:00:00 2001 From: Emilien Mantel Date: Mon, 26 Aug 2024 14:59:55 +0200 Subject: [PATCH] :hammer: Add molecule --- molecule/_shared/Dockerfile.j2 | 19 ++++++++ molecule/_shared/base.yml | 38 +++++++++++++++ molecule/_shared/converge.yml | 34 ++++++++++++++ {tests => molecule/_shared/files}/import1.sql | 1 - .../_shared/inventory}/group_vars/all.yml | 1 + .../_shared/inventory/group_vars/galera.yml | 4 ++ .../_shared/inventory}/group_vars/master.yml | 1 - .../_shared/inventory}/group_vars/slave.yml | 3 +- molecule/_shared/prepare.yml | 21 +++++++++ molecule/_shared/verify.yml | 32 +++++++++++++ molecule/debian12_master_slave/molecule.yml | 46 +++++++++++++++++++ tests/group_vars/galera.yml | 7 --- 12 files changed, 197 insertions(+), 10 deletions(-) create mode 100644 molecule/_shared/Dockerfile.j2 create mode 100644 molecule/_shared/base.yml create mode 100644 molecule/_shared/converge.yml rename {tests => molecule/_shared/files}/import1.sql (99%) rename {tests => molecule/_shared/inventory}/group_vars/all.yml (87%) create mode 100644 molecule/_shared/inventory/group_vars/galera.yml rename {tests => molecule/_shared/inventory}/group_vars/master.yml (94%) rename {tests => molecule/_shared/inventory}/group_vars/slave.yml (65%) create mode 100644 molecule/_shared/prepare.yml create mode 100644 molecule/_shared/verify.yml create mode 100644 molecule/debian12_master_slave/molecule.yml delete mode 100644 tests/group_vars/galera.yml diff --git a/molecule/_shared/Dockerfile.j2 b/molecule/_shared/Dockerfile.j2 new file mode 100644 index 0000000..ca98098 --- /dev/null +++ b/molecule/_shared/Dockerfile.j2 @@ -0,0 +1,19 @@ +# Molecule managed + +{% if item.registry is defined %} +FROM {{ item.registry.url }}/{{ item.image }} +{% else %} +FROM {{ item.image }} +{% endif %} + +{% if item.env is defined %} +{% for var, value in item.env.items() %} +{% if value %} +ENV {{ var }} {{ value }} +{% endif %} +{% endfor %} +{% endif %} + +RUN apt-get update && \ + apt-get install -y python3 sudo bash ca-certificates iproute2 python-apt-common \ + && apt-get clean \ No newline at end of file diff --git a/molecule/_shared/base.yml b/molecule/_shared/base.yml new file mode 100644 index 0000000..61cc729 --- /dev/null +++ b/molecule/_shared/base.yml @@ -0,0 +1,38 @@ +--- + +scenario: + test_sequence: + - dependency + - syntax + - create + - prepare + - converge + - idempotence + - verify + - destroy +driver: + name: docker +role_name_check: 1 +provisioner: + name: ansible + config_options: + defaults: + deprecation_warnings: false + callback_whitelist: timer,profile_tasks + fact_caching: jsonfile + fact_caching_connection: ./cache + forks: 100 + connection: + pipelining: true + playbooks: + converge: ../_shared/converge.yml + prepare: ../_shared/prepare.yml + verify: ../_shared/verify.yml + inventory: + group_vars: + all: + is_dev: true + links: + group_vars: ../_shared/inventory/group_vars/ +verifier: + name: ansible diff --git a/molecule/_shared/converge.yml b/molecule/_shared/converge.yml new file mode 100644 index 0000000..1bc8dd0 --- /dev/null +++ b/molecule/_shared/converge.yml @@ -0,0 +1,34 @@ +--- + +# Note: master must be converged first +- name: Converge master + hosts: master + gather_facts: true + tasks: + - name: Include role + ansible.builtin.include_role: + name: "hanxhx.mysql" + + - name: COPY | Deploy first dump + ansible.builtin.copy: + src: files/import1.sql + dest: /tmp/import1.sql + mode: 0644 + owner: root + group: root + + - name: MYSQL_DB | Import first dump + community.mysql.mysql_db: + name: "{{ item }}" + state: import + target: /tmp/import1.sql + login_unix_socket: "/run/mysqld/mysqld.sock" + loop: "{{ mariadb_databases }}" + +- name: Converge slave + hosts: slave + gather_facts: true + tasks: + - name: Include role + ansible.builtin.include_role: + name: "hanxhx.mysql" diff --git a/tests/import1.sql b/molecule/_shared/files/import1.sql similarity index 99% rename from tests/import1.sql rename to molecule/_shared/files/import1.sql index d5f6f8a..29f1e79 100644 --- a/tests/import1.sql +++ b/molecule/_shared/files/import1.sql @@ -9,4 +9,3 @@ INSERT IGNORE INTO `user` (`id`,`email`) VALUES (11,"vitae.dolor@rutrumnon.net") INSERT IGNORE INTO `user` (`id`,`email`) VALUES (21,"aliquam@atpretium.co.uk"),(22,"cursus@sapienmolestie.edu"),(23,"ornare.Fusce@pede.ca"),(24,"at@estac.co.uk"),(25,"sed@risusodioauctor.ca"),(26,"sit.amet.consectetuer@necorciDonec.com"),(27,"Nulla@infaucibus.co.uk"),(28,"tempus.scelerisque@utlacusNulla.com"),(29,"pellentesque.a@nostraper.com"),(30,"libero.et.tristique@Nunclaoreet.co.uk"); INSERT IGNORE INTO `user` (`id`,`email`) VALUES (31,"dolor.sit.amet@Donecelementum.net"),(32,"ornare@massanonante.ca"),(33,"quam.elementum@semper.net"),(34,"Duis.ac.arcu@Integermollis.com"),(35,"magna.Sed.eu@magnaPraesent.co.uk"),(36,"Mauris.eu.turpis@mattis.ca"),(37,"ornare.facilisis.eget@urna.net"),(38,"Sed.diam.lorem@fringilla.co.uk"),(39,"pellentesque.Sed.dictum@Donec.edu"),(40,"faucibus.Morbi@nuncid.com"); INSERT IGNORE INTO `user` (`id`,`email`) VALUES (41,"ullamcorper@at.co.uk"),(42,"nec.ante.blandit@utcursus.edu"),(43,"diam.vel.arcu@egestasFuscealiquet.com"),(44,"ridiculus@musAeneaneget.com"),(45,"ad@turpisvitaepurus.ca"),(46,"ultrices.posuere.cubilia@purusMaecenas.net"),(47,"luctus@libero.org"),(48,"mi@elitNulla.com"),(49,"elementum@ipsumdolorsit.edu"),(50,"orci@Donec.co.uk"); - diff --git a/tests/group_vars/all.yml b/molecule/_shared/inventory/group_vars/all.yml similarity index 87% rename from tests/group_vars/all.yml rename to molecule/_shared/inventory/group_vars/all.yml index 9d27d19..d751075 100644 --- a/tests/group_vars/all.yml +++ b/molecule/_shared/inventory/group_vars/all.yml @@ -5,3 +5,4 @@ mariadb_extra_configuration: innodb_commit_concurrency: 0 mariadb_install_xtrabackup_package: true mariadb_slow_query_log_enabled: true +mariadb_debug_role: true diff --git a/molecule/_shared/inventory/group_vars/galera.yml b/molecule/_shared/inventory/group_vars/galera.yml new file mode 100644 index 0000000..1b540db --- /dev/null +++ b/molecule/_shared/inventory/group_vars/galera.yml @@ -0,0 +1,4 @@ +--- + +mariadb_use_galera: true +mariadb_galera_members: "{{ groups['galera'] }}" diff --git a/tests/group_vars/master.yml b/molecule/_shared/inventory/group_vars/master.yml similarity index 94% rename from tests/group_vars/master.yml rename to molecule/_shared/inventory/group_vars/master.yml index c2571cc..94fa271 100644 --- a/tests/group_vars/master.yml +++ b/molecule/_shared/inventory/group_vars/master.yml @@ -15,6 +15,5 @@ mariadb_users: host: '%' mariadb_replication_master: true mariadb_replication_slave: false -mariadb_server_id: 1 # vim: set ft=yaml : diff --git a/tests/group_vars/slave.yml b/molecule/_shared/inventory/group_vars/slave.yml similarity index 65% rename from tests/group_vars/slave.yml rename to molecule/_shared/inventory/group_vars/slave.yml index b325ce0..b8cd3b3 100644 --- a/tests/group_vars/slave.yml +++ b/molecule/_shared/inventory/group_vars/slave.yml @@ -4,7 +4,8 @@ mariadb_replication_master: false mariadb_replication_slave: true mariadb_replication_user: 'replication' mariadb_replication_password: '1a2b3c' -mariadb_server_id: 2 mariadb_slave_ignore_db: ['norepl'] +mariadb_slave_import_from: "{{ groups['master'][0] }}" +mariadb_replication_host: "{{ groups['master'][0] }}" # vim: set ft=yaml : diff --git a/molecule/_shared/prepare.yml b/molecule/_shared/prepare.yml new file mode 100644 index 0000000..eb19519 --- /dev/null +++ b/molecule/_shared/prepare.yml @@ -0,0 +1,21 @@ +--- + +- name: Prepare hosts + hosts: all + gather_facts: false + tasks: + - name: APT | Install some packages + ansible.builtin.apt: + name: "{{ p }}" + update_cache: true + cache_valid_time: 3600 + vars: + p: ['ca-certificates', 'curl', 'rsync', 'rsyslog', 'ssh', 'strace', 'vim'] + + - name: SERVICE | Ensure daemon are started + ansible.builtin.service: + name: "{{ item }}" + state: started + loop: + - rsyslog + - ssh diff --git a/molecule/_shared/verify.yml b/molecule/_shared/verify.yml new file mode 100644 index 0000000..baf36d3 --- /dev/null +++ b/molecule/_shared/verify.yml @@ -0,0 +1,32 @@ +--- + +- name: Verify slave + hosts: slave + gather_facts: false + tasks: + - name: MYSQL_REPLICATION | Get slave infos + community.mysql.mysql_replication: + mode: getreplica + register: slave + + - name: ASSERT | If slave threads are not running + ansible.builtin.assert: + that: + - slave.Slave_IO_Running == 'Yes' + - slave.Slave_SQL_Running == 'Yes' + fail_msg: "Slave issue, please check" + success_msg: "Slave is running" + + - name: Check replication data + community.mysql.mysql_query: + login_db: testrepl + query: 'SELECT COUNT(*) AS c FROM user' + login_unix_socket: /run/mysqld/mysqld.sock + register: result + + - name: ASSERT | Check replication data + ansible.builtin.assert: + that: + - result.query_result.0.0.c == 50 + fail_msg: "Replication data is not correct" + success_msg: "Replication data is correct" diff --git a/molecule/debian12_master_slave/molecule.yml b/molecule/debian12_master_slave/molecule.yml new file mode 100644 index 0000000..0d8ba01 --- /dev/null +++ b/molecule/debian12_master_slave/molecule.yml @@ -0,0 +1,46 @@ +--- + +platforms: + - name: debian12-master1 + image: dokken/debian-12 + command: /lib/systemd/systemd + dockerfile: ../_shared/Dockerfile.j2 + capabilities: + - SYS_ADMIN + cgroupns_mode: host + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:rw + privileged: true + networks: + - name: "00-molecule-d12-ms" + ipv4_address: '172.16.50.1' + docker_networks: + - name: "00-molecule-d12-ms" + ipam_config: + - subnet: "172.16.50.0/24" + gateway: "172.16.50.254" + groups: + - master + + - name: debian12-slave1 + image: dokken/debian-12 + command: /lib/systemd/systemd + dockerfile: ../_shared/Dockerfile.j2 + capabilities: + - SYS_ADMIN + cgroupns_mode: host + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:rw + privileged: true + networks: + - name: "00-molecule-d12-ms" + ipv4_address: '172.16.50.2' + groups: + - slave + +provisioner: + inventory: + host_vars: ~ + group_vars: + master: ~ + slave: ~ diff --git a/tests/group_vars/galera.yml b/tests/group_vars/galera.yml deleted file mode 100644 index 8bd732f..0000000 --- a/tests/group_vars/galera.yml +++ /dev/null @@ -1,7 +0,0 @@ ---- - -mariadb_use_galera: true -mariadb_galera_members: - - '{% if is_docker %}docker-{% else %}vbox-{% endif %}{{ ansible_distribution_release }}-{{ mariadb_origin }}-galera-1' - - '{% if is_docker %}docker-{% else %}vbox-{% endif %}{{ ansible_distribution_release }}-{{ mariadb_origin }}-galera-2' - - '{% if is_docker %}docker-{% else %}vbox-{% endif %}{{ ansible_distribution_release }}-{{ mariadb_origin }}-galera-3'