2020-09-25 08:01:17 +02:00
|
|
|
####################################################################
|
|
|
|
# WARNING: These are designed specifically for Ansible tests #
|
|
|
|
# and should not be used as examples of how to write Ansible roles #
|
|
|
|
####################################################################
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
# Test code for the pids module
|
|
|
|
# Copyright: (c) 2019, Saranya Sridharan
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
- name: "Installing the psutil module"
|
|
|
|
pip:
|
2020-07-19 14:50:40 +02:00
|
|
|
name: psutil < 5.7.0
|
|
|
|
# Version 5.7.0 breaks on older pip versions. See https://github.com/ansible/ansible/pull/70667
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
- name: "Checking the empty result"
|
|
|
|
pids:
|
|
|
|
name: "blahblah"
|
|
|
|
register: emptypids
|
|
|
|
|
|
|
|
- name: "Verify that the list of Process IDs (PIDs) returned is empty"
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- emptypids is not changed
|
|
|
|
- emptypids.pids == []
|
|
|
|
|
|
|
|
- name: "Picking a random process name"
|
2020-10-23 10:53:02 +02:00
|
|
|
set_fact:
|
|
|
|
random_name: some-random-long-name-{{ 99999999 | random }}
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
- name: "finding the 'sleep' binary"
|
|
|
|
command: which sleep
|
|
|
|
register: find_sleep
|
|
|
|
|
2020-10-23 10:53:02 +02:00
|
|
|
- name: "Copying 'sleep' binary"
|
2021-09-09 08:10:12 +02:00
|
|
|
command: cp {{ find_sleep.stdout }} {{ remote_tmp_dir }}/{{ random_name }}
|
|
|
|
# The following does not work on macOS 11.1 (it uses shutil.copystat, and that will die with a PermissionError):
|
|
|
|
# copy:
|
|
|
|
# src: "{{ find_sleep.stdout }}"
|
|
|
|
# dest: "{{ remote_tmp_dir }}/{{ random_name }}"
|
|
|
|
# mode: "0777"
|
|
|
|
# remote_src: true
|
|
|
|
|
|
|
|
- name: Copy helper script
|
2020-03-09 10:11:07 +01:00
|
|
|
copy:
|
2021-09-09 08:10:12 +02:00
|
|
|
src: obtainpid.sh
|
|
|
|
dest: "{{ remote_tmp_dir }}/obtainpid.sh"
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
- name: "Running the copy of 'sleep' binary"
|
2021-09-09 08:10:12 +02:00
|
|
|
command: "sh {{ remote_tmp_dir }}/obtainpid.sh '{{ remote_tmp_dir }}/{{ random_name }}' '{{ remote_tmp_dir }}/obtainpid.txt'"
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
async: 100
|
|
|
|
poll: 0
|
|
|
|
|
2020-10-23 10:53:02 +02:00
|
|
|
- name: "Wait for one second to make sure that the sleep copy has actually been started"
|
|
|
|
pause:
|
|
|
|
seconds: 1
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
- name: "Checking the process IDs (PIDs) of sleep binary"
|
|
|
|
pids:
|
2020-10-23 10:53:02 +02:00
|
|
|
name: "{{ random_name }}"
|
2020-03-09 10:11:07 +01:00
|
|
|
register: pids
|
|
|
|
|
|
|
|
- name: "Checking that exact non-substring matches are required"
|
|
|
|
pids:
|
2020-10-23 10:53:02 +02:00
|
|
|
name: "{{ random_name[0:5] }}"
|
2020-03-09 10:11:07 +01:00
|
|
|
register: exactpidmatch
|
|
|
|
|
2021-04-21 12:45:09 +02:00
|
|
|
- name: "Checking that patterns can be used with the pattern option"
|
|
|
|
pids:
|
|
|
|
pattern: "{{ random_name[0:5] }}"
|
|
|
|
register: pattern_pid_match
|
|
|
|
|
|
|
|
- name: "Checking that case-insensitive patterns can be used with the pattern option"
|
|
|
|
pids:
|
|
|
|
pattern: "{{ random_name[0:5] | upper }}"
|
|
|
|
ignore_case: true
|
|
|
|
register: caseinsensitive_pattern_pid_match
|
|
|
|
|
|
|
|
- name: "Checking that .* includes test pid"
|
|
|
|
pids:
|
|
|
|
pattern: .*
|
|
|
|
register: match_all
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
- name: "Reading pid from the file"
|
|
|
|
slurp:
|
2021-09-09 08:10:12 +02:00
|
|
|
src: "{{ remote_tmp_dir }}/obtainpid.txt"
|
2020-03-09 10:11:07 +01:00
|
|
|
register: newpid
|
|
|
|
|
|
|
|
- name: "Verify that the Process IDs (PIDs) returned is not empty and also equal to the PIDs obtained in console"
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "pids.pids | join(' ') == newpid.content | b64decode | trim"
|
|
|
|
- "pids.pids | length > 0"
|
|
|
|
- "exactpidmatch.pids == []"
|
2021-04-21 12:45:09 +02:00
|
|
|
- "pattern_pid_match.pids | join(' ') == newpid.content | b64decode | trim"
|
|
|
|
- "caseinsensitive_pattern_pid_match.pids | join(' ') == newpid.content | b64decode | trim"
|
|
|
|
- newpid.content | b64decode | trim | int in match_all.pids
|
|
|
|
|
|
|
|
- name: "Register output of bad input pattern"
|
|
|
|
pids:
|
|
|
|
pattern: (unterminated
|
|
|
|
register: bad_pattern_result
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
|
|
- name: "Verify that bad input pattern result is failed"
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- bad_pattern_result is failed
|