def test_oci_create_subnet(self, mock_init): """Test oci create subnet method.""" mock_init.return_value = None client = MagicMock() subnet = MagicMock() response = MagicMock() vcn = MagicMock() vcn.id = ( 'ocid1.vcn.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') response.data = subnet client.create_subnet_and_wait_for_state.return_value = response compartment_id = ( 'ocid1.compartment.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') cloud = OCICloud(**self.kwargs) cloud.vnet_composite_client = client result = cloud._create_subnet(compartment_id, 'Omic:PHX-AD-1', vcn, 'subnet-123') assert result == subnet
def test_set_instance_ip(self, mock_init, mock_get_instance, mock_get_vnic_attachments): """Test oci set instance ip method.""" mock_init.return_value = None instance = MagicMock() instance.id = 'instance123' instance.compartment_id = ( 'ocid1.compartment.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') attachment = MagicMock() attachment.vnic_id = 'vnic123' mock_get_vnic_attachments.return_value = [attachment] vnic = MagicMock() vnic.public_ip = '0.0.0.0' vnic.private_ip = '10.0.0.123' response = MagicMock() response.data = vnic client = MagicMock() client.get_vnic.return_value = response mock_get_instance.return_value = instance cloud = OCICloud(**self.kwargs) cloud.vnet_client = client cloud._set_instance_ip()
def test_oci_get_subnet_by_name(self, mock_init, mock_oci): """Test oci get subnet by name method.""" mock_init.return_value = None client = MagicMock() subnet = MagicMock() subnet.display_name = 'subnet-123' response = MagicMock() response.data = [subnet] mock_oci.pagination.list_call_get_all_results.return_value = response cloud = OCICloud(**self.kwargs) cloud.compartment_id = ( 'ocid1.compartment.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ) cloud.vnet_client = client vcn_id = ( 'ocid1.vcn.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ) result = cloud._get_subnet_by_name('subnet-123', vcn_id) assert result == subnet
def test_oci_get_internet_gateway_by_name(self, mock_init, mock_oci): """Test oci get internet gateway by name method.""" mock_init.return_value = None gateway = MagicMock() gateway.display_name = 'test-gw2' client = MagicMock() response = MagicMock() response.data = [gateway] mock_oci.pagination.list_call_get_all_results.return_value = response vcn_id = ( 'ocid1.vcn.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ) cloud = OCICloud(**self.kwargs) cloud.vnet_client = client cloud.compartment_id = ( 'ocid1.compartment.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ) result = cloud._get_gateway_in_vcn_by_name(vcn_id, 'test-gw2') assert result == gateway
def test_get_ssh_public_key(self, mock_init, mock_get_public_ssh_key): """Test oci get ssh public key method.""" mock_init.return_value = None mock_get_public_ssh_key.return_value = b'key123' cloud = OCICloud(**self.kwargs) cloud.ssh_private_key_file = 'tests/oci/api_key.pem' result = cloud._get_ssh_public_key() assert result == 'key123'
def test_set_image_id(self, mock_init, mock_get_instance): """Test oci set image id method.""" mock_init.return_value = None instance = MagicMock() instance.display_name = 'instance 123' instance.source_details.image_id = 'image123' mock_get_instance.return_value = instance cloud = OCICloud(**self.kwargs) cloud._set_image_id()
def test_oci_delete_internet_gateway(self, mock_init): """Test oci delete internet gateway method.""" mock_init.return_value = None client = MagicMock() gateway_id = ( 'ocid1.gateway.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') cloud = OCICloud(**self.kwargs) cloud.vnet_composite_client = client cloud._delete_internet_gateway(gateway_id)
def test_oci_delete_subnet(self, mock_init): """Test oci delete subnet method.""" mock_init.return_value = None client = MagicMock() cloud = OCICloud(**self.kwargs) cloud.vnet_composite_client = client subnet_id = ( 'ocid1.subnet.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') cloud._delete_subnet(subnet_id)
def test_oci_clear_route_rules(self, mock_init): """Test oci clear route rules method.""" mock_init.return_value = None client = MagicMock() vcn = MagicMock() cloud = OCICloud(**self.kwargs) cloud.vnet_composite_client = client vcn.default_route_table_id = ( 'ocid1.routeTable.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') cloud._clear_route_rules(vcn)
def test_oci_is_instance_running(self, mock_init): """Test oci is instance running method.""" mock_init.return_value = None client = MagicMock() instance = MagicMock() instance.lifecycle_state = 'RUNNING' response = MagicMock() response.data = instance client.get_instance.return_value = response cloud = OCICloud(**self.kwargs) cloud.running_instance_id = 'instance-123' cloud.compute_client = client result = cloud._is_instance_running() assert result
def test_oci_exception_required_args(self): """Test an exception is raised if required args missing.""" # Test missing availability domain with pytest.raises(OCICloudException): OCICloud(**self.kwargs) self.kwargs['availability_domain'] = 'Omic:PHX-AD-1' # Test missing compartment id with pytest.raises(OCICloudException): OCICloud(**self.kwargs) self.kwargs['compartment_id'] = ( 'ocid1.compartment.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') # Test missing ssh private key file with pytest.raises(OCICloudException): OCICloud(**self.kwargs)
def test_oci_create_vcn(self, mock_init): """Test oci create vcn method.""" mock_init.return_value = None client = MagicMock() vcn = MagicMock() response = MagicMock() response.data = vcn client.create_vcn_and_wait_for_state.return_value = response cloud = OCICloud(**self.kwargs) cloud.compartment_id = ( 'ocid1.compartment.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') cloud.vnet_composite_client = client result = cloud._create_vcn('vcn-123') assert result == vcn
def test_oci_get_vcn(self, mock_init): """Test oci get vcn method.""" mock_init.return_value = None client = MagicMock() vcn = MagicMock() response = MagicMock() response.data = vcn client.get_vcn.return_value = response cloud = OCICloud(**self.kwargs) cloud.vnet_client = client vcn_id = ( 'ocid1.vcn.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') result = cloud._get_vcn(vcn_id) assert result == vcn
def test_oci_get_subnet(self, mock_init): """Test oci get subnet method.""" mock_init.return_value = None client = MagicMock() subnet = MagicMock() response = MagicMock() response.data = subnet client.get_subnet.return_value = response cloud = OCICloud(**self.kwargs) cloud.vnet_client = client subnet_id = ( 'ocid1.subnet.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') result = cloud._get_subnet(subnet_id) assert result == subnet
def test_oci_get_vnic_attachments(self, mock_init, mock_oci): """Test oci get vnic attachments method.""" mock_init.return_value = None client = MagicMock() vnic = MagicMock() response = MagicMock() response.data = [vnic] mock_oci.pagination.list_call_get_all_results.return_value = response cloud = OCICloud(**self.kwargs) cloud.compute_client = client compartment_id = ( 'ocid1.compartment.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') instance_id = ( 'ocid1.instance.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') cloud._get_vnic_attachments(compartment_id, instance_id)
def test_stop_instance(self, mock_init, mock_wait_on_instance): """Test oci stop instance method.""" mock_init.return_value = None client = MagicMock() cloud = OCICloud(**self.kwargs) cloud.compute_client = client cloud.running_instance_id = 'instance123' cloud.timeout = 600 cloud._stop_instance()
def test_oci_init(self, mock_oci): """Test oci init method and config setup is successful.""" client = MagicMock() mock_oci.core.ComputeClient.return_value = client mock_oci.core.ComputeClientCompositeOperations.return_value = client mock_oci.core.VirtualNetworkClient.return_value = client mock_oci.core.VirtualNetworkClientCompsiteOperations.return_value = client # noqa self.kwargs['availability_domain'] = 'Omic:PHX-AD-1' self.kwargs['compartment_id'] = ( 'ocid1.compartment.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') self.kwargs['ssh_private_key_file'] = 'tests/oci/api_key.pem' OCICloud(**self.kwargs)
def test_get_console_log(self, mock_init): """Test oci get console log method.""" mock_init.return_value = None response = MagicMock() response.data = b'Test output' client = MagicMock() client.get_console_history_content.return_value = response cloud = OCICloud(**self.kwargs) cloud.compute_client = client cloud.compute_composite_client = client cloud.running_instance_id = 'instance-123' log = cloud.get_console_log() assert log == 'Test output'
def test_oci_create_internet_gateway(self, mock_init): """Test oci create internet gateway method.""" mock_init.return_value = None client = MagicMock() vcn = MagicMock() route_table = MagicMock() vcn.id = ( 'ocid1.vcn.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ) vcn.default_route_table_id = ( 'ocid1.routeTable.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ) gateway = MagicMock() gateway.id = ( 'ocid1.gateway.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ) gateway_response = MagicMock() gateway_response.data = gateway client.create_internet_gateway_and_wait_for_state.return_value = gateway_response # noqa route_table_response = MagicMock() route_table_response.data = route_table route_table.route_rules = [] client.get_route_table.return_value = route_table_response update_table_response = MagicMock() update_table_response.data = [] client.update_route_table_and_wait_for_state.return_value = update_table_response # noqa cloud = OCICloud(**self.kwargs) cloud.vnet_composite_client = client cloud.vnet_client = client compartment_id = ( 'ocid1.compartment.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ) cloud._create_internet_gateway(compartment_id, vcn, 'test-name')
def test_terminate_instance(self, mock_init, mock_get_vcn, mock_get_subnet, mock_delete_subnet, mock_clear_route_rules, mock_get_gateway, mock_delete_gateway, mock_delete_vcn): """Test oci terminate instance method.""" mock_init.return_value = None client = MagicMock() vcn = MagicMock() vcn.id = 'vcn123' mock_get_vcn.return_value = vcn subnet = MagicMock() subnet.id = 'subnet123' mock_get_subnet.return_value = subnet gateway = MagicMock() gateway.id = 'gateway123' mock_get_gateway.return_value = gateway cloud = OCICloud(**self.kwargs) cloud.compute_composite_client = client cloud.running_instance_id = 'instance123' cloud.display_name = 'oci-ipa-test' cloud.timeout = 600 cloud._terminate_instance() # No VCN mock_get_vcn.return_value = None cloud._terminate_instance() # Display name not created by img-proof cloud.display_name = 'not-ipa-test' cloud._terminate_instance()
def test_image( cloud_name, accelerated_networking=None, access_key_id=None, account=None, cleanup=None, config=None, description=None, distro=None, early_exit=None, history_log=None, image_id=None, image_project=None, inject=None, instance_type=None, ip_address=None, log_level=None, no_default_test_dirs=None, cloud_config=None, region=None, results_dir=None, running_instance_id=None, secret_access_key=None, security_group_id=None, service_account_file=None, ssh_key_name=None, ssh_private_key_file=None, ssh_user=None, subnet_id=None, test_dirs=None, tests=None, timeout=None, vnet_name=None, vnet_resource_group=None, collect_vm_info=None, compartment_id=None, availability_domain=None, signing_key_fingerprint=None, signing_key_file=None, tenancy=None, oci_user_id=None, enable_secure_boot=None, enable_uefi=None, log_callback=None, prefix_name=None, retry_count=None ): """Creates a cloud framework instance and initiates testing.""" kwargs = { 'cleanup': cleanup, 'config': config, 'description': description, 'distro_name': distro, 'early_exit': early_exit, 'history_log': history_log, 'image_id': image_id, 'inject': inject, 'instance_type': instance_type, 'log_level': log_level, 'no_default_test_dirs': no_default_test_dirs, 'cloud_config': cloud_config, 'region': region, 'results_dir': results_dir, 'running_instance_id': running_instance_id, 'ssh_private_key_file': ssh_private_key_file, 'ssh_user': ssh_user, 'subnet_id': subnet_id, 'test_dirs': test_dirs, 'test_files': tests, 'timeout': timeout, 'collect_vm_info': collect_vm_info, 'enable_secure_boot': enable_secure_boot, 'enable_uefi': enable_uefi, 'log_callback': log_callback, 'prefix_name': prefix_name, 'retry_count': retry_count } cloud_name = cloud_name.lower() if cloud_name == 'azure': cloud = AzureCloud( accelerated_networking=accelerated_networking, service_account_file=service_account_file, vnet_name=vnet_name, vnet_resource_group=vnet_resource_group, **kwargs ) elif cloud_name == 'ec2': cloud = EC2Cloud( access_key_id=access_key_id, account_name=account, secret_access_key=secret_access_key, security_group_id=security_group_id, ssh_key_name=ssh_key_name, **kwargs ) elif cloud_name == 'gce': cloud = GCECloud( service_account_file=service_account_file, image_project=image_project, **kwargs ) elif cloud_name == 'ssh': cloud = SSHCloud( ip_address=ip_address, **kwargs ) elif cloud_name == 'oci': cloud = OCICloud( compartment_id=compartment_id, availability_domain=availability_domain, signing_key_fingerprint=signing_key_fingerprint, signing_key_file=signing_key_file, tenancy=tenancy, oci_user_id=oci_user_id, **kwargs ) else: raise IpaControllerException( 'Cloud framework: %s unavailable.' % cloud_name ) return cloud.test_image()
def test_launch_instance(self, mock_init, mock_create_vcn, mock_create_subnet, mock_create_ig, mock_get_ssh_key, mock_terminate_instance, mock_wait_on_instance): """Test oci launch instance method.""" mock_init.return_value = None logger = MagicMock() vcn = MagicMock() mock_create_vcn.return_value = vcn instance = MagicMock() instance.id = 'instance-123' response = MagicMock() response.data = instance client = MagicMock() client.launch_instance.return_value = response subnet = MagicMock() subnet.id = 'subnet-123' mock_create_subnet.return_value = subnet mock_get_ssh_key.return_value = 'key123' cloud = OCICloud(**self.kwargs) cloud.compute_client = client cloud.compartment_id = ( 'ocid1.compartment.oc1..' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') cloud.availability_domain = 'Omic:PHX-AD-1' cloud.timeout = 600 cloud.subnet_id = None cloud.instance_type = None cloud.image_id = 'image123' cloud.logger = logger cloud._launch_instance() # Test exception case client.launch_instance.side_effect = Exception('Broken') mock_terminate_instance.side_effect = Exception( 'Cannot terminate instance') with pytest.raises(Exception): cloud._launch_instance()