11
11
12
12
from collections import OrderedDict
13
13
import json
14
+ import os
14
15
15
16
import pytest
16
17
18
+ from ansible import constants as C
17
19
from ansible .inventory .data import InventoryData
18
- from ansible .parsing .dataloader import DataLoader
20
+ from ansible .inventory .manager import InventoryManager
21
+ from ansible .module_utils .common .text .converters import to_native
19
22
from ansible .template import Templar
23
+
20
24
from ansible_collections .community .general .plugins .inventory .opennebula import InventoryModule
21
- from ansible_collections .community .general .tests .unit .compat .mock import create_autospec
25
+ from ansible_collections .community .general .tests .unit .mock .loader import DictDataLoader
26
+ from ansible_collections .community .general .tests .unit .mock .path import mock_unfrackpath_noop
27
+
28
+
29
+ original_exists = os .path .exists
30
+ original_access = os .access
31
+
32
+
33
+ def exists_mock (path , exists = True ):
34
+ def exists (f ):
35
+ if to_native (f ) == path :
36
+ return exists
37
+ return original_exists (f )
38
+
39
+ return exists
40
+
41
+
42
+ def access_mock (path , can_access = True ):
43
+ def access (f , m , * args , ** kwargs ):
44
+ if to_native (f ) == path :
45
+ return can_access
46
+ return original_access (f , m , * args , ** kwargs ) # pragma: no cover
47
+
48
+ return access
22
49
23
50
24
51
class HistoryEntry (object ):
@@ -239,18 +266,6 @@ def get_vm_pool():
239
266
'filter_by_label' : None ,
240
267
}
241
268
242
- options_constructable_test = options_base_test .copy ()
243
- options_constructable_test .update ({
244
- 'compose' : {'is_linux' : "GUEST_OS == 'linux'" },
245
- 'filter_by_label' : 'bench' ,
246
- 'groups' : {
247
- 'benchmark_clients' : "TGROUP.endswith('clients')" ,
248
- 'lin' : 'is_linux == True'
249
- },
250
- 'keyed_groups' : [{'key' : 'TGROUP' , 'prefix' : 'tgroup' }],
251
-
252
- })
253
-
254
269
255
270
# given a dictionary `opts_dict`, return a function that behaves like ansible's inventory get_options
256
271
def mk_get_options (opts_dict ):
@@ -266,56 +281,73 @@ def test_get_connection_info(inventory, mocker):
266
281
assert (auth .username and auth .password )
267
282
268
283
269
- def test_populate_constructable_templating (inventory , mocker ):
270
- # bypass API fetch call
271
- inventory ._get_vm_pool = mocker .MagicMock (side_effect = get_vm_pool_json )
272
- inventory .get_option = mocker .MagicMock (side_effect = mk_get_options (options_constructable_test ))
284
+ def test_populate_constructable_templating (mocker ):
285
+ inventory_filename = '/fake/opennebula.yml'
286
+
287
+ mocker .patch .object (InventoryModule , '_get_vm_pool' , side_effect = get_vm_pool_json )
288
+ mocker .patch ('ansible_collections.community.general.plugins.inventory.opennebula.HAS_PYONE' , True )
289
+ mocker .patch ('ansible.inventory.manager.unfrackpath' , mock_unfrackpath_noop )
290
+ mocker .patch ('os.path.exists' , exists_mock (inventory_filename ))
291
+ mocker .patch ('os.access' , access_mock (inventory_filename ))
273
292
274
293
# the templating engine is needed for the constructable groups/vars
275
294
# so give that some fake data and instantiate it.
276
- fake_config_filepath = '/fake/opennebula.yml'
277
- fake_cache = {fake_config_filepath : options_constructable_test .copy ()}
278
- fake_cache [fake_config_filepath ]['plugin' ] = 'community.general.opennebula'
279
- dataloader = create_autospec (DataLoader , instance = True )
280
- dataloader ._FILE_CACHE = fake_cache
281
- inventory .templar = Templar (loader = dataloader )
282
-
283
- inventory ._populate ()
295
+ C .INVENTORY_ENABLED = ['community.general.opennebula' ]
296
+ inventory_file = {inventory_filename : r'''
297
+ ---
298
+ plugin: community.general.opennebula
299
+ api_url: https://opennebula:2633/RPC2
300
+ api_username: username
301
+ api_password: password
302
+ api_authfile: '~/.one/one_auth'
303
+ hostname: v4_first_ip
304
+ group_by_labels: true
305
+ compose:
306
+ is_linux: GUEST_OS == 'linux'
307
+ filter_by_label: bench
308
+ groups:
309
+ benchmark_clients: TGROUP.endswith('clients')
310
+ lin: is_linux == true
311
+ keyed_groups:
312
+ - key: TGROUP
313
+ prefix: tgroup
314
+ ''' }
315
+ im = InventoryManager (loader = DictDataLoader (inventory_file ), sources = inventory_filename )
284
316
285
317
# note the vm_pool (and json data file) has four hosts,
286
- # but options_constructable_test asks ansible to filter it out
318
+ # but the options above asks ansible to filter one out
287
319
assert len (get_vm_pool_json ().VM ) == 4
288
320
assert set ([vm .NAME for vm in get_vm_pool_json ().VM ]) == set ([
289
321
'terraform_demo_00' ,
290
322
'terraform_demo_01' ,
291
323
'terraform_demo_srv_00' ,
292
324
'bs-windows' ,
293
325
])
294
- assert set (inventory . inventory .hosts ) == set (['terraform_demo_00' , 'terraform_demo_01' , 'terraform_demo_srv_00' ])
326
+ assert set (im . _inventory .hosts ) == set (['terraform_demo_00' , 'terraform_demo_01' , 'terraform_demo_srv_00' ])
295
327
296
- host_demo00 = inventory . inventory .get_host ('terraform_demo_00' )
297
- host_demo01 = inventory . inventory .get_host ('terraform_demo_01' )
298
- host_demosrv = inventory . inventory .get_host ('terraform_demo_srv_00' )
328
+ host_demo00 = im . _inventory .get_host ('terraform_demo_00' )
329
+ host_demo01 = im . _inventory .get_host ('terraform_demo_01' )
330
+ host_demosrv = im . _inventory .get_host ('terraform_demo_srv_00' )
299
331
300
- assert 'benchmark_clients' in inventory . inventory .groups
301
- assert 'lin' in inventory . inventory .groups
302
- assert inventory . inventory .groups ['benchmark_clients' ].hosts == [host_demo00 , host_demo01 ]
303
- assert inventory . inventory .groups ['lin' ].hosts == [host_demo00 , host_demo01 , host_demosrv ]
332
+ assert 'benchmark_clients' in im . _inventory .groups
333
+ assert 'lin' in im . _inventory .groups
334
+ assert im . _inventory .groups ['benchmark_clients' ].hosts == [host_demo00 , host_demo01 ]
335
+ assert im . _inventory .groups ['lin' ].hosts == [host_demo00 , host_demo01 , host_demosrv ]
304
336
305
337
# test group by label:
306
- assert 'bench' in inventory . inventory .groups
307
- assert 'foo' in inventory . inventory .groups
308
- assert inventory . inventory .groups ['bench' ].hosts == [host_demo00 , host_demo01 , host_demosrv ]
309
- assert inventory . inventory .groups ['serv' ].hosts == [host_demosrv ]
310
- assert inventory . inventory .groups ['foo' ].hosts == [host_demo00 , host_demo01 ]
338
+ assert 'bench' in im . _inventory .groups
339
+ assert 'foo' in im . _inventory .groups
340
+ assert im . _inventory .groups ['bench' ].hosts == [host_demo00 , host_demo01 , host_demosrv ]
341
+ assert im . _inventory .groups ['serv' ].hosts == [host_demosrv ]
342
+ assert im . _inventory .groups ['foo' ].hosts == [host_demo00 , host_demo01 ]
311
343
312
344
# test `compose` transforms GUEST_OS=Linux to is_linux == True
313
345
assert host_demo00 .get_vars ()['GUEST_OS' ] == 'linux'
314
346
assert host_demo00 .get_vars ()['is_linux' ] is True
315
347
316
348
# test `keyed_groups`
317
- assert inventory . inventory .groups ['tgroup_bench_clients' ].hosts == [host_demo00 , host_demo01 ]
318
- assert inventory . inventory .groups ['tgroup_bench_server' ].hosts == [host_demosrv ]
349
+ assert im . _inventory .groups ['tgroup_bench_clients' ].hosts == [host_demo00 , host_demo01 ]
350
+ assert im . _inventory .groups ['tgroup_bench_server' ].hosts == [host_demosrv ]
319
351
320
352
321
353
def test_populate (inventory , mocker ):
0 commit comments