From 6f1bdb3e49200cf47bdf529691e9584995e492ba Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sat, 2 Apr 2022 08:45:53 +0200 Subject: [PATCH] pids: re-enabled tests on Alpine Linux (#4405) (#4439) * [WIP] pids: re-enabled tests on Alpine Linux * trying to compile a simple-faked sleep command * make FreeBSD happy * remove the block testing for Alpine Linux * simpler version of sleeper.c * simpler version of sleeper.c, part II * Update tests/integration/targets/pids/tasks/main.yml Co-authored-by: Felix Fontein * Update tests/integration/targets/pids/tasks/main.yml Co-authored-by: Felix Fontein * added license to sleeper.c file Co-authored-by: Felix Fontein (cherry picked from commit 21ee4c84b7da0f5f540882b6d035c47da473db1f) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- .../targets/pids/files/obtainpid.sh | 2 +- .../integration/targets/pids/files/sleeper.c | 12 ++ tests/integration/targets/pids/tasks/main.yml | 173 +++++++++--------- 3 files changed, 96 insertions(+), 91 deletions(-) create mode 100644 tests/integration/targets/pids/files/sleeper.c diff --git a/tests/integration/targets/pids/files/obtainpid.sh b/tests/integration/targets/pids/files/obtainpid.sh index 8e4f4d3766..0cfa6a9e34 100644 --- a/tests/integration/targets/pids/files/obtainpid.sh +++ b/tests/integration/targets/pids/files/obtainpid.sh @@ -1,3 +1,3 @@ #!/usr/bin/env bash -"$1" 100 & +"$1" 100 & echo "$!" > "$2" diff --git a/tests/integration/targets/pids/files/sleeper.c b/tests/integration/targets/pids/files/sleeper.c new file mode 100644 index 0000000000..dcf9feaa6e --- /dev/null +++ b/tests/integration/targets/pids/files/sleeper.c @@ -0,0 +1,12 @@ +/* + * (c) 2022, Alexei Znamensky + * GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + */ + +#include +#include + +int main(int argc, char **argv) { + int delay = atoi(argv[1]); + sleep(delay); +} diff --git a/tests/integration/targets/pids/tasks/main.yml b/tests/integration/targets/pids/tasks/main.yml index 1b836b6c59..52be9f1f99 100644 --- a/tests/integration/targets/pids/tasks/main.yml +++ b/tests/integration/targets/pids/tasks/main.yml @@ -7,112 +7,105 @@ # Copyright: (c) 2019, Saranya Sridharan # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -- when: - - not (ansible_os_family == 'Alpine') # TODO - block: +- name: Attempt installation of latest 'psutil' version + pip: + name: psutil + ignore_errors: true + register: psutil_latest_install - - name: Attempt installation of latest 'psutil' version - pip: - name: psutil - ignore_errors: true - register: psutil_latest_install +- name: Install greatest 'psutil' version which will work with all pip versions + pip: + name: psutil < 5.7.0 + when: psutil_latest_install is failed - - name: Install greatest 'psutil' version which will work with all pip versions - pip: - name: psutil < 5.7.0 - when: psutil_latest_install is failed +- name: "Checking the empty result" + pids: + name: "blahblah" + register: emptypids - - 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: "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" + set_fact: + random_name: some-random-long-name-{{ 99999999 | random }} - - name: "Picking a random process name" - set_fact: - random_name: some-random-long-name-{{ 99999999 | random }} +- name: Copy the fake 'sleep' source code + copy: + src: sleeper.c + dest: "{{ remote_tmp_dir }}/sleeper.c" + mode: 0644 - - name: "finding the 'sleep' binary" - command: which sleep - register: find_sleep +- name: Compile fake 'sleep' binary + command: cc {{ remote_tmp_dir }}/sleeper.c -o {{ remote_tmp_dir }}/{{ random_name }} - - name: "Copying 'sleep' binary" - 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 + copy: + src: obtainpid.sh + dest: "{{ remote_tmp_dir }}/obtainpid.sh" + mode: 0755 - - name: Copy helper script - copy: - src: obtainpid.sh - dest: "{{ remote_tmp_dir }}/obtainpid.sh" +- name: "Run the fake 'sleep' binary" + command: "sh {{ remote_tmp_dir }}/obtainpid.sh '{{ remote_tmp_dir }}/{{ random_name }}' '{{ remote_tmp_dir }}/obtainpid.txt'" - - name: "Running the copy of 'sleep' binary" - command: "sh {{ remote_tmp_dir }}/obtainpid.sh '{{ remote_tmp_dir }}/{{ random_name }}' '{{ remote_tmp_dir }}/obtainpid.txt'" + async: 100 + poll: 0 - async: 100 - poll: 0 +- name: "Wait for one second to make sure that the fake 'sleep' binary has actually been started" + pause: + seconds: 1 - - name: "Wait for one second to make sure that the sleep copy has actually been started" - pause: - seconds: 1 +- name: "Checking the process IDs (PIDs) of fake 'sleep' binary" + pids: + name: "{{ random_name }}" + register: pids - - name: "Checking the process IDs (PIDs) of sleep binary" - pids: - name: "{{ random_name }}" - register: pids +- name: "Checking that exact non-substring matches are required" + pids: + name: "{{ random_name[0:5] }}" + register: exactpidmatch - - name: "Checking that exact non-substring matches are required" - pids: - name: "{{ random_name[0:5] }}" - register: exactpidmatch +- name: "Checking that patterns can be used with the pattern option" + pids: + pattern: "{{ random_name[0:5] }}" + register: pattern_pid_match - - 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 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 - - name: "Checking that .* includes test pid" - pids: - pattern: .* - register: match_all +- name: "Reading pid from the file" + slurp: + src: "{{ remote_tmp_dir }}/obtainpid.txt" + register: newpid - - name: "Reading pid from the file" - slurp: - src: "{{ remote_tmp_dir }}/obtainpid.txt" - 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 == []" + - "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: "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 == []" - - "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: "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 \ No newline at end of file +- name: "Verify that bad input pattern result is failed" + assert: + that: + - bad_pattern_result is failed