From 2bc679be07bee5b1ba9bf134fd6b925d58674e36 Mon Sep 17 00:00:00 2001 From: Gennady Trafimenkov Date: Fri, 29 Jul 2016 19:35:03 +0400 Subject: [PATCH] Testing that service not enabled during check run (#16739) * Enabled more tests in test_service on systems with systemd. * Fix inconsistency in cleaning up test service. The conditions for service setup and service cleanup were different. * Add check mode run for service enable. --- .../roles/test_service/files/ansible.systemd | 3 ++ .../roles/test_service/tasks/main.yml | 49 ++++++++++++++----- .../test_service/tasks/systemd_setup.yml | 6 +-- .../roles/test_service/tasks/sysv_setup.yml | 3 -- .../test_service/tasks/upstart_setup.yml | 3 -- 5 files changed, 42 insertions(+), 22 deletions(-) diff --git a/test/integration/roles/test_service/files/ansible.systemd b/test/integration/roles/test_service/files/ansible.systemd index 77c4911c23..c1a710a1b3 100644 --- a/test/integration/roles/test_service/files/ansible.systemd +++ b/test/integration/roles/test_service/files/ansible.systemd @@ -5,3 +5,6 @@ Description=Ansible Test Service ExecStart=/usr/sbin/ansible_test_service "Test\nthat newlines in scripts\nwork" ExecReload=/bin/true Type=forking + +[Install] +WantedBy=multi-user.target diff --git a/test/integration/roles/test_service/tasks/main.yml b/test/integration/roles/test_service/tasks/main.yml index 5b7e6c0ceb..e11702c4df 100644 --- a/test/integration/roles/test_service/tasks/main.yml +++ b/test/integration/roles/test_service/tasks/main.yml @@ -10,21 +10,47 @@ - "install_result.state == 'file'" - "install_result.mode == '0755'" -- include: 'sysv_setup.yml' +# determine init system is in use +- name: detect sysv init system + set_fact: service_type=sysv when: ansible_distribution in ['RedHat', 'CentOS', 'ScientificLinux'] and (ansible_distribution_version|version_compare('6', '>=') and ansible_distribution_version|version_compare('7', '<')) -- include: 'systemd_setup.yml' +- name: detect systemd init system + set_fact: service_type=systemd when: (ansible_distribution in ['RedHat', 'CentOS', 'ScientificLinux'] and (ansible_distribution_version|version_compare('7', '>=') and ansible_distribution_version|version_compare('8', '<'))) or ansible_distribution == 'Fedora' or (ansible_distribution == 'Ubuntu' and ansible_distribution_version|version_compare('15.04', '>=')) or (ansible_distribution == 'Debian' and ansible_distribution_version|version_compare('8', '>=')) or ansible_os_family == 'Suse' -- include: 'upstart_setup.yml' +- name: detect upstart init system + set_fact: service_type=upstart when: ansible_distribution == 'Ubuntu' and ansible_distribution_version|version_compare('15.04', '<') +# setup test service script +- include: 'sysv_setup.yml' + when: service_type == "sysv" +- include: 'systemd_setup.yml' + when: service_type == "systemd" +- include: 'upstart_setup.yml' + when: service_type == "upstart" + +- name: disable the ansible test service + service: name=ansible_test enabled=no + +- name: (check mode run) enable the ansible test service + service: name=ansible_test enabled=yes + register: enable_in_check_mode_result + check_mode: yes + +- name: assert that changes reported for check mode run + assert: + that: + - "enable_in_check_mode_result.changed == true" + - name: enable the ansible test service service: name=ansible_test enabled=yes register: enable_result -- name: assert that the service was enabled +- name: assert that the service was enabled and changes reported assert: that: - "enable_result.enabled == true" + - "enable_result.changed == true" - name: start the ansible test service service: name=ansible_test state=started @@ -38,6 +64,7 @@ - name: find the service with a pattern service: name=ansible_test pattern="ansible_test_ser*" state=started register: start2_result + # don't enable this check yet on systems with systemd because of https://github.com/ansible/ansible/issues/16694 when: service_type != "systemd" - name: assert that the service was started via the pattern @@ -50,17 +77,16 @@ - name: restart the ansible test service service: name=ansible_test state=restarted register: restart_result - when: service_type != "systemd" - name: assert that the service was restarted assert: that: - "restart_result.state == 'started'" - when: service_type != "systemd" - name: restart the ansible test service with a sleep service: name=ansible_test state=restarted sleep=2 register: restart_sleep_result + # don't enable this check yet on systems with systemd because of https://github.com/ansible/ansible/issues/16694 when: service_type != "systemd" - name: assert that the service was restarted with a sleep @@ -72,6 +98,8 @@ - name: reload the ansible test service service: name=ansible_test state=reloaded register: reload_result + # don't do this on systems with systemd because it triggers error: + # Unable to reload service ansible_test: ansible_test.service is not active, cannot reload. when: service_type != "systemd" - name: assert that the service was reloaded @@ -92,13 +120,11 @@ - name: disable the ansible test service service: name=ansible_test enabled=no register: disable_result - when: service_type != "systemd" - name: assert that the service was disabled assert: that: - "disable_result.enabled == false" - when: service_type != "systemd" - name: try to enable a broken service service: name=ansible_broken_test enabled=yes @@ -120,9 +146,10 @@ - "remove_result.path == '/usr/sbin/ansible_test_service'" - "remove_result.state == 'absent'" +# cleaning up changes made by this playbook - include: 'sysv_cleanup.yml' - when: ansible_distribution in ['RedHat', 'CentOS', 'ScientificLinux'] + when: service_type == "sysv" - include: 'systemd_cleanup.yml' - when: ansible_distribution == 'Fedora' + when: service_type == "systemd" - include: 'upstart_cleanup.yml' - when: ansible_distribution == 'Ubuntu' + when: service_type == "upstart" diff --git a/test/integration/roles/test_service/tasks/systemd_setup.yml b/test/integration/roles/test_service/tasks/systemd_setup.yml index f03a774959..bee5ce1f28 100644 --- a/test/integration/roles/test_service/tasks/systemd_setup.yml +++ b/test/integration/roles/test_service/tasks/systemd_setup.yml @@ -1,6 +1,3 @@ -- name: set service_type fact - set_fact: service_type=systemd - - name: install the systemd unit file copy: src=ansible.systemd dest=/etc/systemd/system/ansible_test.service register: install_systemd_result @@ -15,7 +12,6 @@ - "install_systemd_result.dest == '/etc/systemd/system/ansible_test.service'" - "install_systemd_result.state == 'file'" - "install_systemd_result.mode == '0644'" - - "install_systemd_result.checksum == 'ca4b413fdf3cb2002f51893b9e42d2e449ec5afb'" + - "install_systemd_result.checksum == '6b5f2b9318524a387c77c550cef4dd411a471acf'" - "install_broken_systemd_result.dest == '/etc/systemd/system/ansible_test_broken.service'" - "install_broken_systemd_result.state == 'link'" - diff --git a/test/integration/roles/test_service/tasks/sysv_setup.yml b/test/integration/roles/test_service/tasks/sysv_setup.yml index 53387dcb7a..796a2fe9a7 100644 --- a/test/integration/roles/test_service/tasks/sysv_setup.yml +++ b/test/integration/roles/test_service/tasks/sysv_setup.yml @@ -1,6 +1,3 @@ -- name: set service_type fact - set_fact: service_type=sysv - - name: install the sysV init file copy: src=ansible.sysv dest=/etc/init.d/ansible_test mode=0755 register: install_sysv_result diff --git a/test/integration/roles/test_service/tasks/upstart_setup.yml b/test/integration/roles/test_service/tasks/upstart_setup.yml index 83fa765d18..e9607bb030 100644 --- a/test/integration/roles/test_service/tasks/upstart_setup.yml +++ b/test/integration/roles/test_service/tasks/upstart_setup.yml @@ -1,6 +1,3 @@ -- name: set service_type fact - set_fact: service_type=upstart - - name: install the upstart init file copy: src=ansible.upstart dest=/etc/init/ansible_test.conf mode=0644 register: install_upstart_result