From 7eaae2010b13226a0f35f688a783237703e93f97 Mon Sep 17 00:00:00 2001 From: XaverStiensmeier <36056823+XaverStiensmeier@users.noreply.github.com> Date: Wed, 24 Apr 2024 08:03:08 +0200 Subject: [PATCH] Update tests (#493) * updated tests * removed print * updated tests * updated tests * fixed too loose condition * updated tests --- .gitignore | 3 +- bibigrid/core/actions/create.py | 1 - bibigrid/core/utility/ansible_configurator.py | 5 +- .../utility/handler/configuration_handler.py | 1 + .../core/utility/validate_configuration.py | 9 +- resources/tests/bibigrid_test_example.yml | 9 + tests/provider/test_provider.py | 19 +- tests/test_ansible_configurator.py | 59 ++-- tests/test_configuration_handler.py | 1 + tests/test_create.py | 39 +-- tests/test_ssh_handler.py | 48 +-- tests/test_validate_configuration.py | 273 ++++++++++-------- 12 files changed, 253 insertions(+), 214 deletions(-) create mode 100644 resources/tests/bibigrid_test_example.yml diff --git a/.gitignore b/.gitignore index 8375a9a6..0f7fbc2f 100644 --- a/.gitignore +++ b/.gitignore @@ -10,8 +10,7 @@ resources/playbook/ansible_hosts resources/playbook/vars/ resources/playbook/host_vars/ resources/playbook/group_vars/ -tests/resources/* -!test/resources/test_configuration.yml +resources/tests/bibigrid_test.yml # any log files *.log diff --git a/bibigrid/core/actions/create.py b/bibigrid/core/actions/create.py index c1fbd0f8..b6a8d66f 100644 --- a/bibigrid/core/actions/create.py +++ b/bibigrid/core/actions/create.py @@ -196,7 +196,6 @@ def start_vpn_or_master(self, configuration, provider): # create a server and block until it is up and running server = provider.create_server(name=name, flavor=flavor, key_name=self.key_name, image=image, network=network, volumes=volumes, security_groups=configuration["security_groups"], wait=True) - print("MASTER", server) configuration["private_v4"] = server["private_v4"] self.log.debug(f"Created Server {name}: {server['private_v4']}.") # get mac address for given private address diff --git a/bibigrid/core/utility/ansible_configurator.py b/bibigrid/core/utility/ansible_configurator.py index 7d99253e..0ef6a39f 100644 --- a/bibigrid/core/utility/ansible_configurator.py +++ b/bibigrid/core/utility/ansible_configurator.py @@ -55,9 +55,7 @@ def generate_site_file_yaml(custom_roles): @param custom_roles: ansibleRoles given by the config @return: site_yaml (dict) """ - site_yaml = [{'hosts': 'master', "pre_tasks": [ - {"name": "Print ansible.cfg timeout", "command": "ansible-config dump | grep 'DEFAULT_TIMEOUT'", - "register": "ansible_cfg_output"}, {"debug": {"msg": "{{ ansible_cfg_output.stdout }}"}}], "become": "yes", + site_yaml = [{'hosts': 'master', "become": "yes", "vars_files": VARS_FILES, "roles": MASTER_ROLES}, {'hosts': 'vpngtw', "become": "yes", "vars_files": VARS_FILES, "roles": vpngtw_ROLES}, {"hosts": "workers", "become": "yes", "vars_files": VARS_FILES, "roles": WORKER_ROLES}] # , @@ -173,7 +171,6 @@ def generate_common_configuration_yaml(cidrs, configurations, cluster_id, ssh_us """ master_configuration = configurations[0] log.info("Generating common configuration file...") - # print(configuration.get("slurmConf", {})) common_configuration_yaml = {"bibigrid_version": __version__, "auto_mount": master_configuration.get("autoMount", False), "cluster_id": cluster_id, "cluster_cidrs": cidrs, "default_user": default_user, diff --git a/bibigrid/core/utility/handler/configuration_handler.py b/bibigrid/core/utility/handler/configuration_handler.py index bc124993..8746a76f 100644 --- a/bibigrid/core/utility/handler/configuration_handler.py +++ b/bibigrid/core/utility/handler/configuration_handler.py @@ -92,6 +92,7 @@ def get_clouds_files(log): clouds_public = clouds_public_yaml.get(CLOUD_PUBLIC_ROOT_KEY) if not clouds_public: log.warning("%s is not valid. Must contain key '%s'", CLOUDS_PUBLIC_YAML, CLOUD_PUBLIC_ROOT_KEY) + return clouds, clouds_public diff --git a/bibigrid/core/utility/validate_configuration.py b/bibigrid/core/utility/validate_configuration.py index 1980abda..256858b5 100644 --- a/bibigrid/core/utility/validate_configuration.py +++ b/bibigrid/core/utility/validate_configuration.py @@ -181,7 +181,6 @@ def __init__(self, configurations, providers, log): self.log = log self.configurations = configurations self.providers = providers - self.required_resources_dict = { provider.cloud_specification['identifier']: {'total_cores': 0, 'floating_ips': 0, 'instances': 0, 'total_ram': 0, 'volumes': 0, 'volume_gigabytes': 0, @@ -227,11 +226,11 @@ def check_master_vpn_worker(self): """ self.log.info("Checking master/vpn") success = True - if not self.configurations[0].get("masterInstance"): + if not self.configurations[0].get("masterInstance") or self.configurations[0].get("vpnInstance"): self.log.warning(f"{self.configurations[0].get('cloud')} has no master instance!") success = False for configuration in self.configurations[1:]: - if not configuration.get("vpnInstance"): + if not configuration.get("vpnInstance") or configuration.get("masterInstance"): self.log.warning(f"{configuration.get('cloud')} has no vpn instance!") success = False return success @@ -356,9 +355,9 @@ def check_volumes(self): else: self.log.info(f"Snapshot '{volume_name_or_id}' found on " f"{provider.cloud_specification['identifier']}.") - self.required_resources_dict[provider.cloud_specification['identifier']]["Volumes"] += 1 + self.required_resources_dict[provider.cloud_specification['identifier']]["volumes"] += 1 self.required_resources_dict[provider.cloud_specification['identifier']][ - "VolumeGigabytes"] += snapshot["size"] + "volume_gigabytes"] += snapshot["size"] else: self.log.info(f"Volume '{volume_name_or_id}' found on " f"{provider.cloud_specification['identifier']}.") diff --git a/resources/tests/bibigrid_test_example.yml b/resources/tests/bibigrid_test_example.yml new file mode 100644 index 00000000..9e3a9b1d --- /dev/null +++ b/resources/tests/bibigrid_test_example.yml @@ -0,0 +1,9 @@ +- infrastructure: # former mode. + cloud: #credentials # name of clouds.yaml entry + + flavor: de.NBI small + image: ^Ubuntu 22\.04 LTS \(.*\)$ + sshUser: ubuntu + network: # network + + snapshotImage: # name of a snapshot diff --git a/tests/provider/test_provider.py b/tests/provider/test_provider.py index 45b0de7e..56108d79 100644 --- a/tests/provider/test_provider.py +++ b/tests/provider/test_provider.py @@ -6,6 +6,8 @@ import os import unittest +from bibigrid.core import startup +from bibigrid.core.utility import image_selection import bibigrid.core.utility.paths.basic_path as bP from bibigrid.core.utility.handler import configuration_handler from bibigrid.core.utility.handler import provider_handler @@ -58,8 +60,8 @@ 'encrypted', 'multiattach', 'availability_zone', 'source_volid', 'user_id', 'os-vol-tenant-attr:tenant_id'} -FREE_RESOURCES_KEYS = {'total_cores', 'floating_ips', 'instances', 'total_ram', 'Volumes', 'VolumeGigabytes', - 'Snapshots', 'Backups', 'BackupGigabytes'} +FREE_RESOURCES_KEYS = {'total_cores', 'floating_ips', 'instances', 'total_ram', 'volumes', 'volume_gigabytes', + 'snapshots', 'backups', 'backup_gigabytes'} KEYPAIR = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDORPauyW3O7M4Uk8/Qo557h2zxd9fwByljG9S1/zHKIEzOMcOBb7WUSmyNa5XHh5IB0/" \ "BTsQvSag/O9IAhax2wlp9A2za6EkALYiRdEXeGOMNORw8yylRBqzLluKTErZ5sKYxENf1WGHsE3ifzct0G/moEPmIkixTHR9fZrZgOzQwj" \ @@ -70,7 +72,7 @@ CONFIGURATIONS = configuration_handler.read_configuration(logging, os.path.join(bP.ROOT_PATH, - "tests/resources/infrastructure_cloud.yml")) + "resources/tests/bibigrid_test.yml")) PROVIDERS = provider_handler.get_providers(CONFIGURATIONS, logging) @@ -82,6 +84,9 @@ class ProviderServer: def __init__(self, provider, name, configuration, key_name=None): self.provider = provider self.name = name + self.log = startup.LOG + configuration["image"] = image_selection.select_image(provider=provider, image=configuration["image"], + log=self.log) self.server_dict = provider.create_server(name=self.name, flavor=configuration["flavor"], image=configuration["image"], network=configuration["network"], key_name=key_name, security_groups=[]) @@ -166,6 +171,7 @@ def test_active_server_methods(self): def test_get_external_network(self): for provider, configuration in zip(PROVIDERS, CONFIGURATIONS): with self.subTest(provider.NAME): + print("FIRE", provider.get_external_network(configuration["network"])) self.assertTrue(provider.get_external_network(configuration["network"])) with self.assertRaises(TypeError): provider.get_external_network("ERROR") @@ -220,16 +226,17 @@ def test_get_image_mismatch(self): with self.subTest(provider.NAME): self.assertIsNone(provider.get_image_by_id_or_name("NONE")) - if CONFIGURATIONS[0].get("snapshot_image"): + if CONFIGURATIONS[0].get("snapshotImage"): def test_get_snapshot(self): for provider, configuration in zip(PROVIDERS, CONFIGURATIONS): with self.subTest(provider.NAME): self.assertEqual(SNAPSHOT_KEYS, set(provider.get_volume_snapshot_by_id_or_name( - configuration["snapshot_image"]).keys())) + configuration["snapshotImage"]).keys())) def test_create_volume_from_snapshot(self): for provider, configuration in zip(PROVIDERS, CONFIGURATIONS): with self.subTest(provider.NAME): - volume_id = provider.create_volume_from_snapshot(configuration["snapshot_image"]) + volume_id = provider.create_volume_from_snapshot(configuration["snapshotImage"]) + print(volume_id) volume = provider.get_volume_by_id_or_name(volume_id) self.assertEqual(VOLUME_KEYS, set(volume.keys())) diff --git a/tests/test_ansible_configurator.py b/tests/test_ansible_configurator.py index dbaef9a9..9bd92df0 100644 --- a/tests/test_ansible_configurator.py +++ b/tests/test_ansible_configurator.py @@ -5,9 +5,10 @@ from unittest.mock import MagicMock, Mock, patch, call, mock_open, ANY import bibigrid.core.utility.paths.ansible_resources_path as aRP -from bibigrid.core.utility.yaml_dumper import NoAliasSafeDumper from bibigrid.core import startup +from bibigrid.core.actions import version from bibigrid.core.utility import ansible_configurator +from bibigrid.core.utility.yaml_dumper import NoAliasSafeDumper class TestAnsibleConfigurator(TestCase): @@ -48,10 +49,12 @@ def test_generate_common_configuration_false(self): default_user = "ubuntu" ssh_user = "test" configuration = [{}] - common_configuration_yaml = {'auto_mount': False, 'cluster_cidrs': cidrs, 'cluster_id': cluster_id, - 'default_user': default_user, 'dns_server_list': ['8.8.8.8'], 'enable_ide': False, - 'enable_nfs': False, 'enable_slurm': False, 'enable_zabbix': False, - 'local_dns_lookup': False, 'local_fs': False, 'slurm': True, + common_configuration_yaml = {'auto_mount': False, 'bibigrid_version': version.__version__, + 'cloud_scheduling': {'sshTimeout': 4}, 'cluster_cidrs': cidrs, + 'cluster_id': cluster_id, 'default_user': default_user, + 'dns_server_list': ['8.8.8.8'], 'enable_ide': False, 'enable_nfs': False, + 'enable_slurm': False, 'enable_zabbix': False, 'local_dns_lookup': False, + 'local_fs': False, 'slurm': True, 'slurm_conf': {'db': 'slurm', 'db_password': 'changeme', 'db_user': 'slurm', 'elastic_scheduling': {'ResumeTimeout': 900, 'SuspendTime': 3600, 'TreeWidth': 128}, @@ -72,9 +75,11 @@ def test_generate_common_configuration_true(self): ssh_user = "test" configuration = [ {elem: "True" for elem in ["localFS", "localDNSlookup", "useMasterAsCompute", "slurm", "zabbix", "ide"]}] - common_configuration_yaml = {'auto_mount': False, 'cluster_cidrs': cidrs, 'cluster_id': cluster_id, - 'default_user': default_user, 'dns_server_list': ['8.8.8.8'], 'enable_ide': 'True', - 'enable_nfs': False, 'enable_slurm': 'True', 'enable_zabbix': 'True', + common_configuration_yaml = {'auto_mount': False, 'bibigrid_version': version.__version__, + 'cloud_scheduling': {'sshTimeout': 4}, 'cluster_cidrs': cidrs, + 'cluster_id': cluster_id, 'default_user': default_user, + 'dns_server_list': ['8.8.8.8'], 'enable_ide': 'True', 'enable_nfs': False, + 'enable_slurm': 'True', 'enable_zabbix': 'True', 'ide_conf': {'build': False, 'ide': False, 'port_end': 8383, 'port_start': 8181, 'workspace': '${HOME}'}, 'local_dns_lookup': 'True', 'local_fs': 'True', 'slurm': 'True', @@ -99,10 +104,12 @@ def test_generate_common_configuration_nfs_shares(self): cluster_id = "21" default_user = "ubuntu" ssh_user = "test" - common_configuration_yaml = {'auto_mount': False, 'cluster_cidrs': cidrs, 'cluster_id': cluster_id, - 'default_user': default_user, 'dns_server_list': ['8.8.8.8'], 'enable_ide': False, - 'enable_nfs': 'True', 'enable_slurm': False, 'enable_zabbix': False, - 'ext_nfs_mounts': [], 'local_dns_lookup': False, 'local_fs': False, + common_configuration_yaml = {'auto_mount': False, 'bibigrid_version': version.__version__, + 'cloud_scheduling': {'sshTimeout': 4}, 'cluster_cidrs': cidrs, + 'cluster_id': cluster_id, 'default_user': default_user, + 'dns_server_list': ['8.8.8.8'], 'enable_ide': False, 'enable_nfs': 'True', + 'enable_slurm': False, 'enable_zabbix': False, 'ext_nfs_mounts': [], + 'local_dns_lookup': False, 'local_fs': False, 'nfs_mounts': [{'dst': '//vil/mil', 'src': '//vil/mil'}, {'dst': '//vol/spool', 'src': '//vol/spool'}], 'slurm': True, 'slurm_conf': {'db': 'slurm', 'db_password': 'changeme', 'db_user': 'slurm', @@ -123,10 +130,12 @@ def test_generate_common_configuration_nfs(self): cluster_id = "21" default_user = "ubuntu" ssh_user = "test" - common_configuration_yaml = {'auto_mount': False, 'cluster_cidrs': cidrs, 'cluster_id': cluster_id, - 'default_user': default_user, 'dns_server_list': ['8.8.8.8'], 'enable_ide': False, - 'enable_nfs': 'True', 'enable_slurm': False, 'enable_zabbix': False, - 'ext_nfs_mounts': [], 'local_dns_lookup': False, 'local_fs': False, + common_configuration_yaml = {'auto_mount': False, 'bibigrid_version': version.__version__, + 'cloud_scheduling': {'sshTimeout': 4}, 'cluster_cidrs': cidrs, + 'cluster_id': cluster_id, 'default_user': default_user, + 'dns_server_list': ['8.8.8.8'], 'enable_ide': False, 'enable_nfs': 'True', + 'enable_slurm': False, 'enable_zabbix': False, 'ext_nfs_mounts': [], + 'local_dns_lookup': False, 'local_fs': False, 'nfs_mounts': [{'dst': '//vol/spool', 'src': '//vol/spool'}], 'slurm': True, 'slurm_conf': {'db': 'slurm', 'db_password': 'changeme', 'db_user': 'slurm', 'elastic_scheduling': {'ResumeTimeout': 900, 'SuspendTime': 3600, @@ -146,9 +155,11 @@ def test_generate_common_configuration_ext_nfs_shares(self): cluster_id = "21" default_user = "ubuntu" ssh_user = "test" - common_configuration_yaml = {'auto_mount': False, 'cluster_cidrs': cidrs, 'cluster_id': cluster_id, - 'default_user': default_user, 'dns_server_list': ['8.8.8.8'], 'enable_ide': False, - 'enable_nfs': 'True', 'enable_slurm': False, 'enable_zabbix': False, + common_configuration_yaml = {'auto_mount': False, 'bibigrid_version': version.__version__, + 'cloud_scheduling': {'sshTimeout': 4}, 'cluster_cidrs': cidrs, + 'cluster_id': cluster_id, 'default_user': default_user, + 'dns_server_list': ['8.8.8.8'], 'enable_ide': False, 'enable_nfs': 'True', + 'enable_slurm': False, 'enable_zabbix': False, 'ext_nfs_mounts': [{'dst': '/vil/mil', 'src': '/vil/mil'}], 'local_dns_lookup': False, 'local_fs': False, 'nfs_mounts': [{'dst': '//vol/spool', 'src': '//vol/spool'}], 'slurm': True, @@ -162,6 +173,7 @@ def test_generate_common_configuration_ext_nfs_shares(self): default_user, startup.LOG) common_configuration_yaml["slurm_conf"]["munge_key"] = generated_common_configuration["slurm_conf"]["munge_key"] + print(generated_common_configuration) self.assertEqual(common_configuration_yaml, generated_common_configuration) def test_generate_common_configuration_ide(self): @@ -170,10 +182,11 @@ def test_generate_common_configuration_ide(self): cluster_id = "21" default_user = "ubuntu" ssh_user = "test" - common_configuration_yaml = {'auto_mount': False, 'cluster_cidrs': cidrs, 'cluster_id': cluster_id, - 'default_user': default_user, 'dns_server_list': ['8.8.8.8'], - 'enable_ide': 'Some1', 'enable_nfs': False, 'enable_slurm': False, - 'enable_zabbix': False, + common_configuration_yaml = {'auto_mount': False, 'bibigrid_version': version.__version__, + 'cloud_scheduling': {'sshTimeout': 4}, 'cluster_cidrs': cidrs, + 'cluster_id': cluster_id, 'default_user': default_user, + 'dns_server_list': ['8.8.8.8'], 'enable_ide': 'Some1', 'enable_nfs': False, + 'enable_slurm': False, 'enable_zabbix': False, 'ide_conf': {'build': False, 'ide': False, 'key1': 'Some2', 'port_end': 8383, 'port_start': 8181, 'workspace': '${HOME}'}, 'local_dns_lookup': False, 'local_fs': False, 'slurm': True, diff --git a/tests/test_configuration_handler.py b/tests/test_configuration_handler.py index 86564f84..d442a650 100644 --- a/tests/test_configuration_handler.py +++ b/tests/test_configuration_handler.py @@ -14,6 +14,7 @@ class TestConfigurationHandler(TestCase): """ Class to test configuration_handler """ + # pylint: disable=R0904 def test_get_list_by_name_none(self): configurations = [{}, {}] diff --git a/tests/test_create.py b/tests/test_create.py index 339a90db..612681d2 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -111,16 +111,20 @@ def test_prepare_args_keyerror(self): creator.prepare_vpn_or_master_args(configuration, provider)) prepare_mock.assert_not_called() - @patch("bibigrid.core.utility.handler.ssh_handler.ansible_preparation") - def test_initialize_instances_master(self, mock_ansible): + @patch("bibigrid.core.utility.handler.ssh_handler.execute_ssh") + def test_initialize_master(self, mock_execute_ssh): provider = MagicMock() provider.list_servers.return_value = [] floating_ip = 21 configuration = {"masterInstance": 42, "floating_ip": floating_ip} creator = create.Create([provider], [configuration], "", startup.LOG) creator.initialize_instances() - mock_ansible.assert_called_with(floating_ip=floating_ip, private_key=create.KEY_FOLDER + creator.key_name, - username=creator.ssh_user, commands=[], log=startup.LOG, gateway={}) + ssh_data = {'floating_ip': floating_ip, 'private_key': create.KEY_FOLDER + creator.key_name, + 'username': creator.ssh_user, + 'commands': creator.ssh_add_public_key_commands + ssh_handler.ANSIBLE_SETUP, + 'filepaths': [(create.KEY_FOLDER + creator.key_name, '.ssh/id_ecdsa')], + 'gateway': {}, 'timeout': 4} + mock_execute_ssh.assert_called_with(ssh_data, startup.LOG) def test_prepare_volumes_none(self): provider = MagicMock() @@ -203,22 +207,23 @@ def test_upload_playbooks(self, mock_execute_ssh, mock_ac_ssh, mock_configure_an creator.upload_data() mock_configure_ansible.assert_called_with(providers=creator.providers, configurations=creator.configurations, cluster_id=creator.cluster_id, log=startup.LOG) - mock_execute_ssh.assert_called_with(floating_ip=creator.master_ip, - private_key=create.KEY_FOLDER + creator.key_name, username=creator.ssh_user, - filepaths=create.FILEPATHS, - commands=[mock_ac_ssh()] + ssh_handler.ANSIBLE_START, log=startup.LOG, - gateway={}) + ssh_data = {'floating_ip': creator.master_ip, 'private_key': create.KEY_FOLDER + creator.key_name, + 'username': creator.ssh_user, + 'commands': [mock_ac_ssh()] + ssh_handler.ANSIBLE_START, + 'filepaths': create.FILEPATHS, + 'gateway': {}, 'timeout': 4} + mock_execute_ssh.assert_called_with(ssh_data=ssh_data, log=startup.LOG) @patch.object(create.Create, "generate_keypair") @patch.object(create.Create, "prepare_configurations") - @patch.object(create.Create, "start_start_instance_threads") + @patch.object(create.Create, "start_start_server_threads") @patch.object(create.Create, "upload_data") @patch.object(create.Create, "log_cluster_start_info") @patch("bibigrid.core.actions.terminate.terminate") def test_create_non_debug(self, mock_terminate, mock_info, mock_up, mock_start, mock_conf, mock_key): provider = MagicMock() provider.list_servers.return_value = [] - configuration = {} + configuration = {"floating_ip": 42} creator = create.Create([provider], [configuration], "", startup.LOG, False) self.assertEqual(0, creator.create()) for mock in [mock_info, mock_up, mock_start, mock_conf, mock_key]: @@ -227,18 +232,16 @@ def test_create_non_debug(self, mock_terminate, mock_info, mock_up, mock_start, @patch.object(create.Create, "generate_keypair") @patch.object(create.Create, "prepare_configurations") - @patch.object(create.Create, "start_start_instance_threads") - @patch.object(create.Create, "upload_data") + @patch.object(create.Create, "start_start_server_threads") @patch.object(create.Create, "log_cluster_start_info") @patch("bibigrid.core.actions.terminate.terminate") - def test_create_non_debug_upload_raise(self, mock_terminate, mock_info, mock_up, mock_start, mock_conf, mock_key): + def test_create_non_debug_upload_raise(self, mock_terminate, mock_info, mock_start, mock_conf, mock_key): provider = MagicMock() provider.list_servers.return_value = [] configuration = {} creator = create.Create([provider], [configuration], "", startup.LOG, False) - mock_up.side_effect = [ConnectionError()] self.assertEqual(1, creator.create()) - for mock in [mock_start, mock_conf, mock_key, mock_up]: + for mock in [mock_start, mock_conf, mock_key]: mock.assert_called() for mock in [mock_info]: mock.assert_not_called() @@ -247,14 +250,14 @@ def test_create_non_debug_upload_raise(self, mock_terminate, mock_info, mock_up, @patch.object(create.Create, "generate_keypair") @patch.object(create.Create, "prepare_configurations") - @patch.object(create.Create, "start_start_instance_threads") + @patch.object(create.Create, "start_start_server_threads") @patch.object(create.Create, "upload_data") @patch.object(create.Create, "log_cluster_start_info") @patch("bibigrid.core.actions.terminate.terminate") def test_create_debug(self, mock_terminate, mock_info, mock_up, mock_start, mock_conf, mock_key): provider = MagicMock() provider.list_servers.return_value = [] - configuration = {} + configuration = {"floating_ip": 42} creator = create.Create([provider], [configuration], "", startup.LOG, True) self.assertEqual(0, creator.create()) for mock in [mock_info, mock_up, mock_start, mock_conf, mock_key]: diff --git a/tests/test_ssh_handler.py b/tests/test_ssh_handler.py index 7c0cfd46..73aa4687 100644 --- a/tests/test_ssh_handler.py +++ b/tests/test_ssh_handler.py @@ -1,15 +1,12 @@ """ Module to test ssh_handler """ -import socket from unittest import TestCase from unittest.mock import mock_open, Mock, MagicMock, patch, call -from paramiko.ssh_exception import NoValidConnectionsError - from bibigrid.core import startup from bibigrid.core.utility.handler import ssh_handler -from bibigrid.models.exceptions import ExecutionException, ConnectionException +from bibigrid.models.exceptions import ExecutionException class TestSshHandler(TestCase): @@ -49,25 +46,6 @@ def test_copy_to_server_folder(self, mock_listdir): mock_listdir.assert_called_with("Jim") sftp.mkdir.assert_called_with("Joe") - @patch("logging.info") - def test_is_active(self, mock_log): - client = Mock() - client.connect = MagicMock(return_value=True) - self.assertFalse(ssh_handler.is_active(client, 42, 32, 22, startup.LOG, {}, timeout=5)) - mock_log.assert_not_called() - - def test_is_active_on_second_attempt(self): - client = Mock() - client.connect = MagicMock(side_effect=[NoValidConnectionsError({('127.0.0.1', 22): socket.error}), True]) - self.assertFalse(ssh_handler.is_active(client, 42, 32, 22, startup.LOG, {}, timeout=5)) - - def test_is_active_exception(self): - client = Mock() - client.connect = MagicMock(side_effect=NoValidConnectionsError({('127.0.0.1', 22): socket.error})) - with self.assertRaises(ConnectionException): - ssh_handler.is_active(client, 42, 32, 22, startup.LOG, {}, timeout=0) - client.connect.assert_called_with(hostname=42, username=22, pkey=32, timeout=7, auth_timeout=5, port=22) - @patch("bibigrid.core.utility.handler.ssh_handler.execute_ssh_cml_commands") @patch("paramiko.ECDSAKey.from_private_key_file") @patch("paramiko.SSHClient") @@ -79,24 +57,14 @@ def test_execute_ssh(self, mock_client, mock_paramiko_key, mock_exec): mock.__enter__ = client mock.__exit__ = Mock(return_value=None) with patch("bibigrid.core.utility.handler.ssh_handler.is_active") as mock_active: - ssh_handler.execute_ssh(42, 32, 22, startup.LOG, {}, [12]) + ssh_data = {'floating_ip': 42, 'private_key': "key", 'username': "ubuntu", 'commands': ["ho"], + 'filepaths': [], 'gateway': {}, 'timeout': 4} + ssh_handler.execute_ssh(ssh_data, startup.LOG) mock_client.assert_called_with() - mock_active.assert_called_with(client=client(), floating_ip_address=42, username=22, private_key=2, - log=startup.LOG, gateway={}) - mock_exec.assert_called_with(client=client(), commands=[12], log=startup.LOG) - mock_paramiko_key.assert_called_with(32) - - @patch("bibigrid.core.utility.handler.ssh_handler.execute_ssh") - def test_ansible_preparation(self, mock_execute): - ssh_handler.ansible_preparation(1, 2, 3, startup.LOG, {}, [], []) - mock_execute.assert_called_with(1, 2, 3, startup.LOG, {}, ssh_handler.ANSIBLE_SETUP, - [(2, ssh_handler.PRIVATE_KEY_FILE)]) - - @patch("bibigrid.core.utility.handler.ssh_handler.execute_ssh") - def test_ansible_preparation_elem(self, mock_execute): - ssh_handler.ansible_preparation(1, 2, 3, startup.LOG, {}, [42], [42]) - mock_execute.assert_called_with(1, 2, 3, startup.LOG, {}, ssh_handler.ANSIBLE_SETUP + [42], - [42, (2, ssh_handler.PRIVATE_KEY_FILE)]) + mock_active.assert_called_with(client=client(), paramiko_key=mock_paramiko_key.return_value, + ssh_data=ssh_data, log=startup.LOG) + mock_exec.assert_called_with(client=client(), commands=["ho"], log=startup.LOG) + mock_paramiko_key.assert_called_with("key") def test_execute_ssh_cml_commands(self): client = Mock() diff --git a/tests/test_validate_configuration.py b/tests/test_validate_configuration.py index 5deb65c8..322cbb36 100644 --- a/tests/test_validate_configuration.py +++ b/tests/test_validate_configuration.py @@ -6,6 +6,7 @@ from unittest import TestCase from unittest.mock import Mock, patch, MagicMock, call +from bibigrid.core import startup from bibigrid.core.utility import validate_configuration from bibigrid.models.exceptions import ImageNotActiveException @@ -32,12 +33,14 @@ def test_check_provider_data_unique(self): @patch("bibigrid.core.utility.image_selection.select_image") def test_check_master_vpn_worker_ordered(self, mock_select_image): # pylint: disable=unused-argument + provider1 = Mock() + provider1.cloud_specification = {"identifier": "1"} master = {"masterInstance": "Value"} vpn = {"vpnInstance": "Value"} vpn_master = {} vpn_master.update(master) vpn_master.update(vpn) - v_c = validate_configuration.ValidateConfiguration(providers=None, configurations=[master], log=Mock()) + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[master], log=Mock()) self.assertTrue(v_c.check_master_vpn_worker()) v_c.configurations = [master, vpn] self.assertTrue(v_c.check_master_vpn_worker()) @@ -47,12 +50,15 @@ def test_check_master_vpn_worker_ordered(self, mock_select_image): # pylint: di self.assertFalse(v_c.check_master_vpn_worker()) def test_check_master_vpn_worker_unique(self): + provider1 = Mock() + provider1.cloud_specification = {"identifier": "1"} master = {"masterInstance": "Value"} vpn = {"vpnInstance": "Value"} vpn_master = {} vpn_master.update(master) vpn_master.update(vpn) - v_c = validate_configuration.ValidateConfiguration(providers=None, configurations=[vpn_master], log=Mock()) + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[vpn_master], + log=Mock()) self.assertFalse(v_c.check_master_vpn_worker()) v_c.configurations = [master, vpn_master] self.assertFalse(v_c.check_master_vpn_worker()) @@ -70,242 +76,279 @@ def test_check_provider_connection(self): self.assertTrue(v_c.check_provider_connections()) def test_check_instances_master(self): - v_c = validate_configuration.ValidateConfiguration(providers=["31"], configurations=[{"masterInstance": "42"}], - log=Mock()) + provider1 = Mock() + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], + configurations=[{"masterInstance": "42"}], log=Mock()) with patch.object(v_c, "check_instance") as mock: v_c.check_instances() - mock.assert_called_with("masterInstance", "42", "31") + mock.assert_called_with("masterInstance", "42", provider1) def test_check_instances_vpn(self): - v_c = validate_configuration.ValidateConfiguration(providers=["31"], configurations=[{"vpnInstance": "42"}], - log=Mock()) + provider1 = Mock() + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], + configurations=[{"vpnInstance": "42"}], log=Mock()) with patch.object(v_c, "check_instance") as mock: v_c.check_instances() - mock.assert_called_with("vpnInstance", "42", "31") + mock.assert_called_with("vpnInstance", "42", provider1) def test_check_instances_vpn_worker(self): - v_c = validate_configuration.ValidateConfiguration(providers=["31"], configurations=[ + provider1 = Mock() + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[ {"masterInstance": "42", "workerInstances": ["42"]}], log=Mock()) with patch.object(v_c, "check_instance") as mock: v_c.check_instances() - mock.assert_called_with("workerInstance", "42", "31") + mock.assert_called_with("workerInstance", "42", provider1) def test_check_instances_vpn_master_missing(self): - v_c = validate_configuration.ValidateConfiguration(providers=["31"], configurations=[{}], log=Mock()) + provider1 = Mock() + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[{}], log=Mock()) self.assertFalse(v_c.check_instances()) - v_c = validate_configuration.ValidateConfiguration(providers=["31"], + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[{"workerInstances": ["42"]}], log=Mock()) self.assertFalse(v_c.check_instances()) def test_check_instances_vpn_master_count(self): for i in range(1, 4): - v_c = validate_configuration.ValidateConfiguration(providers=["31"] * i, + provider1 = Mock() + provider1.cloud_specification = {"identifier": i} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1] * i, configurations=[{"masterInstance": {"count": 1}}] + [ {"vpnInstance": {"count": 1}}] * (i - 1), log=Mock()) # with patch.object(v_c, "check_instance") as mock: with patch.object(v_c, "check_instance", return_value=True): v_c.check_instances() - self.assertTrue(v_c.required_resources_dict["floating_ips"] == i) + self.assertTrue(v_c.required_resources_dict[i]["floating_ips"] == i) @patch("bibigrid.core.utility.image_selection.select_image") def test_check_instance_image_not_active(self, mock_select_image): mock_select_image.side_effect = ImageNotActiveException() - v_c = validate_configuration.ValidateConfiguration(providers=None, configurations=None, log=Mock()) - provider = Mock() - provider.get_active_images.return_value = [] - provider.get_flavor = MagicMock(return_value={"disk": None, "ram": None}) - provider.get_image_by_id_or_name = MagicMock(return_value={"min_disk": None, "min_ram": None}) - self.assertFalse(v_c.check_instance(None, {"count": 1, "image": 2, "type": 3}, provider)) + + provider1 = Mock() + provider1.cloud_specification = {"identifier": "1"} + provider1.get_active_images.return_value = [] + provider1.get_flavor = MagicMock(return_value={"disk": None, "ram": None}) + provider1.get_image_by_id_or_name = MagicMock(return_value={"min_disk": None, "min_ram": None}) + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=None, log=Mock()) + + self.assertFalse(v_c.check_instance(None, {"count": 1, "image": 2, "type": 3}, provider1)) @patch("bibigrid.core.utility.image_selection.select_image") def test_check_instance_image_active_combination_call(self, mock_select_image): - v_c = validate_configuration.ValidateConfiguration(providers=None, configurations=None, log=Mock()) - provider = Mock() - provider.get_image_by_id_or_name = MagicMock(return_value={"status": "active"}) + provider1 = MagicMock() + provider1.get_image_by_id_or_name = MagicMock(return_value={"status": "active"}) + provider1.cloud_specification = {"identifier": "1"} + image = 2 + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=None, log=startup.LOG) + type3 = 3 + with patch.object(v_c, "check_instance_type_image_combination") as mock: - v_c.check_instance(42, {"count": 1, "image": 2, "type": 3}, provider) - mock.assert_called_with(3, mock_select_image(2), provider) + v_c.check_instance(42, {"count": 1, "image": image, "type": type3}, provider1) + mock.assert_called_with(type3, mock_select_image(), provider1) + mock_select_image.assert_called() def test_check_instance_type_image_combination_has_enough_calls(self): log = Mock() - v_c = validate_configuration.ValidateConfiguration(providers=None, configurations=None, log=log) - provider = MagicMock() - provider.get_flavor.return_value = {"disk": 42, "ram": 32, "vcpus": 10} - provider.get_image_by_id_or_name.return_value = {"min_disk": 22, "min_ram": 12} + provider1 = MagicMock() + provider1.get_flavor.return_value = {"disk": 42, "ram": 32, "vcpus": 10} + provider1.get_image_by_id_or_name.return_value = {"min_disk": 22, "min_ram": 12} + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=None, log=log) with patch.object(validate_configuration, "has_enough") as mock: v_c.check_instance_type_image_combination(instance_image=None, instance_type="de.NBI tiny", - provider=provider) + provider=provider1) self.assertEqual(call(42, 22, "Type de.NBI tiny", "disk space", log), mock.call_args_list[0]) self.assertEqual(call(32, 12, "Type de.NBI tiny", "ram", log), mock.call_args_list[1]) def test_check_instance_type_image_combination_result(self): - provider = MagicMock() - provider.get_flavor.return_value = {"disk": 42, "ram": 32, "vcpus": 10} - provider.get_image_by_id_or_name.return_value = {"min_disk": 22, "min_ram": 12} - v_c = validate_configuration.ValidateConfiguration(providers=None, configurations=None, log=Mock()) + provider1 = MagicMock() + provider1.get_flavor.return_value = {"disk": 42, "ram": 32, "vcpus": 10} + provider1.get_image_by_id_or_name.return_value = {"min_disk": 22, "min_ram": 12} + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=None, log=Mock()) with patch.object(validate_configuration, "has_enough") as mock: mock.side_effect = [True, True, False, False, True, False, False, True] # True True self.assertTrue(v_c.check_instance_type_image_combination(instance_image=None, instance_type="de.NBI tiny", - provider=provider)) + provider=provider1)) # False False self.assertFalse(v_c.check_instance_type_image_combination(instance_image=None, instance_type="de.NBI tiny", - provider=provider)) + provider=provider1)) # True False self.assertFalse(v_c.check_instance_type_image_combination(instance_image=None, instance_type="de.NBI tiny", - provider=provider)) + provider=provider1)) # False True self.assertFalse(v_c.check_instance_type_image_combination(instance_image=None, instance_type="de.NBI tiny", - provider=provider)) + provider=provider1)) def test_check_instance_type_image_combination_count(self): for i in range(3): - provider = MagicMock() - provider.get_flavor.return_value = {"disk": 42, "ram": i * 32, "vcpus": i * 10} - provider.get_image_by_id_or_name.return_value = {"min_disk": 22, "min_ram": 12} + provider1 = MagicMock() + provider1.get_flavor.return_value = {"disk": 42, "ram": i * 32, "vcpus": i * 10} + provider1.get_image_by_id_or_name.return_value = {"min_disk": 22, "min_ram": 12} + provider1.cloud_specification = {"identifier": "1"} log = Mock() - v_c = validate_configuration.ValidateConfiguration(providers=None, configurations=None, log=log) + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=None, log=log) with patch.object(validate_configuration, "has_enough") as mock: v_c.check_instance_type_image_combination(instance_image=None, instance_type="de.NBI tiny", - provider=provider) - self.assertEqual(32 * i, v_c.required_resources_dict["total_ram"]) - self.assertEqual(10 * i, v_c.required_resources_dict["total_cores"]) + provider=provider1) + self.assertEqual(32 * i, v_c.required_resources_dict["1"]["total_ram"]) + self.assertEqual(10 * i, v_c.required_resources_dict["1"]["total_cores"]) mock.assert_called_with(32 * i, 12, 'Type de.NBI tiny', 'ram', log) def test_check_volumes_none(self): - v_c = validate_configuration.ValidateConfiguration(providers=[42], configurations=[{}], log=Mock()) + provider1 = MagicMock() + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[{}], log=Mock()) self.assertTrue(v_c.check_volumes()) def test_check_volumes_mismatch(self): - provider = Mock() - provider.get_volume_by_id_or_name = MagicMock(return_value=None) - provider.get_volume_snapshot_by_id_or_name = MagicMock(return_value=None) - v_c = validate_configuration.ValidateConfiguration(providers=[provider], + provider1 = Mock() + provider1.get_volume_by_id_or_name = MagicMock(return_value=None) + provider1.get_volume_snapshot_by_id_or_name = MagicMock(return_value=None) + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[{"masterMounts": ["Test"]}], log=Mock()) self.assertFalse(v_c.check_volumes()) def test_check_volumes_match_snapshot(self): - provider = Mock() - provider.get_volume_by_id_or_name = MagicMock(return_value=None) - provider.get_volume_snapshot_by_id_or_name = MagicMock(return_value={"size": 1}) - v_c = validate_configuration.ValidateConfiguration(providers=[provider], + provider1 = Mock() + provider1.get_volume_by_id_or_name = MagicMock(return_value=None) + provider1.get_volume_snapshot_by_id_or_name = MagicMock(return_value={"size": 1}) + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[{"masterMounts": ["Test"]}], log=Mock()) self.assertTrue(v_c.check_volumes()) def test_check_volumes_match_snapshot_count(self): - for i in range(3): - provider = Mock() - provider.get_volume_by_id_or_name = MagicMock(return_value=None) - provider.get_volume_snapshot_by_id_or_name = MagicMock(return_value={"size": i}) - v_c = validate_configuration.ValidateConfiguration(providers=[provider] * i, + for i in range(1, 3): + provider1 = Mock() + provider1.get_volume_by_id_or_name = MagicMock(return_value=None) + provider1.get_volume_snapshot_by_id_or_name = MagicMock(return_value={"size": i}) + provider1.cloud_specification = {"identifier": i} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1] * i, configurations=[{"masterMounts": ["Test"] * i}], log=Mock()) self.assertTrue(v_c.check_volumes()) - self.assertTrue(v_c.required_resources_dict["Volumes"] == i) - self.assertTrue(v_c.required_resources_dict["VolumeGigabytes"] == i ** 2) + self.assertTrue(v_c.required_resources_dict[i]["volumes"] == i) + self.assertTrue(v_c.required_resources_dict[i]["volume_gigabytes"] == i ** 2) def test_check_volumes_match_volume(self): - provider = Mock() - provider.get_volume_by_id_or_name = MagicMock(return_value={"size": 1}) - provider.get_volume_snapshot_by_id_or_name = MagicMock(return_value=None) - v_c = validate_configuration.ValidateConfiguration(providers=[provider], + provider1 = Mock() + provider1.get_volume_by_id_or_name = MagicMock(return_value={"size": 1}) + provider1.get_volume_snapshot_by_id_or_name = MagicMock(return_value=None) + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[{"masterMounts": ["Test"]}], log=Mock()) self.assertTrue(v_c.check_volumes()) - self.assertTrue(v_c.required_resources_dict["Volumes"] == 0) - self.assertTrue(v_c.required_resources_dict["VolumeGigabytes"] == 0) + self.assertTrue(v_c.required_resources_dict["1"]["volumes"] == 0) + self.assertTrue(v_c.required_resources_dict["1"]["volume_gigabytes"] == 0) def test_check_network_none(self): - provider = Mock() - provider.get_network_by_id_or_name = MagicMock(return_value=None) - v_c = validate_configuration.ValidateConfiguration(providers=[provider], configurations=[{}], log=Mock()) + provider1 = Mock() + provider1.get_network_by_id_or_name = MagicMock(return_value=None) + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[{}], log=Mock()) self.assertFalse(v_c.check_network()) def test_check_network_no_network(self): - provider = Mock() - provider.get_subnet_by_id_or_name = MagicMock(return_value="network") - v_c = validate_configuration.ValidateConfiguration(providers=[provider], + provider1 = Mock() + provider1.get_subnet_by_id_or_name = MagicMock(return_value="network") + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[{"subnet": "subnet_name"}], log=Mock()) self.assertTrue(v_c.check_network()) - provider.get_subnet_by_id_or_name.assert_called_with("subnet_name") + provider1.get_subnet_by_id_or_name.assert_called_with("subnet_name") def test_check_network_no_network_mismatch_subnet(self): - provider = Mock() - provider.get_subnet_by_id_or_name = MagicMock(return_value=None) - v_c = validate_configuration.ValidateConfiguration(providers=[provider], + provider1 = Mock() + provider1.get_subnet_by_id_or_name = MagicMock(return_value=None) + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[{"subnet": "subnet_name"}], log=Mock()) self.assertFalse(v_c.check_network()) - provider.get_subnet_by_id_or_name.assert_called_with("subnet_name") + provider1.get_subnet_by_id_or_name.assert_called_with("subnet_name") def test_check_network_no_subnet_mismatch_network(self): - provider = Mock() - provider.get_network_by_id_or_name = MagicMock(return_value=None) - v_c = validate_configuration.ValidateConfiguration(providers=[provider], + provider1 = Mock() + provider1.get_network_by_id_or_name = MagicMock(return_value=None) + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[{"network": "network_name"}], log=Mock()) self.assertFalse(v_c.check_network()) - provider.get_network_by_id_or_name.assert_called_with("network_name") + provider1.get_network_by_id_or_name.assert_called_with("network_name") def test_check_network_no_subnet(self): - provider = Mock() - provider.get_network_by_id_or_name = MagicMock(return_value="network") - v_c = validate_configuration.ValidateConfiguration(providers=[provider], + provider1 = Mock() + provider1.get_network_by_id_or_name = MagicMock(return_value="network") + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[{"network": "network_name"}], log=Mock()) self.assertTrue(v_c.check_network()) - provider.get_network_by_id_or_name.assert_called_with("network_name") + provider1.get_network_by_id_or_name.assert_called_with("network_name") def test_check_network_subnet_network(self): - provider = Mock() - provider.get_network_by_id_or_name = MagicMock(return_value="network") - provider.get_subnet_by_id_or_name = MagicMock(return_value="network") - v_c = validate_configuration.ValidateConfiguration(providers=[provider], + provider1 = Mock() + provider1.get_network_by_id_or_name = MagicMock(return_value="network") + provider1.get_subnet_by_id_or_name = MagicMock(return_value="network") + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[{"network": "network_name"}], log=Mock()) self.assertTrue(v_c.check_network()) - provider.get_network_by_id_or_name.assert_called_with("network_name") + provider1.get_network_by_id_or_name.assert_called_with("network_name") def test_check_server_group_none(self): - provider = Mock() - provider.get_network_by_id_or_name = MagicMock(return_value=None) - v_c = validate_configuration.ValidateConfiguration(providers=[provider], configurations=[{}], log=Mock()) + provider1 = Mock() + provider1.get_network_by_id_or_name = MagicMock(return_value=None) + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[{}], log=Mock()) self.assertTrue(v_c.check_server_group()) def test_check_server_group_mismatch(self): - provider = Mock() - provider.get_server_group_by_id_or_name = MagicMock(return_value=None) - v_c = validate_configuration.ValidateConfiguration(providers=[provider], + provider1 = Mock() + provider1.get_server_group_by_id_or_name = MagicMock(return_value=None) + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[{"serverGroup": "GroupName"}], log=Mock()) self.assertFalse(v_c.check_server_group()) - provider.get_server_group_by_id_or_name.assert_called_with("GroupName") + provider1.get_server_group_by_id_or_name.assert_called_with("GroupName") def test_check_server_group_match(self): - provider = Mock() - provider.get_server_group_by_id_or_name = MagicMock(return_value="Group") - v_c = validate_configuration.ValidateConfiguration(providers=[provider], + provider1 = Mock() + provider1.get_server_group_by_id_or_name = MagicMock(return_value="Group") + provider1.cloud_specification = {"identifier": "1"} + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=[{"serverGroup": "GroupName"}], log=Mock()) self.assertTrue(v_c.check_server_group()) - provider.get_server_group_by_id_or_name.assert_called_with("GroupName") + provider1.get_server_group_by_id_or_name.assert_called_with("GroupName") def test_check_quotas_true(self): - provider = MagicMock() - provider.cloud_specification = {"auth": {"project_name": "name"}, "identifier": "identifier"} - test_dict = {'total_cores': 42, 'floating_ips': 42, 'instances': 42, 'total_ram': 42, 'Volumes': 42, - 'VolumeGigabytes': 42, 'Snapshots': 42, 'Backups': 42, 'BackupGigabytes': 42} - provider.get_free_resources.return_value = test_dict - v_c = validate_configuration.ValidateConfiguration(providers=[provider], configurations=None, log=Mock()) + provider1 = MagicMock() + provider1.cloud_specification = {"auth": {"project_name": "name"}, "identifier": "1"} + test_dict = {'total_cores': 42, 'floating_ips': 42, 'instances': 42, 'total_ram': 42, 'volumes': 42, + 'volume_gigabytes': 42, 'snapshots': 42, 'backups': 42, 'backup_gigabytes': 42} + provider1.get_free_resources.return_value = test_dict + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=None, log=Mock()) with patch.object(validate_configuration, "has_enough") as mock: mock.side_effect = [True] * len(test_dict) self.assertTrue(v_c.check_quotas()) - provider.get_free_resources.assert_called() + provider1.get_free_resources.assert_called() def test_check_quotas_false(self): - provider = MagicMock() - test_dict = {'total_cores': 42, 'floating_ips': 42, 'instances': 42, 'total_ram': 42, 'Volumes': 42, - 'VolumeGigabytes': 42, 'Snapshots': 42, 'Backups': 42, 'BackupGigabytes': 42} - provider.get_free_resources.return_value = test_dict + provider1 = MagicMock() + provider1.cloud_specification = {"identifier": "1"} + test_dict = {'total_cores': 42, 'floating_ips': 42, 'instances': 42, 'total_ram': 42, 'volumes': 42, + 'volume_gigabytes': 42, 'snapshots': 42, 'backups': 42, 'backup_gigabytes': 42} + provider1.get_free_resources.return_value = test_dict os.environ['OS_PROJECT_NAME'] = "name" - v_c = validate_configuration.ValidateConfiguration(providers=[provider], configurations=None, log=Mock()) + v_c = validate_configuration.ValidateConfiguration(providers=[provider1], configurations=None, log=Mock()) with patch.object(validate_configuration, "has_enough") as mock: mock.side_effect = [True] * (len(test_dict) - 1) + [False] self.assertFalse(v_c.check_quotas()) - provider.get_free_resources.assert_called() + provider1.get_free_resources.assert_called() mock.assert_called() def test_has_enough_lower(self):