| 1 |
#################################################################### |
| 2 |
# WARNING: These are designed specifically for Ansible tests # |
| 3 |
# and should not be used as examples of how to write Ansible roles # |
| 4 |
#################################################################### |
| 5 |
|
| 6 |
# Test code for the pids module |
| 7 |
# Copyright (c) 2019, Saranya Sridharan |
| 8 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 9 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 10 |
|
| 11 |
- name: Attempt installation of latest 'psutil' version |
| 12 |
pip: |
| 13 |
name: psutil |
| 14 |
ignore_errors: true |
| 15 |
register: psutil_latest_install |
| 16 |
|
| 17 |
- name: Install greatest 'psutil' version which will work with all pip versions |
| 18 |
pip: |
| 19 |
name: psutil < 5.7.0 |
| 20 |
when: psutil_latest_install is failed |
| 21 |
|
| 22 |
- name: "Checking the empty result" |
| 23 |
pids: |
| 24 |
name: "blahblah" |
| 25 |
register: emptypids |
| 26 |
|
| 27 |
- name: "Verify that the list of Process IDs (PIDs) returned is empty" |
| 28 |
assert: |
| 29 |
that: |
| 30 |
- emptypids is not changed |
| 31 |
- emptypids.pids == [] |
| 32 |
|
| 33 |
- name: "Picking a random process name" |
| 34 |
set_fact: |
| 35 |
random_name: some-random-long-name-{{ 10000000000 + (9999999999 | random) }} |
| 36 |
|
| 37 |
- name: Copy the fake 'sleep' source code |
| 38 |
copy: |
| 39 |
src: sleeper.c |
| 40 |
dest: "{{ remote_tmp_dir }}/sleeper.c" |
| 41 |
mode: "0644" |
| 42 |
|
| 43 |
- name: Compile fake 'sleep' binary |
| 44 |
command: cc {{ remote_tmp_dir }}/sleeper.c -o {{ remote_tmp_dir }}/{{ random_name }} |
| 45 |
|
| 46 |
- name: Copy templated helper script |
| 47 |
template: |
| 48 |
src: obtainpid.sh.j2 |
| 49 |
dest: "{{ remote_tmp_dir }}/obtainpid.sh" |
| 50 |
mode: "0755" |
| 51 |
|
| 52 |
- name: "Run the fake 'sleep' binary" |
| 53 |
command: sh {{ remote_tmp_dir }}/obtainpid.sh |
| 54 |
async: 100 |
| 55 |
poll: 0 |
| 56 |
|
| 57 |
- name: "Wait for one second to make sure that the fake 'sleep' binary has actually been started" |
| 58 |
pause: |
| 59 |
seconds: 1 |
| 60 |
|
| 61 |
- name: "Checking the process IDs (PIDs) of fake 'sleep' binary" |
| 62 |
pids: |
| 63 |
name: "{{ random_name }}" |
| 64 |
register: pids |
| 65 |
|
| 66 |
- name: "Checking that exact non-substring matches are required" |
| 67 |
pids: |
| 68 |
name: "{{ random_name[0:25] }}" |
| 69 |
register: exactpidmatch |
| 70 |
|
| 71 |
- name: "Checking that patterns can be used with the pattern option" |
| 72 |
pids: |
| 73 |
pattern: "{{ random_name[0:25] }}" |
| 74 |
register: pattern_pid_match |
| 75 |
|
| 76 |
- name: "Checking that case-insensitive patterns can be used with the pattern option" |
| 77 |
pids: |
| 78 |
pattern: "{{ random_name[0:25] | upper }}" |
| 79 |
ignore_case: true |
| 80 |
register: caseinsensitive_pattern_pid_match |
| 81 |
|
| 82 |
- name: "Checking that .* includes test pid" |
| 83 |
pids: |
| 84 |
pattern: .* |
| 85 |
register: match_all |
| 86 |
|
| 87 |
- name: "Reading pid from the file" |
| 88 |
slurp: |
| 89 |
src: "{{ remote_tmp_dir }}/obtainpid.txt" |
| 90 |
register: newpid |
| 91 |
|
| 92 |
- name: Gather all processes to make debugging easier |
| 93 |
command: ps aux |
| 94 |
register: result |
| 95 |
no_log: true |
| 96 |
|
| 97 |
- name: List all processes to make debugging easier |
| 98 |
debug: |
| 99 |
var: result.stdout_lines |
| 100 |
|
| 101 |
- name: "Verify that the Process IDs (PIDs) returned is not empty and also equal to the PIDs obtained in console" |
| 102 |
assert: |
| 103 |
that: |
| 104 |
- "pids.pids | join(' ') == newpid.content | b64decode | trim" |
| 105 |
- "pids.pids | length > 0" |
| 106 |
- "exactpidmatch.pids == []" |
| 107 |
- "pattern_pid_match.pids | join(' ') == newpid.content | b64decode | trim" |
| 108 |
- "caseinsensitive_pattern_pid_match.pids | join(' ') == newpid.content | b64decode | trim" |
| 109 |
- newpid.content | b64decode | trim | int in match_all.pids |
| 110 |
|
| 111 |
- name: "Register output of bad input pattern" |
| 112 |
pids: |
| 113 |
pattern: (unterminated |
| 114 |
register: bad_pattern_result |
| 115 |
ignore_errors: true |
| 116 |
|
| 117 |
- name: "Verify that bad input pattern result is failed" |
| 118 |
assert: |
| 119 |
that: |
| 120 |
- bad_pattern_result is failed |