diff --git a/.gitignore b/.gitignore index c9b2377..f56f5b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.retry -tests/test.sh +*/__pycache__ +*.pyc diff --git a/.travis.yml b/.travis.yml index 9bed9ab..43da859 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,74 +1,35 @@ --- +language: python services: docker env: - # Test package install on all supported OSes. - - distro: centos7 - playbook: test.yml - php_version: 7.0 - - distro: fedora27 - playbook: test.yml - php_version: 7.1 - - distro: debian9 - playbook: test.yml - 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 + global: + - ROLE_NAME: php + matrix: + - MOLECULE_DISTRO: centos7 + MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd + - MOLECULE_DISTRO: ubuntu1804 + - MOLECULE_DISTRO: ubuntu1604 + - MOLECULE_DISTRO: ubuntu1404 + - MOLECULE_DISTRO: debian9 - # Only test source install on latest supported OSes. - - distro: centos7 - playbook: test-source.yml - php_version: 7.1.17 - - distro: ubuntu1804 - playbook: test-source.yml - php_version: 7.1.17 + - MOLECULE_DISTRO: centos7 + MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd + MOLECULE_PLAYBOOK: playbook-source.yml + +install: + # Install test dependencies. + - 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: - # 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. - - ${PWD}/tests/test.sh - - # 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 + - molecule test notifications: webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/README.md b/README.md index 0c8b938..adff28d 100644 --- a/README.md +++ b/README.md @@ -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). - 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!). diff --git a/defaults/main.yml b/defaults/main.yml index d73a6dc..b2f42a2 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -8,7 +8,7 @@ php_enablerepo: "" php_packages_state: present # 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. php_enable_webserver: true diff --git a/meta/main.yml b/meta/main.yml index a9051e6..0baef25 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -8,21 +8,21 @@ galaxy_info: license: "license (BSD, MIT)" min_ansible_version: 2.4 platforms: - - name: EL - versions: - - 6 - - 7 - - name: Fedora - versions: - - all - - name: Debian - versions: - - all - - name: Ubuntu - versions: - - trusty - - xenial - - bionic + - name: EL + versions: + - 6 + - 7 + - name: Fedora + versions: + - all + - name: Debian + versions: + - all + - name: Ubuntu + versions: + - trusty + - xenial + - bionic galaxy_tags: - development - web diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..0339702 --- /dev/null +++ b/molecule/default/molecule.yml @@ -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 diff --git a/molecule/default/playbook-source.yml b/molecule/default/playbook-source.yml new file mode 100644 index 0000000..1cc193d --- /dev/null +++ b/molecule/default/playbook-source.yml @@ -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 diff --git a/tests/test.yml b/molecule/default/playbook.yml similarity index 71% rename from tests/test.yml rename to molecule/default/playbook.yml index ff72fc3..5284104 100644 --- a/tests/test.yml +++ b/molecule/default/playbook.yml @@ -1,16 +1,18 @@ --- -- hosts: all +- name: Converge + hosts: all + become: true vars: php_enable_webserver: false php_enable_php_fpm: true php_memory_limit: "192M" php_enablerepo: "remi,remi-php70" - php_install_recommends: no + php_install_recommends: false pre_tasks: - 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' changed_when: false @@ -22,10 +24,10 @@ # Debian-specific tasks. - name: Add dependencies for PHP versions (Debian). apt: - name: "{{ item }}" - with_items: - - apt-transport-https - - ca-certificates + name: + - apt-transport-https + - ca-certificates + state: present when: ansible_distribution == "Debian" - name: Add Ondrej Sury's apt key (Debian). @@ -47,5 +49,12 @@ roles: - role: geerlingguy.repo-remi - when: ansible_os_family == 'RedHat' and ansible_distribution != 'Fedora' - - role_under_test + when: + - 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 diff --git a/tests/requirements.yml b/molecule/default/requirements.yml similarity index 100% rename from tests/requirements.yml rename to molecule/default/requirements.yml diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py new file mode 100644 index 0000000..eedd64a --- /dev/null +++ b/molecule/default/tests/test_default.py @@ -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' diff --git a/molecule/default/yaml-lint.yml b/molecule/default/yaml-lint.yml new file mode 100644 index 0000000..a3dbc38 --- /dev/null +++ b/molecule/default/yaml-lint.yml @@ -0,0 +1,6 @@ +--- +extends: default +rules: + line-length: + max: 120 + level: warning diff --git a/tasks/configure-apcu.yml b/tasks/configure-apcu.yml index 5a3badb..a29f8d6 100644 --- a/tasks/configure-apcu.yml +++ b/tasks/configure-apcu.yml @@ -22,7 +22,7 @@ dest: "{{ item }}/{{ php_apc_conf_filename }}" owner: root group: root - force: yes + force: true mode: 0644 with_items: "{{ php_extension_conf_paths }}" when: php_enable_apc diff --git a/tasks/configure-fpm.yml b/tasks/configure-fpm.yml index c9c08bc..66db220 100644 --- a/tasks/configure-fpm.yml +++ b/tasks/configure-fpm.yml @@ -40,7 +40,7 @@ owner: root group: root mode: 0644 - force: no + force: false when: php_enable_php_fpm - name: Configure php-fpm pool (if enabled). @@ -73,7 +73,7 @@ service: name: "{{ php_fpm_daemon }}" state: started - enabled: yes + enabled: true when: php_enable_php_fpm and ansible_distribution != "Debian" # See: https://github.com/ansible/ansible/issues/22303 @@ -81,6 +81,6 @@ service: name: "{{ php_fpm_daemon }}" state: started - enabled: yes + enabled: true use: service when: php_enable_php_fpm and ansible_distribution == "Debian" diff --git a/tasks/configure-opcache.yml b/tasks/configure-opcache.yml index cd02e3f..4ed13ed 100644 --- a/tasks/configure-opcache.yml +++ b/tasks/configure-opcache.yml @@ -22,7 +22,7 @@ dest: "{{ item }}/{{ php_opcache_conf_filename }}" owner: root group: root - force: yes + force: true mode: 0644 with_items: "{{ php_extension_conf_paths }}" when: php_opcache_enable diff --git a/tasks/install-from-source.yml b/tasks/install-from-source.yml index 3fefa03..e999ba0 100644 --- a/tasks/install-from-source.yml +++ b/tasks/install-from-source.yml @@ -1,28 +1,29 @@ --- - name: Ensure dependencies for building from source are installed (RedHat). - package: "name={{ item }} state=present" - with_items: - - autoconf - - automake - - libtool - - bison - - make - - curl-devel - - recode-devel - - aspell-devel - - libxml2-devel - - pkgconfig - - libmcrypt-devel - - t1lib-devel - - libXpm-devel - - libpng-devel - - libjpeg-turbo-devel - - bzip2-devel - - openssl-devel - - freetype-devel - - libicu-devel - - mariadb-devel - - gmp-devel + package: + name: + - autoconf + - automake + - libtool + - bison + - make + - curl-devel + - recode-devel + - aspell-devel + - libxml2-devel + - pkgconfig + - libmcrypt-devel + - t1lib-devel + - libXpm-devel + - libpng-devel + - libjpeg-turbo-devel + - bzip2-devel + - openssl-devel + - freetype-devel + - libicu-devel + - mariadb-devel + - gmp-devel + state: present when: ansible_os_family == 'RedHat' - name: Update apt cache (Debian). @@ -30,28 +31,29 @@ when: ansible_os_family == 'Debian' - name: Ensure dependencies for building from source are installed (Debian). - apt: "pkg={{ item }} state=present" - with_items: - - build-essential - - autoconf - - automake - - libtool - - bison - - pkg-config - - re2c - - libxml2-dev - - libcurl4-openssl-dev - - libbz2-dev - - libjpeg-dev - - libpng-dev - - libxpm-dev - - libfreetype6-dev - - libgmp3-dev - - libmcrypt-dev - - libmysqlclient-dev - - libpspell-dev - - librecode-dev - - libssl-dev + apt: + name: + - build-essential + - autoconf + - automake + - libtool + - bison + - pkg-config + - re2c + - libxml2-dev + - libcurl4-openssl-dev + - libbz2-dev + - libjpeg-dev + - libpng-dev + - libxpm-dev + - libfreetype6-dev + - libgmp3-dev + - libmcrypt-dev + - libmysqlclient-dev + - libpspell-dev + - librecode-dev + - libssl-dev + state: present when: ansible_os_family == 'Debian' - name: Define php_fpm_daemon (if not defined already). @@ -81,7 +83,7 @@ repo: "{{ php_source_repo }}" dest: "{{ php_source_clone_dir }}" version: "{{ php_source_version }}" - accept_hostkey: yes + accept_hostkey: true depth: "{{ php_source_clone_depth }}" when: php_installed.rc != 0 @@ -93,19 +95,19 @@ when: php_installed.rc != 0 - name: Build configure script. - shell: > + command: > ./buildconf --force chdir={{ php_source_clone_dir }} when: php_installed.rc != 0 - name: Run configure script. - shell: > + command: > {{ php_source_configure_command }} chdir={{ php_source_clone_dir }} when: php_installed.rc != 0 - name: Make and install PHP. - shell: > + command: > {{ item }} chdir={{ php_source_clone_dir }} with_items: diff --git a/tests/README.md b/tests/README.md deleted file mode 100644 index 6fb2117..0000000 --- a/tests/README.md +++ /dev/null @@ -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)` diff --git a/tests/test-source.yml b/tests/test-source.yml deleted file mode 100644 index 7c0d143..0000000 --- a/tests/test-source.yml +++ /dev/null @@ -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