Exemplo n.º 1
0
 def __init__(self):
     self.aws_shell = AWSShell()
     self.request_parser = DriverRequestParser()
     self.request_parser.add_deployment_model(
         deployment_model_cls=DeployAWSEc2AMIInstanceResourceModel)
     deploy_name = "Amazon AWS Cloud Provider Shell 2G.Amazon AWS EC2 Instance 2G"
     self.deployments = {deploy_name: self.deploy_ami}
Exemplo n.º 2
0
    def setUp(self):
        self.aws_shell_api = AWSShell()

        self.aws_shell_api.credentials_manager = Mock()
        self.aws_shell_api.ec2_instance_waiter = Mock()
        self.aws_shell_api.cloudshell_session_helper = Mock()
        self.aws_shell_api.aws_session_manager.get_ec2_session = Mock(
            return_value=Mock())

        self.command_context = Mock()
        self.command_context.resource = Mock()
        self.command_context.remote_endpoints = []

        self.command_context.connectivity = Mock()
        self.command_context.connectivity.server_address = Mock()
        self.command_context.connectivity.admin_auth_token = Mock()

        self.command_context.reservation = Mock()
        self.command_context.reservation.domain = Mock()

        self.command_context.remote_reservation = Mock()
        self.command_context.remote_reservation.domain = Mock()

        self.aws_shell_api.model_parser.convert_to_aws_resource_model = Mock(
            return_value=(AWSEc2CloudProviderResourceModel()))
Exemplo n.º 3
0
 def __init__(self):
     """
     ctor must be without arguments, it is created with reflection at run time
     """
     self.aws_shell = AWSShell()
     self.request_parser = DriverRequestParser()
     self.request_parser.add_deployment_model(deployment_model_cls=DeployAWSEc2AMIInstanceResourceModel)
     self.deployments = dict()
     self.deployments['AWS EC2 Instance'] = self.deploy_ami
Exemplo n.º 4
0
    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_vm_details(self):
     shell = AWSShell()
     requests_json = encode({
         "items": [{
             "deployedAppJson": {
                 "name": "something",
                 "vmdetails": {
                     "uid": "514"
                 },
             }
         }]
     })
     shell.get_vm_details(Mock(), Mock(), requests_json)
Exemplo n.º 6
0
    def setUp(self):
        self.aws_shell = AWSShell()

        self.aws_shell.credentials_manager = Mock()
        self.aws_shell.ec2_storage_service = Mock()
        self.aws_shell.ec2_instance_waiter = Mock()
        self.aws_shell.cloudshell_session_helper = Mock()
        self.aws_shell.aws_session_manager.get_ec2_session = Mock(
            return_value=Mock())
        self.aws_shell.aws_session_manager.get_s3_session = Mock(
            return_value=Mock())
        self.aws_shell.aws_session_manager.get_ec2_client = Mock(
            return_value=Mock())

        self.command_context = Mock()
        self.command_context.resource = Mock()
        self.command_context.remote_endpoints = []

        self.command_context.connectivity = Mock()
        self.command_context.connectivity.server_address = Mock()
        self.command_context.connectivity.admin_auth_token = Mock()

        self.command_context.reservation = Mock()
        self.command_context.reservation.domain = Mock()

        self.command_context.remote_reservation = Mock()
        self.command_context.remote_reservation.domain = Mock()

        self.aws_shell.model_parser.convert_to_aws_resource_model = Mock(
            return_value=(AWSEc2CloudProviderResourceModel()))
        self.reservation_model = ReservationModel(
            self.command_context.reservation)
        self.aws_shell.model_parser.convert_to_reservation_model = Mock(
            return_value=self.reservation_model)

        self.expected_shell_context = Mock(spec=AwsShellContextModel)
        self.expected_shell_context.logger = Mock()
        self.expected_shell_context.cloudshell_session = Mock()
        self.expected_shell_context.aws_ec2_resource_model = Mock()
        self.expected_shell_context.aws_api = Mock()
        self.expected_shell_context.aws_api.ec2_session = Mock()
        self.expected_shell_context.aws_api.s3_session = Mock()
        self.expected_shell_context.aws_api.ec2_client = Mock()

        self.mock_context = Mock()
        self.mock_context.__enter__ = Mock(
            return_value=self.expected_shell_context)
        self.mock_context.__exit__ = Mock(return_value=False)
    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")
Exemplo n.º 8
0
class AWSShellDriver(ResourceDriverInterface):
    def cleanup(self):
        pass

    def __init__(self):
        """
        ctor must be without arguments, it is created with reflection at run time
        """
        self.aws_shell = AWSShell()
        pass

    def initialize(self, context):
        pass

    def deploy_ami(self, context, request):
        return self.aws_shell.deploy_ami(context, request)

    def PowerOn(self, context, ports):
        return self.aws_shell.power_on_ami(context)

    def PowerOff(self, context, ports):
        return self.aws_shell.power_off_ami(context)

    def PowerCycle(self, context, ports, delay):
        pass

    def remote_refresh_ip(self, context, ports, cancellation_context):
        pass

    def delete(self, context, ports):
        return self.aws_shell.delete_ami(context)

    def destroy_vm_only(self, context, ports):
        return self.aws_shell.delete_ami(context, False)

    def ApplyConnectivityChanges(self, context, ports, request):
        pass

    def PrepareConnectivityChanges(self, context, request):
        pass

    def get_inventory(self, context):
        pass
Exemplo n.º 9
0
    def setUp(self):
        self.aws_shell = AWSShell()

        self.aws_shell.credentials_manager = Mock()
        self.aws_shell.ec2_storage_service = Mock()
        self.aws_shell.ec2_instance_waiter = Mock()
        self.aws_shell.cloudshell_session_helper = Mock()
        self.aws_shell.aws_session_manager.get_ec2_session = Mock(return_value=Mock())
        self.aws_shell.aws_session_manager.get_s3_session = Mock(return_value=Mock())
        self.aws_shell.aws_session_manager.get_ec2_client = Mock(return_value=Mock())

        self.command_context = Mock()
        self.command_context.resource = Mock()
        self.command_context.remote_endpoints = []

        self.command_context.connectivity = Mock()
        self.command_context.connectivity.server_address = Mock()
        self.command_context.connectivity.admin_auth_token = Mock()

        self.command_context.reservation = Mock()
        self.command_context.reservation.domain = Mock()

        self.command_context.remote_reservation = Mock()
        self.command_context.remote_reservation.domain = Mock()

        self.aws_shell.model_parser.convert_to_aws_resource_model = Mock(
                return_value=(AWSEc2CloudProviderResourceModel()))
        self.reservation_model = ReservationModel(self.command_context.reservation)
        self.aws_shell.model_parser.convert_to_reservation_model = Mock(
                return_value=self.reservation_model)

        self.expected_shell_context = Mock(spec=AwsShellContextModel)
        self.expected_shell_context.logger = Mock()
        self.expected_shell_context.cloudshell_session = Mock()
        self.expected_shell_context.aws_ec2_resource_model = Mock()
        self.expected_shell_context.aws_api = Mock()
        self.expected_shell_context.aws_api.ec2_session = Mock()
        self.expected_shell_context.aws_api.s3_session = Mock()
        self.expected_shell_context.aws_api.ec2_client = Mock()

        self.mock_context = Mock()
        self.mock_context.__enter__ = Mock(return_value=self.expected_shell_context)
        self.mock_context.__exit__ = Mock(return_value=False)
Exemplo n.º 10
0
class AWSShellDriver(ResourceDriverInterface):
    def cleanup(self):
        pass

    def __init__(self):
        """
        ctor must be without arguments, it is created with reflection at run time
        """
        self.aws_shell = AWSShell()
        self.request_parser = DriverRequestParser()
        self.request_parser.add_deployment_model(deployment_model_cls=DeployAWSEc2AMIInstanceResourceModel)
        self.deployments = dict()
        self.deployments['AWS EC2 Instance'] = self.deploy_ami

    def initialize(self, context):
        pass

    def Deploy(self, context, request=None, cancellation_context=None):
        actions = self.request_parser.convert_driver_request_to_actions(request)
        deploy_action = single(actions, lambda x: isinstance(x, DeployApp))
        deployment_name = deploy_action.actionParams.deployment.deploymentPath
        self.parse_vnicename(actions)


        if deployment_name in self.deployments.keys():
            deploy_method = self.deployments[deployment_name]
            deploy_result = deploy_method(context, actions, cancellation_context)
            return DriverResponse(deploy_result).to_driver_response_json()
        else:
            raise Exception('Could not find the deployment')

    def parse_vnicename(self, actions):
        network_actions = [a for a in actions if isinstance(a, ConnectSubnet)]
        for network_action in network_actions:
            try:
                network_action.actionParams.vnicName = int(network_action.actionParams.vnicName)
            except:
                network_action.actionParams.vnicName = None

    def deploy_ami(self, context, actions, cancellation_context):
        return self.aws_shell.deploy_ami(context, actions, cancellation_context)

    def PowerOn(self, context, ports):
        return self.aws_shell.power_on_ami(context)

    def PowerOff(self, context, ports):
        return self.aws_shell.power_off_ami(context)

    def PowerCycle(self, context, ports, delay):
        pass

    def remote_refresh_ip(self, context, ports, cancellation_context):
        return self.aws_shell.refresh_ip(context)

    def DeleteInstance(self, context, ports):
        return self.aws_shell.delete_instance(context)

    def PrepareSandboxInfra(self, context, request, cancellation_context):
        actions = self.request_parser.convert_driver_request_to_actions(request)
        action_results = self.aws_shell.prepare_connectivity(context, actions, cancellation_context)
        return DriverResponse(action_results).to_driver_response_json()

    def CleanupSandboxInfra(self, context, request):
        actions = self.request_parser.convert_driver_request_to_actions(request)
        return self.aws_shell.cleanup_connectivity(context, actions)

    def GetApplicationPorts(self, context, ports):
        return self.aws_shell.get_application_ports(context)

    def get_inventory(self, context):
        return AutoLoadDetails([], [])

    def GetAccessKey(self, context, ports):
        return self.aws_shell.get_access_key(context)

    def SetAppSecurityGroups(self, context, request):
        return self.aws_shell.set_app_security_groups(context, request)

    def GetVmDetails(self, context, cancellation_context, requests):
        return self.aws_shell.get_vm_details(context, cancellation_context, requests)
Exemplo n.º 11
0
class TestAWSShell(TestCase):
    def setUp(self):
        self.aws_shell_api = AWSShell()

        self.aws_shell_api.credentials_manager = Mock()
        self.aws_shell_api.ec2_instance_waiter = Mock()
        self.aws_shell_api.cloudshell_session_helper = Mock()
        self.aws_shell_api.aws_session_manager.get_ec2_session = Mock(
            return_value=Mock())

        self.command_context = Mock()
        self.command_context.resource = Mock()
        self.command_context.remote_endpoints = []

        self.command_context.connectivity = Mock()
        self.command_context.connectivity.server_address = Mock()
        self.command_context.connectivity.admin_auth_token = Mock()

        self.command_context.reservation = Mock()
        self.command_context.reservation.domain = Mock()

        self.command_context.remote_reservation = Mock()
        self.command_context.remote_reservation.domain = Mock()

        self.aws_shell_api.model_parser.convert_to_aws_resource_model = Mock(
            return_value=(AWSEc2CloudProviderResourceModel()))

    def test_deploying_ami_returns_deploy_result(self):
        name = 'my instance name'
        result = Mock()
        result.instance_id = 'my instance id'

        deploymock = DeployAWSEc2AMIInstanceResourceModel()
        deploymock.auto_power_on = "True"
        deploymock.auto_power_off = "True"
        deploymock.wait_for_ip = "True"
        deploymock.auto_delete = "True"
        deploymock.autoload = "True"
        deploymock.aws_ec2 = "some_name"

        self.aws_shell_api.model_parser.convert_to_deployment_resource_model = Mock(
            return_value=(deploymock, name))

        self.aws_shell_api.deploy_ami_operation.deploy = Mock(
            return_value=(result, name))

        aws_cloud_provider = AWSEc2CloudProviderResourceModel()

        res = self.aws_shell_api.deploy_ami(self.command_context,
                                            aws_cloud_provider)

        self.assertEqual(jsonpickle.decode(res)['vm_name'], name)
        self.assertEqual(jsonpickle.decode(res)['vm_uuid'], result.instance_id)
        self.assertEqual(
            jsonpickle.decode(res)['auto_power_on'], deploymock.auto_power_on)
        self.assertEqual(
            jsonpickle.decode(res)['auto_power_off'],
            deploymock.auto_power_off)
        self.assertEqual(
            jsonpickle.decode(res)['wait_for_ip'], deploymock.wait_for_ip)
        self.assertEqual(
            jsonpickle.decode(res)['auto_delete'], deploymock.auto_delete)
        self.assertEqual(
            jsonpickle.decode(res)['autoload'], deploymock.autoload)
        self.assertEqual(
            jsonpickle.decode(res)['cloud_provider_resource_name'],
            deploymock.aws_ec2)

    def test_power_on(self):
        deployed_model = DeployDataHolder({'vmdetails': {'uid': 'id'}})
        remote_resource = Mock()
        remote_resource.fullname = 'my ami name'
        self.command_context.remote_endpoints = [remote_resource]
        self.aws_shell_api.model_parser.convert_app_resource_to_deployed_app = Mock(
            return_value=deployed_model)
        self.aws_shell_api.power_management_operation.power_on = Mock(
            return_value=True)
        self.aws_shell_api.power_on_ami(self.command_context)

        self.assertTrue(
            self.aws_shell_api.power_management_operation.power_on.called_with(
                self.aws_shell_api.aws_session_manager.get_ec2_session(),
                'id'))

    def test_power_off(self):
        deployed_model = DeployDataHolder({'vmdetails': {'uid': 'id'}})
        remote_resource = Mock()
        remote_resource.fullname = 'my ami name'
        self.command_context.remote_endpoints = [remote_resource]
        self.aws_shell_api.model_parser.convert_app_resource_to_deployed_app = Mock(
            return_value=deployed_model)
        self.aws_shell_api.power_management_operation.power_off = Mock(
            return_value=True)
        self.aws_shell_api.power_off_ami(self.command_context)

        self.assertTrue(
            self.aws_shell_api.power_management_operation.power_off.
            called_with(
                self.aws_shell_api.aws_session_manager.get_ec2_session(),
                'id'))
Exemplo n.º 12
0
class AmazonAwsCloudProviderShell2GDriver(ResourceDriverInterface):
    def cleanup(self):
        pass

    def __init__(self):
        self.aws_shell = AWSShell()
        self.request_parser = DriverRequestParser()
        self.request_parser.add_deployment_model(
            deployment_model_cls=DeployAWSEc2AMIInstanceResourceModel)
        deploy_name = "Amazon AWS Cloud Provider Shell 2G.Amazon AWS EC2 Instance 2G"
        self.deployments = {deploy_name: self.deploy_ami}

    def initialize(self, context):
        pass

    def Deploy(self, context, request=None, cancellation_context=None):
        actions = self.request_parser.convert_driver_request_to_actions(
            request)
        deploy_action = single(actions, lambda x: isinstance(x, DeployApp))
        deployment_name = deploy_action.actionParams.deployment.deploymentPath
        self.parse_vnicename(actions)

        if deployment_name in self.deployments.keys():
            deploy_method = self.deployments[deployment_name]
            deploy_result = deploy_method(context, actions,
                                          cancellation_context)
            return DriverResponse(deploy_result).to_driver_response_json()
        else:
            raise Exception("Could not find the deployment")

    def parse_vnicename(self, actions):
        network_actions = [a for a in actions if isinstance(a, ConnectSubnet)]
        for network_action in network_actions:
            try:
                network_action.actionParams.vnicName = int(
                    network_action.actionParams.vnicName)
            except Exception:
                network_action.actionParams.vnicName = None

    def deploy_ami(self, context, actions, cancellation_context):
        return self.aws_shell.deploy_ami(context, actions,
                                         cancellation_context)

    def PowerOn(self, context, ports):
        return self.aws_shell.power_on_ami(context)

    def PowerOff(self, context, ports):
        return self.aws_shell.power_off_ami(context)

    def orchestration_power_on(self, context, ports):
        return self.aws_shell.power_on_ami(context)

    def orchestration_power_off(self, context, ports):
        return self.aws_shell.power_off_ami(context)

    def PowerCycle(self, context, ports, delay):
        pass

    def remote_refresh_ip(self, context, ports, cancellation_context):
        return self.aws_shell.refresh_ip(context)

    def DeleteInstance(self, context, ports):
        return self.aws_shell.delete_instance(context)

    def PrepareSandboxInfra(self, context, request, cancellation_context):
        actions = self.request_parser.convert_driver_request_to_actions(
            request)
        action_results = self.aws_shell.prepare_connectivity(
            context, actions, cancellation_context)
        return DriverResponse(action_results).to_driver_response_json()

    def CleanupSandboxInfra(self, context, request):
        actions = self.request_parser.convert_driver_request_to_actions(
            request)
        return self.aws_shell.cleanup_connectivity(context, actions)

    def GetApplicationPorts(self, context, ports):
        return self.aws_shell.get_application_ports(context)

    def get_inventory(self, context):
        return self.aws_shell.get_inventory(command_context=context)

    def GetAccessKey(self, context, ports):
        return self.aws_shell.get_access_key(context)

    def SetAppSecurityGroups(self, context, request):
        return self.aws_shell.set_app_security_groups(context, request)

    def GetVmDetails(self, context, cancellation_context, requests):
        return self.aws_shell.get_vm_details(context, cancellation_context,
                                             requests)

    def CreateTrafficMirroring(self,
                               context,
                               request,
                               cancellation_context=None):
        action_results = self.aws_shell.create_traffic_mirroring(
            context, cancellation_context, request)
        return DriverResponse(action_results).to_driver_response_json()

    def RemoveTrafficMirroring(self, context, request):
        action_results = self.aws_shell.remove_traffic_mirroring(
            context, request)
        return DriverResponse(action_results).to_driver_response_json()

    def AddCustomTags(self, context, request, ports):
        return self.aws_shell.add_custom_tags(context, request)

    def save_app(self, context, cancellation_context, ports):
        return self.aws_shell.save_app(context, cancellation_context)

    def remote_save_snapshot(
        self,
        context: ResourceCommandContext,
        ports: List[str],
        snapshot_name: str,
        save_memory: str,
    ):
        """Saves virtual machine to a snapshot.

        :param context: resource context of the vCenterShell
        :param ports:list[string] ports: the ports of the connection between the remote
            resource and the local resource
        :param snapshot_name: snapshot name to save to
        :param save_memory: Snapshot the virtual machine's memory. Lookup, Yes / No
        """
        self.aws_shell.remote_save_snapshot(context, snapshot_name)

    def remote_restore_snapshot(self, context: ResourceCommandContext,
                                ports: List[str], snapshot_name: str):
        """Restores virtual machine from a snapshot.

        :param context: resource context of the vCenterShell
        :param ports:list[string] ports: the ports of the connection between the remote
            resource and the local resource
        :param snapshot_name: Snapshot name to restore from
        """
        self.aws_shell.remote_restore_snapshot(context, snapshot_name)

    def remote_get_snapshots(self, context: ResourceCommandContext,
                             ports: List[str]) -> list:
        """Returns list of snapshots.

        :param context: resource context of the vCenterShell
        :param ports:list[string] ports: the ports of the connection between the remote
            resource and the local resource
        """
        return self.aws_shell.remote_get_snapshots(context)

    def assign_additional_private_ipv4s(self, context, ports, vnic_id,
                                        new_ips):
        return self.aws_shell.assign_additional_private_ipv4s(
            context, vnic_id, new_ips)
Exemplo n.º 13
0
 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)
Exemplo n.º 14
0
class TestAWSShell(TestCase):
    def setUp(self):
        self.aws_shell = AWSShell()

        self.aws_shell.credentials_manager = Mock()
        self.aws_shell.ec2_storage_service = Mock()
        self.aws_shell.ec2_instance_waiter = Mock()
        self.aws_shell.cloudshell_session_helper = Mock()
        self.aws_shell.aws_session_manager.get_ec2_session = Mock(return_value=Mock())
        self.aws_shell.aws_session_manager.get_s3_session = Mock(return_value=Mock())
        self.aws_shell.aws_session_manager.get_ec2_client = Mock(return_value=Mock())

        self.command_context = Mock()
        self.command_context.resource = Mock()
        self.command_context.remote_endpoints = []

        self.command_context.connectivity = Mock()
        self.command_context.connectivity.server_address = Mock()
        self.command_context.connectivity.admin_auth_token = Mock()

        self.command_context.reservation = Mock()
        self.command_context.reservation.domain = Mock()

        self.command_context.remote_reservation = Mock()
        self.command_context.remote_reservation.domain = Mock()

        self.aws_shell.model_parser.convert_to_aws_resource_model = Mock(
                return_value=(AWSEc2CloudProviderResourceModel()))
        self.reservation_model = ReservationModel(self.command_context.reservation)
        self.aws_shell.model_parser.convert_to_reservation_model = Mock(
                return_value=self.reservation_model)

        self.expected_shell_context = Mock(spec=AwsShellContextModel)
        self.expected_shell_context.logger = Mock()
        self.expected_shell_context.cloudshell_session = Mock()
        self.expected_shell_context.aws_ec2_resource_model = Mock()
        self.expected_shell_context.aws_api = Mock()
        self.expected_shell_context.aws_api.ec2_session = Mock()
        self.expected_shell_context.aws_api.s3_session = Mock()
        self.expected_shell_context.aws_api.ec2_client = Mock()

        self.mock_context = Mock()
        self.mock_context.__enter__ = Mock(return_value=self.expected_shell_context)
        self.mock_context.__exit__ = Mock(return_value=False)

    def test_deploying_ami_returns_deploy_result(self):
        # arrange
        deploymock = DeployAWSEc2AMIInstanceResourceModel()
        deploymock.auto_power_off = "True"
        deploymock.wait_for_ip = "True"
        deploymock.auto_delete = "True"
        deploymock.autoload = "True"
        deploymock.cloud_provider = "some_name"
        deploymock.app_name = 'my instance name'
        cancellation_context = Mock()

        result = DeployResult(vm_name=deploymock.app_name,
                              vm_uuid='my instance id',
                              cloud_provider_resource_name=deploymock.cloud_provider,
                              autoload=deploymock.autoload,
                              auto_delete=deploymock.auto_delete,
                              wait_for_ip=deploymock.wait_for_ip,
                              auto_power_off=deploymock.auto_power_off,
                              inbound_ports='',
                              deployed_app_attributes=dict(),
                              deployed_app_address='',
                              public_ip='',
                              network_configuration_results=[],
                              vm_details_data=dict())

        self.aws_shell.model_parser.convert_to_deployment_resource_model = Mock(return_value=deploymock)
        self.aws_shell.deploy_ami_operation.deploy = Mock(return_value=result)
        aws_cloud_provider = AWSEc2CloudProviderResourceModel()

        res = None
        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context

            # act
            res = self.aws_shell.deploy_ami(self.command_context, aws_cloud_provider, cancellation_context)

        decoded_res = jsonpickle.decode(res)
        self.assertEqual(decoded_res['vm_name'], deploymock.app_name)
        self.assertEqual(decoded_res['vm_uuid'], result.vm_uuid)
        self.assertEqual(decoded_res['auto_power_off'], deploymock.auto_power_off)
        self.assertEqual(decoded_res['wait_for_ip'], deploymock.wait_for_ip)
        self.assertEqual(decoded_res['auto_delete'], deploymock.auto_delete)
        self.assertEqual(decoded_res['autoload'], deploymock.autoload)
        self.assertEqual(decoded_res['cloud_provider_resource_name'], deploymock.cloud_provider)
        self.aws_shell.deploy_ami_operation.deploy.assert_called_with(
                ec2_session=self.expected_shell_context.aws_api.ec2_session,
                s3_session=self.expected_shell_context.aws_api.s3_session,
                name=deploymock.app_name,
                reservation=self.reservation_model,
                aws_ec2_cp_resource_model=self.expected_shell_context.aws_ec2_resource_model,
                ami_deployment_model=deploymock,
                ec2_client=self.expected_shell_context.aws_api.ec2_client,
                cancellation_context=cancellation_context,
                logger=self.expected_shell_context.logger)

    def test_cleanup_connectivity(self):
        # prepare
        req = '{"driverRequest": {"actions": [{"type": "cleanupNetwork", "actionId": "ba7d54a5-79c3-4b55-84c2-d7d9bdc19356"}]}}'
        self.aws_shell.clean_up_operation.cleanup = Mock(return_value=True)
        actions_mock = Mock()
        result = None

        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context
            with patch('cloudshell.cp.aws.aws_shell.NetworkActionsParser') as net_parser:
                net_parser.parse_network_actions_data = Mock(return_value=actions_mock)

                # act
                result = self.aws_shell.cleanup_connectivity(self.command_context, req)

        # assert
        self.aws_shell.clean_up_operation.cleanup.assert_called_with(
                ec2_client=self.expected_shell_context.aws_api.ec2_client,
                ec2_session=self.expected_shell_context.aws_api.ec2_session,
                s3_session=self.expected_shell_context.aws_api.s3_session,
                aws_ec2_data_model=self.expected_shell_context.aws_ec2_resource_model,
                reservation_id=self.command_context.reservation.reservation_id,
                actions=actions_mock,
                logger=self.expected_shell_context.logger)
        self.assertEquals(result, '{"driverResponse": {"actionResults": [true]}}')

    def test_prepare_connectivity(self):
        # Assert
        cancellation_context = Mock()
        req = '{"driverRequest": {"actions": [{"actionId": "ba7d54a5-79c3-4b55-84c2-d7d9bdc19356","actionTarget": null, "type": "prepareNetwork", "connectionParams": {"type": "prepareNetworkParams", "cidr": "10.0.0.0/24"}}]}}'
        self.aws_shell.prepare_connectivity_operation.prepare_connectivity = Mock(return_value=True)
        res = None
        actions_mock = Mock()
        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context
            with patch('cloudshell.cp.aws.aws_shell.NetworkActionsParser') as net_parser:
                net_parser.parse_network_actions_data = Mock(return_value=actions_mock)

                # Act
                res = self.aws_shell.prepare_connectivity(self.command_context, req, cancellation_context)

            # Assert
            self.aws_shell.prepare_connectivity_operation.prepare_connectivity.assert_called_with(
                    ec2_client=self.expected_shell_context.aws_api.ec2_client,
                    ec2_session=self.expected_shell_context.aws_api.ec2_session,
                    s3_session=self.expected_shell_context.aws_api.s3_session,
                    reservation=self.reservation_model,
                    aws_ec2_datamodel=self.expected_shell_context.aws_ec2_resource_model,
                    actions=actions_mock,
                    cancellation_context=cancellation_context,
                    logger=self.expected_shell_context.logger)
            self.assertEqual(res, '{"driverResponse": {"actionResults": true}}')

    def test_prepare_connectivity_invalid_req(self):
        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext'):
            req = '{"aa": {"actions": [{"actionId": "ba7d54a5-79c3-4b55-84c2-d7d9bdc19356","actionTarget": null,"customActionAttributes": [{"attributeName": "Network","attributeValue": "10.0.0.0/24","type": "customAttribute"}],"type": "prepareNetwork"}]}}'
            self.aws_shell.prepare_connectivity_operation.prepare_connectivity = Mock(return_value=True)

            self.assertRaises(ValueError, self.aws_shell.prepare_connectivity, self.command_context, req, Mock())

    def test_delete_instance(self):
        deployed_model = DeployDataHolder({'vmdetails': {'uid': 'id'}})
        remote_resource = Mock()
        remote_resource.fullname = 'my ami name'
        self.command_context.remote_endpoints = [remote_resource]
        self.aws_shell.model_parser.convert_app_resource_to_deployed_app = Mock(return_value=deployed_model)
        self.aws_shell.delete_ami_operation.delete_instance = Mock(return_value=True)

        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context

            # act
            self.aws_shell.delete_instance(self.command_context)

        self.aws_shell.delete_ami_operation.delete_instance.assert_called_with(
                logger=self.expected_shell_context.logger,
                ec2_session=self.expected_shell_context.aws_api.ec2_session,
                instance_id=deployed_model.vmdetails.uid)

    def test_power_on(self):
        deployed_model = DeployDataHolder({'vmdetails': {'uid': 'id'}})
        remote_resource = Mock()
        remote_resource.fullname = 'my ami name'
        self.command_context.remote_endpoints = [remote_resource]
        self.aws_shell.model_parser.convert_app_resource_to_deployed_app = Mock(return_value=deployed_model)
        self.aws_shell.power_management_operation.power_on = Mock(return_value=True)

        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context

            # act
            self.aws_shell.power_on_ami(self.command_context)

        self.aws_shell.power_management_operation.power_on.assert_called_with(
                ec2_session=self.expected_shell_context.aws_api.ec2_session,
                ami_id=deployed_model.vmdetails.uid)

    def test_power_off(self):
        deployed_model = DeployDataHolder({'vmdetails': {'uid': 'id'}})
        remote_resource = Mock()
        remote_resource.fullname = 'my ami name'
        self.command_context.remote_endpoints = [remote_resource]
        self.aws_shell.model_parser.convert_app_resource_to_deployed_app = Mock(return_value=deployed_model)
        self.aws_shell.power_management_operation.power_off = Mock(return_value=True)

        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context

            # act
            self.aws_shell.power_off_ami(self.command_context)

        self.aws_shell.power_management_operation.power_off.assert_called_with(
                ec2_session=self.expected_shell_context.aws_api.ec2_session,
                ami_id=deployed_model.vmdetails.uid)

    def test_get_application_ports(self):
        remote_resource = Mock()
        remote_resource.fullname = 'my ami name'
        self.command_context.remote_endpoints = [remote_resource]

        deployed_model = Mock()
        deployed_model.vmdetails = Mock()
        deployed_model.vmdetails.vmCustomParams = Mock()
        self.aws_shell.model_parser.convert_app_resource_to_deployed_app = Mock(return_value=deployed_model)

        self.aws_shell.model_parser.try_get_deployed_connected_resource_instance_id = Mock(return_value='instance_id')
        self.aws_shell.deployed_app_ports_operation.get_app_ports_from_cloud_provider = Mock(return_value='bla')

        self.aws_shell.model_parser.get_allow_all_storage_traffic_from_connected_resource_details = Mock(return_value='True')

        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context

            # act
            res = self.aws_shell.get_application_ports(self.command_context)

        assert res == 'bla'
        self.aws_shell.deployed_app_ports_operation.get_app_ports_from_cloud_provider.assert_called_with(
            ec2_session=self.expected_shell_context.aws_api.ec2_session,
            instance_id='instance_id',
            resource=remote_resource,
            allow_all_storage_traffic='True'
        )

    def test_get_access_key(self):
        self.command_context.remote_reservation = Mock()
        self.command_context.remote_reservation.reservation_id = 'reservation_id'
        self.aws_shell.access_key_operation.get_access_key = Mock(return_value='access_key')

        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context

            # act
            res = self.aws_shell.get_access_key(self.command_context)

        assert res == 'access_key'
        self.aws_shell.access_key_operation.get_access_key(
                s3_session=self.expected_shell_context.aws_api.ec2_session,
                aws_ec2_resource_model=self.expected_shell_context.aws_ec2_resource_model,
                reservation_id=self.command_context.remote_reservation.reservation_id)

    def test_refresh_ip(self):
        self.aws_shell.model_parser.get_private_ip_from_connected_resource_details = Mock(return_value='private_ip')
        self.aws_shell.model_parser.get_public_ip_from_connected_resource_details = Mock(return_value='public_ip')
        self.aws_shell.model_parser.try_get_deployed_connected_resource_instance_id = Mock(return_value='instance_id')
        self.aws_shell.model_parser.get_connectd_resource_fullname = Mock(return_value='resource_name')
        self.aws_shell.refresh_ip_operation.refresh_ip = Mock()

        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context

            # act
            self.aws_shell.refresh_ip(self.command_context)

        self.aws_shell.refresh_ip_operation.refresh_ip(
            cloudshell_session=self.expected_shell_context.cloudshell_session,
            ec2_session=self.expected_shell_context.aws_api.ec2_session,
            deployed_instance_id='instance_id',
            private_ip_on_resource='private_ip',
            public_ip_on_resource='public_ip',
            resource_fullname='resource_name')
Exemplo n.º 15
0
 def __init__(self):
     """
     ctor must be without arguments, it is created with reflection at run time
     """
     self.aws_shell = AWSShell()
     pass
Exemplo n.º 16
0
class TestAWSShell(TestCase):
    def setUp(self):
        self.aws_shell = AWSShell()

        self.aws_shell.credentials_manager = Mock()
        self.aws_shell.ec2_storage_service = Mock()
        self.aws_shell.ec2_instance_waiter = Mock()
        self.aws_shell.cloudshell_session_helper = Mock()
        self.aws_shell.aws_session_manager.get_ec2_session = Mock(return_value=Mock())
        self.aws_shell.aws_session_manager.get_s3_session = Mock(return_value=Mock())
        self.aws_shell.aws_session_manager.get_ec2_client = Mock(return_value=Mock())

        self.command_context = Mock()
        self.command_context.resource = Mock()
        self.command_context.remote_endpoints = []

        self.command_context.connectivity = Mock()
        self.command_context.connectivity.server_address = Mock()
        self.command_context.connectivity.admin_auth_token = Mock()

        self.command_context.reservation = Mock()
        self.command_context.reservation.domain = Mock()

        self.command_context.remote_reservation = Mock()
        self.command_context.remote_reservation.domain = Mock()

        self.aws_shell.model_parser.convert_to_aws_resource_model = Mock()
        self.reservation_model = ReservationModel(self.command_context.reservation)
        self.aws_shell.model_parser.convert_to_reservation_model = Mock(
            return_value=self.reservation_model
        )

        self.expected_shell_context = Mock(spec=AwsShellContextModel)
        self.expected_shell_context.logger = Mock()
        self.expected_shell_context.cloudshell_session = Mock()
        self.expected_shell_context.aws_ec2_resource_model = Mock()
        self.expected_shell_context.aws_api = Mock()
        self.expected_shell_context.aws_api.ec2_session = Mock()
        self.expected_shell_context.aws_api.s3_session = Mock()
        self.expected_shell_context.aws_api.ec2_client = Mock()

        self.mock_context = Mock()
        self.mock_context.__enter__ = Mock(return_value=self.expected_shell_context)
        self.mock_context.__exit__ = Mock(return_value=False)

    def test_deploying_ami_returns_deploy_result(self):
        # arrange
        cancellation_context = Mock()

        result = Mock()

        self.aws_shell.deploy_ami_operation.deploy = Mock(return_value=result)

        with patch("cloudshell.cp.aws.aws_shell.AwsShellContext") as shell_context:
            shell_context.return_value = self.mock_context
            deploy_app = DeployApp()
            deploy_app.actionParams = Mock()

            # act
            res = self.aws_shell.deploy_ami(
                self.command_context, [deploy_app], cancellation_context
            )

        self.assertEqual(res, result)
        self.aws_shell.deploy_ami_operation.deploy.assert_called_with(
            ec2_session=self.expected_shell_context.aws_api.ec2_session,
            s3_session=self.expected_shell_context.aws_api.s3_session,
            name=deploy_app.actionParams.appName,
            reservation=self.reservation_model,
            aws_ec2_cp_resource_model=self.expected_shell_context.aws_ec2_resource_model,  # noqa: E501
            ami_deploy_action=deploy_app,
            network_actions=[],
            ec2_client=self.expected_shell_context.aws_api.ec2_client,
            cancellation_context=cancellation_context,
            logger=self.expected_shell_context.logger,
        )

    def test_cleanup_connectivity(self):
        # prepare
        req = '{"driverRequest": {"actions": [{"type": "cleanupNetwork", "actionId": "ba7d54a5-79c3-4b55-84c2-d7d9bdc19356"}]}}'  # noqa
        self.aws_shell.clean_up_operation.cleanup = Mock(return_value=True)
        actions_mock = Mock()
        result = None

        with patch("cloudshell.cp.aws.aws_shell.AwsShellContext") as shell_context:
            shell_context.return_value = self.mock_context

            # act
            result = self.aws_shell.cleanup_connectivity(
                self.command_context, actions_mock
            )

        # assert
        self.aws_shell.clean_up_operation.cleanup.assert_called_with(
            ec2_client=self.expected_shell_context.aws_api.ec2_client,
            ec2_session=self.expected_shell_context.aws_api.ec2_session,
            s3_session=self.expected_shell_context.aws_api.s3_session,
            aws_ec2_data_model=self.expected_shell_context.aws_ec2_resource_model,
            reservation_id=self.command_context.reservation.reservation_id,
            actions=actions_mock,
            logger=self.expected_shell_context.logger,
        )
        self.assertEquals(result, '{"driverResponse": {"actionResults": [true]}}')

    @pytest.mark.skip(reason="skip for now")
    def test_prepare_connectivity(self):
        # Assert
        cancellation_context = Mock()
        req = '{"driverRequest": {"actions": [{"actionId": "ba7d54a5-79c3-4b55-84c2-d7d9bdc19356","actionTarget": null, "type": "prepareCloudInfra", "actionParams": {"type": "prepareCloudInfraParams", "cidr": "10.0.0.0/24"}}]}}'  # noqa
        self.aws_shell.prepare_connectivity_operation.prepare_connectivity = Mock(
            return_value=True
        )
        res = None
        actions_mock = Mock()

        with patch("cloudshell.cp.aws.aws_shell.AwsShellContext") as shell_context:
            shell_context.return_value = self.mock_context

            # Act
            res = self.aws_shell.prepare_connectivity(
                self.command_context, actions_mock, cancellation_context
            )

            # Assert
            self.aws_shell.prepare_connectivity_operation.prepare_connectivity.assert_called_with(  # noqa: E501
                ec2_client=self.expected_shell_context.aws_api.ec2_client,
                ec2_session=self.expected_shell_context.aws_api.ec2_session,
                s3_session=self.expected_shell_context.aws_api.s3_session,
                reservation=self.reservation_model,
                aws_ec2_datamodel=self.expected_shell_context.aws_ec2_resource_model,
                actions=actions_mock,
                cancellation_context=cancellation_context,
                logger=self.expected_shell_context.logger,
            )
            self.assertEqual(res, True)

    def test_delete_instance(self):
        deployed_model = DeployDataHolder({"vmdetails": {"uid": "id"}})
        remote_resource = Mock()
        remote_resource.fullname = "my ami name"
        self.command_context.remote_endpoints = [remote_resource]
        self.aws_shell.model_parser.convert_app_resource_to_deployed_app = Mock(
            return_value=deployed_model
        )
        self.aws_shell.delete_ami_operation.delete_instance = Mock(return_value=True)

        with patch("cloudshell.cp.aws.aws_shell.AwsShellContext") as shell_context:
            shell_context.return_value = self.mock_context

            # act
            self.aws_shell.delete_instance(self.command_context)

        self.aws_shell.delete_ami_operation.delete_instance.assert_called_with(
            logger=self.expected_shell_context.logger,
            ec2_session=self.expected_shell_context.aws_api.ec2_session,
            instance_id=deployed_model.vmdetails.uid,
        )

    def test_power_on(self):
        deployed_model = DeployDataHolder({"vmdetails": {"uid": "id"}})
        remote_resource = Mock()
        remote_resource.fullname = "my ami name"
        self.command_context.remote_endpoints = [remote_resource]
        self.aws_shell.model_parser.convert_app_resource_to_deployed_app = Mock(
            return_value=deployed_model
        )
        self.aws_shell.power_management_operation.power_on = Mock(return_value=True)

        with patch("cloudshell.cp.aws.aws_shell.AwsShellContext") as shell_context:
            shell_context.return_value = self.mock_context

            # act
            self.aws_shell.power_on_ami(self.command_context)

        self.aws_shell.power_management_operation.power_on.assert_called_with(
            ec2_session=self.expected_shell_context.aws_api.ec2_session,
            ami_id=deployed_model.vmdetails.uid,
        )

    def test_power_off(self):
        deployed_model = DeployDataHolder({"vmdetails": {"uid": "id"}})
        remote_resource = Mock()
        remote_resource.fullname = "my ami name"
        self.command_context.remote_endpoints = [remote_resource]
        self.aws_shell.model_parser.convert_app_resource_to_deployed_app = Mock(
            return_value=deployed_model
        )
        self.aws_shell.power_management_operation.power_off = Mock(return_value=True)

        with patch("cloudshell.cp.aws.aws_shell.AwsShellContext") as shell_context:
            shell_context.return_value = self.mock_context

            # act
            self.aws_shell.power_off_ami(self.command_context)

        self.aws_shell.power_management_operation.power_off.assert_called_with(
            ec2_session=self.expected_shell_context.aws_api.ec2_session,
            ami_id=deployed_model.vmdetails.uid,
        )

    def test_get_application_ports(self):
        remote_resource = Mock()
        remote_resource.fullname = "my ami name"
        self.command_context.remote_endpoints = [remote_resource]

        deployed_model = Mock()
        deployed_model.vmdetails = Mock()
        deployed_model.vmdetails.vmCustomParams = Mock()
        self.aws_shell.model_parser.convert_app_resource_to_deployed_app = Mock(
            return_value=deployed_model
        )

        self.aws_shell.model_parser.try_get_deployed_connected_resource_instance_id = (
            Mock(return_value="instance_id")
        )
        self.aws_shell.deployed_app_ports_operation.get_app_ports_from_cloud_provider = Mock(  # noqa: E501
            return_value="bla"
        )

        self.aws_shell.model_parser.get_allow_all_storage_traffic_from_connected_resource_details = Mock(  # noqa: E501
            return_value="True"
        )

        with patch("cloudshell.cp.aws.aws_shell.AwsShellContext") as shell_context:
            shell_context.return_value = self.mock_context

            # act
            res = self.aws_shell.get_application_ports(self.command_context)

        assert res == "bla"
        self.aws_shell.deployed_app_ports_operation.get_app_ports_from_cloud_provider.assert_called_with(  # noqa: E501
            ec2_session=self.expected_shell_context.aws_api.ec2_session,
            instance_id="instance_id",
            resource=remote_resource,
            allow_all_storage_traffic="True",
        )

    def test_get_access_key(self):
        self.command_context.remote_reservation = Mock()
        self.command_context.remote_reservation.reservation_id = "reservation_id"
        self.aws_shell.access_key_operation.get_access_key = Mock(
            return_value="access_key"
        )

        with patch("cloudshell.cp.aws.aws_shell.AwsShellContext") as shell_context:
            shell_context.return_value = self.mock_context

            # act
            res = self.aws_shell.get_access_key(self.command_context)

        assert res == "access_key"
        self.aws_shell.access_key_operation.get_access_key(
            s3_session=self.expected_shell_context.aws_api.ec2_session,
            aws_ec2_resource_model=self.expected_shell_context.aws_ec2_resource_model,
            reservation_id=self.command_context.remote_reservation.reservation_id,
        )

    def test_refresh_ip(self):
        self.aws_shell.model_parser.get_private_ip_from_connected_resource_details = (
            Mock(return_value="private_ip")
        )
        self.aws_shell.model_parser.get_public_ip_attr_from_connected_resource_details = Mock(  # noqa: E501
            return_value=("public_ip", "public_ip")
        )
        self.aws_shell.model_parser.try_get_deployed_connected_resource_instance_id = (
            Mock(return_value="instance_id")
        )
        self.aws_shell.model_parser.get_connectd_resource_fullname = Mock(
            return_value="resource_name"
        )
        self.aws_shell.refresh_ip_operation.refresh_ip = Mock()

        with patch("cloudshell.cp.aws.aws_shell.AwsShellContext") as shell_context:
            shell_context.return_value = self.mock_context

            # act
            self.aws_shell.refresh_ip(self.command_context)

        self.aws_shell.refresh_ip_operation.refresh_ip(
            cloudshell_session=self.expected_shell_context.cloudshell_session,
            ec2_session=self.expected_shell_context.aws_api.ec2_session,
            deployed_instance_id="instance_id",
            private_ip_on_resource="private_ip",
            public_ip_on_resource="public_ip",
            resource_fullname="resource_name",
        )
Exemplo n.º 17
0
class TestAWSShell(TestCase):
    def setUp(self):
        self.aws_shell = AWSShell()

        self.aws_shell.credentials_manager = Mock()
        self.aws_shell.ec2_storage_service = Mock()
        self.aws_shell.ec2_instance_waiter = Mock()
        self.aws_shell.cloudshell_session_helper = Mock()
        self.aws_shell.aws_session_manager.get_ec2_session = Mock(return_value=Mock())
        self.aws_shell.aws_session_manager.get_s3_session = Mock(return_value=Mock())
        self.aws_shell.aws_session_manager.get_ec2_client = Mock(return_value=Mock())

        self.command_context = Mock()
        self.command_context.resource = Mock()
        self.command_context.remote_endpoints = []

        self.command_context.connectivity = Mock()
        self.command_context.connectivity.server_address = Mock()
        self.command_context.connectivity.admin_auth_token = Mock()

        self.command_context.reservation = Mock()
        self.command_context.reservation.domain = Mock()

        self.command_context.remote_reservation = Mock()
        self.command_context.remote_reservation.domain = Mock()

        self.aws_shell.model_parser.convert_to_aws_resource_model = Mock(
                return_value=(AWSEc2CloudProviderResourceModel()))
        self.reservation_model = ReservationModel(self.command_context.reservation)
        self.aws_shell.model_parser.convert_to_reservation_model = Mock(
                return_value=self.reservation_model)

        self.expected_shell_context = Mock(spec=AwsShellContextModel)
        self.expected_shell_context.logger = Mock()
        self.expected_shell_context.cloudshell_session = Mock()
        self.expected_shell_context.aws_ec2_resource_model = Mock()
        self.expected_shell_context.aws_api = Mock()
        self.expected_shell_context.aws_api.ec2_session = Mock()
        self.expected_shell_context.aws_api.s3_session = Mock()
        self.expected_shell_context.aws_api.ec2_client = Mock()

        self.mock_context = Mock()
        self.mock_context.__enter__ = Mock(return_value=self.expected_shell_context)
        self.mock_context.__exit__ = Mock(return_value=False)

    def test_deploying_ami_returns_deploy_result(self):
        # arrange
        cancellation_context = Mock()

        result = Mock()

        self.aws_shell.deploy_ami_operation.deploy = Mock(return_value=result)

        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context
            deploy_app = DeployApp()
            deploy_app.actionParams = Mock()

            # act
            res = self.aws_shell.deploy_ami(self.command_context, [deploy_app], cancellation_context)


        self.assertEqual(res, result)
        self.aws_shell.deploy_ami_operation.deploy.assert_called_with(
                ec2_session=self.expected_shell_context.aws_api.ec2_session,
                s3_session=self.expected_shell_context.aws_api.s3_session,
                name=deploy_app.actionParams.appName,
                reservation=self.reservation_model,
                aws_ec2_cp_resource_model=self.expected_shell_context.aws_ec2_resource_model,
                ami_deploy_action=deploy_app,
                network_actions=[],
                ec2_client=self.expected_shell_context.aws_api.ec2_client,
                cancellation_context=cancellation_context,
                logger=self.expected_shell_context.logger)

    def test_cleanup_connectivity(self):
        # prepare
        req = '{"driverRequest": {"actions": [{"type": "cleanupNetwork", "actionId": "ba7d54a5-79c3-4b55-84c2-d7d9bdc19356"}]}}'
        self.aws_shell.clean_up_operation.cleanup = Mock(return_value=True)
        actions_mock = Mock()
        result = None

        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context

                # act
            result = self.aws_shell.cleanup_connectivity(self.command_context, actions_mock)

        # assert
        self.aws_shell.clean_up_operation.cleanup.assert_called_with(
                ec2_client=self.expected_shell_context.aws_api.ec2_client,
                ec2_session=self.expected_shell_context.aws_api.ec2_session,
                s3_session=self.expected_shell_context.aws_api.s3_session,
                aws_ec2_data_model=self.expected_shell_context.aws_ec2_resource_model,
                reservation_id=self.command_context.reservation.reservation_id,
                actions=actions_mock,
                logger=self.expected_shell_context.logger)
        self.assertEquals(result, '{"driverResponse": {"actionResults": [true]}}')

    def test_prepare_connectivity(self):
        # Assert
        cancellation_context = Mock()
        req = '{"driverRequest": {"actions": [{"actionId": "ba7d54a5-79c3-4b55-84c2-d7d9bdc19356","actionTarget": null, "type": "prepareCloudInfra", "actionParams": {"type": "prepareCloudInfraParams", "cidr": "10.0.0.0/24"}}]}}'
        self.aws_shell.prepare_connectivity_operation.prepare_connectivity = Mock(return_value=True)
        res = None
        actions_mock = Mock()

        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context

            # Act
            res = self.aws_shell.prepare_connectivity(self.command_context, actions_mock, cancellation_context)

            # Assert
            self.aws_shell.prepare_connectivity_operation.prepare_connectivity.assert_called_with(
                    ec2_client=self.expected_shell_context.aws_api.ec2_client,
                    ec2_session=self.expected_shell_context.aws_api.ec2_session,
                    s3_session=self.expected_shell_context.aws_api.s3_session,
                    reservation=self.reservation_model,
                    aws_ec2_datamodel=self.expected_shell_context.aws_ec2_resource_model,
                    actions=actions_mock,
                    cancellation_context=cancellation_context,
                    logger=self.expected_shell_context.logger)
            self.assertEqual(res, True)

    def test_delete_instance(self):
        deployed_model = DeployDataHolder({'vmdetails': {'uid': 'id'}})
        remote_resource = Mock()
        remote_resource.fullname = 'my ami name'
        self.command_context.remote_endpoints = [remote_resource]
        self.aws_shell.model_parser.convert_app_resource_to_deployed_app = Mock(return_value=deployed_model)
        self.aws_shell.delete_ami_operation.delete_instance = Mock(return_value=True)

        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context

            # act
            self.aws_shell.delete_instance(self.command_context)

        self.aws_shell.delete_ami_operation.delete_instance.assert_called_with(
                logger=self.expected_shell_context.logger,
                ec2_session=self.expected_shell_context.aws_api.ec2_session,
                instance_id=deployed_model.vmdetails.uid)

    def test_power_on(self):
        deployed_model = DeployDataHolder({'vmdetails': {'uid': 'id'}})
        remote_resource = Mock()
        remote_resource.fullname = 'my ami name'
        self.command_context.remote_endpoints = [remote_resource]
        self.aws_shell.model_parser.convert_app_resource_to_deployed_app = Mock(return_value=deployed_model)
        self.aws_shell.power_management_operation.power_on = Mock(return_value=True)

        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context

            # act
            self.aws_shell.power_on_ami(self.command_context)

        self.aws_shell.power_management_operation.power_on.assert_called_with(
                ec2_session=self.expected_shell_context.aws_api.ec2_session,
                ami_id=deployed_model.vmdetails.uid)

    def test_power_off(self):
        deployed_model = DeployDataHolder({'vmdetails': {'uid': 'id'}})
        remote_resource = Mock()
        remote_resource.fullname = 'my ami name'
        self.command_context.remote_endpoints = [remote_resource]
        self.aws_shell.model_parser.convert_app_resource_to_deployed_app = Mock(return_value=deployed_model)
        self.aws_shell.power_management_operation.power_off = Mock(return_value=True)

        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context

            # act
            self.aws_shell.power_off_ami(self.command_context)

        self.aws_shell.power_management_operation.power_off.assert_called_with(
                ec2_session=self.expected_shell_context.aws_api.ec2_session,
                ami_id=deployed_model.vmdetails.uid)

    def test_get_application_ports(self):
        remote_resource = Mock()
        remote_resource.fullname = 'my ami name'
        self.command_context.remote_endpoints = [remote_resource]

        deployed_model = Mock()
        deployed_model.vmdetails = Mock()
        deployed_model.vmdetails.vmCustomParams = Mock()
        self.aws_shell.model_parser.convert_app_resource_to_deployed_app = Mock(return_value=deployed_model)

        self.aws_shell.model_parser.try_get_deployed_connected_resource_instance_id = Mock(return_value='instance_id')
        self.aws_shell.deployed_app_ports_operation.get_app_ports_from_cloud_provider = Mock(return_value='bla')

        self.aws_shell.model_parser.get_allow_all_storage_traffic_from_connected_resource_details = Mock(return_value='True')

        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context

            # act
            res = self.aws_shell.get_application_ports(self.command_context)

        assert res == 'bla'
        self.aws_shell.deployed_app_ports_operation.get_app_ports_from_cloud_provider.assert_called_with(
            ec2_session=self.expected_shell_context.aws_api.ec2_session,
            instance_id='instance_id',
            resource=remote_resource,
            allow_all_storage_traffic='True'
        )

    def test_get_access_key(self):
        self.command_context.remote_reservation = Mock()
        self.command_context.remote_reservation.reservation_id = 'reservation_id'
        self.aws_shell.access_key_operation.get_access_key = Mock(return_value='access_key')

        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context

            # act
            res = self.aws_shell.get_access_key(self.command_context)

        assert res == 'access_key'
        self.aws_shell.access_key_operation.get_access_key(
                s3_session=self.expected_shell_context.aws_api.ec2_session,
                aws_ec2_resource_model=self.expected_shell_context.aws_ec2_resource_model,
                reservation_id=self.command_context.remote_reservation.reservation_id)

    def test_refresh_ip(self):
        self.aws_shell.model_parser.get_private_ip_from_connected_resource_details = Mock(return_value='private_ip')
        self.aws_shell.model_parser.get_public_ip_from_connected_resource_details = Mock(return_value='public_ip')
        self.aws_shell.model_parser.try_get_deployed_connected_resource_instance_id = Mock(return_value='instance_id')
        self.aws_shell.model_parser.get_connectd_resource_fullname = Mock(return_value='resource_name')
        self.aws_shell.refresh_ip_operation.refresh_ip = Mock()

        with patch('cloudshell.cp.aws.aws_shell.AwsShellContext') as shell_context:
            shell_context.return_value = self.mock_context

            # act
            self.aws_shell.refresh_ip(self.command_context)

        self.aws_shell.refresh_ip_operation.refresh_ip(
            cloudshell_session=self.expected_shell_context.cloudshell_session,
            ec2_session=self.expected_shell_context.aws_api.ec2_session,
            deployed_instance_id='instance_id',
            private_ip_on_resource='private_ip',
            public_ip_on_resource='public_ip',
            resource_fullname='resource_name')