diff --git a/defaults/main.yml b/defaults/main.yml index 2849199..7365769 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -86,7 +86,7 @@ pdns_backends: # - "localhost" # Full path to mysql schema file -pdns_mysql_schema_file: +pdns_mysql_debian_schema_file: os: /usr/share/dbconfig-common/data/pdns-backend-mysql/install/mysql powerdns: /usr/share/doc/pdns-backend-mysql/schema.mysql.sql diff --git a/molecule.yml b/molecule.yml index b7e3372..50612c5 100644 --- a/molecule.yml +++ b/molecule.yml @@ -18,7 +18,22 @@ ansible: gsqlite3: database: '/var/lib/powerdns/pdns.db' dnssec: yes - + mysql: + - pdns_repo_provider: 'powerdns' + pdns_repo_branch: '40' + pdns_backends_mysql_credential: + gmysql: + priv_user: 'root' + priv_password: '' + priv_host: + - '%' + - 'localhost' + pdns_backends: + gmysql: + host: 'localhost' + dbname: pdns + user: pdns + password: pdns driver: name: vagrant @@ -43,3 +58,6 @@ vagrant: - name: sqlite ansible_groups: - sqlite + - name: mysql + ansible_groups: + - mysql diff --git a/tasks/database-mysql.yml b/tasks/database-mysql.yml index bdef83b..fa091e1 100644 --- a/tasks/database-mysql.yml +++ b/tasks/database-mysql.yml @@ -59,6 +59,23 @@ tags: - pdns-mysql-db +- name: Get the version of the installed MySQL backend + yum_madison: + name: 'pdns-backend-mysql' + register: pdns_mysql_backend_version + changed_when: False + when: pdns_backends_mysql_credential is defined and ansible_os_family == "RedHat" + +- name: Set the directory for the schema (RedHat) + set_fact: + pdns_mysql_schema_file: "/usr/share/doc/pdns-backend-mysql-{{ pdns_mysql_backend_version.versions | map(attribute='version') | first | regex_replace('-[^-]*$', '')}}/schema.mysql.sql" + when: pdns_backends_mysql_credential is defined and ansible_os_family == "RedHat" + +- name: Set the directory for the schema (Debian) + set_fact: + pdns_mysql_schema_file: "{{pdns_mysql_debian_schema_file[pdns_repo_provider]}}" + when: pdns_backends_mysql_credential is defined and ansible_os_family == "Debian" + - name: Import initial mysql schema mysql_db: login_user: "{{ item.item.value.user }}" @@ -66,7 +83,7 @@ login_host: "{{ item.item.value.host }}" name: "{{ item.item.value.dbname }}" state: import - target: "{{ pdns_mysql_schema_file[pdns_repo_provider] }}" + target: "{{ pdns_mysql_schema_file }}" when: item.item.key.split(':')[0] == 'gmysql' and item.stdout == "0" with_items: "{{ pdns_check_mysql_db.results }}" tags: diff --git a/tests/playbook.yml b/tests/playbook.yml index 21ec71f..77e5c00 100644 --- a/tests/playbook.yml +++ b/tests/playbook.yml @@ -1,4 +1,22 @@ --- +- hosts: mysql + tasks: + - name: Install mariadb (RedHat) + yum: + name: "{{ item }}" + with_items: ['mariadb', 'mariadb-server', 'MySQL-python'] + when: ansible_os_family == 'RedHat' + - name: start mariadb (RedHat) + service: + name: mariadb + state: running + when: ansible_os_family == 'RedHat' + - name: Install MySQL (Debian) + apt: + name: "{{ item }}" + with_items: ['mysql-client', 'mysql-server', 'python-mysqldb'] + when: ansible_os_family == 'Debian' + - hosts: all roles: - { role: pdns-ansible } diff --git a/tests/test_mysql.py b/tests/test_mysql.py new file mode 100644 index 0000000..3a01c6c --- /dev/null +++ b/tests/test_mysql.py @@ -0,0 +1,46 @@ +import testinfra.utils.ansible_runner +import pytest + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + '.molecule/ansible_inventory').get_hosts('mysql') + +debian_os = ['debian', 'ubuntu'] +rhel_os = ['redhat', 'centos'] + + +def test_package(Package): + p = Package('pdns-backend-mysql') + + assert p.is_installed + + +def test_config(File, SystemInfo, Sudo): + with Sudo(): + f = None + if SystemInfo.distribution in debian_os: + f = File('/etc/powerdns/pdns.conf') + if SystemInfo.distribution in rhel_os: + f = File('/etc/pdns/pdns.conf') + + assert f.exists + assert 'launch+=gmysql' in f.content + assert 'gmysql-host=localhost' in f.content + assert 'gmysql-password=pdns' in f.content + assert 'gmysql-dbname=pdns' in f.content + assert 'gmysql-user=pdns' in f.content + + +@pytest.mark.parametrize("table", [ + ('domains'), + ('records'), + ('supermasters'), + ('comments'), + ('domainmetadata'), + ('cryptokeys'), + ('tsigkeys'), +]) +def test_database_exists(File, Sudo, table): + with Sudo(): + f = File('/var/lib/mysql/pdns/%s.frm' % table) + + assert f.exists