From d753376dda43b2e65ff91659043cb35fe70688c3 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Sun, 1 Mar 2020 11:49:25 +0100 Subject: [PATCH] test: replace kitchen to molecule --- .gitignore | 1 + .kitchen.yml | 27 ---- .travis.yml | 28 ++-- CHANGELOG.md | 4 + Gemfile | 8 -- Gemfile.lock | 133 ------------------ README.md | 21 +-- .../default/converge.yml | 5 +- molecule/default/molecule.yml | 18 +++ molecule/default/tests/test_default.py | 49 +++++++ .../default/serverspec/default_spec.rb | 68 --------- test/integration/inventory | 1 - 12 files changed, 92 insertions(+), 271 deletions(-) delete mode 100644 .kitchen.yml delete mode 100644 Gemfile delete mode 100644 Gemfile.lock rename test/integration/default/default.yml => molecule/default/converge.yml (90%) create mode 100644 molecule/default/molecule.yml create mode 100644 molecule/default/tests/test_default.py delete mode 100644 test/integration/default/serverspec/default_spec.rb delete mode 100644 test/integration/inventory diff --git a/.gitignore b/.gitignore index 79776e8..1331d18 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .kitchen/* +*.pyc diff --git a/.kitchen.yml b/.kitchen.yml deleted file mode 100644 index c30c24a..0000000 --- a/.kitchen.yml +++ /dev/null @@ -1,27 +0,0 @@ ---- -driver: - name: docker_cli - -transport: - name: docker_cli - -provisioner: - name: ansible_playbook - hosts: localhost - require_ansible_repo: false - require_ansible_omnibus: false - require_chef_for_busser: true - ansible_verbose: false - ansible_inventory: ./test/integration/inventory - -platforms: - - name: debian - driver_config: - image: "nishiki/debian<%= ENV['DEBIAN_VERSION'] ? ENV['DEBIAN_VERSION'] : '10' %>:ansible-<%= ENV['ANSIBLE_VERSION'] ? ENV['ANSIBLE_VERSION'] : '2.9' %>" - command: /bin/systemd - volume: - - /sys/fs/cgroup:/sys/fs/cgroup:ro - privileged: cap-add=SYS_ADMIN - -suites: - - name: default diff --git a/.travis.yml b/.travis.yml index 44fb128..e701353 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,29 +1,29 @@ --- sudo: required -language: ruby +dist: bionic +addons: + apt: + packages: + - python3 + - python3-pip + - python3-setuptools env: - - DEBIAN_VERSION=9 ANSIBLE_VERSION=2.8 - - DEBIAN_VERSION=9 ANSIBLE_VERSION=2.9 - - DEBIAN_VERSION=10 ANSIBLE_VERSION=2.8 - - DEBIAN_VERSION=10 ANSIBLE_VERSION=2.9 + - ANSIBLE_VERSION=2.7.16 + - ANSIBLE_VERSION=2.8.8 + - ANSIBLE_VERSION=2.9.4 services: - docker before_install: - - bundle install - - sudo pip install --upgrade pip - - sudo pip install yamllint - - sudo pip install ansible-lint + - sudo pip3 install ansible==${ANSIBLE_VERSION} + - sudo pip3 install molecule 'molecule[docker]' docker testinfra ansible-lint yamllint - git clone https://github.com/ansible/galaxy-lint-rules.git script: - - kitchen conv - - kitchen conv | grep changed=0 - - kitchen verify - - ansible-lint -r galaxy-lint-rules/rules . - - yamllint . + - ansible --version + - molecule test notifications: webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 4230439..933da72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/) ## [Unreleased] +### Changed + +- test: replace kitchen to molecule + ## [v2.0.0] - 2019-06-05 - breaking: remove support for ansible < 2.8 diff --git a/Gemfile b/Gemfile deleted file mode 100644 index 0d96441..0000000 --- a/Gemfile +++ /dev/null @@ -1,8 +0,0 @@ -source 'https://rubygems.org' - -group :development do - gem 'kitchen-ansible' - gem 'kitchen-docker_cli' - gem 'rubocop', '0.50.0' - gem 'test-kitchen' -end diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index 78f3028..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,133 +0,0 @@ -GEM - remote: https://rubygems.org/ - specs: - ast (2.4.0) - bcrypt_pbkdf (1.0.1) - builder (3.2.3) - ed25519 (1.2.4) - equatable (0.5.0) - erubis (2.7.0) - ffi (1.11.1) - gssapi (1.3.0) - ffi (>= 1.0.1) - gyoku (1.3.1) - builder (>= 2.1.2) - httpclient (2.8.3) - kitchen-ansible (0.50.0) - net-ssh (>= 3) - test-kitchen (>= 1.4) - kitchen-docker_cli (0.19.0) - test-kitchen (>= 1.3) - license-acceptance (1.0.12) - pastel (~> 0.7) - tomlrb (~> 1.2) - tty-box (~> 0.3) - tty-prompt (~> 0.18) - little-plugger (1.1.4) - logging (2.2.2) - little-plugger (~> 1.1) - multi_json (~> 1.10) - mixlib-install (3.11.18) - mixlib-shellout - mixlib-versioning - thor - mixlib-shellout (2.4.4) - mixlib-versioning (1.2.7) - multi_json (1.13.1) - necromancer (0.4.0) - net-scp (2.0.0) - net-ssh (>= 2.6.5, < 6.0.0) - net-ssh (5.2.0) - net-ssh-gateway (2.0.0) - net-ssh (>= 4.0.0) - nori (2.6.0) - parallel (1.17.0) - parser (2.6.3.0) - ast (~> 2.4.0) - pastel (0.7.2) - equatable (~> 0.5.0) - tty-color (~> 0.4.0) - powerpack (0.1.2) - rainbow (2.2.2) - rake - rake (12.3.2) - rubocop (0.50.0) - parallel (~> 1.10) - parser (>= 2.3.3.1, < 3.0) - powerpack (~> 0.1) - rainbow (>= 2.2.2, < 3.0) - ruby-progressbar (~> 1.7) - unicode-display_width (~> 1.0, >= 1.0.1) - ruby-progressbar (1.10.1) - rubyntlm (0.6.2) - rubyzip (1.2.3) - strings (0.1.5) - strings-ansi (~> 0.1) - unicode-display_width (~> 1.5) - unicode_utils (~> 1.4) - strings-ansi (0.1.0) - test-kitchen (2.2.5) - bcrypt_pbkdf (~> 1.0) - ed25519 (~> 1.2) - license-acceptance (~> 1.0, >= 1.0.11) - mixlib-install (~> 3.6) - mixlib-shellout (>= 1.2, < 3.0) - net-scp (>= 1.1, < 3.0) - net-ssh (>= 2.9, < 6.0) - net-ssh-gateway (>= 1.2, < 3.0) - thor (~> 0.19) - winrm (~> 2.0) - winrm-elevated (~> 1.0) - winrm-fs (~> 1.1) - thor (0.20.3) - timers (4.3.0) - tomlrb (1.2.8) - tty-box (0.3.0) - pastel (~> 0.7.2) - strings (~> 0.1.4) - tty-cursor (~> 0.6.0) - tty-color (0.4.3) - tty-cursor (0.6.1) - tty-prompt (0.18.1) - necromancer (~> 0.4.0) - pastel (~> 0.7.0) - timers (~> 4.0) - tty-cursor (~> 0.6.0) - tty-reader (~> 0.5.0) - tty-reader (0.5.0) - tty-cursor (~> 0.6.0) - tty-screen (~> 0.6.4) - wisper (~> 2.0.0) - tty-screen (0.6.5) - unicode-display_width (1.6.0) - unicode_utils (1.4.0) - winrm (2.3.2) - builder (>= 2.1.2) - erubis (~> 2.7) - gssapi (~> 1.2) - gyoku (~> 1.0) - httpclient (~> 2.2, >= 2.2.0.2) - logging (>= 1.6.1, < 3.0) - nori (~> 2.0) - rubyntlm (~> 0.6.0, >= 0.6.1) - winrm-elevated (1.1.1) - winrm (~> 2.0) - winrm-fs (~> 1.0) - winrm-fs (1.3.2) - erubis (~> 2.7) - logging (>= 1.6.1, < 3.0) - rubyzip (~> 1.1) - winrm (~> 2.0) - wisper (2.0.0) - -PLATFORMS - ruby - -DEPENDENCIES - kitchen-ansible - kitchen-docker_cli - rubocop (= 0.50.0) - test-kitchen - -BUNDLED WITH - 1.16.0 diff --git a/README.md b/README.md index ad09d70..57ff7c9 100644 --- a/README.md +++ b/README.md @@ -54,25 +54,12 @@ Install and configure MariaDB ## Development -### Test syntax with yamllint - -* install `python` and `python-pip` -* install yamllint `pip install yamllint` -* run `yamllint .` - -### Test syntax with ansible-lint - -* install `python` and `python-pip` -* install yamllint `pip install ansible-lint` -* run `ansible-lint .` - -### Tests with docker +### Test with molecule and docker * install [docker](https://docs.docker.com/engine/installation/) -* install ruby -* install bundler `gem install bundler` -* install dependencies `bundle install` -* run the tests `kitchen test` +* install `python3` and `python3-pip` +* install molecule and dependencies `pip3 install molecule 'molecule[docker]' docker ansible-lint testinfra yamllint` +* run `molecule test` ## License diff --git a/test/integration/default/default.yml b/molecule/default/converge.yml similarity index 90% rename from test/integration/default/default.yml rename to molecule/default/converge.yml index 99ddbde..dfde097 100644 --- a/test/integration/default/default.yml +++ b/molecule/default/converge.yml @@ -1,6 +1,6 @@ --- -- hosts: localhost - connection: local +- name: Converge + hosts: all vars: mariadb_autorestart: yes mariadb_master: yes @@ -18,6 +18,5 @@ password: test privileges: - 'test.*:ALL' - roles: - ansible-role-mariadb diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..cd99616 --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,18 @@ +--- +driver: + name: docker +platforms: + - name: debian10 + image: nishiki/debian10:molecule + privileged: true + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + command: /bin/systemd + capabilities: + - SYS_ADMIN +lint: | + set -e + yamllint . + ansible-lint +verifier: + name: testinfra diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py new file mode 100644 index 0000000..a151412 --- /dev/null +++ b/molecule/default/tests/test_default.py @@ -0,0 +1,49 @@ +import os, re +import testinfra.utils.ansible_runner + +def test_packages(host): + for package_name in ['mariadb-server', 'python-mysqldb']: + package = host.package(package_name) + assert package.is_installed + +def test_config_file(host): + config = host.file('/etc/mysql/my.cnf') + assert config.exists + assert config.is_file + assert config.user == 'root' + assert config.group == 'root' + assert config.mode == 0o644 + assert config.contains('server-id = 5') + +def test_data_directory(host): + config = host.file('/opt/mariadb') + assert config.exists + assert config.is_directory + assert config.user == 'mysql' + assert config.group == 'root' + assert config.mode == 0o750 + +def test_service(host): + service = host.service('mysql') + assert service.is_running + assert service.is_enabled + +def test_socket(host): + socket = host.socket('tcp://127.0.0.1:3306') + assert socket.is_listening + +def test_user_exists(host): + result = host.check_output('mysql -uroot -psecret -e "show grants for toto@\'%\'"') + assert re.search('toto.*%', result) + +def test_root_user(host): + result = host.check_output('mysql -uroot -psecret -e "select count(*) from mysql.user where user=\'root\'"') + assert '1' in result + +def test_grant_access(host): + result = host.check_output('mysql -uroot -psecret -e "show grants for toto@\'%\'"') + assert "GRANT ALL PRIVILEGES ON `test`.* TO 'toto'@'%'" in result + +def test_logbin_enabled(host): + result = host.check_output('mysql -uroot -psecret -e "show variables where variable_name = \'log_bin\'"') + assert 'ON' in result diff --git a/test/integration/default/serverspec/default_spec.rb b/test/integration/default/serverspec/default_spec.rb deleted file mode 100644 index 567db25..0000000 --- a/test/integration/default/serverspec/default_spec.rb +++ /dev/null @@ -1,68 +0,0 @@ -require 'serverspec' - -set :backend, :exec - -puts -puts '================================' -puts %x(ansible --version) -puts '================================' - -%w[ - mariadb-server - python-mysqldb -].each do |name| - describe package(name) do - it { should be_installed } - end -end - -describe file('/opt/mariadb') do - it { should be_directory } - it { should be_mode 750 } - it { should be_owned_by 'mysql' } - it { should be_grouped_into 'root' } -end - -describe file('/etc/mysql/my.cnf') do - it { should be_file } - it { should be_mode 644 } - it { should be_owned_by 'root' } - it { should be_grouped_into 'root' } - it { should contain 'server-id = 5' } -end - -describe service('mysql') do - it { should be_enabled } - it { should be_running.under('systemd') } -end - -describe port(3306) do - it { should be_listening } -end - -describe command('mysql -uroot -psecret -e "show databases"') do - its(:exit_status) { should eq 0 } - its(:stdout) { should contain 'test' } -end - -describe command('mysql -uroot -psecret -e "select user, host, password from mysql.user"') do - its(:exit_status) { should eq 0 } - its(:stdout) { should contain(/toto.*%/) } -end - -describe command('mysql -uroot -psecret -e "select count(*) from mysql.user where user=\'root\'"') do - its(:exit_status) { should eq 0 } - its(:stdout) { should contain('1') } -end - -describe command('mysql -uroot -psecret -e "show grants for toto@\'%\'"') do - its(:exit_status) { should eq 0 } - its(:stdout) do - should contain "GRANT ALL PRIVILEGES ON `test`.* TO 'toto'@'%'" - end -end - -describe command('mysql -uroot -psecret -e "show variables where variable_name = \'log_bin\'"') do - its(:exit_status) { should eq 0 } - its(:stdout) { should contain 'ON' } -end diff --git a/test/integration/inventory b/test/integration/inventory deleted file mode 100644 index 2fbb50c..0000000 --- a/test/integration/inventory +++ /dev/null @@ -1 +0,0 @@ -localhost