summaryrefslogtreecommitdiff
path: root/test/integration/targets/group/tasks/tests.yml
blob: d07ad36a3d65f16dae7e8f2a33de2db0eab5e7f5 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
---
##
## group add
##

- name: create group (check mode)
  group:
    name: ansibullgroup
    state: present
  register: create_group_check
  check_mode: True

- name: get result of create group (check mode)
  script: 'grouplist.sh "{{ ansible_distribution }}"'
  register: create_group_actual_check

- name: assert create group (check mode)
  assert:
    that:
    - create_group_check is changed
    - '"ansibullgroup" not in create_group_actual_check.stdout_lines'

- name: create group
  group:
    name: ansibullgroup
    state: present
  register: create_group

- name: get result of create group
  script: 'grouplist.sh "{{ ansible_distribution }}"'
  register: create_group_actual

- name: assert create group
  assert:
    that:
    - create_group is changed
    - create_group.gid is defined
    - '"ansibullgroup" in create_group_actual.stdout_lines'

- name: create group (idempotent)
  group:
    name: ansibullgroup
    state: present
  register: create_group_again

- name: assert create group (idempotent)
  assert:
    that:
    - not create_group_again is changed

##
## group check
##

- name: run existing group check tests
  group:
    name: "{{ create_group_actual.stdout_lines|random }}"
    state: present
  with_sequence: start=1 end=5
  register: group_test1

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

- name: validate change results for testcase 1
  assert:
    that:
    - not group_test1 is changed

##
## group add with gid
##

- name: get the next available gid
  script: gidget.py
  args:
    executable: '{{ ansible_python_interpreter }}'
  register: gid

- name: create a group with a gid (check mode)
  group:
    name: ansibullgroup2
    gid: '{{ gid.stdout_lines[0] }}'
    state: present
  register: create_group_gid_check
  check_mode: True

- name: get result of create a group with a gid (check mode)
  script: 'grouplist.sh "{{ ansible_distribution }}"'
  register: create_group_gid_actual_check

- name: assert create group with a gid (check mode)
  assert:
      that:
      - create_group_gid_check is changed
      - '"ansibullgroup2" not in create_group_gid_actual_check.stdout_lines'

- name: create a group with a gid
  group:
    name: ansibullgroup2
    gid: '{{ gid.stdout_lines[0] }}'
    state: present
  register: create_group_gid

- name: get gid of created group
  command: "{{ ansible_python_interpreter | quote }} -c \"import grp; print(grp.getgrnam('ansibullgroup2').gr_gid)\""
  register: create_group_gid_actual

- name: assert create group with a gid
  assert:
      that:
      - create_group_gid is changed
      - create_group_gid.gid | int == gid.stdout_lines[0] | int
      - create_group_gid_actual.stdout | trim | int == gid.stdout_lines[0] | int

- name: create a group with a gid (idempotent)
  group:
    name: ansibullgroup2
    gid: '{{ gid.stdout_lines[0] }}'
    state: present
  register: create_group_gid_again

- name: assert create group with a gid (idempotent)
  assert:
      that:
      - not create_group_gid_again is changed
      - create_group_gid_again.gid | int == gid.stdout_lines[0] | int

- block:
    - name: create a group with a non-unique gid
      group:
        name: ansibullgroup3
        gid: '{{ gid.stdout_lines[0] }}'
        non_unique: true
        state: present
      register: create_group_gid_non_unique

    - name: assert create group with a non unique gid
      assert:
          that:
          - create_group_gid_non_unique is changed
          - create_group_gid_non_unique.gid | int == gid.stdout_lines[0] | int
  when: ansible_facts.distribution not in ['MacOSX', 'Alpine']

##
## group remove
##

- name: delete group (check mode)
  group:
    name: ansibullgroup
    state: absent
  register: delete_group_check
  check_mode: True

- name: get result of delete group (check mode)
  script: grouplist.sh "{{ ansible_distribution }}"
  register: delete_group_actual_check

- name: assert delete group (check mode)
  assert:
    that:
    - delete_group_check is changed
    - '"ansibullgroup" in delete_group_actual_check.stdout_lines'

- name: delete group
  group:
    name: ansibullgroup
    state: absent
  register: delete_group

- name: get result of delete group
  script: grouplist.sh "{{ ansible_distribution }}"
  register: delete_group_actual

- name: assert delete group
  assert:
      that:
      - delete_group is changed
      - '"ansibullgroup" not in delete_group_actual.stdout_lines'

- name: delete group (idempotent)
  group:
    name: ansibullgroup
    state: absent
  register: delete_group_again

- name: assert delete group (idempotent)
  assert:
    that:
    - not delete_group_again is changed

- name: Ensure lgroupadd is present
  action: "{{ ansible_facts.pkg_mgr }}"
  args:
    name: libuser
    state: present
  when: ansible_facts.system in ['Linux']
  tags:
    - user_test_local_mode

# https://github.com/ansible/ansible/issues/56481
- block:
  - name: Test duplicate GID with local=yes
    group:
      name: "{{ item }}"
      gid: 1337
      local: yes
    loop:
      - group1_local_test
      - group2_local_test
    ignore_errors: yes
    register: local_duplicate_gid_result

  - assert:
     that:
       - local_duplicate_gid_result['results'][0] is success
       - local_duplicate_gid_result['results'][1]['msg'] == "GID '1337' already exists with group 'group1_local_test'"
  always:
    - name: Cleanup
      group:
        name: group1_local_test
        state: absent
  # only applicable to Linux, limit further to CentOS where 'luseradd' is installed
  when: ansible_distribution == 'CentOS'

# https://github.com/ansible/ansible/pull/59769
- block:
  - name: create a local group with a gid
    group:
      name: group1_local_test
      gid: 1337
      local: yes
      state: present
    register: create_local_group_gid

  - name: get gid of created local group
    command: "{{ ansible_python_interpreter | quote }} -c \"import grp; print(grp.getgrnam('group1_local_test').gr_gid)\""
    register: create_local_group_gid_actual

  - name: assert create local group with a gid
    assert:
        that:
        - create_local_group_gid is changed
        - create_local_group_gid.gid | int == 1337 | int
        - create_local_group_gid_actual.stdout | trim | int == 1337 | int

  - name: create a local group with a gid (idempotent)
    group:
      name: group1_local_test
      gid: 1337
      state: present
    register: create_local_group_gid_again

  - name: assert create local group with a gid (idempotent)
    assert:
        that:
        - not create_local_group_gid_again is changed
        - create_local_group_gid_again.gid | int == 1337 | int
  always:
    - name: Cleanup create local group with a gid
      group:
        name: group1_local_test
        state: absent
  # only applicable to Linux, limit further to CentOS where 'luseradd' is installed
  when: ansible_distribution == 'CentOS'

# https://github.com/ansible/ansible/pull/59772
- block:
  - name: create group with a gid
    group:
      name: group1_test
      gid: 1337
      local: no
      state: present
    register: create_group_gid

  - name: get gid of created group
    command: "{{ ansible_python_interpreter | quote }} -c \"import grp; print(grp.getgrnam('group1_test').gr_gid)\""
    register: create_group_gid_actual

  - name: assert create group with a gid
    assert:
        that:
        - create_group_gid is changed
        - create_group_gid.gid | int == 1337 | int
        - create_group_gid_actual.stdout | trim | int == 1337 | int

  - name: create local group with the same gid
    group:
      name: group1_test
      gid: 1337
      local: yes
      state: present
    register: create_local_group_gid

  - name: assert create local group with a gid
    assert:
        that:
        - create_local_group_gid.gid | int == 1337 | int
  always:
    - name: Cleanup create group with a gid
      group:
        name: group1_test
        local: no
        state: absent
    - name: Cleanup create local group with the same gid
      group:
        name: group1_test
        local: yes
        state: absent
  # only applicable to Linux, limit further to CentOS where 'lgroupadd' is installed
  when: ansible_distribution == 'CentOS'