From 907650c7467a4915bdc9bc49ba6656c1fb675c1f Mon Sep 17 00:00:00 2001 From: Thomas Woerner Date: Thu, 1 Aug 2024 10:59:00 +0200 Subject: [PATCH] tests/user/test_users_present_*: Use new generate_test_data.yml The new generate_test_data.yml is creating the test data with ansible.builtin.shell and is not calling shell scripts any more. The generation in the yml file and also the set_fact calls make sure that the test data is used. --- tests/user/create_users_json.yml | 13 ----- tests/user/generate_test_data.yml | 48 +++++++++++++++++++ tests/user/test_users_present_absent.yml | 31 ++++++------ .../user/test_users_present_absent_slice.yml | 31 +++++------- tests/user/users.sh | 29 ----------- tests/user/users_absent.sh | 22 --------- 6 files changed, 76 insertions(+), 98 deletions(-) delete mode 100644 tests/user/create_users_json.yml create mode 100644 tests/user/generate_test_data.yml delete mode 100644 tests/user/users.sh delete mode 100644 tests/user/users_absent.sh diff --git a/tests/user/create_users_json.yml b/tests/user/create_users_json.yml deleted file mode 100644 index 15c3701f3f..0000000000 --- a/tests/user/create_users_json.yml +++ /dev/null @@ -1,13 +0,0 @@ ---- -- name: Create users.json - hosts: localhost - - tasks: - - name: Check if users.json exists - ansible.builtin.stat: - path: users.json - register: register_stat_users - - - name: Create users.json - ansible.builtin.command: /bin/bash users.sh 500 - when: not register_stat_users.stat.exists diff --git a/tests/user/generate_test_data.yml b/tests/user/generate_test_data.yml new file mode 100644 index 0000000000..2289954447 --- /dev/null +++ b/tests/user/generate_test_data.yml @@ -0,0 +1,48 @@ +# Generate lists for users +--- +- name: Create present services.json data + ansible.builtin.shell: | + date=$(date --date='+2 years' "+%Y-%m-%d %H:%M:%S") + echo "[" + for i in $(seq -w 1 "{{ NUM }}"); do + echo " {" + echo " \"name\": \"testuser_${i}\"," + echo " \"first\": \"First ${i}\"," + echo " \"last\": \"Last ${i}\"," + echo " \"password\": \"user${i}PW\"," + echo " \"passwordexpiration\": \"${date}\"" + if [ "$i" -lt "{{ NUM }}" ]; then + echo " }," + else + echo " }" + fi + done + echo "]" + vars: + NUM: 500 + register: command + +- name: Set user_list + ansible.builtin.set_fact: + user_list: "{{ command.stdout | from_json }}" + +- name: Create absent user.json data + ansible.builtin.shell: | + echo "[" + for i in $(seq -w 1 "{{ NUM }}"); do + echo " {" + echo " \"name\": \"testuser_${i}\"" + if [ "$i" -lt "{{ NUM }}" ]; then + echo " }," + else + echo " }" + fi + done + echo "]" + vars: + NUM: 500 + register: command + +- name: Set user_absent_list + ansible.builtin.set_fact: + user_absent_list: "{{ command.stdout | from_json }}" diff --git a/tests/user/test_users_present_absent.yml b/tests/user/test_users_present_absent.yml index 38752ce8e9..33a67af2a6 100644 --- a/tests/user/test_users_present_absent.yml +++ b/tests/user/test_users_present_absent.yml @@ -1,39 +1,38 @@ --- -- name: Include create_users_json.yml - ansible.builtin.import_playbook: create_users_json.yml - - name: Test users present and absent hosts: ipaserver become: true gather_facts: false tasks: - - name: Include users.json - ansible.builtin.include_vars: - file: users.json # noqa missing-import + - name: Include generate_test_data.yml + ansible.builtin.include_tasks: generate_test_data.yml + + - name: Size of user_list + ansible.builtin.debug: + msg: "{{ user_list | length }}" - - name: Create dict with user names - ansible.builtin.set_fact: - user_names: "{{ user_names | default([]) + [{'name': item.name}] }}" - loop: "{{ users }}" + - name: Size of user_absent_list + ansible.builtin.debug: + msg: "{{ user_absent_list | length }}" - - name: Cleanup test users len:{{ users | length }} + - name: Cleanup test users ipauser: ipaadmin_password: SomeADMINpassword - users: "{{ user_names }}" + users: "{{ user_absent_list }}" state: absent - - name: Users present len:{{ users | length }} + - name: Users present ipauser: ipaadmin_password: SomeADMINpassword - users: "{{ users }}" + users: "{{ user_list }}" register: result failed_when: not result.changed or result.failed - - name: Users absent len:{{ users | length }} + - name: Users absent ipauser: ipaadmin_password: SomeADMINpassword - users: "{{ user_names }}" + users: "{{ user_absent_list }}" state: absent register: result failed_when: not result.changed or result.failed diff --git a/tests/user/test_users_present_absent_slice.yml b/tests/user/test_users_present_absent_slice.yml index 885d910c85..fc3b51c366 100644 --- a/tests/user/test_users_present_absent_slice.yml +++ b/tests/user/test_users_present_absent_slice.yml @@ -1,7 +1,4 @@ --- -- name: Include create_users_json.yml - ansible.builtin.import_playbook: create_users_json.yml - - name: Test users present and absent with slice hosts: ipaserver become: true @@ -10,39 +7,37 @@ vars: slice_size: 100 tasks: - - name: Include users.json - ansible.builtin.include_vars: - file: users.json # noqa missing-import + - name: Include generate_test_data.yml + ansible.builtin.include_tasks: generate_test_data.yml - - name: Create dict with user names - ansible.builtin.set_fact: - user_names: "{{ user_names | default([]) + [{'name': item.name}] }}" - loop: "{{ users }}" + - name: Size of user_list + ansible.builtin.debug: + msg: "{{ user_list | length }}" - - name: Size of users slice. + - name: Size of user_absent_list ansible.builtin.debug: - msg: "{{ users | length }}" + msg: "{{ user_absent_list | length }}" - name: Cleanup test users ipauser: ipaadmin_password: SomeADMINpassword - users: "{{ user_names[item : item + slice_size] }}" + users: "{{ user_absent_list[item : item + slice_size] }}" state: absent - loop: "{{ range(0, user_names | length, slice_size) | list }}" + loop: "{{ range(0, user_absent_list | length, slice_size) | list }}" - name: Users present ipauser: ipaadmin_password: SomeADMINpassword - users: "{{ users[item : item + slice_size] }}" - loop: "{{ range(0, users | length, slice_size) | list }}" + users: "{{ user_list[item : item + slice_size] }}" + loop: "{{ range(0, user_list | length, slice_size) | list }}" register: result failed_when: not result.changed or result.failed - name: Users absent ipauser: ipaadmin_password: SomeADMINpassword - users: "{{ user_names[item : item + slice_size] }}" + users: "{{ user_absent_list[item : item + slice_size] }}" state: absent - loop: "{{ range(0, user_names | length, slice_size) | list }}" + loop: "{{ range(0, user_absent_list | length, slice_size) | list }}" register: result failed_when: not result.changed or result.failed diff --git a/tests/user/users.sh b/tests/user/users.sh deleted file mode 100644 index 8b1f5d298c..0000000000 --- a/tests/user/users.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash -eu - -NUM=${1-1000} -FILE="users.json" -date=$(date --date='+2 years' "+%Y-%m-%d %H:%M:%S") - -echo "{" > "$FILE" - -echo " \"users\": [" >> "$FILE" - -for i in $(seq 1 "$NUM"); do - { - echo " {" - echo " \"name\": \"user$i\"," - echo " \"first\": \"First $i\"," - echo " \"last\": \"Last $i\"," - echo " \"password\": \"user${i}PW\"," - echo " \"passwordexpiration\": \"$date\"" - } >> "$FILE" - if [ "$i" -lt "$NUM" ]; then - echo " }," >> "$FILE" - else - echo " }" >> "$FILE" - fi -done - -echo " ]" >> "$FILE" - -echo "}" >> "$FILE" diff --git a/tests/user/users_absent.sh b/tests/user/users_absent.sh deleted file mode 100644 index 812c5d6ffe..0000000000 --- a/tests/user/users_absent.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash -eu - -NUM=1000 -FILE="users_absent.json" - -echo "{" > "$FILE" - -echo " \"users\": [" >> "$FILE" - -for i in $(seq 1 "$NUM"); do - echo " {" >> "$FILE" - echo " \"name\": \"user$i\"," >> "$FILE" - if [ "$i" -lt "$NUM" ]; then - echo " }," >> "$FILE" - else - echo " }" >> "$FILE" - fi -done - -echo " ]" >> "$FILE" - -echo "}" >> "$FILE"