forked from ansible-collections/vmware_rest_code_generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_refresh_modules.py
599 lines (559 loc) · 20 KB
/
test_refresh_modules.py
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import ast
import pytest
import types
import refresh_modules as rm
my_parameters = [
{"name": "aaa", "type": "boolean", "description": "a second parameter"},
{
"name": "aaa",
"type": "integer",
"required": True,
"description": "a second parameter",
"subkeys": [{"type": "ccc", "name": "a_subkey", "description": "more blabla"}],
},
{
"name": "ccc",
"type": "list",
"description": "3rd parameter is ':' enum,\n\nand this string is long and comes with a ' on purpose. This way, we can use it to ensure format_documentation() can break it up.",
"enum": ["a", "c", "b"],
},
]
documentation_data_expectation = {
"author": [],
"description": "bar",
"module": "foo",
"notes": [],
"options": {
"aaa": {
"description": [
"a second parameter",
"Validate attributes are:",
" - C(a_subkey) (ccc): more blabla",
],
"required": True,
"type": "int",
},
"ccc": {
"choices": ["a", "b", "c"],
"description": [
"3rd parameter is ':' enum,",
"and this string is long and comes with a "
"' on purpose. This way, we can use it to "
"ensure format_documentation() can break "
"it up.",
],
"type": "list",
},
},
"requirements": ["python >= 3.6"],
"short_description": "bar",
"version_added": "1.0.0",
}
my_raw_paths_data = {
"/rest/vcenter/vm/{vm}": {
"get": {
"operationId": "get",
"parameters": [
{
"description": "Id of the VM",
"in": "path",
"name": "vm",
"required": True,
"type": "string",
}
],
"responses": {
"200": {
"description": "Information about the VM.",
"schema": {"$ref": "#/definitions/vcenter.VM_resp"},
},
"400": {
"description": "things went bad.",
"schema": {
"$ref": "#/definitions/vapi.std.errors.resource_inaccessible_error"
},
},
},
}
},
"/rest/vcenter/vm": {
"get": {
"operationId": "list",
"parameters": [
{
"collectionFormat": "multi",
"description": "desc of multi",
"in": "query",
"items": {"type": "string"},
"name": "filter.vms",
"type": "array",
}
],
"responses": {
"200": {
"description": "A list",
"schema": {"$ref": "#/definitions/vcenter.VM.list_resp"},
},
"400": {
"description": "my 400 error",
"schema": {
"$ref": "#/definitions/vapi.std.errors.unable_to_allocate_resource_error"
},
},
},
}
},
}
my_raw_paths_data_with_param_in_path = {
"/rest/vcenter/vm-template/library-items/{template_library_item}/check-outs": {
"post": {
"consumes": ["application/json"],
"operationId": "check_out",
"parameters": [
{
"description": "Identifier of the content library item containing the source virtual machine template to be checked out.",
"in": "path",
"name": "template_library_item",
"required": True,
"type": "string",
},
{
"in": "body",
"name": "request_body",
"schema": {
"$ref": "#/definitions/vcenter.vm_template.library_items.check_outs_check_out"
},
},
{
"description": "action=check-out",
"enum": ["check-out"],
"in": "query",
"name": "action",
"required": True,
"type": "string",
},
],
"responses": {
"200": {
"description": "Identifier of the virtual machine that was checked out of the library item.",
"schema": {
"$ref": "#/definitions/vcenter.vm_template.library_items.check_outs.check_out_resp"
},
}
},
}
},
"/rest/vcenter/vm-template/library-items/{template_library_item}/check-outs/{vm}": {
"post": {
"consumes": ["application/json"],
"operationId": "check_in",
"parameters": [
{
"description": "Identifier of the content library item in which the virtual machine is checked in.",
"in": "path",
"name": "template_library_item",
"required": True,
"type": "string",
},
{
"description": "Identifier of the virtual machine to check into the library item.",
"in": "path",
"name": "vm",
"required": True,
"type": "string",
},
{
"in": "body",
"name": "request_body",
"schema": {
"$ref": "#/definitions/vcenter.vm_template.library_items.check_outs_check_in"
},
},
{
"description": "action=check-in",
"enum": ["check-in"],
"in": "query",
"name": "action",
"required": True,
"type": "string",
},
],
"responses": {
"200": {
"description": "The new version of the library item.",
"schema": {
"$ref": "#/definitions/vcenter.vm_template.library_items.check_outs.check_in_resp"
},
},
},
}
},
}
my_definitions = {
"vcenter.VM.list_resp": {
"properties": {
"value": {
"items": {"$ref": "#/definitions/vcenter.VM.summary"},
"type": "array",
}
},
"required": ["value"],
"type": "object",
},
# vm_template related definitions
"vcenter.vm_template.library_items.check_outs_check_in": {
"properties": {
"spec": {
"$ref": "#/definitions/vcenter.vm_template.library_items.check_outs.check_in_spec",
"description": "Specification used to check in the virtual machine into the library item.",
}
},
"type": "object",
},
"vcenter.vm_template.library_items.check_outs_check_out": {
"properties": {
"spec": {
"$ref": "#/definitions/vcenter.vm_template.library_items.check_outs.check_out_spec",
"description": "Specification used to check out the source virtual machine template as a virtual machine.",
}
},
"type": "object",
},
"vcenter.vm_template.library_items.check_outs.check_out_spec": {
"properties": {
"name": {
"description": "Name of the virtual machine to check out of the library item.",
"type": "string",
},
"placement": {
"$ref": "#/definitions/vcenter.vm_template.library_items.check_outs.placement_spec",
"description": "Information used to place the checked out virtual machine.",
},
"powered_on": {
"description": "Specifies whether the virtual machine should be powered on after check out.",
"type": "boolean",
},
},
"type": "object",
},
"vcenter.vm_template.library_items.check_outs.placement_spec": {
"properties": {
"cluster": {
"description": "Cluster onto which the virtual machine should be placed. If {@name #cluster} and {@name #resourcePool} are both specified, {@name #resourcePool} must belong to {@name #cluster}. If {@name #cluster} and {@name #host} are both specified, {@name #host} must be a member of {@name #cluster}.",
"type": "string",
},
"folder": {
"description": "Virtual machine folder into which the virtual machine should be placed.",
"type": "string",
},
"host": {
"description": "Host onto which the virtual machine should be placed. If {@name #host} and {@name #resourcePool} are both specified, {@name #resourcePool} must belong to {@name #host}. If {@name #host} and {@name #cluster} are both specified, {@name #host} must be a member of {@name #cluster}.",
"type": "string",
},
"resource_pool": {
"description": "Resource pool into which the virtual machine should be placed.",
"type": "string",
},
},
"type": "object",
},
"vcenter.vm_template.library_items.check_outs.check_in_spec": {
"properties": {
"message": {
"description": "Message describing the changes made to the virtual machine.",
"type": "string",
}
},
"required": ["message"],
"type": "object",
},
}
def test_normalize_description():
assert rm.normalize_description(["a", "b"]) == ["a", "b"]
assert rm.normalize_description(["{@name DayOfWeek}"]) == ["day of the week"]
assert rm.normalize_description([" {@term enumerated type}"]) == [""]
def test_python_type():
assert rm.python_type("array") == "list"
assert rm.python_type("list") == "list"
assert rm.python_type("boolean") == "bool"
def test_path_to_name():
assert rm.path_to_name("/rest/cis/tasks") == "rest_cis_tasks"
assert (
rm.path_to_name("/rest/com/vmware/cis/tagging/category")
== "rest_com_vmware_cis_tagging_category"
)
assert (
rm.path_to_name("/rest/com/vmware/cis/tagging/category/id:{category_id}")
== "rest_com_vmware_cis_tagging_category"
)
assert (
rm.path_to_name(
"/rest/com/vmware/cis/tagging/category/id:{category_id}?~action=add-to-used-by"
)
== "rest_com_vmware_cis_tagging_category"
)
assert (
rm.path_to_name("/rest/vcenter/vm/{vm}/hardware/ethernet/{nic}/disconnect")
== "vcenter_vm_hardware_ethernet_disconnect"
)
def test_gen_documentation():
assert (
rm.gen_documentation("foo", "bar", my_parameters)
== documentation_data_expectation
)
def test_format_documentation():
expectation = """DOCUMENTATION = \'\'\'
module: foo
short_description: bar
description: bar
options:
aaa:
description:
- a second parameter
- 'Validate attributes are:'
- ' - C(a_subkey) (ccc): more blabla'
required: true
type: int
ccc:
choices:
- a
- b
- c
description:
- '3rd parameter is : enum,'
- and this string is long and comes with a ' on purpose. This way, we can use
it to ensure format_documentation() can break it up.
type: list
author:
- Jeff Groom
version_added: 1.0.0
requirements:
- python >= 3.6
\'\'\'"""
assert rm.format_documentation(documentation_data_expectation) == expectation
def test_format_documentation_quote():
documentation = {
"module": "a",
"short_description": "a",
"description": "':'",
"options": "a",
"author": "a",
"version_added": "a",
"requirements": "a",
}
expectation = """DOCUMENTATION = \'\'\'
module: a
short_description: a
description: ':'
options: a
author: a
version_added: a
requirements: a
\'\'\'"""
assert rm.format_documentation(documentation) == expectation
def test_gen_arguments_py(monkeypatch):
assert isinstance(rm.gen_arguments_py([]), types.GeneratorType)
assert list(rm.gen_arguments_py([])) == []
ret = rm.gen_arguments_py(my_parameters)
assert ast.dump(ret.__next__().value) == ast.dump(
ast.Dict(
keys=[[ast.Constant(value="type")]], values=[[ast.Constant(value="bool")]]
)
)
assert ast.dump(ret.__next__().value) == ast.dump(
ast.Dict(
keys=[[ast.Constant(value="required")], [ast.Constant(value="type")]],
values=[[ast.Constant(value=True)], [ast.Constant(value="int")]],
)
)
def test_SwaggerFile_load_paths():
paths = rm.SwaggerFile.load_paths(my_raw_paths_data, "")
assert paths["/rest/vcenter/vm"].operations == {
"list": (
"get",
"/rest/vcenter/vm",
[
{
"collectionFormat": "multi",
"description": "desc of multi",
"in": "query",
"items": {"type": "string"},
"name": "filter.vms",
"type": "array",
}
],
"",
)
}
def test_SwaggerFile_init_resources():
paths = rm.SwaggerFile.load_paths(my_raw_paths_data, "")
resources = rm.SwaggerFile.init_resources(paths.values())
assert resources["vcenter_vm"].name == "vcenter_vm"
assert resources["vcenter_vm"].operations == {
"get": (
"get",
"/rest/vcenter/vm/{vm}",
[
{
"description": "Id of the VM",
"in": "path",
"name": "vm",
"required": True,
"type": "string",
}
],
"",
),
"list": (
"get",
"/rest/vcenter/vm",
[
{
"collectionFormat": "multi",
"description": "desc of multi",
"in": "query",
"items": {"type": "string"},
"name": "filter.vms",
"type": "array",
}
],
"",
),
}
def test_Definitions__ref_to_dotted():
ref = {"$ref": "#/definitions/vapi.std.errors.resource_inaccessible_error"}
dotted = "vapi.std.errors.resource_inaccessible_error"
assert rm.Definitions._ref_to_dotted(ref) == dotted
# AnsibleModuleBase
def test_AnsibleModuleBase():
paths = rm.SwaggerFile.load_paths(my_raw_paths_data)
resources = rm.SwaggerFile.init_resources(paths.values())
definitions = rm.Definitions(my_definitions)
module = rm.AnsibleModuleBase(resources["vcenter_vm"], definitions)
assert module.name == "vcenter_vm"
# AnsibleModule
def test_AnsibleModule_parameters():
paths = rm.SwaggerFile.load_paths(my_raw_paths_data)
resources = rm.SwaggerFile.init_resources(paths.values())
definitions = rm.Definitions(my_definitions)
module = rm.AnsibleModule(resources["vcenter_vm"], definitions)
assert module.name == "vcenter_vm"
assert module.parameters() == [
{"default": "present", "enum": [], "name": "state", "type": "str"}
]
# AnsibleModule - with complex URL
def test_AnsibleModule_parameters_complex():
paths = rm.SwaggerFile.load_paths(my_raw_paths_data_with_param_in_path)
resources = rm.SwaggerFile.init_resources(paths.values())
definitions = rm.Definitions(my_definitions)
print("R: %s" % resources)
module = rm.AnsibleModule(
resources["vcenter_vmtemplate_libraryitems_checkouts"], definitions
)
assert module.name == "vcenter_vmtemplate_libraryitems_checkouts"
assert module.parameters() == [
{
"description": "action=check-out",
"enum": ["check-in", "check-out"],
"in": "query",
"name": "action",
"operationIds": ["check_in", "check_out"],
"required_if": ["check_in", "check_out"],
"type": "string",
},
{
"description": "Message describing the changes made to the virtual machine. "
"Required with I(state=['check_in'])",
"name": "message",
"operationIds": ["check_in"],
"required_if": ["check_in"],
"type": "string",
},
{
"description": "Name of the virtual machine to check out of the library "
"item.",
"name": "name",
"operationIds": ["check_out"],
"required": False,
"type": "string",
},
{
"description": "Information used to place the checked out virtual machine.",
"name": "placement",
"operationIds": ["check_out"],
"required": False,
"subkeys": [
{
"description": "Cluster onto which the virtual machine should "
"be placed. If {@name #cluster} and {@name "
"#resourcePool} are both specified, {@name "
"#resourcePool} must belong to {@name #cluster}. "
"If {@name #cluster} and {@name #host} are both "
"specified, {@name #host} must be a member of "
"{@name #cluster}.",
"name": "cluster",
"required": False,
"type": "string",
},
{
"description": "Virtual machine folder into which the virtual "
"machine should be placed.",
"name": "folder",
"required": False,
"type": "string",
},
{
"description": "Host onto which the virtual machine should be "
"placed. If {@name #host} and {@name "
"#resourcePool} are both specified, {@name "
"#resourcePool} must belong to {@name #host}. If "
"{@name #host} and {@name #cluster} are both "
"specified, {@name #host} must be a member of "
"{@name #cluster}.",
"name": "host",
"required": False,
"type": "string",
},
{
"description": "Resource pool into which the virtual machine "
"should be placed.",
"name": "resource_pool",
"required": False,
"type": "string",
},
],
"type": "dict",
},
{
"description": "Specifies whether the virtual machine should be powered on "
"after check out.",
"name": "powered_on",
"operationIds": ["check_out"],
"required": False,
"type": "boolean",
},
{
"default": "present",
"enum": ["check_in", "check_out"],
"name": "state",
"type": "str",
},
{
"description": "Identifier of the content library item containing the source "
+ "virtual machine template to be checked out.",
"in": "path",
"name": "template_library_item",
"operationIds": ["check_in", "check_out"],
"required_if": ["check_in", "check_out"],
"type": "string",
},
{
"description": "Identifier of the virtual machine to check into the library "
"item. Required with I(state=['check_in'])",
"in": "path",
"name": "vm",
"operationIds": ["check_in"],
"required_if": ["check_in"],
"type": "string",
},
]