summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/roles_arg_spec/test.yml
blob: 5eca7c73f9c2cf8fe8d166b5a54dea4e860bc207 (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
---
- hosts: localhost
  gather_facts: false
  roles:
      - { role: a, a_str: "roles" }

  vars:
    INT_VALUE: 42

  tasks:

      - name: "Valid simple role usage with include_role"
        include_role:
          name: a
        vars:
          a_str: "include_role"

      - name: "Valid simple role usage with import_role"
        import_role:
          name: a
        vars:
            a_str: "import_role"

      - name: "Valid role usage (more args)"
        include_role:
          name: b
        vars:
          b_str: "xyz"
          b_int: 5
          b_bool: true

      - name: "Valid simple role usage with include_role of different entry point"
        include_role:
          name: a
          tasks_from: "alternate"
        vars:
          a_int: 256

      - name: "Valid simple role usage with import_role of different entry point"
        import_role:
          name: a
          tasks_from: "alternate"
        vars:
          a_int: 512

      - name: "Valid simple role usage with a templated value"
        import_role:
          name: a
        vars:
          a_int: "{{ INT_VALUE }}"

      - name: "Call role entry point that is defined, but has no spec data"
        import_role:
          name: a
          tasks_from: "no_spec_entrypoint"

- name: "New play to reset vars: Test include_role fails"
  hosts: localhost
  gather_facts: false
  vars:
    expected_returned_spec:
      b_bool:
        required: true
        type: "bool"
      b_int:
        required: true
        type: "int"
      b_str:
        required: true
        type: "str"

  tasks:
      - block:
        - name: "Invalid role usage"
          include_role:
            name: b
          vars:
            b_bool: 7

        - fail:
            msg: "Should not get here"

        rescue:
          - debug:
              var: ansible_failed_result

          - name: "Validate failure"
            assert:
              that:
                - ansible_failed_task.name == "Validating arguments against arg spec 'main' - Main entry point for role B."
                - ansible_failed_result.argument_errors | length == 2
                - "'missing required arguments: b_int, b_str' in ansible_failed_result.argument_errors"
                - ansible_failed_result.validate_args_context.argument_spec_name == "main"
                - ansible_failed_result.validate_args_context.name == "b"
                - ansible_failed_result.validate_args_context.type == "role"
                - "ansible_failed_result.validate_args_context.path is search('roles_arg_spec/roles/b')"
                - ansible_failed_result.argument_spec_data == expected_returned_spec


- name: "New play to reset vars: Test import_role fails"
  hosts: localhost
  gather_facts: false
  vars:
    expected_returned_spec:
      b_bool:
        required: true
        type: "bool"
      b_int:
        required: true
        type: "int"
      b_str:
        required: true
        type: "str"

  tasks:
      - block:
        - name: "Invalid role usage"
          import_role:
            name: b
          vars:
            b_bool: 7

        - fail:
            msg: "Should not get here"

        rescue:
          - debug:
              var: ansible_failed_result

          - name: "Validate failure"
            assert:
              that:
                - ansible_failed_task.name == "Validating arguments against arg spec 'main' - Main entry point for role B."
                - ansible_failed_result.argument_errors | length == 2
                - "'missing required arguments: b_int, b_str' in ansible_failed_result.argument_errors"
                - ansible_failed_result.validate_args_context.argument_spec_name == "main"
                - ansible_failed_result.validate_args_context.name == "b"
                - ansible_failed_result.validate_args_context.type == "role"
                - "ansible_failed_result.validate_args_context.path is search('roles_arg_spec/roles/b')"
                - ansible_failed_result.argument_spec_data == expected_returned_spec


- name: "New play to reset vars: Test nested role including/importing role succeeds"
  hosts: localhost
  gather_facts: false
  vars:
    c_int: 1
    a_str: "some string"
    a_int: 42
  tasks:
      - name: "Test import_role of role C"
        import_role:
          name: c

      - name: "Test include_role of role C"
        include_role:
          name: c


- name: "New play to reset vars: Test nested role including/importing role fails"
  hosts: localhost
  gather_facts: false
  vars:
    main_expected_returned_spec:
      a_str:
        required: true
        type: "str"
    alternate_expected_returned_spec:
      a_int:
        required: true
        type: "int"

  tasks:
      - block:
        - name: "Test import_role of role C (missing a_str)"
          import_role:
            name: c
          vars:
            c_int: 100

        - fail:
            msg: "Should not get here"

        rescue:
          - debug:
              var: ansible_failed_result
          - name: "Validate import_role failure"
            assert:
              that:
                # NOTE: a bug here that prevents us from getting ansible_failed_task
                - ansible_failed_result.argument_errors | length == 1
                - "'missing required arguments: a_str' in ansible_failed_result.argument_errors"
                - ansible_failed_result.validate_args_context.argument_spec_name == "main"
                - ansible_failed_result.validate_args_context.name == "a"
                - ansible_failed_result.validate_args_context.type == "role"
                - "ansible_failed_result.validate_args_context.path is search('roles_arg_spec/roles/a')"
                - ansible_failed_result.argument_spec_data == main_expected_returned_spec

      - block:
        - name: "Test include_role of role C (missing a_int from `alternate` entry point)"
          include_role:
            name: c
          vars:
            c_int: 200
            a_str: "some string"

        - fail:
            msg: "Should not get here"

        rescue:
          - debug:
              var: ansible_failed_result
          - name: "Validate include_role failure"
            assert:
              that:
                # NOTE: a bug here that prevents us from getting ansible_failed_task
                - ansible_failed_result.argument_errors | length == 1
                - "'missing required arguments: a_int' in ansible_failed_result.argument_errors"
                - ansible_failed_result.validate_args_context.argument_spec_name == "alternate"
                - ansible_failed_result.validate_args_context.name == "a"
                - ansible_failed_result.validate_args_context.type == "role"
                - "ansible_failed_result.validate_args_context.path is search('roles_arg_spec/roles/a')"
                - ansible_failed_result.argument_spec_data == alternate_expected_returned_spec

- name: "New play to reset vars: Test role with no tasks can fail"
  hosts: localhost
  gather_facts: false
  tasks:
      - block:
        - name: "Test import_role of role role_with_no_tasks (missing a_str)"
          import_role:
            name: role_with_no_tasks

        - fail:
            msg: "Should not get here"

        rescue:
          - debug:
              var: ansible_failed_result
          - name: "Validate import_role failure"
            assert:
              that:
                # NOTE: a bug here that prevents us from getting ansible_failed_task
                - ansible_failed_result.argument_errors | length == 1
                - "'missing required arguments: a_str' in ansible_failed_result.argument_errors"
                - ansible_failed_result.validate_args_context.argument_spec_name == "main"
                - ansible_failed_result.validate_args_context.name == "role_with_no_tasks"
                - ansible_failed_result.validate_args_context.type == "role"
                - "ansible_failed_result.validate_args_context.path is search('roles_arg_spec/roles/role_with_no_tasks')"

- name: "New play to reset vars: Test disabling role validation with rolespec_validate=False"
  hosts: localhost
  gather_facts: false
  tasks:
    - block:
      - name: "Test import_role of role C (missing a_str), but validation turned off"
        import_role:
          name: c
          rolespec_validate: False
      - fail:
          msg: "Should not get here"

      rescue:
        - debug:
            var: ansible_failed_result
        - name: "Validate import_role failure"
          assert:
            that:
              # We expect the role to actually run, but will fail because an undefined variable was referenced
              # and validation wasn't performed up front (thus not returning 'argument_errors').
              - "'argument_errors' not in ansible_failed_result"
              - "'The task includes an option with an undefined variable.' in ansible_failed_result.msg"

- name: "New play to reset vars: Test collection-based role"
  hosts: localhost
  gather_facts: false
  tasks:
    - name: "Valid collection-based role usage"
      import_role:
        name: "foo.bar.blah"
      vars:
        blah_str: "some string"


- name: "New play to reset vars: Test collection-based role will fail"
  hosts: localhost
  gather_facts: false
  tasks:
    - block:
      - name: "Invalid collection-based role usage"
        import_role:
          name: "foo.bar.blah"
      - fail:
          msg: "Should not get here"
      rescue:
        - debug: var=ansible_failed_result
        - name: "Validate import_role failure for collection-based role"
          assert:
            that:
              - ansible_failed_result.argument_errors | length == 1
              - "'missing required arguments: blah_str' in ansible_failed_result.argument_errors"
              - ansible_failed_result.validate_args_context.argument_spec_name == "main"
              - ansible_failed_result.validate_args_context.name == "blah"
              - ansible_failed_result.validate_args_context.type == "role"
              - "ansible_failed_result.validate_args_context.path is search('roles_arg_spec/collections/ansible_collections/foo/bar/roles/blah')"

- name: "New play to reset vars: Test templating succeeds"
  hosts: localhost
  gather_facts: false
  vars:
    value_some_choices: "choice2"
    value_some_list: [1.5]
    value_some_dict: {"some_key": "some_value"}
    value_some_int: 1
    value_some_float: 1.5
    value_some_json: '{[1, 3, 3] 345345|45v<#!}'
    value_some_jsonarg: {"foo": [1, 3, 3]}
    value_some_second_level: True
    value_third_level: 5
  tasks:
    - block:
      - include_role:
          name: test1
        vars:
          some_choices: "{{ value_some_choices }}"
          some_list: "{{ value_some_list }}"
          some_dict: "{{ value_some_dict }}"
          some_int: "{{ value_some_int }}"
          some_float: "{{ value_some_float }}"
          some_json: "{{ value_some_json }}"
          some_jsonarg: "{{ value_some_jsonarg }}"
          some_dict_options:
            some_second_level: "{{ value_some_second_level }}"
          multi_level_option:
            second_level:
              third_level: "{{ value_third_level }}"
      rescue:
        - debug: var=ansible_failed_result
        - fail:
            msg: "Should not get here"

- name: "New play to reset vars: Test empty argument_specs.yml"
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Import role with an empty argument_specs.yml
      import_role:
        name: empty_file

- name: "New play to reset vars: Test empty argument_specs key"
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Import role with an empty argument_specs key
      import_role:
        name: empty_argspec