11
11
from ansible_collections .community .proxmox .plugins .module_utils .proxmox import ProxmoxAnsible
12
12
13
13
14
+ @pytest .fixture
15
+ def dir_storage_args ():
16
+ return {
17
+ "api_host" : "localhost" ,
18
+ "api_user" : "root@pam" ,
19
+ "api_password" : "secret" ,
20
+ "validate_certs" : False ,
21
+ "node_name" : "pve01" ,
22
+ "nodes" : ["pve01" , "pve02" ],
23
+ "state" : "present" ,
24
+ "name" : "dir-storage" ,
25
+ "type" : "dir" ,
26
+ "dir_options" : {
27
+ "path" : "/dir" ,
28
+ },
29
+ "content" : ["images" ]
30
+ }
31
+
32
+
14
33
@pytest .fixture
15
34
def pbs_storage_args ():
16
35
return {
@@ -54,6 +73,25 @@ def nfs_storage_args():
54
73
}
55
74
56
75
76
+ @pytest .fixture
77
+ def zfspool_storage_args ():
78
+ return {
79
+ "api_host" : "localhost" ,
80
+ "api_user" : "root@pam" ,
81
+ "api_password" : "secret" ,
82
+ "validate_certs" : False ,
83
+ "node_name" : "pve01" ,
84
+ "nodes" : ["pve01" , "pve02" ],
85
+ "state" : "present" ,
86
+ "name" : "zfspool-storage" ,
87
+ "type" : "zfspooldir" ,
88
+ "zfspool_options" : {
89
+ "pool" : "mypool" ,
90
+ },
91
+ "content" : ["images" ]
92
+ }
93
+
94
+
57
95
@pytest .fixture
58
96
def existing_storages ():
59
97
return [
@@ -62,6 +100,29 @@ def existing_storages():
62
100
]
63
101
64
102
103
+ @patch .object (ProxmoxAnsible , "__init__" , return_value = None )
104
+ @patch .object (ProxmoxAnsible , "proxmox_api" , create = True )
105
+ def test_add_dir_storage (mock_api , mock_init , dir_storage_args ):
106
+ module = MagicMock (spec = AnsibleModule )
107
+ module .params = dir_storage_args
108
+ module .check_mode = False
109
+
110
+ mock_api_instance = MagicMock ()
111
+ mock_api .return_value = mock_api_instance
112
+ mock_api_instance .nodes .get .return_value = [{"node" : "pve01" , "status" : "online" }]
113
+ mock_api_instance .storage .get .return_value = []
114
+ mock_api_instance .storage .post .return_value = {}
115
+
116
+ proxmox = proxmox_storage .ProxmoxNodeAnsible (module )
117
+ proxmox .module = module
118
+ proxmox .proxmox_api = mock_api_instance
119
+
120
+ changed , msg = proxmox .add_storage ()
121
+
122
+ assert changed is True
123
+ assert "created successfully" in msg
124
+
125
+
65
126
@patch .object (ProxmoxAnsible , "__init__" , return_value = None )
66
127
@patch .object (ProxmoxAnsible , "proxmox_api" , create = True )
67
128
def test_add_pbs_storage (mock_api , mock_init , pbs_storage_args ):
@@ -231,3 +292,98 @@ def test_add_cephfs_storage(mock_api, mock_init):
231
292
232
293
assert changed is True
233
294
assert "created successfully" in msg
295
+
296
+
297
+ @patch .object (ProxmoxAnsible , "__init__" , return_value = None )
298
+ @patch .object (ProxmoxAnsible , "proxmox_api" , create = True )
299
+ def test_add_zfspool_storage (mock_api , mock_init , zfspool_storage_args ):
300
+ module = MagicMock (spec = AnsibleModule )
301
+ module .params = zfspool_storage_args
302
+ module .check_mode = False
303
+
304
+ mock_api_instance = MagicMock ()
305
+ mock_api .return_value = mock_api_instance
306
+ mock_api_instance .nodes .get .return_value = [{"node" : "pve01" , "status" : "online" }]
307
+ mock_api_instance .storage .get .return_value = []
308
+ mock_api_instance .storage .post .return_value = {}
309
+
310
+ proxmox = proxmox_storage .ProxmoxNodeAnsible (module )
311
+ proxmox .module = module
312
+ proxmox .proxmox_api = mock_api_instance
313
+
314
+ changed , msg = proxmox .add_storage ()
315
+
316
+ assert changed is True
317
+ assert "created successfully" in msg
318
+
319
+
320
+ @patch .object (ProxmoxAnsible , "__init__" , return_value = None )
321
+ @patch .object (ProxmoxAnsible , "proxmox_api" , create = True )
322
+ def test_add_dir_missing_required_path (mock_api , mock_init ):
323
+ dir_args = {
324
+ "api_host" : "localhost" ,
325
+ "api_user" : "root@pam" ,
326
+ "api_password" : "secret" ,
327
+ "validate_certs" : False ,
328
+ "node_name" : "pve01" ,
329
+ "nodes" : ["pve01" ],
330
+ "state" : "present" ,
331
+ "name" : "dir-storage" ,
332
+ "type" : "dir" ,
333
+ "dir_options" : {}, # Missing 'path' parameter
334
+ "content" : ["images" ]
335
+ }
336
+
337
+ module = MagicMock (spec = AnsibleModule )
338
+ module .params = dir_args
339
+ module .check_mode = False
340
+ module .fail_json = lambda ** kwargs : (result for result in ()).throw (SystemExit (kwargs ))
341
+
342
+ mock_api_instance = MagicMock ()
343
+ mock_api .return_value = mock_api_instance
344
+
345
+ proxmox = proxmox_storage .ProxmoxNodeAnsible (module )
346
+ proxmox .module = module
347
+ proxmox .proxmox_api = mock_api_instance
348
+
349
+ with pytest .raises (SystemExit ) as exc :
350
+ proxmox .add_storage ()
351
+
352
+ result = exc .value .args [0 ]
353
+ assert "Directory storage requires 'path' parameter" in result ["msg" ]
354
+
355
+
356
+ @patch .object (ProxmoxAnsible , "__init__" , return_value = None )
357
+ @patch .object (ProxmoxAnsible , "proxmox_api" , create = True )
358
+ def test_add_zfspool_missing_required_pool (mock_api , mock_init ):
359
+ zfspool_args = {
360
+ "api_host" : "localhost" ,
361
+ "api_user" : "root@pam" ,
362
+ "api_password" : "secret" ,
363
+ "validate_certs" : False ,
364
+ "node_name" : "pve01" ,
365
+ "nodes" : ["pve01" ],
366
+ "state" : "present" ,
367
+ "name" : "zfspool-storage" ,
368
+ "type" : "zfspool" ,
369
+ "zfspool_options" : {}, # Missing 'pool' parameter
370
+ "content" : ["images" ]
371
+ }
372
+
373
+ module = MagicMock (spec = AnsibleModule )
374
+ module .params = zfspool_args
375
+ module .check_mode = False
376
+ module .fail_json = lambda ** kwargs : (result for result in ()).throw (SystemExit (kwargs ))
377
+
378
+ mock_api_instance = MagicMock ()
379
+ mock_api .return_value = mock_api_instance
380
+
381
+ proxmox = proxmox_storage .ProxmoxNodeAnsible (module )
382
+ proxmox .module = module
383
+ proxmox .proxmox_api = mock_api_instance
384
+
385
+ with pytest .raises (SystemExit ) as exc :
386
+ proxmox .add_storage ()
387
+
388
+ result = exc .value .args [0 ]
389
+ assert "ZFS storage requires 'pool' parameter" in result ["msg" ]
0 commit comments