summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/become
diff options
context:
space:
mode:
authorMatt Clay <matt@mystile.com>2022-07-19 23:12:33 +0200
committerGitHub <noreply@github.com>2022-07-19 23:12:33 +0200
commitde2163b160c94df5396f5466ac52b52fc4e76194 (patch)
treec603ad568e85393b77fd246de6c9e5d033edcf37 /test/integration/targets/become
parentansible-test - Use sudo for all remotes. (#78310) (diff)
downloadansible-de2163b160c94df5396f5466ac52b52fc4e76194.tar.xz
ansible-de2163b160c94df5396f5466ac52b52fc4e76194.zip
Overhaul become integration tests. (#78312)
Diffstat (limited to 'test/integration/targets/become')
-rw-r--r--test/integration/targets/become/aliases1
-rw-r--r--test/integration/targets/become/files/copy.txt (renamed from test/integration/targets/become/files/baz.txt)0
-rw-r--r--test/integration/targets/become/meta/main.yml2
-rw-r--r--test/integration/targets/become/tasks/become.yml49
-rw-r--r--test/integration/targets/become/tasks/default.yml82
-rw-r--r--test/integration/targets/become/tasks/main.yml24
-rw-r--r--test/integration/targets/become/tasks/su.yml91
-rw-r--r--test/integration/targets/become/tasks/sudo.yml91
-rw-r--r--test/integration/targets/become/templates/bar.j21
-rw-r--r--test/integration/targets/become/vars/default.yml1
-rw-r--r--test/integration/targets/become/vars/main.yml18
11 files changed, 90 insertions, 270 deletions
diff --git a/test/integration/targets/become/aliases b/test/integration/targets/become/aliases
index db54e68c92..0c490f1c5f 100644
--- a/test/integration/targets/become/aliases
+++ b/test/integration/targets/become/aliases
@@ -1,3 +1,4 @@
destructive
shippable/posix/group1
context/target
+gather_facts/no
diff --git a/test/integration/targets/become/files/baz.txt b/test/integration/targets/become/files/copy.txt
index b8d834daa4..b8d834daa4 100644
--- a/test/integration/targets/become/files/baz.txt
+++ b/test/integration/targets/become/files/copy.txt
diff --git a/test/integration/targets/become/meta/main.yml b/test/integration/targets/become/meta/main.yml
new file mode 100644
index 0000000000..0cef72c5ab
--- /dev/null
+++ b/test/integration/targets/become/meta/main.yml
@@ -0,0 +1,2 @@
+dependencies:
+ - setup_test_user
diff --git a/test/integration/targets/become/tasks/become.yml b/test/integration/targets/become/tasks/become.yml
new file mode 100644
index 0000000000..e581e1bb0a
--- /dev/null
+++ b/test/integration/targets/become/tasks/become.yml
@@ -0,0 +1,49 @@
+- name: test becoming user ({{ become_test }})
+ raw: whoami
+ register: whoami
+
+- name: implicit tilde expansion reflects become user (broken={{ become_test_config.implicit_tilde_broken | default(False) }}) ({{ become_test }})
+ stat:
+ path: "~"
+ register: stat_home_implicit
+
+- name: explicit tilde expansion reflects become user ({{ become_test }})
+ stat:
+ path: "~{{ ansible_become_user }}"
+ register: stat_home_explicit
+
+- name: put a file ({{ become_test }})
+ copy:
+ src: copy.txt
+ dest: "~{{ ansible_become_user }}/{{ ansible_become_method }}-{{ ansible_become_user }}-copy.txt"
+ register: put_file
+
+- name: fetch a file ({{ become_test }})
+ fetch:
+ src: "~{{ ansible_become_user }}/{{ ansible_become_method }}-{{ ansible_become_user }}-copy.txt"
+ dest: "{{ output_dir }}"
+ register: fetch_file
+
+- name: explicit tilde expansion reflects become user ({{ become_test }})
+ stat:
+ path: "~{{ ansible_become_user }}/{{ ansible_become_method }}-{{ ansible_become_user }}-copy.txt"
+ register: stat_file
+
+- name: verify results from previous tasks ({{ become_test }})
+ assert:
+ that:
+ - "whoami.stdout|trim == '{{ ansible_become_user }}'"
+
+ - "stat_home_implicit.stat.exists == True"
+ - "stat_home_implicit.stat.path|basename == '{{ ansible_become_user }}' or (become_test_config.implicit_tilde_broken | default(False))"
+
+ - "stat_home_explicit.stat.exists == True"
+ - "stat_home_explicit.stat.path|basename == '{{ ansible_become_user }}'"
+
+ - "put_file.uid == test_user.uid"
+ - "put_file.gid == test_user.group"
+
+ - "fetch_file.remote_checksum == put_file.checksum"
+
+ - "stat_file.stat.exists == True"
+ - "stat_file.stat.path|dirname|basename == '{{ ansible_become_user }}'"
diff --git a/test/integration/targets/become/tasks/default.yml b/test/integration/targets/become/tasks/default.yml
deleted file mode 100644
index 4ba10170dd..0000000000
--- a/test/integration/targets/become/tasks/default.yml
+++ /dev/null
@@ -1,82 +0,0 @@
-- name: Create test user (become_method=default)
- become: True
- become_user: root
- user:
- name: "{{ become_test_user }}"
- group: '{{ "staff" if ansible_facts.distribution == "MacOSX" else omit }}'
-
-- name: test becoming user (become_method=default)
- shell: whoami
- become: True
- become_user: "{{ become_test_user }}"
- register: results
-
-- assert:
- that:
- - "results.stdout == '{{ become_test_user }}'"
-
-- name: tilde expansion honors become in file (become_method=default)
- become: True
- become_user: "{{ become_test_user }}"
- file:
- path: "~/foo.txt"
- state: touch
-
-- name: check that the path in the user's home dir was created (become_method=default)
- become: True
- become_user: "{{ become_test_user }}"
- stat:
- path: "~{{ become_test_user }}/foo.txt"
- register: results
-
-- assert:
- that:
- - "results.stat.exists == True"
- - "results.stat.path|dirname|basename == '{{ become_test_user }}'"
-
-- name: tilde expansion honors become in template (become_method=default)
- become: True
- become_user: "{{ become_test_user }}"
- template:
- src: "bar.j2"
- dest: "~/bar.txt"
-
-- name: check that the path in the user's home dir was created (become_method=default)
- become: True
- become_user: "{{ become_test_user }}"
- stat:
- path: "~{{ become_test_user }}/bar.txt"
- register: results
-
-- assert:
- that:
- - "results.stat.exists == True"
- - "results.stat.path|dirname|basename == '{{ become_test_user }}'"
-
-- name: tilde expansion honors become in copy (become_method=default)
- become: True
- become_user: "{{ become_test_user }}"
- copy:
- src: baz.txt
- dest: "~/baz.txt"
-
-- name: check that the path in the user's home dir was created (become_method=default)
- become: True
- become_user: "{{ become_test_user }}"
- stat:
- path: "~{{ become_test_user }}/baz.txt"
- register: results
-
-- assert:
- that:
- - "results.stat.exists == True"
- - "results.stat.path|dirname|basename == '{{ become_test_user }}'"
-
-- name: Remove test user and their home dir (become_method=default)
- become: True
- become_user: root
- user:
- name: "{{ become_test_user }}"
- state: "absent"
- remove: "yes"
- force: "yes"
diff --git a/test/integration/targets/become/tasks/main.yml b/test/integration/targets/become/tasks/main.yml
index b4c7b6015d..286624c57b 100644
--- a/test/integration/targets/become/tasks/main.yml
+++ b/test/integration/targets/become/tasks/main.yml
@@ -1,5 +1,21 @@
-- include_vars: default.yml
+- name: determine connection user
+ command: whoami
+ register: connection_user
+ vars:
+ ansible_become: no
-- import_tasks: default.yml
-- import_tasks: sudo.yml
-- import_tasks: su.yml
+- name: include become tests
+ include_tasks: become.yml
+ vars:
+ ansible_become: yes
+ ansible_become_user: "{{ become_test_config.user }}"
+ ansible_become_flags: "{{ become_test_config.flags | default(None) }}"
+ ansible_become_method: "{{ become_test_config.method }}"
+ ansible_become_password: "{{ become_test_config.password | default(None) }}"
+ loop: "{{
+ (become_methods | selectattr('skip', 'undefined') | list)+
+ (become_methods | selectattr('skip', 'defined') | rejectattr('skip') | list)
+ }}"
+ loop_control:
+ loop_var: become_test_config
+ label: "{{ become_test }}"
diff --git a/test/integration/targets/become/tasks/su.yml b/test/integration/targets/become/tasks/su.yml
deleted file mode 100644
index d314b0a87e..0000000000
--- a/test/integration/targets/become/tasks/su.yml
+++ /dev/null
@@ -1,91 +0,0 @@
-- name: Create test user (become_method=su)
- become: True
- become_user: root
- become_method: su
- user:
- name: "{{ become_test_user }}"
- group: '{{ "staff" if ansible_facts.distribution == "MacOSX" else omit }}'
-
-- name: test becoming user (become_method=su)
- shell: whoami
- become: True
- become_user: "{{ become_test_user }}"
- become_method: su
- register: results
-
-- assert:
- that:
- - "results.stdout == '{{ become_test_user }}'"
-
-- name: tilde expansion honors become in file (become_method=su)
- become: True
- become_user: "{{ become_test_user }}"
- become_method: su
- file:
- path: "~/foo.txt"
- state: touch
-
-- name: check that the path in the user's home dir was created (become_method=su)
- become: True
- become_user: "{{ become_test_user }}"
- become_method: su
- stat:
- path: "~{{ become_test_user }}/foo.txt"
- register: results
-
-- assert:
- that:
- - "results.stat.exists == True"
- - "results.stat.path|dirname|basename == '{{ become_test_user }}'"
-
-- name: tilde expansion honors become in template (become_method=su)
- become: True
- become_user: "{{ become_test_user }}"
- become_method: su
- template:
- src: "bar.j2"
- dest: "~/bar.txt"
-
-- name: check that the path in the user's home dir was created (become_method=su)
- become: True
- become_user: "{{ become_test_user }}"
- become_method: su
- stat:
- path: "~{{ become_test_user }}/bar.txt"
- register: results
-
-- assert:
- that:
- - "results.stat.exists == True"
- - "results.stat.path|dirname|basename == '{{ become_test_user }}'"
-
-- name: tilde expansion honors become in copy (become_method=su)
- become: True
- become_user: "{{ become_test_user }}"
- become_method: su
- copy:
- src: baz.txt
- dest: "~/baz.txt"
-
-- name: check that the path in the user's home dir was created (become_method=su)
- become: True
- become_user: "{{ become_test_user }}"
- become_method: su
- stat:
- path: "~{{ become_test_user }}/baz.txt"
- register: results
-
-- assert:
- that:
- - "results.stat.exists == True"
- - "results.stat.path|dirname|basename == '{{ become_test_user }}'"
-
-- name: Remove test user and their home dir (become_method=su)
- become: True
- become_user: root
- become_method: su
- user:
- name: "{{ become_test_user }}"
- state: "absent"
- remove: "yes"
- force: "yes"
diff --git a/test/integration/targets/become/tasks/sudo.yml b/test/integration/targets/become/tasks/sudo.yml
deleted file mode 100644
index 636ec378be..0000000000
--- a/test/integration/targets/become/tasks/sudo.yml
+++ /dev/null
@@ -1,91 +0,0 @@
-- name: Create test user (become_method=sudo)
- become: True
- become_user: root
- become_method: sudo
- user:
- name: "{{ become_test_user }}"
- group: '{{ "staff" if ansible_facts.distribution == "MacOSX" else omit }}'
-
-- name: test becoming user (become_method=sudo)
- shell: whoami
- become: True
- become_user: "{{ become_test_user }}"
- become_method: sudo
- register: results
-
-- assert:
- that:
- - "results.stdout == '{{ become_test_user }}'"
-
-- name: tilde expansion honors become in file (become_method=sudo)
- become: True
- become_user: "{{ become_test_user }}"
- become_method: sudo
- file:
- path: "~/foo.txt"
- state: touch
-
-- name: check that the path in the user's home dir was created (become_method=sudo)
- become: True
- become_user: "{{ become_test_user }}"
- become_method: sudo
- stat:
- path: "~{{ become_test_user }}/foo.txt"
- register: results
-
-- assert:
- that:
- - "results.stat.exists == True"
- - "results.stat.path|dirname|basename == '{{ become_test_user }}'"
-
-- name: tilde expansion honors become in template (become_method=sudo)
- become: True
- become_user: "{{ become_test_user }}"
- become_method: sudo
- template:
- src: "bar.j2"
- dest: "~/bar.txt"
-
-- name: check that the path in the user's home dir was created (become_method=sudo)
- become: True
- become_user: "{{ become_test_user }}"
- become_method: sudo
- stat:
- path: "~{{ become_test_user }}/bar.txt"
- register: results
-
-- assert:
- that:
- - "results.stat.exists == True"
- - "results.stat.path|dirname|basename == '{{ become_test_user }}'"
-
-- name: tilde expansion honors become in copy (become_method=sudo)
- become: True
- become_user: "{{ become_test_user }}"
- become_method: sudo
- copy:
- src: baz.txt
- dest: "~/baz.txt"
-
-- name: check that the path in the user's home dir was created (become_method=sudo)
- become: True
- become_user: "{{ become_test_user }}"
- become_method: sudo
- stat:
- path: "~{{ become_test_user }}/baz.txt"
- register: results
-
-- assert:
- that:
- - "results.stat.exists == True"
- - "results.stat.path|dirname|basename == '{{ become_test_user }}'"
-
-- name: Remove test user and their home dir (become_method=sudo)
- become: True
- become_user: root
- become_method: sudo
- user:
- name: "{{ become_test_user }}"
- state: "absent"
- remove: "yes"
- force: "yes"
diff --git a/test/integration/targets/become/templates/bar.j2 b/test/integration/targets/become/templates/bar.j2
deleted file mode 100644
index 7c5fe0ab49..0000000000
--- a/test/integration/targets/become/templates/bar.j2
+++ /dev/null
@@ -1 +0,0 @@
-{{ become_test_user }}
diff --git a/test/integration/targets/become/vars/default.yml b/test/integration/targets/become/vars/default.yml
deleted file mode 100644
index 223d44ed24..0000000000
--- a/test/integration/targets/become/vars/default.yml
+++ /dev/null
@@ -1 +0,0 @@
-become_test_user: ansibletest1
diff --git a/test/integration/targets/become/vars/main.yml b/test/integration/targets/become/vars/main.yml
new file mode 100644
index 0000000000..5bb1c02123
--- /dev/null
+++ b/test/integration/targets/become/vars/main.yml
@@ -0,0 +1,18 @@
+become_test: >-
+ {{ become_test_config.method }} from {{ connection_user.stdout }} to {{ become_test_config.user }}
+ {{ 'with' if become_test_config.password else 'without' }} password
+ {{ 'with flags: ' + become_test_config.flags if become_test_config.flags | default('') else 'without flags' }}
+
+become_methods:
+ - method: sudo
+ user: "{{ test_user_name }}"
+ password: "{{ test_user_plaintext_password if connection_user.stdout != 'root' else None }}"
+ # Some systems are not configured to allow sudo for non-root users.
+ # The tests could be updated in the future to temporarily enable sudo for the connection user.
+ skip: "{{ connection_user.stdout != 'root' and ansible_distribution == 'FreeBSD' }}"
+ # Implicit tilde support `~` is broken on systems which do not set HOME to that of the become user.
+ # Explicit tilde support `~username` is unaffected.
+ implicit_tilde_broken: "{{ ansible_distribution == 'MacOSX' }}"
+ - method: su
+ user: "{{ test_user_name }}"
+ password: "{{ test_user_plaintext_password if connection_user.stdout != 'root' else None }}"