33"""
44from unittest import TestCase
55from unittest .mock import MagicMock , Mock , patch , call , mock_open , ANY
6- import os
76
87import bibigrid .core .utility .paths .ansible_resources_path as aRP
98from bibigrid .core import startup
@@ -352,39 +351,48 @@ def test_write_yaml_alias(self, mock_yaml):
352351 output_mock .assert_called_once_with (aRP .HOSTS_CONFIG_FILE , mode = "w+" , encoding = "UTF-8" )
353352 mock_yaml .assert_called_with (data = {"some" : "yaml" }, stream = ANY )
354353
355- @patch ("bibigrid.core.utility.ansible_configurator.write_host_and_group_vars " )
354+ @patch ("bibigrid.core.utility.ansible_configurator.get_host_and_group_vars " )
356355 @patch ("bibigrid.core.utility.ansible_configurator.generate_worker_specification_file_yaml" )
357356 @patch ("bibigrid.core.utility.ansible_configurator.generate_common_configuration_yaml" )
358357 @patch ("bibigrid.core.actions.list_clusters.dict_clusters" )
359358 @patch ("bibigrid.core.utility.ansible_configurator.generate_ansible_hosts_yaml" )
360359 @patch ("bibigrid.core.utility.ansible_configurator.generate_site_file_yaml" )
361- @patch ("bibigrid.core.utility.ansible_configurator.write_yaml" )
362360 @patch ("bibigrid.core.utility.ansible_configurator.get_cidrs" )
363- def test_configure_ansible_yaml (self , mock_cidrs , mock_yaml , mock_site , mock_hosts , mock_list , mock_common ,
364- mock_worker , mock_write ):
361+ def test_configure_ansible_yaml (self , mock_cidrs , mock_site , mock_hosts , mock_list , mock_common ,
362+ mock_worker , mock_get_host_and_group_vars ):
365363 mock_cidrs .return_value = 421
366364 mock_list .return_value = {2 : 422 }
367365 provider = MagicMock ()
368366 provider .cloud_specification = {"auth" : {"username" : "Default" }}
369367 configuration = [{"sshUser" : 42 , "userRoles" : 21 }]
370368 cluster_id = 2
371- ansible_configurator .configure_ansible_yaml ([provider ], configuration , cluster_id , startup .LOG )
369+
370+ mock_site .return_value = 1
371+ mock_hosts .return_value = 2
372+ mock_list .return_value = 3
373+ mock_common .return_value = 4
374+ mock_worker .return_value = 5
375+ mock_get_host_and_group_vars .return_value = [(6 , "nowhere" )]
376+
377+ expected_write_remote = [(5 , '/opt/playbook/vars/worker_specification.yaml' ),
378+ (4 , '/opt/playbook/vars/common_configuration.yaml' ),
379+ (2 , '/opt/playbook/ansible_hosts' ),
380+ (1 , '/opt/playbook/site.yaml' ),
381+ (6 , 'nowhere' )]
382+
383+ write_remote = ansible_configurator .configure_ansible_yaml ([provider ], configuration , cluster_id ,
384+ startup .LOG )
372385 mock_worker .assert_called_with (configuration , startup .LOG )
373386 mock_common .assert_called_with (cidrs = 421 , configurations = configuration , cluster_id = cluster_id , ssh_user = 42 ,
374387 default_user = "Default" , log = startup .LOG )
375388 mock_hosts .assert_called_with (42 , configuration , cluster_id , startup .LOG )
376389 mock_site .assert_called_with (21 )
377390 mock_cidrs .assert_called_with (configuration )
378- mock_write .assert_called ()
379- expected = [call (aRP .WORKER_SPECIFICATION_FILE , mock_worker (), startup .LOG , False ),
380- call (aRP .COMMONS_CONFIG_FILE , mock_common (), startup .LOG , False ),
381- call (aRP .HOSTS_CONFIG_FILE , mock_hosts (), startup .LOG , False ),
382- call (aRP .SITE_CONFIG_FILE , mock_site (), startup .LOG , False )]
383- self .assertEqual (expected , mock_yaml .call_args_list )
391+ mock_get_host_and_group_vars .assert_called ()
392+ self .assertEqual (expected_write_remote , write_remote )
384393
385394 @patch ("bibigrid.core.utility.paths.ansible_resources_path.HOST_VARS_FOLDER" , "mock_path" )
386- @patch ("bibigrid.core.utility.ansible_configurator.write_yaml" )
387- def test_write_worker_host_vars (self , mock_write_yaml ):
395+ def test_write_worker_host_vars (self ):
388396 cluster_id = "foo"
389397 worker_count = 0
390398 log = MagicMock ()
@@ -396,52 +404,35 @@ def test_write_worker_host_vars(self, mock_write_yaml):
396404 {"tmp" : True },
397405 ],
398406 }
399- worker_dict = {
400- "on_demand" : True ,
401- }
402-
403- expected_calls = [
404- call (
405- os .path .join ("mock_path" , "bibigrid-worker-foo-0.yaml" ),
406- {
407- "volumes" : [
408- {"name" : "volume1" , "exists" : True },
409- {"permanent" : True , "name" : "bibigrid-worker-foo-0-perm-1-volume2" },
410- {"tmp" : True , "name" : "bibigrid-worker-foo-0-tmp-2" },
411- ]
412- },
413- log ,
414- ),
415- call (
416- os .path .join ("mock_path" , "bibigrid-worker-foo-1.yaml" ),
417- {
418- "volumes" : [
419- {"name" : "volume1" , "exists" : True },
420- {"permanent" : True , "name" : "bibigrid-worker-foo-1-perm-1-volume2" },
421- {"tmp" : True , "name" : "bibigrid-worker-foo-1-tmp-2" },
422- ]
423- },
424- log ,
425- ),
426- ]
427407
428408 # Call the function
429- ansible_configurator .get_worker_host_vars (
409+ write_host_vars_remote = ansible_configurator .get_worker_host_vars (
430410 cluster_id = cluster_id ,
431411 worker = worker ,
432- worker_count = worker_count ,
433- log = log ,
412+ worker_count = worker_count
434413 )
435-
436- # Validate write_yaml calls
437- mock_write_yaml .assert_has_calls (expected_calls , any_order = False )
414+ # expected values of write_host_vars_remote which contain (dict to write, path to write to on remote)
415+ first = ({'volumes' :
416+ [{'name' : 'volume1' , 'exists' : True },
417+ {'permanent' : True ,
418+ 'name' : 'bibigrid-worker-foo-0-perm-1-volume2' },
419+ {'tmp' : True , 'name' : 'bibigrid-worker-foo-0-tmp-2' }]},
420+ '/opt/playbook/host_vars/bibigrid-worker-foo-0.yaml' )
421+ second = ({'volumes' : [
422+ {'name' : 'volume1' , 'exists' : True }, {'permanent' : True , 'name' : 'bibigrid-worker-foo-1-perm-1-volume2' },
423+ {'tmp' : True , 'name' : 'bibigrid-worker-foo-1-tmp-2' }]},
424+ '/opt/playbook/host_vars/bibigrid-worker-foo-1.yaml' )
425+ expected_write_host_vars_remote = [first , second ]
426+
427+ # validate_vars
428+ self .assertEqual (expected_write_host_vars_remote , write_host_vars_remote )
438429
439430 @patch ("bibigrid.core.utility.paths.ansible_resources_path.GROUP_VARS_FOLDER" , "mock_path" )
440- @patch ("bibigrid.core.utility.ansible_configurator.write_worker_host_vars" )
441- @patch ("bibigrid.core.utility.ansible_configurator.write_yaml" )
442- def test_write_worker_vars (self , mock_write_yaml , mock_write_worker_host_vars ):
431+ @patch ("bibigrid.core.utility.ansible_configurator.get_worker_host_vars" )
432+ def test_write_worker_vars (self , mock_get_worker_host_vars ):
443433 provider = MagicMock ()
444434 provider .create_flavor_dict .return_value = {"flavor_key" : "flavor_value" }
435+ mock_get_worker_host_vars .return_value = [(21 , 42 )]
445436
446437 configuration = {
447438 "network" : "net1" ,
@@ -463,49 +454,33 @@ def test_write_worker_vars(self, mock_write_yaml, mock_write_worker_host_vars):
463454 worker_count = 0
464455 log = MagicMock ()
465456
466- expected_group_vars = {
467- "name" : "bibigrid-worker-foo-[0-1]" ,
468- "regexp" : "bibigrid-worker-foo-\\ d+" ,
469- "image" : "worker-image" ,
470- "network" : "net1" ,
471- "flavor" : {"flavor_key" : "flavor_value" },
472- "gateway_ip" : "10.1.1.1" ,
473- "cloud_identifier" : "cloud1" ,
474- "on_demand" : True ,
475- "state" : "CLOUD" ,
476- "partitions" : ["cloud1" , "all" ],
477- "boot_volume" : {"size" : 10 },
478- "features" : {"feature1" },
479- "meta" : {},
480- "security_groups" : []
481- }
482-
483- ansible_configurator .get_worker_vars (
457+ worker_vars = ansible_configurator .get_worker_vars (
484458 provider = provider ,
485459 configuration = configuration ,
486460 cluster_id = cluster_id ,
487461 worker = worker ,
488- worker_count = worker_count ,
489- log = log
462+ worker_count = worker_count
490463 )
464+
465+ expected_worker_vars = (2 , [({'name' : 'bibigrid-worker-foo-[0-1]' , 'regexp' : 'bibigrid-worker-foo-\\ d+' ,
466+ 'image' : 'worker-image' , 'network' : 'net1' ,
467+ 'flavor' : {'flavor_key' : 'flavor_value' }, 'gateway_ip' : '10.1.1.1' ,
468+ 'cloud_identifier' : 'cloud1' , 'on_demand' : True , 'state' : 'CLOUD' ,
469+ 'partitions' : ['cloud1' , 'all' ], 'boot_volume' : {'size' : 10 }, 'meta' : {},
470+ 'security_groups' : [], 'features' : {'feature1' }},
471+ '/opt/playbook/group_vars/bibigrid_worker_foo_0_1.yaml' ), (21 , 42 )])
472+
491473 # Assert group_vars were written correctly
492- # print(mock_write_yaml.mock_calls)
493- mock_write_yaml .assert_any_call (
494- os .path .join ("mock_path" , "bibigrid_worker_foo_0_1.yaml" ),
495- expected_group_vars ,
496- log
497- )
474+ self .assertEqual (expected_worker_vars , worker_vars )
498475 # Ensure write_worker_host_vars was called
499- mock_write_worker_host_vars .assert_called_once_with (
476+ mock_get_worker_host_vars .assert_called_once_with (
500477 cluster_id = cluster_id ,
501478 worker = worker ,
502- worker_count = worker_count ,
503- log = log
479+ worker_count = worker_count
504480 )
505481
506482 @patch ("bibigrid.core.utility.paths.ansible_resources_path.HOST_VARS_FOLDER" , "mock_path" )
507- @patch ("bibigrid.core.utility.ansible_configurator.write_yaml" )
508- def test_write_vpn_var (self , mock_write_yaml ):
483+ def test_get_vpn_vars (self ):
509484 provider = MagicMock ()
510485 provider .create_flavor_dict .return_value = {"flavor_key" : "flavor_value" }
511486
@@ -527,40 +502,35 @@ def test_write_vpn_var(self, mock_write_yaml):
527502 vpn_count = 0
528503 log = MagicMock ()
529504
530- expected_host_vars = {
531- "name" : "bibigrid-vpngtw-foo-0" ,
532- "regexp" : "bibigrid-worker-foo-\\ d+" , # this is known bug behavior that needs to be fixed
533- "image" : "vpn-image" ,
534- "network" : "net1" ,
535- "network_cidrs" : ["10.0.0.0/16" ],
536- "floating_ip" : "10.1.1.2" ,
537- "private_v4" : "10.1.1.1" ,
538- "flavor" : {"flavor_key" : "flavor_value" },
539- "wireguard_ip" : "10.0.0.2" ,
540- "cloud_identifier" : "cloud1" ,
541- "fallback_on_other_image" : False ,
542- "on_demand" : False ,
543- "wireguard" : {"ip" : "10.0.0.2" , "peer" : "peer-ip" },
544- }
545-
546- ansible_configurator .get_vpn_var (
505+ expected_vpn_vars = ({
506+ "name" : "bibigrid-vpngtw-foo-0" ,
507+ "regexp" : "bibigrid-worker-foo-\\ d+" ,
508+ # this is known bug behavior that needs to be fixed
509+ "image" : "vpn-image" ,
510+ "network" : "net1" ,
511+ "network_cidrs" : ["10.0.0.0/16" ],
512+ "floating_ip" : "10.1.1.2" ,
513+ "private_v4" : "10.1.1.1" ,
514+ "flavor" : {"flavor_key" : "flavor_value" },
515+ "wireguard_ip" : "10.0.0.2" ,
516+ "cloud_identifier" : "cloud1" ,
517+ "fallback_on_other_image" : False ,
518+ "on_demand" : False ,
519+ "wireguard" : {"ip" : "10.0.0.2" , "peer" : "peer-ip" },
520+ }, '/opt/playbook/host_vars/bibigrid-vpngtw-foo-0.yaml' )
521+
522+ vpn_vars = ansible_configurator .get_vpn_vars (
547523 provider = provider ,
548524 configuration = configuration ,
549525 cluster_id = cluster_id ,
550526 vpngtw = vpngtw ,
551527 vpn_count = vpn_count ,
552- log = log ,
553528 )
554529
555- mock_write_yaml .assert_called_once_with (
556- os .path .join ("mock_path" , "bibigrid-vpngtw-foo-0.yaml" ),
557- expected_host_vars ,
558- log
559- )
530+ self .assertEqual (expected_vpn_vars , vpn_vars )
560531
561532 @patch ("bibigrid.core.utility.paths.ansible_resources_path.GROUP_VARS_FOLDER" , "mock_path" )
562- @patch ("bibigrid.core.utility.ansible_configurator.write_yaml" )
563- def test_write_master_var (self , mock_write_yaml ):
533+ def test_write_master_var (self ):
564534 provider = MagicMock ()
565535 provider .create_flavor_dict .return_value = {"flavor_key" : "flavor_value" }
566536
@@ -580,37 +550,30 @@ def test_write_master_var(self, mock_write_yaml):
580550 }
581551
582552 cluster_id = "foo"
583- log = MagicMock ()
584553
585- expected_master_vars = {
586- "name" : "bibigrid-master-foo" ,
587- "image" : "master-image" ,
588- "network" : "net1" ,
589- "network_cidrs" : ["10.0.0.0/24" ],
590- "floating_ip" : True ,
591- "flavor" : {"flavor_key" : "flavor_value" },
592- "private_v4" : "10.1.1.1" ,
593- "cloud_identifier" : "cloud1" ,
594- "fallback_on_other_image" : False ,
595- "state" : "UNKNOWN" , # Based on useMasterAsCompute = True
596- "on_demand" : False ,
597- "partitions" : ["control" , "cloud1" , "all" ],
598- }
554+ expected_master_vars = ( {
555+ "name" : "bibigrid-master-foo" ,
556+ "image" : "master-image" ,
557+ "network" : "net1" ,
558+ "network_cidrs" : ["10.0.0.0/24" ],
559+ "floating_ip" : True ,
560+ "flavor" : {"flavor_key" : "flavor_value" },
561+ "private_v4" : "10.1.1.1" ,
562+ "cloud_identifier" : "cloud1" ,
563+ "fallback_on_other_image" : False ,
564+ "state" : "UNKNOWN" , # Based on useMasterAsCompute = True
565+ "on_demand" : False ,
566+ "partitions" : ["control" , "cloud1" , "all" ],
567+ }, '/opt/playbook/group_vars/master.yaml' )
599568
600569 # Call the function
601- ansible_configurator .get_master_var (
570+ master_vars = ansible_configurator .get_master_vars (
602571 provider = provider ,
603572 configuration = configuration ,
604573 cluster_id = cluster_id ,
605- log = log ,
606574 )
607575
608- # Validate the output
609- mock_write_yaml .assert_called_once_with (
610- os .path .join ("mock_path" , "master.yaml" ),
611- expected_master_vars ,
612- log ,
613- )
576+ self .assertEqual (expected_master_vars , master_vars )
614577
615578 def test_key_present_with_key_to (self ):
616579 dict_from = {'source_key' : 'value1' }
0 commit comments