Skip to content

Commit

Permalink
Test the creation MySQL db import
Browse files Browse the repository at this point in the history
And fix this for CentOS in the process :).
  • Loading branch information
pieterlexis committed Jan 6, 2017
1 parent 192c92f commit afcec3a
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
2 changes: 1 addition & 1 deletion defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 19 additions & 1 deletion molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,3 +58,6 @@ vagrant:
- name: sqlite
ansible_groups:
- sqlite
- name: mysql
ansible_groups:
- mysql
19 changes: 18 additions & 1 deletion tasks/database-mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,31 @@
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 }}"
login_password: "{{ item.item.value.password }}"
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:
Expand Down
18 changes: 18 additions & 0 deletions tests/playbook.yml
Original file line number Diff line number Diff line change
@@ -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 }
46 changes: 46 additions & 0 deletions tests/test_mysql.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit afcec3a

Please sign in to comment.