ansible/test/integration/targets/service/tasks/tests.yml
Andrew Klychkov 00ead98595
systemd: should fail in check_mode when service not found on host (#68136)
* systemd: should fail in check_mode when service not found on host
2020-06-02 11:52:12 -04:00

225 lines
7.1 KiB
YAML

- 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 is changed"
- name: enable the ansible test service
service: name=ansible_test enabled=yes
register: enable_result
- name: assert that the service was enabled and changes reported
assert:
that:
- "enable_result.enabled == true"
- "enable_result is changed"
- name: start the ansible test service
service: name=ansible_test state=started
register: start_result
- name: assert that the service was started
assert:
that:
- "start_result.state == 'started'"
- "start_result is changed"
- name: check that the service was started
shell: 'cat /proc/$(cat /var/run/ansible_test_service.pid)/cmdline'
register: cmdline
failed_when: cmdline is failed or '\0/usr/sbin/ansible_test_service\0' not in cmdline.stdout
# No proc on BSD
when: not ansible_distribution.lower().endswith('bsd')
- name: check that the service was started (*bsd)
shell: 'ps -p $(cat /var/run/ansible_test_service.pid)'
register: cmdline
failed_when: cmdline is failed or '/usr/sbin/ansible_test_service' not in cmdline.stdout
when: ansible_distribution.lower().endswith('bsd')
- name: find the service with a pattern
service: name=ansible_test pattern="ansible_test_ser" state=started
register: start2_result
- name: assert that the service was started via the pattern
assert:
that:
- "start2_result.name == 'ansible_test'"
- "start2_result.state == 'started'"
- "start2_result is not changed"
- name: fetch PID for ansible_test service (before restart)
command: 'cat /var/run/ansible_test_service.pid'
register: pid_before_restart
- name: restart the ansible test service
service: name=ansible_test state=restarted
register: restart_result
- name: assert that the service was restarted
assert:
that:
- "restart_result.state == 'started'"
- "restart_result is changed"
- name: fetch PID for ansible_test service (after restart)
command: 'cat /var/run/ansible_test_service.pid'
register: pid_after_restart
- name: "check that PIDs aren't the same"
fail:
when: pid_before_restart.stdout == pid_after_restart.stdout
- name: check that service is started
command: 'cat /proc/{{ pid_after_restart.stdout }}/cmdline'
register: cmdline
failed_when: cmdline is failed or '\0/usr/sbin/ansible_test_service\0' not in cmdline.stdout
# No proc on BSD
when: not ansible_distribution.lower().endswith('bsd')
- name: check that the service is started (*bsd)
shell: 'ps -p {{ pid_after_restart.stdout }}'
register: cmdline
failed_when: cmdline is failed or '/usr/sbin/ansible_test_service' not in cmdline.stdout
when: ansible_distribution.lower().endswith('bsd')
- name: restart the ansible test service with a sleep
service: name=ansible_test state=restarted sleep=2
register: restart_sleep_result
- name: assert that the service was restarted with a sleep
assert:
that:
- "restart_sleep_result.state == 'started'"
- "restart_sleep_result is changed"
- 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
assert:
that:
- "reload_result.state == 'started'"
- "reload_result is changed"
when: service_type != "systemd"
- name: "test for #42786 (sysvinit)"
when: service_type == "sysv"
block:
- name: "sysvinit (#42786): check state, 'enable' parameter isn't set"
service: use=sysvinit name=ansible_test state=started
- name: "sysvinit (#42786): check that service is still enabled"
service: use=sysvinit name=ansible_test enabled=yes
register: result_enabled
failed_when: result_enabled is changed
- name: fetch PID for ansible_test service
command: 'cat /var/run/ansible_test_service.pid'
register: ansible_test_pid
- name: check that service is started
command: 'cat /proc/{{ ansible_test_pid.stdout }}/cmdline'
register: cmdline
failed_when: cmdline is failed or '\0/usr/sbin/ansible_test_service\0' not in cmdline.stdout
# No proc on BSD
when: not ansible_distribution.lower().endswith('bsd')
- name: check that the service is started (*bsd)
shell: 'ps -p {{ ansible_test_pid.stdout }}'
register: cmdline
failed_when: cmdline is failed or '/usr/sbin/ansible_test_service' not in cmdline.stdout
when: ansible_distribution.lower().endswith('bsd')
- name: stop the ansible test service
service: name=ansible_test state=stopped
register: stop_result
- name: check that the service is stopped
command: 'cat /proc/{{ ansible_test_pid.stdout }}/cmdline'
register: cmdline
failed_when: cmdline is not failed or '\0/usr/sbin/ansible_test_service\0' in cmdline.stdout
# No proc on BSD
when: not ansible_distribution.lower().endswith('bsd')
- name: check that the service is stopped (*bsd)
shell: 'ps -p {{ ansible_test_pid.stdout }}'
register: cmdline
failed_when: cmdline is not failed or '/usr/sbin/ansible_test_service' in cmdline.stdout
when: ansible_distribution.lower().endswith('bsd')
- name: assert that the service was stopped
assert:
that:
- "stop_result.state == 'stopped'"
- "stop_result is changed"
- name: disable the ansible test service
service: name=ansible_test enabled=no
register: disable_result
- name: assert that the service was disabled
assert:
that:
- "disable_result.enabled == false"
- "disable_result is changed"
- name: try to enable a broken service
service: name=ansible_broken_test enabled=yes
register: broken_enable_result
ignore_errors: True
- name: assert that the broken test failed
assert:
that:
- "broken_enable_result is failed"
- name: remove the test daemon script
file: path=/usr/sbin/ansible_test_service state=absent
register: remove_result
- name: assert that the test daemon script was removed
assert:
that:
- "remove_result.path == '/usr/sbin/ansible_test_service'"
- "remove_result.state == 'absent'"
- name: the module must fail when a service is not found
service:
name: 'nonexisting'
state: stopped
register: result
ignore_errors: yes
when: ansible_distribution != 'FreeBSD'
- assert:
that:
- result is failed
- result is search("Could not find the requested service nonexisting")
when: ansible_distribution != 'FreeBSD'
- name: the module must fail in check_mode as well when a service is not found
service:
name: 'nonexisting'
state: stopped
register: result
check_mode: yes
ignore_errors: yes
when: ansible_distribution != 'FreeBSD'
- assert:
that:
- result is failed
- result is search("Could not find the requested service nonexisting")
when: ansible_distribution != 'FreeBSD'