Debian Codegraph @

test/integration/targets/service/tasks/tests.yml

raw
1 - name: disable the ansible test service
2 service: name=ansible_test enabled=no
3
4 - name: (check mode run) enable the ansible test service
5 service: name=ansible_test enabled=yes
6 register: enable_in_check_mode_result
7 check_mode: yes
8
9 - name: assert that changes reported for check mode run
10 assert:
11 that:
12 - "enable_in_check_mode_result is changed"
13
14 - name: (check mode run) test that service defaults are used
15 service:
16 register: enable_in_check_mode_result
17 check_mode: yes
18 module_defaults:
19 service:
20 name: ansible_test
21 enabled: yes
22
23 - name: assert that changes reported for check mode run
24 assert:
25 that:
26 - "enable_in_check_mode_result is changed"
27
28 - name: (check mode run) test that specific module defaults are used
29 service:
30 register: enable_in_check_mode_result
31 check_mode: yes
32 when: "ansible_service_mgr in ['sysvinit', 'systemd']"
33 module_defaults:
34 sysvinit:
35 # Test the action plugin templates sysvinit defaults
36 # https://github.com/ansible/ansible/issues/85848
37 name: "{{ 'ansible_test' }}"
38 enabled: yes
39 systemd:
40 name: "{{ 'ansible_test' }}"
41 enabled: yes
42
43 - name: assert that changes reported for check mode run
44 assert:
45 that:
46 - "enable_in_check_mode_result is changed"
47 when: "ansible_service_mgr in ['sysvinit', 'systemd']"
48
49 - name: enable the ansible test service
50 service: name=ansible_test enabled=yes
51 register: enable_result
52
53 - name: assert that the service was enabled and changes reported
54 assert:
55 that:
56 - "enable_result.enabled == true"
57 - "enable_result is changed"
58
59 - name: start the ansible test service
60 service: name=ansible_test state=started
61 register: start_result
62
63 - name: assert that the service was started
64 assert:
65 that:
66 - "start_result.state == 'started'"
67 - "start_result is changed"
68
69 - name: check that the service was started
70 shell: 'cat /proc/$(cat /var/run/ansible_test_service.pid)/cmdline'
71 register: cmdline
72 failed_when: cmdline is failed or '\0/usr/sbin/ansible_test_service\0' not in cmdline.stdout
73 # No proc on BSD
74 when: not ansible_distribution.lower().endswith('bsd')
75
76 - name: check that the service was started (*bsd)
77 shell: 'ps -p $(cat /var/run/ansible_test_service.pid)'
78 register: cmdline
79 failed_when: cmdline is failed or '/usr/sbin/ansible_test_service' not in cmdline.stdout
80 when: ansible_distribution.lower().endswith('bsd')
81
82 - name: find the service with a pattern
83 service: name=ansible_test pattern="ansible_test_ser" state=started
84 register: start2_result
85
86 - name: assert that the service was started via the pattern
87 assert:
88 that:
89 - "start2_result.name == 'ansible_test'"
90 - "start2_result.state == 'started'"
91 - "start2_result is not changed"
92
93 - name: fetch PID for ansible_test service (before restart)
94 command: 'cat /var/run/ansible_test_service.pid'
95 register: pid_before_restart
96
97 - name: restart the ansible test service
98 service: name=ansible_test state=restarted
99 register: restart_result
100
101 - name: assert that the service was restarted
102 assert:
103 that:
104 - "restart_result.state == 'started'"
105 - "restart_result is changed"
106
107 - name: fetch PID for ansible_test service (after restart)
108 command: 'cat /var/run/ansible_test_service.pid'
109 register: pid_after_restart
110
111 - name: "check that PIDs aren't the same"
112 fail:
113 when: pid_before_restart.stdout == pid_after_restart.stdout
114
115 - name: check that service is started
116 command: 'cat /proc/{{ pid_after_restart.stdout }}/cmdline'
117 register: cmdline
118 failed_when: cmdline is failed or '\0/usr/sbin/ansible_test_service\0' not in cmdline.stdout
119 # No proc on BSD
120 when: not ansible_distribution.lower().endswith('bsd')
121
122 - name: check that the service is started (*bsd)
123 shell: 'ps -p {{ pid_after_restart.stdout }}'
124 register: cmdline
125 failed_when: cmdline is failed or '/usr/sbin/ansible_test_service' not in cmdline.stdout
126 when: ansible_distribution.lower().endswith('bsd')
127
128 - name: restart the ansible test service with a sleep
129 service: name=ansible_test state=restarted sleep=2
130 register: restart_sleep_result
131
132 - name: assert that the service was restarted with a sleep
133 assert:
134 that:
135 - "restart_sleep_result.state == 'started'"
136 - "restart_sleep_result is changed"
137
138 - name: reload the ansible test service
139 service: name=ansible_test state=reloaded
140 register: reload_result
141 # don't do this on systems with systemd because it triggers error:
142 # Unable to reload service ansible_test: ansible_test.service is not active, cannot reload.
143 when: ansible_service_mgr != "systemd"
144
145 - name: assert that the service was reloaded
146 assert:
147 that:
148 - "reload_result.state == 'started'"
149 - "reload_result is changed"
150 when: ansible_service_mgr != "systemd"
151
152 - name: "test for #42786 (sysvinit)"
153 when: ansible_service_mgr == "sysv"
154 block:
155 - name: "sysvinit (#42786): check state, 'enable' parameter isn't set"
156 service: use=sysvinit name=ansible_test state=started
157
158 - name: "sysvinit (#42786): check that service is still enabled"
159 service: use=sysvinit name=ansible_test enabled=yes
160 register: result_enabled
161 failed_when: result_enabled is changed
162
163 - name: fetch PID for ansible_test service
164 command: 'cat /var/run/ansible_test_service.pid'
165 register: ansible_test_pid
166
167 - name: check that service is started
168 command: 'cat /proc/{{ ansible_test_pid.stdout }}/cmdline'
169 register: cmdline
170 failed_when: cmdline is failed or '\0/usr/sbin/ansible_test_service\0' not in cmdline.stdout
171 # No proc on BSD
172 when: not ansible_distribution.lower().endswith('bsd')
173
174 - name: check that the service is started (*bsd)
175 shell: 'ps -p {{ ansible_test_pid.stdout }}'
176 register: cmdline
177 failed_when: cmdline is failed or '/usr/sbin/ansible_test_service' not in cmdline.stdout
178 when: ansible_distribution.lower().endswith('bsd')
179
180 - name: stop the ansible test service
181 service: name=ansible_test state=stopped
182 register: stop_result
183
184 - name: check that the service is stopped
185 command: 'cat /proc/{{ ansible_test_pid.stdout }}/cmdline'
186 register: cmdline
187 failed_when: cmdline is not failed or '\0/usr/sbin/ansible_test_service\0' in cmdline.stdout
188 # No proc on BSD
189 when: not ansible_distribution.lower().endswith('bsd')
190
191 - name: check that the service is stopped (*bsd)
192 shell: 'ps -p {{ ansible_test_pid.stdout }}'
193 register: cmdline
194 failed_when: cmdline is not failed or '/usr/sbin/ansible_test_service' in cmdline.stdout
195 when: ansible_distribution.lower().endswith('bsd')
196
197 - name: assert that the service was stopped
198 assert:
199 that:
200 - "stop_result.state == 'stopped'"
201 - "stop_result is changed"
202
203 - name: disable the ansible test service
204 service: name=ansible_test enabled=no
205 register: disable_result
206
207 - name: assert that the service was disabled
208 assert:
209 that:
210 - "disable_result.enabled == false"
211 - "disable_result is changed"
212
213 - name: try to enable a broken service
214 service: name=ansible_broken_test enabled=yes
215 register: broken_enable_result
216 ignore_errors: True
217
218 - name: assert that the broken test failed
219 assert:
220 that:
221 - "broken_enable_result is failed"
222
223 - name: remove the test daemon script
224 file: path=/usr/sbin/ansible_test_service state=absent
225 register: remove_result
226
227 - name: assert that the test daemon script was removed
228 assert:
229 that:
230 - "remove_result.path == '/usr/sbin/ansible_test_service'"
231 - "remove_result.state == 'absent'"
232
233 - name: the module must fail when a service is not found
234 service:
235 name: 'nonexisting'
236 state: stopped
237 register: result
238 ignore_errors: yes
239 when: ansible_distribution != 'FreeBSD'
240
241 - assert:
242 that:
243 - result is failed
244 - result is search("Could not find the requested service nonexisting")
245 when: ansible_distribution != 'FreeBSD'
246
247 - name: the module must fail in check_mode as well when a service is not found
248 service:
249 name: 'nonexisting'
250 state: stopped
251 register: result
252 check_mode: yes
253 ignore_errors: yes
254 when: ansible_distribution != 'FreeBSD'
255
256 - assert:
257 that:
258 - result is failed
259 - result is search("Could not find the requested service nonexisting")
260 when: ansible_distribution != 'FreeBSD'