def test_ec2_is_instance_running(self, mock_get_instance_state): """Test ec2 provider is instance runnning method.""" mock_get_instance_state.return_value = 'running' provider = EC2Cloud(**self.kwargs) assert provider._is_instance_running() assert mock_get_instance_state.call_count == 1 mock_get_instance_state.return_value = 'stopped' mock_get_instance_state.reset_mock() provider = EC2Cloud(**self.kwargs) assert not provider._is_instance_running() assert mock_get_instance_state.call_count == 1
def test_ec2_terminate_instance(self, mock_get_instance): """Test ec2 terminate instance method.""" instance = MagicMock() instance.terminate.return_value = None mock_get_instance.return_value = instance provider = EC2Cloud(**self.kwargs) provider._terminate_instance() assert mock_get_instance.call_count == 1
def test_ec2_get_instance(self, mock_connect): """Test get instance method.""" instance = MagicMock() resource = MagicMock() resource.Instance.return_value = instance mock_connect.return_value = resource provider = EC2Cloud(**self.kwargs) val = provider._get_instance() assert val == instance assert mock_connect.call_count == 1
def test_ec2_set_image_id(self, mock_get_instance): """Test ec2 provider set image id method.""" instance = MagicMock() instance.image_id = 'ami-123456' mock_get_instance.return_value = instance provider = EC2Cloud(**self.kwargs) provider._set_image_id() assert provider.image_id == instance.image_id assert mock_get_instance.call_count == 1
def test_ec2_exception_required_args(self): """Test an exception is raised if required args missing.""" self.kwargs['config'] = 'tests/data/config.noregion' msg = 'Region is required to connect to EC2.' # Test region required with pytest.raises(EC2CloudException) as error: EC2Cloud(**self.kwargs) assert str(error.value) == msg self.kwargs['cloud_config'] = 'tests/ec2/.ec2utils.conf.nokey' self.kwargs['config'] = 'tests/data/config' msg = 'SSH private key file is required to connect to instance.' # Test ssh private key file required with pytest.raises(EC2CloudException) as error: EC2Cloud(**self.kwargs) assert str(error.value) == msg self.kwargs['cloud_config'] = 'tests/ec2/.ec2utils.conf'
def test_ec2_get_console_log(self, mock_get_instance, mock_time): """Test ec2 get console log method.""" instance = MagicMock() instance.console_output.return_value = { 'Output': 'Console log output...' } mock_get_instance.return_value = instance provider = EC2Cloud(**self.kwargs) output = provider.get_console_log() assert output == 'Console log output...' assert mock_get_instance.call_count == 1
def test_ec2_stop_instance(self, mock_get_instance, mock_wait_on_instance): """Test ec2 stop instance method.""" instance = MagicMock() instance.stop.return_value = None instance.wait_until_stopped.return_value = None mock_get_instance.return_value = instance provider = EC2Cloud(**self.kwargs) provider._stop_instance() mock_wait_on_instance.assert_called_once_with('stopped', 600) assert mock_get_instance.call_count == 1
def test_ec2_bad_connection(self, mock_boto3): """Test an exception is raised if boto3 unable to connect.""" mock_boto3.side_effect = Exception('ERROR!') provider = EC2Cloud(**self.kwargs) msg = 'Could not connect to region: %s' % provider.region # Test ssh private key file required with pytest.raises(EC2CloudException) as error: provider._connect() assert str(error.value) == msg assert mock_boto3.call_count > 0
def test_ec2_get_instance_error(self, mock_connect): """Test get instance method error.""" resource = MagicMock() resource.Instance.side_effect = Exception('Error!') mock_connect.return_value = resource provider = EC2Cloud(**self.kwargs) provider.running_instance_id = 'i-123456789' with pytest.raises(EC2CloudException) as error: provider._get_instance() msg = 'Instance with ID: i-123456789 not found.' assert str(error.value) == msg
def test_ec2_launch_instance(self, mock_connect, mock_wait_on_instance): """Test ec2 provider launch instance method.""" instance = MagicMock() instance.instance_id = 'i-123456789' instances = [instance] resource = MagicMock() resource.create_instances.return_value = instances mock_connect.return_value = resource provider = EC2Cloud(**self.kwargs) provider.subnet_id = 'subnet-123456789' provider.security_group_id = 'sg-123456789' provider._launch_instance() mock_wait_on_instance.assert_called_once_with('running', 600) assert instance.instance_id == provider.running_instance_id assert resource.create_instances.call_count == 1
def test_ec2_set_instance_ip(self, mock_get_instance): """Test ec2 provider set image id method.""" instance = MagicMock() instance.public_ip_address = None instance.private_ip_address = None instance.network_interfaces = [] mock_get_instance.return_value = instance provider = EC2Cloud(**self.kwargs) msg = 'IP address for instance cannot be found.' with pytest.raises(EC2CloudException) as error: provider._set_instance_ip() assert str(error.value) == msg assert mock_get_instance.call_count == 1 mock_get_instance.reset_mock() instance.private_ip_address = '127.0.0.1' provider._set_instance_ip() assert provider.instance_ip == '127.0.0.1' assert mock_get_instance.call_count == 1 mock_get_instance.reset_mock() network_interface = MagicMock() network_interface.ipv6_addresses = ['127.0.0.2'] instance.network_interfaces = [network_interface] provider._set_instance_ip() assert provider.instance_ip == '127.0.0.2' assert mock_get_instance.call_count == 1 mock_get_instance.reset_mock() instance.public_ip_address = '127.0.0.3' provider._set_instance_ip() assert provider.instance_ip == '127.0.0.3' assert mock_get_instance.call_count == 1
def test_ec2_get_instance_state(self, mock_get_instance): """Test an exception is raised if boto3 unable to connect.""" instance = MagicMock() instance.state = {'Name': 'ReadyRole'} mock_get_instance.return_value = instance provider = EC2Cloud(**self.kwargs) val = provider._get_instance_state() assert val == 'ReadyRole' assert mock_get_instance.call_count == 1 instance.state = {} mock_get_instance.reset_mock() msg = 'Instance with id: %s, cannot be found.' \ % provider.running_instance_id # Test exception raised if instance state not found with pytest.raises(EC2CloudException) as error: provider._get_instance_state() assert str(error.value) == msg assert mock_get_instance.call_count == 1
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()