summaryrefslogtreecommitdiff
path: root/test/integration/targets/user/tasks/main.yml
blob: 3d608a8fff7283b7fe0bc13e48a0e972700efe57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Test code for the user module.
# (c) 2017, James Tanner <tanner.jc@gmail.com>

# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
#
- name: get the jinja2 version
  shell: python -c 'import jinja2; print(jinja2.__version__)'
  register: jinja2_version
  delegate_to: localhost
- debug: var=jinja2_version

##
## user add
##
#
- name: remove the test user
  user:
      name: ansibulluser
      state: absent

- name: try to create a user
  user:
      name: ansibulluser
      state: present
  register: user_test0
- debug: var=user_test0

- name: make a list of users
  script: userlist.sh "{{ ansible_distribution }}"
  register: user_names
- debug: var=user_names

- name: validate results for testcase 0
  assert:
      that:
          - 'user_test0.changed is defined'
          - 'user_test0.changed'
          - '"ansibulluser" in user_names.stdout_lines'

##
## user check
##

- name: run existing user check tests
  user:
      name: "{{ user_names.stdout_lines|random }}"
      state: present
      createhome: no
  with_sequence: start=1 end=5
  register: user_test1
- debug: var=user_test1

- name: validate results for testcase 1
  assert:
      that:
          - 'user_test1.results is defined'
          - 'user_test1.results|length == 5'

- name: validate changed results for testcase 1 (jinja >= 2.6)
  assert:
      that:
          - "user_test1.results|map(attribute='changed')|unique|list == [False]"
          - "user_test1.results|map(attribute='state')|unique|list == ['present']"
  when: "jinja2_version.stdout|version_compare('2.6', '>=')"

- name: validate changed results for testcase 1 (jinja >= 2.6)
  assert:
      that:
          - "not user_test1.results[0]['changed']"
          - "not user_test1.results[1]['changed']"
          - "not user_test1.results[2]['changed']"
          - "not user_test1.results[3]['changed']"
          - "not user_test1.results[4]['changed']"
          - "user_test1.results[0]['state'] == 'present'"
          - "user_test1.results[1]['state'] == 'present'"
          - "user_test1.results[2]['state'] == 'present'"
          - "user_test1.results[3]['state'] == 'present'"
          - "user_test1.results[4]['state'] == 'present'"
  when: "jinja2_version.stdout|version_compare('2.6', '<')"

##
## user remove
##
            
- name: try to delete the user
  user:
      name: ansibulluser
      state: absent
  register: user_test2
- name: make a new list of users
  script: userlist.sh "{{ ansible_distribution }}"
  register: user_names2
- debug: var=user_names2
- name: validate results for testcase 2
  assert:
      that:
          - '"ansibulluser" not in user_names2.stdout_lines'


- block:
    - name: create non-system user on OSX to test the shell is set to /bin/bash
      user:
        name: osxuser
      register: osxuser_output

    - name: validate the shell is set to /bin/bash
      assert:
        that:
          - 'osxuser_output.shell == "/bin/bash"'

    - name: cleanup
      user:
        name: osxuser
        state: absent

    - name: create system user on OSX to test the shell is set to /usr/bin/false
      user:
        name: osxuser
        system: yes
      register: osxuser_output

    - name: validate the shell is set to /usr/bin/false
      assert:
        that:
          - 'osxuser_output.shell == "/usr/bin/false"'

    - name: cleanup
      user:
        name: osxuser
        state: absent

    - name: create non-system user on OSX and set the shell to /bin/sh
      user:
        name: osxuser
        shell: /bin/sh
      register: osxuser_output

    - name: validate the shell is set to /bin/sh
      assert:
        that:
          - 'osxuser_output.shell == "/bin/sh"'

    - name: cleanup
      user:
        name: osxuser
        state: absent
  when: ansible_distribution == "MacOSX"