From dc5f83c09bafc39ecf396411370c8fad9d046f51 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Thu, 14 Sep 2017 17:13:43 +0200 Subject: [PATCH] Add sudo/su become_methods for become tests (#30266) * Add sudo/su become_methods for become tests * Fix test on osx --- .../targets/become/tasks/default.yml | 81 ++++++++++++++ .../integration/targets/become/tasks/main.yml | 84 +-------------- test/integration/targets/become/tasks/su.yml | 102 ++++++++++++++++++ .../integration/targets/become/tasks/sudo.yml | 90 ++++++++++++++++ 4 files changed, 276 insertions(+), 81 deletions(-) create mode 100644 test/integration/targets/become/tasks/default.yml create mode 100644 test/integration/targets/become/tasks/su.yml create mode 100644 test/integration/targets/become/tasks/sudo.yml diff --git a/test/integration/targets/become/tasks/default.yml b/test/integration/targets/become/tasks/default.yml new file mode 100644 index 0000000000..59568ed662 --- /dev/null +++ b/test/integration/targets/become/tasks/default.yml @@ -0,0 +1,81 @@ +- name: Create test user (become_method=default) + become: True + become_user: root + user: + name: "{{ become_test_user }}" + +- 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 00a3dd0167..3feb5cc729 100644 --- a/test/integration/targets/become/tasks/main.yml +++ b/test/integration/targets/become/tasks/main.yml @@ -1,83 +1,5 @@ - include_vars: default.yml -- name: Create test user - become: True - become_user: root - user: - name: "{{ become_test_user }}" - -- name: test becoming user - 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: 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: 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: 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: 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: 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: 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: True - become_user: root - user: - name: "{{ become_test_user }}" - state: "absent" - remove: "yes" - force: "yes" +- include: default.yml +- include: sudo.yml +- include: su.yml diff --git a/test/integration/targets/become/tasks/su.yml b/test/integration/targets/become/tasks/su.yml new file mode 100644 index 0000000000..e4d37a3959 --- /dev/null +++ b/test/integration/targets/become/tasks/su.yml @@ -0,0 +1,102 @@ +- name: Create test user (become_method=su) + become: True + become_user: root + become_method: su + user: + name: "{{ become_test_user }}" + when: ansible_distribution != "MacOSX" + + +- name: Create test user (become_method=su) + become: True + become_user: root + become_method: su + user: + name: "{{ become_test_user }}" + # explicitly set user shell since the default shell on OS X is /usr/bin/false + shell: /bin/bash + when: ansible_distribution == "MacOSX" + +- 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 new file mode 100644 index 0000000000..8b0cce5a76 --- /dev/null +++ b/test/integration/targets/become/tasks/sudo.yml @@ -0,0 +1,90 @@ +- name: Create test user (become_method=sudo) + become: True + become_user: root + become_method: sudo + user: + name: "{{ become_test_user }}" + +- 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"