Switch tests to use Molecule.

This commit is contained in:
Jeff Geerling 2018-09-29 14:41:26 -05:00
parent 40b7bd543e
commit 6e92b87062
17 changed files with 196 additions and 175 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.retry *.retry
tests/test.sh */__pycache__
*.pyc

View File

@ -1,74 +1,35 @@
--- ---
language: python
services: docker services: docker
env: env:
# Test package install on all supported OSes. global:
- distro: centos7 - ROLE_NAME: php
playbook: test.yml matrix:
php_version: 7.0 - MOLECULE_DISTRO: centos7
- distro: fedora27 MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd
playbook: test.yml - MOLECULE_DISTRO: ubuntu1804
php_version: 7.1 - MOLECULE_DISTRO: ubuntu1604
- distro: debian9 - MOLECULE_DISTRO: ubuntu1404
playbook: test.yml - MOLECULE_DISTRO: debian9
php_version: 7.0
- distro: debian8
playbook: test.yml
php_version: 7.0
- distro: ubuntu1804
playbook: test.yml
php_version: 7.0
- distro: ubuntu1604
playbook: test.yml
php_version: 7.0
- distro: ubuntu1404
playbook: test.yml
php_version: 7.0
# Only test source install on latest supported OSes. - MOLECULE_DISTRO: centos7
- distro: centos7 MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd
playbook: test-source.yml MOLECULE_PLAYBOOK: playbook-source.yml
php_version: 7.1.17
- distro: ubuntu1804 install:
playbook: test-source.yml # Install test dependencies.
php_version: 7.1.17 - pip install molecule docker
before_script:
# Use actual Ansible Galaxy role name for the project directory.
- cd ../
- mv ansible-role-$ROLE_NAME geerlingguy.$ROLE_NAME
- cd geerlingguy.$ROLE_NAME
script: script:
# Configure test script so we can run extra tests after playbook is run.
- export container_id=$(date +%s)
- export cleanup=false
# Download test shim.
- wget -O ${PWD}/tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/
- chmod +x ${PWD}/tests/test.sh
# Run tests. # Run tests.
- ${PWD}/tests/test.sh - molecule test
# Ensure PHP is installed and at the right version.
- 'docker exec --tty ${container_id} env TERM=xterm which php'
- 'docker exec --tty ${container_id} env TERM=xterm test -x /usr/bin/php'
- 'docker exec --tty ${container_id} env TERM=xterm php --version'
- 'docker exec --tty ${container_id} env TERM=xterm /usr/bin/php --version | grep -qF "PHP ${php_version}"'
# Ensure PHP configurations have taken effect.
- docker exec --tty ${container_id} env TERM=xterm php -i | grep 'memory_limit.*192'
# Check the status of PHP-FPM.
- |
if [ "${playbook}" == "test.yml" ]; then
case "${distro}" in
"centos7"|"fedora24")
docker exec --tty ${container_id} env TERM=xterm systemctl --no-pager status php-fpm status
docker exec --tty ${container_id} env TERM=xterm systemctl --no-pager status php-fpm status | grep -qF "fpm.service; enabled"
;;
"debian8"|"ubuntu1604"|"ubuntu1804")
docker exec --tty ${container_id} env TERM=xterm systemctl --no-pager status php${php_version}-fpm status
docker exec --tty ${container_id} env TERM=xterm systemctl --no-pager status php${php_version}-fpm status | grep -qF "fpm.service; enabled"
;;
esac
fi
notifications: notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/ webhooks: https://galaxy.ansible.com/api/v1/notifications/

View File

@ -40,7 +40,7 @@ If you have enabled any additional repositories such as [geerlingguy.repo-epel](
You can also use the [`geerlingguy.php-versions`](https://galaxy.ansible.com/geerlingguy/php-versions/) role to more easily switch between major PHP versions (e.g. 5.6, 7.0, 7.1, 7.2). You can also use the [`geerlingguy.php-versions`](https://galaxy.ansible.com/geerlingguy/php-versions/) role to more easily switch between major PHP versions (e.g. 5.6, 7.0, 7.1, 7.2).
php_install_recommends: yes php_install_recommends: true
(Debian/Ubuntu only) Whether to install recommended packages when installing `php_packages`; you might want to set this to `no` explicitly if you're installing a PPA that recommends certain packages you don't want (e.g. Ondrej's `php` PPA will install `php7.0-cli` if you install `php-pear` alongside `php5.6-cli`... which is often not desired!). (Debian/Ubuntu only) Whether to install recommended packages when installing `php_packages`; you might want to set this to `no` explicitly if you're installing a PPA that recommends certain packages you don't want (e.g. Ondrej's `php` PPA will install `php7.0-cli` if you install `php-pear` alongside `php5.6-cli`... which is often not desired!).

View File

@ -8,7 +8,7 @@ php_enablerepo: ""
php_packages_state: present php_packages_state: present
# Whether to install recommended packages. Used only for Debian/Ubuntu. # Whether to install recommended packages. Used only for Debian/Ubuntu.
php_install_recommends: yes php_install_recommends: true
# Set this to false if you're not using PHP with Apache/Nginx/etc. # Set this to false if you're not using PHP with Apache/Nginx/etc.
php_enable_webserver: true php_enable_webserver: true

View File

@ -8,21 +8,21 @@ galaxy_info:
license: "license (BSD, MIT)" license: "license (BSD, MIT)"
min_ansible_version: 2.4 min_ansible_version: 2.4
platforms: platforms:
- name: EL - name: EL
versions: versions:
- 6 - 6
- 7 - 7
- name: Fedora - name: Fedora
versions: versions:
- all - all
- name: Debian - name: Debian
versions: versions:
- all - all
- name: Ubuntu - name: Ubuntu
versions: versions:
- trusty - trusty
- xenial - xenial
- bionic - bionic
galaxy_tags: galaxy_tags:
- development - development
- web - web

View File

@ -0,0 +1,27 @@
---
dependency:
name: galaxy
driver:
name: docker
lint:
name: yamllint
options:
config-file: molecule/default/yaml-lint.yml
platforms:
- name: instance
image: geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible
command: ${MOLECULE_DOCKER_COMMAND:-"sleep infinity"}
privileged: true
pre_build_image: true
provisioner:
name: ansible
lint:
name: ansible-lint
playbooks:
converge: ${MOLECULE_PLAYBOOK:-playbook.yml}
scenario:
name: default
verifier:
name: testinfra
lint:
name: flake8

View File

@ -0,0 +1,32 @@
---
- name: Converge
hosts: all
become: true
vars:
php_enable_webserver: false
php_install_from_source: true
php_source_clone_dir: /root/php-src
php_source_make_command: "make --jobs=2"
php_version: "7.1.17"
php_source_version: "php-{{ php_version }}"
php_memory_limit: "192M"
pre_tasks:
- name: Update apt cache.
apt: update_cache=true cache_valid_time=600
when: ansible_os_family == 'Debian'
changed_when: false
roles:
- role: geerlingguy.git
- role: geerlingguy.php
post_tasks:
- name: Confirm PHP configuration is correct.
shell: php -i | grep 'memory_limit.*192'
changed_when: false
- name: Check the installed PHP version.
shell: '/usr/bin/php --version | grep -qF "PHP {{ php_version }}"'
changed_when: false

View File

@ -1,16 +1,18 @@
--- ---
- hosts: all - name: Converge
hosts: all
become: true
vars: vars:
php_enable_webserver: false php_enable_webserver: false
php_enable_php_fpm: true php_enable_php_fpm: true
php_memory_limit: "192M" php_memory_limit: "192M"
php_enablerepo: "remi,remi-php70" php_enablerepo: "remi,remi-php70"
php_install_recommends: no php_install_recommends: false
pre_tasks: pre_tasks:
- name: Update apt cache. - name: Update apt cache.
apt: update_cache=yes cache_valid_time=600 apt: update_cache=true cache_valid_time=600
when: ansible_os_family == 'Debian' when: ansible_os_family == 'Debian'
changed_when: false changed_when: false
@ -22,10 +24,10 @@
# Debian-specific tasks. # Debian-specific tasks.
- name: Add dependencies for PHP versions (Debian). - name: Add dependencies for PHP versions (Debian).
apt: apt:
name: "{{ item }}" name:
with_items: - apt-transport-https
- apt-transport-https - ca-certificates
- ca-certificates state: present
when: ansible_distribution == "Debian" when: ansible_distribution == "Debian"
- name: Add Ondrej Sury's apt key (Debian). - name: Add Ondrej Sury's apt key (Debian).
@ -47,5 +49,12 @@
roles: roles:
- role: geerlingguy.repo-remi - role: geerlingguy.repo-remi
when: ansible_os_family == 'RedHat' and ansible_distribution != 'Fedora' when:
- role_under_test - ansible_os_family == 'RedHat'
- ansible_distribution != 'Fedora'
- geerlingguy.ansible
post_tasks:
- name: Confirm PHP configuration is correct.
shell: php -i | grep 'memory_limit.*192'
changed_when: false

View File

@ -0,0 +1,14 @@
import os
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
def test_hosts_file(host):
f = host.file('/etc/hosts')
assert f.exists
assert f.user == 'root'
assert f.group == 'root'

View File

@ -0,0 +1,6 @@
---
extends: default
rules:
line-length:
max: 120
level: warning

View File

@ -22,7 +22,7 @@
dest: "{{ item }}/{{ php_apc_conf_filename }}" dest: "{{ item }}/{{ php_apc_conf_filename }}"
owner: root owner: root
group: root group: root
force: yes force: true
mode: 0644 mode: 0644
with_items: "{{ php_extension_conf_paths }}" with_items: "{{ php_extension_conf_paths }}"
when: php_enable_apc when: php_enable_apc

View File

@ -40,7 +40,7 @@
owner: root owner: root
group: root group: root
mode: 0644 mode: 0644
force: no force: false
when: php_enable_php_fpm when: php_enable_php_fpm
- name: Configure php-fpm pool (if enabled). - name: Configure php-fpm pool (if enabled).
@ -73,7 +73,7 @@
service: service:
name: "{{ php_fpm_daemon }}" name: "{{ php_fpm_daemon }}"
state: started state: started
enabled: yes enabled: true
when: php_enable_php_fpm and ansible_distribution != "Debian" when: php_enable_php_fpm and ansible_distribution != "Debian"
# See: https://github.com/ansible/ansible/issues/22303 # See: https://github.com/ansible/ansible/issues/22303
@ -81,6 +81,6 @@
service: service:
name: "{{ php_fpm_daemon }}" name: "{{ php_fpm_daemon }}"
state: started state: started
enabled: yes enabled: true
use: service use: service
when: php_enable_php_fpm and ansible_distribution == "Debian" when: php_enable_php_fpm and ansible_distribution == "Debian"

View File

@ -22,7 +22,7 @@
dest: "{{ item }}/{{ php_opcache_conf_filename }}" dest: "{{ item }}/{{ php_opcache_conf_filename }}"
owner: root owner: root
group: root group: root
force: yes force: true
mode: 0644 mode: 0644
with_items: "{{ php_extension_conf_paths }}" with_items: "{{ php_extension_conf_paths }}"
when: php_opcache_enable when: php_opcache_enable

View File

@ -1,28 +1,29 @@
--- ---
- name: Ensure dependencies for building from source are installed (RedHat). - name: Ensure dependencies for building from source are installed (RedHat).
package: "name={{ item }} state=present" package:
with_items: name:
- autoconf - autoconf
- automake - automake
- libtool - libtool
- bison - bison
- make - make
- curl-devel - curl-devel
- recode-devel - recode-devel
- aspell-devel - aspell-devel
- libxml2-devel - libxml2-devel
- pkgconfig - pkgconfig
- libmcrypt-devel - libmcrypt-devel
- t1lib-devel - t1lib-devel
- libXpm-devel - libXpm-devel
- libpng-devel - libpng-devel
- libjpeg-turbo-devel - libjpeg-turbo-devel
- bzip2-devel - bzip2-devel
- openssl-devel - openssl-devel
- freetype-devel - freetype-devel
- libicu-devel - libicu-devel
- mariadb-devel - mariadb-devel
- gmp-devel - gmp-devel
state: present
when: ansible_os_family == 'RedHat' when: ansible_os_family == 'RedHat'
- name: Update apt cache (Debian). - name: Update apt cache (Debian).
@ -30,28 +31,29 @@
when: ansible_os_family == 'Debian' when: ansible_os_family == 'Debian'
- name: Ensure dependencies for building from source are installed (Debian). - name: Ensure dependencies for building from source are installed (Debian).
apt: "pkg={{ item }} state=present" apt:
with_items: name:
- build-essential - build-essential
- autoconf - autoconf
- automake - automake
- libtool - libtool
- bison - bison
- pkg-config - pkg-config
- re2c - re2c
- libxml2-dev - libxml2-dev
- libcurl4-openssl-dev - libcurl4-openssl-dev
- libbz2-dev - libbz2-dev
- libjpeg-dev - libjpeg-dev
- libpng-dev - libpng-dev
- libxpm-dev - libxpm-dev
- libfreetype6-dev - libfreetype6-dev
- libgmp3-dev - libgmp3-dev
- libmcrypt-dev - libmcrypt-dev
- libmysqlclient-dev - libmysqlclient-dev
- libpspell-dev - libpspell-dev
- librecode-dev - librecode-dev
- libssl-dev - libssl-dev
state: present
when: ansible_os_family == 'Debian' when: ansible_os_family == 'Debian'
- name: Define php_fpm_daemon (if not defined already). - name: Define php_fpm_daemon (if not defined already).
@ -81,7 +83,7 @@
repo: "{{ php_source_repo }}" repo: "{{ php_source_repo }}"
dest: "{{ php_source_clone_dir }}" dest: "{{ php_source_clone_dir }}"
version: "{{ php_source_version }}" version: "{{ php_source_version }}"
accept_hostkey: yes accept_hostkey: true
depth: "{{ php_source_clone_depth }}" depth: "{{ php_source_clone_depth }}"
when: php_installed.rc != 0 when: php_installed.rc != 0
@ -93,19 +95,19 @@
when: php_installed.rc != 0 when: php_installed.rc != 0
- name: Build configure script. - name: Build configure script.
shell: > command: >
./buildconf --force ./buildconf --force
chdir={{ php_source_clone_dir }} chdir={{ php_source_clone_dir }}
when: php_installed.rc != 0 when: php_installed.rc != 0
- name: Run configure script. - name: Run configure script.
shell: > command: >
{{ php_source_configure_command }} {{ php_source_configure_command }}
chdir={{ php_source_clone_dir }} chdir={{ php_source_clone_dir }}
when: php_installed.rc != 0 when: php_installed.rc != 0
- name: Make and install PHP. - name: Make and install PHP.
shell: > command: >
{{ item }} {{ item }}
chdir={{ php_source_clone_dir }} chdir={{ php_source_clone_dir }}
with_items: with_items:

View File

@ -1,11 +0,0 @@
# Ansible Role tests
To run the test playbook(s) in this directory:
1. Install and start Docker.
1. Download the test shim (see .travis.yml file for the URL) into `tests/test.sh`:
- `wget -O tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/`
1. Make the test shim executable: `chmod +x tests/test.sh`.
1. Run (from the role root directory) `distro=[distro] playbook=[playbook] ./tests/test.sh`
If you don't want the container to be automatically deleted after the test playbook is run, add the following environment variables: `cleanup=false container_id=$(date +%s)`

View File

@ -1,20 +0,0 @@
---
- hosts: all
vars:
php_enable_webserver: false
php_install_from_source: true
php_source_clone_dir: /root/php-src
php_source_make_command: "make --jobs=2"
php_source_version: "php-7.1.17"
php_memory_limit: "192M"
pre_tasks:
- name: Update apt cache.
apt: update_cache=yes cache_valid_time=600
when: ansible_os_family == 'Debian'
changed_when: false
roles:
- geerlingguy.git
- role_under_test