Skip to content

Commit 2609b41

Browse files
committed
Switch tests to use Molecule.
1 parent 5884983 commit 2609b41

18 files changed

+128
-142
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.retry
2-
tests/test.sh
2+
*/__pycache__
3+
*.pyc

.travis.yml

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,30 @@
11
---
2+
language: python
23
services: docker
34

45
env:
5-
- distro: centos7
6-
playbook: centos-7-test.yml
7-
- distro: centos6
8-
playbook: test.yml
9-
- distro: debian8
10-
playbook: test.yml
11-
- distro: ubuntu1604
12-
playbook: test.yml
13-
- distro: ubuntu1404
14-
playbook: test.yml
6+
global:
7+
- ROLE_NAME: mysql
8+
matrix:
9+
- MOLECULE_DISTRO: centos7
10+
MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd
11+
- MOLECULE_DISTRO: centos6
12+
- MOLECULE_DISTRO: ubuntu1604
13+
- MOLECULE_DISTRO: debian8
14+
15+
install:
16+
# Install test dependencies.
17+
- pip install molecule docker
18+
19+
before_script:
20+
# Use actual Ansible Galaxy role name for the project directory.
21+
- cd ../
22+
- mv ansible-role-$ROLE_NAME geerlingguy.$ROLE_NAME
23+
- cd geerlingguy.$ROLE_NAME
1524

1625
script:
17-
# Configure test script so we can run extra tests after playbook is run.
18-
- export container_id=$(date +%s)
19-
- export cleanup=false
20-
21-
# Download test shim.
22-
- wget -O ${PWD}/tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/
23-
- chmod +x ${PWD}/tests/test.sh
24-
2526
# Run tests.
26-
- ${PWD}/tests/test.sh
27-
28-
# Some MySQL debugging (show all the logs).
29-
- docker exec --tty ${container_id} env TERM=xterm ls -lah /var/log
30-
- docker exec --tty ${container_id} env TERM=xterm cat /var/log/mysql/error.log || true
31-
- docker exec --tty ${container_id} env TERM=xterm cat /var/log/mysql.err || true
32-
33-
# Check to make sure we can connect to MySQL via Unix socket.
34-
- >
35-
sudo docker exec ${container_id} mysql -u root -proot -e 'show databases;'
36-
| grep -q 'information_schema'
37-
&& (echo 'MySQL running normally' && exit 0)
38-
|| (echo 'MySQL not running' && exit 1)
39-
40-
# Check to make sure we can connect to MySQL via TCP.
41-
- >
42-
sudo docker exec ${container_id} mysql -u root -proot -h 127.0.0.1 -e 'show databases;'
43-
| grep -q 'information_schema'
44-
&& (echo 'MySQL running normally' && exit 0)
45-
|| (echo 'MySQL not running' && exit 1)
46-
47-
after_failure:
48-
# Check MySQL settings.
49-
- 'docker exec --tty ${container_id} env TERM=xterm cat /var/log/mysql/error.log'
50-
- 'docker exec --tty ${container_id} env TERM=xterm cat /var/log/mysql.err'
51-
- 'docker exec --tty ${container_id} env TERM=xterm cat /var/log/mysql.log'
27+
- molecule test
5228

5329
notifications:
5430
webhooks: https://galaxy.ansible.com/api/v1/notifications/

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ The home directory inside which Python MySQL settings will be stored, which Ansi
2929

3030
The MySQL root user account details.
3131

32-
mysql_root_password_update: no
32+
mysql_root_password_update: false
3333

3434
Whether to force update the MySQL root user's password. By default, this role will only change the root user's password when MySQL is first configured. You can force an update by setting this to `yes`.
3535

3636
> Note: If you get an error like `ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)` after a failed or interrupted playbook run, this usually means the root password wasn't originally updated to begin with. Try either removing the `.my.cnf` file inside the configured `mysql_user_home` or updating it and setting `password=''` (the insecure default password). Run the playbook again, with `mysql_root_password_update` set to `yes`, and the setup should complete.
3737
3838
> Note: If you get an error like `ERROR 1698 (28000): Access denied for user 'root'@'localhost' (using password: YES)` when trying to log in from the CLI you might need to run as root or sudoer.
3939
40-
mysql_enabled_on_startup: yes
40+
mysql_enabled_on_startup: true
4141

4242
Whether MySQL should be enabled on startup.
4343

@@ -46,7 +46,7 @@ Whether MySQL should be enabled on startup.
4646

4747
The main my.cnf configuration file and include directory.
4848

49-
overwrite_global_mycnf: yes
49+
overwrite_global_mycnf: true
5050

5151
Whether the global my.cnf should be overwritten each time this role is run. Setting this to `no` tells Ansible to only create the `my.cnf` file if it doesn't exist. This should be left at its default value (`yes`) if you'd like to use this role's variables to configure MySQL.
5252

@@ -99,7 +99,7 @@ Default MySQL connection configuration.
9999

100100
MySQL logging configuration. Setting `mysql_log` (the general query log) or `mysql_log_error` to `syslog` will make MySQL log to syslog using the `mysql_syslog_tag`.
101101

102-
mysql_slow_query_log_enabled: no
102+
mysql_slow_query_log_enabled: false
103103
mysql_slow_query_log_file: *default value depends on OS*
104104
mysql_slow_query_time: 2
105105

defaults/main.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ mysql_root_home: /root
1010
mysql_root_username: root
1111
mysql_root_password: root
1212

13-
# Set this to `yes` to forcibly update the root password.
14-
mysql_root_password_update: no
15-
mysql_user_password_update: no
13+
# Set this to `true` to forcibly update the root password.
14+
mysql_root_password_update: false
15+
mysql_user_password_update: false
1616

17-
mysql_enabled_on_startup: yes
17+
mysql_enabled_on_startup: true
1818

19-
# update my.cnf. each time role is run? yes | no
20-
overwrite_global_mycnf: yes
19+
# Whether my.cnf should be updated on every run.
20+
overwrite_global_mycnf: true
2121

2222
# The following variables have a default value depending on operating system.
2323
# mysql_config_file: /etc/my.cnf
@@ -37,7 +37,7 @@ mysql_enablerepo: ""
3737
# MySQL connection settings.
3838
mysql_port: "3306"
3939
mysql_bind_address: '0.0.0.0'
40-
mysql_skip_name_resolve: no
40+
mysql_skip_name_resolve: false
4141
mysql_datadir: /var/lib/mysql
4242
mysql_sql_mode: ''
4343
# The following variables have a default value depending on operating system.
@@ -48,7 +48,7 @@ mysql_sql_mode: ''
4848
mysql_log_file_group: mysql
4949

5050
# Slow query log settings.
51-
mysql_slow_query_log_enabled: no
51+
mysql_slow_query_log_enabled: false
5252
mysql_slow_query_time: "2"
5353
# The following variable has a default value depending on operating system.
5454
# mysql_slow_query_log_file: /var/log/mysql-slow.log

meta/main.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ galaxy_info:
1010
platforms:
1111
- name: EL
1212
versions:
13-
- 6
14-
- 7
13+
- 6
14+
- 7
1515
- name: Ubuntu
1616
versions:
17-
- all
17+
- all
1818
- name: Debian
1919
versions:
20-
- all
20+
- all
2121
- name: Archlinux
2222
versions:
23-
- all
23+
- all
2424
galaxy_tags:
2525
- database
26+
- mysql
27+
- mariadb
28+
- db
29+
- sql

molecule/default/molecule.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
dependency:
3+
name: galaxy
4+
driver:
5+
name: docker
6+
lint:
7+
name: yamllint
8+
options:
9+
config-file: molecule/default/yaml-lint.yml
10+
platforms:
11+
- name: instance
12+
image: geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible
13+
command: ${MOLECULE_DOCKER_COMMAND:-"sleep infinity"}
14+
privileged: true
15+
pre_build_image: true
16+
provisioner:
17+
name: ansible
18+
lint:
19+
name: ansible-lint
20+
playbooks:
21+
converge: ${MOLECULE_PLAYBOOK:-playbook.yml}
22+
scenario:
23+
name: default
24+
verifier:
25+
name: testinfra
26+
lint:
27+
name: flake8

molecule/default/playbook.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
- name: Converge
3+
hosts: all
4+
become: true
5+
6+
roles:
7+
- role: geerlingguy.mysql
8+
9+
post_tasks:
10+
- name: Make sure we can connect to MySQL via Unix socket.
11+
command: "mysql -u root -proot -e 'show databases;'"
12+
changed_when: false
13+
14+
- name: Make sure we can connect to MySQL via TCP.
15+
command: "mysql -u root -proot -h 127.0.0.1 -e 'show databases;'"
16+
changed_when: false
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
3+
import testinfra.utils.ansible_runner
4+
5+
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
6+
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
7+
8+
9+
def test_hosts_file(host):
10+
f = host.file('/etc/hosts')
11+
12+
assert f.exists
13+
assert f.user == 'root'
14+
assert f.group == 'root'

molecule/default/yaml-lint.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
extends: default
3+
rules:
4+
line-length:
5+
max: 150
6+
level: warning

tasks/configure.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
command: 'mysql --version'
44
register: mysql_cli_version
55
changed_when: false
6-
check_mode: no
6+
check_mode: false
77

88
- name: Copy my.cnf global MySQL configuration.
99
template:
@@ -39,7 +39,7 @@
3939
command: "touch {{ mysql_slow_query_log_file }}"
4040
args:
4141
creates: "{{ mysql_slow_query_log_file }}"
42-
warn: no
42+
warn: false
4343
when: mysql_slow_query_log_enabled
4444

4545
- name: Create datadir if it does not exist
@@ -48,7 +48,7 @@
4848
state: directory
4949
owner: mysql
5050
group: mysql
51-
mode: 0755
51+
mode: 0755
5252
setype: mysqld_db_t
5353

5454
- name: Set ownership on slow query log file (if configured).
@@ -64,7 +64,7 @@
6464
command: "touch {{ mysql_log_error }}"
6565
args:
6666
creates: "{{ mysql_log_error }}"
67-
warn: no
67+
warn: false
6868
when: mysql_log == "" and mysql_log_error != ""
6969

7070
- name: Set ownership on error log file (if configured).

tasks/replication.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
master_password: "{{ mysql_replication_user.password }}"
4040
master_log_file: "{{ master.File }}"
4141
master_log_pos: "{{ master.Position }}"
42-
ignore_errors: True
42+
ignore_errors: true
4343
when: >
4444
((slave.Is_Slave is defined and not slave.Is_Slave) or (slave.Is_Slave is not defined and slave is failed))
4545
and (mysql_replication_role == 'slave')

tasks/secure-installation.yml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
dest: "{{ mysql_user_home }}/.my.cnf"
1616
owner: "{{ mysql_user_name }}"
1717
mode: 0600
18-
when: mysql_user_name != mysql_root_username and (mysql_install_packages | bool or mysql_user_password_update)
18+
when: >
19+
mysql_user_name != mysql_root_username
20+
and (mysql_install_packages | bool or mysql_user_password_update)
1921
2022
- name: Disallow root login remotely
2123
command: 'mysql -NBe "{{ item }}"'
@@ -27,7 +29,7 @@
2729
command: mysql -NBe "SELECT Host FROM mysql.user WHERE User = '{{ mysql_root_username }}' ORDER BY (Host='localhost') ASC"
2830
register: mysql_root_hosts
2931
changed_when: false
30-
check_mode: no
32+
check_mode: false
3133
when: mysql_install_packages | bool or mysql_root_password_update
3234

3335
# Note: We do not use mysql_user for this operation, as it doesn't always update
@@ -38,15 +40,19 @@
3840
mysql -u root -NBe
3941
'ALTER USER "{{ mysql_root_username }}"@"{{ item }}" IDENTIFIED WITH mysql_native_password BY "{{ mysql_root_password }}";'
4042
with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}"
41-
when: ((mysql_install_packages | bool) or mysql_root_password_update) and ('5.7.' in mysql_cli_version.stdout or '8.0.' in mysql_cli_version.stdout)
43+
when: >
44+
((mysql_install_packages | bool) or mysql_root_password_update)
45+
and ('5.7.' in mysql_cli_version.stdout or '8.0.' in mysql_cli_version.stdout)
4246
4347
# Set root password for MySQL < 5.7.x.
4448
- name: Update MySQL root password for localhost root account (< 5.7.x).
4549
shell: >
4650
mysql -NBe
4751
'SET PASSWORD FOR "{{ mysql_root_username }}"@"{{ item }}" = PASSWORD("{{ mysql_root_password }}");'
4852
with_items: "{{ mysql_root_hosts.stdout_lines|default([]) }}"
49-
when: ((mysql_install_packages | bool) or mysql_root_password_update) and ('5.7.' not in mysql_cli_version.stdout and '8.0.' not in mysql_cli_version.stdout)
53+
when: >
54+
((mysql_install_packages | bool) or mysql_root_password_update)
55+
and ('5.7.' not in mysql_cli_version.stdout and '8.0.' not in mysql_cli_version.stdout)
5056
5157
# Has to be after the root password assignment, for idempotency.
5258
- name: Copy .my.cnf file with root password credentials.
@@ -62,13 +68,13 @@
6268
command: mysql -NBe 'SELECT Host FROM mysql.user WHERE User = ""'
6369
register: mysql_anonymous_hosts
6470
changed_when: false
65-
check_mode: no
71+
check_mode: false
6672

6773
- name: Remove anonymous MySQL users.
6874
mysql_user:
69-
name: ""
70-
host: "{{ item }}"
71-
state: absent
75+
name: ""
76+
host: "{{ item }}"
77+
state: absent
7278
with_items: "{{ mysql_anonymous_hosts.stdout_lines|default([]) }}"
7379

7480
- name: Remove MySQL test database.

tasks/setup-Debian.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
- name: Delete innodb log files created by apt package after initial install.
2929
file: path={{ mysql_datadir }}/{{item}} state=absent
3030
with_items:
31-
- "ib_logfile0"
32-
- "ib_logfile1"
31+
- "ib_logfile0"
32+
- "ib_logfile1"
3333
when: mysql_installed.stat.exists == false

tasks/variables.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
include_vars: "{{ item }}"
55
with_first_found:
66
- files:
7-
- "vars/{{ ansible_os_family }}.yml"
7+
- "vars/{{ ansible_os_family }}.yml"
88
skip: true
99
when: ansible_os_family != "RedHat"
1010

tests/README.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/centos-7-test.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)