class TestGetVmDetails(TestCase): def setUp(self): self.vmDetailsProvider = VmDetailsProvider() def test_get_vm_details(self): shell = AWSShell() requests_json = encode({'items': [{'deployedAppJson': { 'name': 'something', 'vmdetails': {'uid': '514'}}}]}) shell.get_vm_details(Mock(), Mock(), requests_json) def test_get_vm_details_name_exist(self): shell = AWSShell() requests_json = encode({'items': [{'deployedAppJson': { 'name': 'something', 'vmdetails': {'uid': '514'}}}]}) cancellation_context = Mock() cancellation_context.is_cancelled = False res = shell.get_vm_details(Mock(), cancellation_context, requests_json) res_list = jsonpickle.decode(res) self.assertTrue(res_list[0][u'appName'] == 'something') def test_get_volume_when_empty(self): instance = Mock() instance.volumes.all = lambda: [] volume = self.vmDetailsProvider._get_volume(instance) self.assertTrue(volume is None) def test_get_volume(self): volume1 = 'hi' instance = Mock() instance.volumes.all = lambda: [volume1] volume = self.vmDetailsProvider._get_volume(instance) self.assertTrue(volume==volume1) def test_get_volume_size(self): volume = Mock() volume.size = 10 size = self.vmDetailsProvider._get_volume_size(volume) self.assertTrue(size == '10 GiB') volume = None size = self.vmDetailsProvider._get_volume_size(volume) self.assertTrue(size is None) def test_get_volume_type(self): volume = Mock() volume.volume_type = 'hi' volume_type = self.vmDetailsProvider._get_volume_type(volume) self.assertTrue(volume_type == 'hi') volume = None volume_type = self.vmDetailsProvider._get_volume_type(volume) self.assertTrue(volume_type is None) def test_get_volume_id(self): volume = Mock() volume.volume_id = 'hi' volume_id = self.vmDetailsProvider._get_volume_id(volume) self.assertTrue(volume_id == 'hi') volume = None volume_id = self.vmDetailsProvider._get_volume_id(volume) self.assertTrue(volume_id is None)
def setUp(self): self.vm_details_provider = VmDetailsProvider()
class TestVmDetailsProvider(TestCase): def setUp(self): self.vm_details_provider = VmDetailsProvider() def test_prepare_vm_details(self): instance = Mock() instance.image_id = 'image_id' instance.instance_type = 'instance_type' instance.platform = 'instance_platform' instance.network_interfaces = [] instance.volumes.all = lambda: [] instance.iam_instance_profile = {"Arn": "arn:aws:iam::admin_role"} vm_instance_data = self.vm_details_provider.create( instance).vmInstanceData self.assertTrue( self._get_value(vm_instance_data, 'AMI ID') == instance.image_id) self.assertTrue( self._get_value(vm_instance_data, 'instance type') == instance.instance_type) self.assertTrue( self._get_value(vm_instance_data, 'platform') == instance.platform) self.assertTrue( self._get_value(vm_instance_data, 'IAM Role') == instance.iam_instance_profile['Arn']) def test_prepare_network_interface_objects_with_elastic_ip(self): # elastic_ip network_interface = Mock() network_interface.association_attribute = { 'IpOwnerId': '9929230', 'PublicIp': 'public_ip' } network_interface.network_interface_id = 'interface_id' network_interface.mac_address = 'mac_address' network_interface.subnet_id = 'subnet_id' network_interface.attachment = {'DeviceIndex': 0} network_interface.private_ip_address = 'private_ip' instance = Mock() instance.network_interfaces = [network_interface] network_interface_objects = self.vm_details_provider._get_vm_network_data( instance) nio = network_interface_objects[0] self.assertTrue(nio.interfaceId == 'interface_id') self.assertTrue(nio.networkId == 'subnet_id') self.assertTrue(nio.isPrimary == True) nio_data = nio.networkData self.assertTrue( self._get_value(nio_data, 'MAC Address') == 'mac_address') self.assertTrue(self._get_value(nio_data, 'Elastic IP') == True) self.assertTrue(self._get_value(nio_data, 'IP') == 'private_ip') self.assertTrue(self._get_value(nio_data, 'Public IP') == 'public_ip') def test_prepare_network_interface_objects_with_public_ip(self): network_interface = Mock() network_interface.association_attribute = dict() network_interface.network_interface_id = 'interface_id' network_interface.mac_address = 'mac_address' network_interface.subnet_id = 'subnet_id' network_interface.attachment = {'DeviceIndex': 0} network_interface.private_ip_address = 'private_ip' instance = Mock() instance.public_ip_address = 'public_ip' instance.network_interfaces = [network_interface] network_interface_objects = self.vm_details_provider._get_vm_network_data( instance) nio = network_interface_objects[0] self.assertTrue(nio.interfaceId == 'interface_id') self.assertTrue(nio.networkId == 'subnet_id') self.assertTrue(nio.isPrimary == True) nio_data = nio.networkData self.assertTrue( self._get_value(nio_data, 'MAC Address') == 'mac_address') self.assertTrue(self._get_value(nio_data, 'Elastic IP') == False) self.assertTrue(self._get_value(nio_data, 'IP') == 'private_ip') self.assertTrue(self._get_value(nio_data, 'Public IP') == '') def test_prepare_network_interface_objects_without_public_ip(self): network_interface = Mock() network_interface.association_attribute = dict() network_interface.network_interface_id = 'interface_id' network_interface.mac_address = 'mac_address' network_interface.subnet_id = 'subnet_id' network_interface.attachment = {'DeviceIndex': 1} network_interface.private_ip_address = 'private_ip' instance = Mock() instance.network_interfaces = [network_interface] network_interface_objects = self.vm_details_provider._get_vm_network_data( instance) nio = network_interface_objects[0] self.assertTrue(nio.interfaceId == 'interface_id') self.assertTrue(nio.networkId == 'subnet_id') self.assertTrue(nio.isPrimary == False) nio_data = nio.networkData self.assertTrue( self._get_value(nio_data, 'MAC Address') == 'mac_address') self.assertTrue(self._get_value(nio_data, 'Elastic IP') == False) self.assertTrue(self._get_value(nio_data, 'IP') == 'private_ip') self.assertTrue(self._get_value(nio_data, 'Public IP') == "") def _get_value(self, data, key): for item in data: if item.key == key: return item.value return None
def __init__(self): self.image_waiter = AMIWaiter() self.command_result_parser = CommandResultsParser() self.cancellation_service = CommandCancellationService() self.client_err_wrapper = ClientErrorWrapper() self.tag_service = TagService( client_err_wrapper=self.client_err_wrapper) self.ec2_instance_waiter = InstanceWaiter( cancellation_service=self.cancellation_service) self.instance_service = InstanceService(self.tag_service, self.ec2_instance_waiter) self.ec2_storage_service = EC2StorageService() self.model_parser = AWSModelsParser() self.cloudshell_session_helper = CloudshellDriverHelper() self.aws_session_manager = AWSSessionProvider() self.password_waiter = PasswordWaiter(self.cancellation_service) self.vm_custom_params_extractor = VmCustomParamsExtractor() self.ami_credentials_service = InstanceCredentialsService( self.password_waiter) self.security_group_service = SecurityGroupService(self.tag_service) self.subnet_waiter = SubnetWaiter() self.subnet_service = SubnetService(self.tag_service, self.subnet_waiter) self.s3_service = S3BucketService() self.vpc_peering_waiter = VpcPeeringConnectionWaiter() self.key_pair_service = KeyPairService(self.s3_service) self.vpc_waiter = VPCWaiter() self.route_tables_service = RouteTablesService(self.tag_service) self.network_interface_service = NetworkInterfaceService( subnet_service=self.subnet_service, security_group_service=self.security_group_service, ) self.elastic_ip_service = ElasticIpService() self.vm_details_provider = VmDetailsProvider() self.session_number_service = SessionNumberService() self.traffic_mirror_service = TrafficMirrorService() self.request_parser = DriverRequestParser() self.vpc_service = VPCService( tag_service=self.tag_service, subnet_service=self.subnet_service, instance_service=self.instance_service, vpc_waiter=self.vpc_waiter, vpc_peering_waiter=self.vpc_peering_waiter, sg_service=self.security_group_service, route_table_service=self.route_tables_service, traffic_mirror_service=self.traffic_mirror_service, ) self.prepare_connectivity_operation = PrepareSandboxInfraOperation( vpc_service=self.vpc_service, security_group_service=self.security_group_service, key_pair_service=self.key_pair_service, tag_service=self.tag_service, route_table_service=self.route_tables_service, cancellation_service=self.cancellation_service, subnet_service=self.subnet_service, subnet_waiter=self.subnet_waiter, ) self.deploy_ami_operation = DeployAMIOperation( instance_service=self.instance_service, ami_credential_service=self.ami_credentials_service, security_group_service=self.security_group_service, tag_service=self.tag_service, vpc_service=self.vpc_service, key_pair_service=self.key_pair_service, subnet_service=self.subnet_service, elastic_ip_service=self.elastic_ip_service, network_interface_service=self.network_interface_service, cancellation_service=self.cancellation_service, device_index_strategy=AllocateMissingValuesDeviceIndexStrategy(), vm_details_provider=self.vm_details_provider, ) self.refresh_ip_operation = RefreshIpOperation( instance_service=self.instance_service) self.power_management_operation = PowerOperation( instance_service=self.instance_service, instance_waiter=self.ec2_instance_waiter, ) self.delete_ami_operation = DeleteAMIOperation( instance_service=self.instance_service, ec2_storage_service=self.ec2_storage_service, security_group_service=self.security_group_service, tag_service=self.tag_service, elastic_ip_service=self.elastic_ip_service, ) self.clean_up_operation = CleanupSandboxInfraOperation( vpc_service=self.vpc_service, key_pair_service=self.key_pair_service, route_table_service=self.route_tables_service, traffic_mirror_service=self.traffic_mirror_service, ) self.deployed_app_ports_operation = DeployedAppPortsOperation( self.vm_custom_params_extractor, security_group_service=self.security_group_service, instance_service=self.instance_service, ) self.access_key_operation = GetAccessKeyOperation( key_pair_service=self.key_pair_service) self.set_app_security_groups_operation = SetAppSecurityGroupsOperation( instance_service=self.instance_service, tag_service=self.tag_service, security_group_service=self.security_group_service, ) self.vm_details_operation = VmDetailsOperation( instance_service=self.instance_service, vm_details_provider=self.vm_details_provider, ) self.autoload_operation = AutoloadOperation() self.snapshot_operation = SnapshotOperation(self.instance_service, self.image_waiter) self.traffic_mirroring_operation = TrafficMirrorOperation( tag_service=self.tag_service, session_number_service=self.session_number_service, traffic_mirror_service=self.traffic_mirror_service, cancellation_service=self.cancellation_service, )
class TestVmDetailsProvider(TestCase): def setUp(self): self.vm_details_provider = VmDetailsProvider() def test_prepare_vm_details(self): instance = Mock() instance.image_id = 'image_id' instance.instance_type = 'instance_type' instance.platform = 'instance_platform' instance.network_interfaces = [] instance.volumes.all = lambda: [] instance.iam_instance_profile = {"Arn": "arn:aws:iam::admin_role"} vm_instance_data = self.vm_details_provider.create(instance).vmInstanceData self.assertTrue(self._get_value(vm_instance_data, 'AMI ID') == instance.image_id) self.assertTrue(self._get_value(vm_instance_data, 'instance type') == instance.instance_type) self.assertTrue(self._get_value(vm_instance_data, 'platform') == instance.platform) self.assertTrue(self._get_value(vm_instance_data, 'IAM Role') == instance.iam_instance_profile['Arn']) def test_prepare_network_interface_objects_with_elastic_ip(self): # elastic_ip network_interface = Mock() network_interface.association_attribute = {'IpOwnerId': '9929230', 'PublicIp': 'public_ip'} network_interface.network_interface_id = 'interface_id' network_interface.mac_address = 'mac_address' network_interface.subnet_id = 'subnet_id' network_interface.attachment = {'DeviceIndex': 0} network_interface.private_ip_address = 'private_ip' instance = Mock() instance.network_interfaces = [ network_interface ] network_interface_objects = self.vm_details_provider._get_vm_network_data(instance) nio = network_interface_objects[0] self.assertTrue(nio.interfaceId == 'interface_id') self.assertTrue(nio.networkId == 'subnet_id') self.assertTrue(nio.isPrimary == True) nio_data = nio.networkData self.assertTrue(self._get_value(nio_data, 'MAC Address') == 'mac_address') self.assertTrue(self._get_value(nio_data, 'Elastic IP') == True) self.assertTrue(self._get_value(nio_data, 'IP') == 'private_ip') self.assertTrue(self._get_value(nio_data, 'Public IP') == 'public_ip') def test_prepare_network_interface_objects_with_public_ip(self): network_interface = Mock() network_interface.association_attribute = dict() network_interface.network_interface_id = 'interface_id' network_interface.mac_address = 'mac_address' network_interface.subnet_id = 'subnet_id' network_interface.attachment = {'DeviceIndex': 0} network_interface.private_ip_address = 'private_ip' instance = Mock() instance.public_ip_address = 'public_ip' instance.network_interfaces = [ network_interface ] network_interface_objects = self.vm_details_provider._get_vm_network_data(instance) nio = network_interface_objects[0] self.assertTrue(nio.interfaceId == 'interface_id') self.assertTrue(nio.networkId == 'subnet_id') self.assertTrue(nio.isPrimary == True) nio_data = nio.networkData self.assertTrue(self._get_value(nio_data, 'MAC Address') == 'mac_address') self.assertTrue(self._get_value(nio_data, 'Elastic IP') == False) self.assertTrue(self._get_value(nio_data, 'IP') == 'private_ip') self.assertTrue(self._get_value(nio_data, 'Public IP') == '') def test_prepare_network_interface_objects_without_public_ip(self): network_interface = Mock() network_interface.association_attribute = dict() network_interface.network_interface_id = 'interface_id' network_interface.mac_address = 'mac_address' network_interface.subnet_id = 'subnet_id' network_interface.attachment = {'DeviceIndex': 1} network_interface.private_ip_address = 'private_ip' instance = Mock() instance.network_interfaces = [ network_interface ] network_interface_objects = self.vm_details_provider._get_vm_network_data(instance) nio = network_interface_objects[0] self.assertTrue(nio.interfaceId == 'interface_id') self.assertTrue(nio.networkId == 'subnet_id') self.assertTrue(nio.isPrimary == False) nio_data = nio.networkData self.assertTrue(self._get_value(nio_data, 'MAC Address') == 'mac_address') self.assertTrue(self._get_value(nio_data, 'Elastic IP') == False) self.assertTrue(self._get_value(nio_data, 'IP') == 'private_ip') self.assertTrue(self._get_value(nio_data, 'Public IP') == "") def _get_value(self, data, key): for item in data: if item.key == key: return item.value return None
class TestVmDetailsProvider(TestCase): def setUp(self): self.vm_details_provider = VmDetailsProvider() def test_prepare_vm_details(self): instance = Mock() instance.image_id = 'image_id' instance.instance_type = 'instance_type' instance.platform = 'instance_platform' instance.network_interfaces = [] vm_instance_data = self.vm_details_provider.create( instance).vm_instance_data self.assertTrue(vm_instance_data['ami id'] == instance.image_id) self.assertTrue( vm_instance_data['instance type'] == instance.instance_type) self.assertTrue(vm_instance_data['platform'] == instance.platform) def test_prepare_network_interface_objects_with_elastic_ip(self): # elastic_ip network_interface = Mock() network_interface.association_attribute = { 'IpOwnerId': '9929230', 'PublicIp': 'public_ip' } network_interface.network_interface_id = 'interface_id' network_interface.mac_address = 'mac_address' network_interface.subnet_id = 'subnet_id' network_interface.attachment = {'DeviceIndex': 0} network_interface.private_ip_address = 'private_ip' instance = Mock() instance.network_interfaces = [network_interface] network_interface_objects = self.vm_details_provider._get_vm_network_data( instance) nio = network_interface_objects[0] self.assertTrue(nio['interface_id'] == 'interface_id') self.assertTrue(nio['network_id'] == 'subnet_id') self.assertTrue(nio['is_primary'] == True) self.assertTrue(nio['network_data']['mac address'] == 'mac_address') self.assertTrue(nio['network_data']['device index'] == 0) self.assertTrue(nio['network_data']['elastic ip'] == True) self.assertTrue(nio['network_data']['ip'] == 'private_ip') self.assertTrue(nio['network_data']['public ip'] == 'public_ip') def test_prepare_network_interface_objects_with_public_ip(self): network_interface = Mock() network_interface.association_attribute = dict() network_interface.network_interface_id = 'interface_id' network_interface.mac_address = 'mac_address' network_interface.subnet_id = 'subnet_id' network_interface.attachment = {'DeviceIndex': 0} network_interface.private_ip_address = 'private_ip' instance = Mock() instance.public_ip_address = 'public_ip' instance.network_interfaces = [network_interface] network_interface_objects = self.vm_details_provider._get_vm_network_data( instance) nio = network_interface_objects[0] self.assertTrue(nio['interface_id'] == 'interface_id') self.assertTrue(nio['network_id'] == 'subnet_id') self.assertTrue(nio['is_primary'] == True) self.assertTrue(nio['network_data']['mac address'] == 'mac_address') self.assertTrue(nio['network_data']['device index'] == 0) self.assertTrue('is_elastic_ip' not in nio['network_data']) self.assertTrue(nio['network_data']['ip'] == 'private_ip') self.assertTrue('public_ip' not in nio['network_data']) def test_prepare_network_interface_objects_without_public_ip(self): network_interface = Mock() network_interface.association_attribute = dict() network_interface.network_interface_id = 'interface_id' network_interface.mac_address = 'mac_address' network_interface.subnet_id = 'subnet_id' network_interface.attachment = {'DeviceIndex': 1} network_interface.private_ip_address = 'private_ip' instance = Mock() instance.network_interfaces = [network_interface] network_interface_objects = self.vm_details_provider._get_vm_network_data( instance) nio = network_interface_objects[0] self.assertTrue(nio['interface_id'] == 'interface_id') self.assertTrue(nio['network_id'] == 'subnet_id') self.assertTrue('is_primary' not in nio) self.assertTrue(nio['network_data']['mac address'] == 'mac_address') self.assertTrue(nio['network_data']['device index'] == 1) self.assertTrue(nio['network_data']['elastic ip'] == False) self.assertTrue(nio['network_data']['ip'] == 'private_ip') self.assertTrue(nio['network_data']['public ip'] == "")
class TestGetVmDetails(TestCase): def setUp(self): self.vmDetailsProvider = VmDetailsProvider() def test_get_vm_details(self): shell = AWSShell() requests_json = encode({ "items": [{ "deployedAppJson": { "name": "something", "vmdetails": { "uid": "514" }, } }] }) shell.get_vm_details(Mock(), Mock(), requests_json) def test_get_vm_details_name_exist(self): shell = AWSShell() requests_json = encode({ "items": [{ "deployedAppJson": { "name": "something", "vmdetails": { "uid": "514" }, } }] }) cancellation_context = Mock() cancellation_context.is_cancelled = False res = shell.get_vm_details(Mock(), cancellation_context, requests_json) res_list = jsonpickle.decode(res) self.assertTrue(res_list[0]["appName"] == "something") def test_get_volume_when_empty(self): instance = Mock() instance.volumes.all = lambda: [] volume = self.vmDetailsProvider._get_volume(instance) self.assertTrue(volume is None) def test_get_volume(self): volume1 = "hi" instance = Mock() instance.volumes.all = lambda: [volume1] volume = self.vmDetailsProvider._get_volume(instance) self.assertTrue(volume == volume1) def test_get_volume_size(self): volume = Mock() volume.size = 10 size = self.vmDetailsProvider._get_volume_size(volume) self.assertTrue(size == "10 GiB") volume = None size = self.vmDetailsProvider._get_volume_size(volume) self.assertTrue(size is None) def test_get_volume_type(self): volume = Mock() volume.volume_type = "hi" volume_type = self.vmDetailsProvider._get_volume_type(volume) self.assertTrue(volume_type == "hi") volume = None volume_type = self.vmDetailsProvider._get_volume_type(volume) self.assertTrue(volume_type is None) def test_get_volume_id(self): volume = Mock() volume.volume_id = "hi" volume_id = self.vmDetailsProvider._get_volume_id(volume) self.assertTrue(volume_id == "hi") volume = None volume_id = self.vmDetailsProvider._get_volume_id(volume) self.assertTrue(volume_id is None)
class TestVmDetailsProvider(TestCase): def setUp(self): self.vm_details_provider = VmDetailsProvider() def test_prepare_vm_details(self): instance = Mock() instance.image_id = "image_id" instance.instance_type = "instance_type" instance.platform = "instance_platform" instance.network_interfaces = [] instance.volumes.all = lambda: [] instance.iam_instance_profile = {"Arn": "arn:aws:iam::admin_role"} vm_instance_data = self.vm_details_provider.create( instance).vmInstanceData self.assertTrue( self._get_value(vm_instance_data, "AMI ID") == instance.image_id) self.assertTrue( self._get_value(vm_instance_data, "instance type") == instance.instance_type) self.assertTrue( self._get_value(vm_instance_data, "platform") == instance.platform) self.assertTrue( self._get_value(vm_instance_data, "IAM Role") == instance.iam_instance_profile["Arn"]) def test_prepare_network_interface_objects_with_elastic_ip(self): # elastic_ip network_interface = Mock() network_interface.association_attribute = { "IpOwnerId": "9929230", "PublicIp": "public_ip", } network_interface.network_interface_id = "interface_id" network_interface.mac_address = "mac_address" network_interface.subnet_id = "subnet_id" network_interface.attachment = {"DeviceIndex": 0} network_interface.private_ip_address = "private_ip" instance = Mock() instance.network_interfaces = [network_interface] network_interface_objects = self.vm_details_provider._get_vm_network_data( instance) nio = network_interface_objects[0] self.assertTrue(nio.interfaceId == "interface_id") self.assertTrue(nio.networkId == "subnet_id") self.assertTrue(nio.isPrimary) nio_data = nio.networkData self.assertTrue( self._get_value(nio_data, "MAC Address") == "mac_address") self.assertTrue(self._get_value(nio_data, "Elastic IP")) self.assertTrue(self._get_value(nio_data, "IP") == "private_ip") self.assertTrue(self._get_value(nio_data, "Public IP") == "public_ip") def test_prepare_network_interface_objects_with_public_ip(self): network_interface = Mock() network_interface.association_attribute = {} network_interface.network_interface_id = "interface_id" network_interface.mac_address = "mac_address" network_interface.subnet_id = "subnet_id" network_interface.attachment = {"DeviceIndex": 0} network_interface.private_ip_address = "private_ip" instance = Mock() instance.public_ip_address = "public_ip" instance.network_interfaces = [network_interface] network_interface_objects = self.vm_details_provider._get_vm_network_data( instance) nio = network_interface_objects[0] self.assertTrue(nio.interfaceId == "interface_id") self.assertTrue(nio.networkId == "subnet_id") self.assertTrue(nio.isPrimary) nio_data = nio.networkData self.assertTrue( self._get_value(nio_data, "MAC Address") == "mac_address") self.assertTrue(self._get_value(nio_data, "Elastic IP") is False) self.assertTrue(self._get_value(nio_data, "IP") == "private_ip") self.assertTrue(self._get_value(nio_data, "Public IP") == "") def test_prepare_network_interface_objects_without_public_ip(self): network_interface = Mock() network_interface.association_attribute = {} network_interface.network_interface_id = "interface_id" network_interface.mac_address = "mac_address" network_interface.subnet_id = "subnet_id" network_interface.attachment = {"DeviceIndex": 1} network_interface.private_ip_address = "private_ip" instance = Mock() instance.network_interfaces = [network_interface] network_interface_objects = self.vm_details_provider._get_vm_network_data( instance) nio = network_interface_objects[0] self.assertTrue(nio.interfaceId == "interface_id") self.assertTrue(nio.networkId == "subnet_id") self.assertTrue(nio.isPrimary is False) nio_data = nio.networkData self.assertTrue( self._get_value(nio_data, "MAC Address") == "mac_address") self.assertTrue(self._get_value(nio_data, "Elastic IP") is False) self.assertTrue(self._get_value(nio_data, "IP") == "private_ip") self.assertTrue(self._get_value(nio_data, "Public IP") == "") def _get_value(self, data, key): for item in data: if item.key == key: return item.value return None