def test_exception_on_command_cancellation(self):
        # arrange
        cancellation_service = CommandCancellationService()
        cancellation_context = Mock(is_cancelled=True)

        # act & assert
        with self.assertRaisesRegexp(CancellationException, "Command was cancelled"):
            cancellation_service.check_if_cancelled(cancellation_context)
    def test_exception_on_command_cancellation(self):
        # arrange
        cancellation_service = CommandCancellationService()
        cancellation_context = Mock(is_cancelled=True)

        # act & assert
        with self.assertRaisesRegexp(CancellationException, "Command was cancelled"):
            cancellation_service.check_if_cancelled(cancellation_context)
    def test_exception_with_data_on_command_cancellation(self):
        # arrange
        cancellation_service = CommandCancellationService()
        cancellation_context = Mock(is_cancelled=True)
        data = Mock()

        # act & assert
        with self.assertRaisesRegexp(CancellationException, "Command was cancelled") as assert_exc:
            cancellation_service.check_if_cancelled(cancellation_context, data=data)

        self.assertEquals(assert_exc.exception.data, data)
    def test_exception_with_data_on_command_cancellation(self):
        # arrange
        cancellation_service = CommandCancellationService()
        cancellation_context = Mock(is_cancelled=True)
        data = Mock()

        # act & assert
        with self.assertRaisesRegexp(CancellationException,
                                     "Command was cancelled") as assert_exc:
            cancellation_service.check_if_cancelled(cancellation_context,
                                                    data=data)

        self.assertEquals(assert_exc.exception.data, data)
    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,
        )
Example #6
0
    def test_valid_create_returns_success_actions(self):
        tag_service = TagService(Mock())
        session_number_service = SessionNumberService()
        traffic_mirror_service = TrafficMirrorService()
        cancellation_service = CommandCancellationService()
        reservation_context = Mock()
        reservation_context.reservation_id = str(uuid4())
        reservation = ReservationModel(reservation_context)
        reservation.blueprint = "lalala"
        reservation.owner = "admin"
        reservation.domain = "global"
        describe_mirror_targets_result = {
            "TrafficMirrorTargets": [{
                "NetworkInterfaceId": "bbbb",
                "TrafficMirrorTargetId": "cccc"
            }]
        }

        create_traffic_mirror_target_result = {
            "TrafficMirrorTarget": {
                "TrafficMirrorTargetId": "tmt-5050"
            }
        }

        create_filter_result = {
            "TrafficMirrorFilter": {
                "TrafficMirrorFilterId": "tmf-5050"
            }
        }

        create_traffic_mirror_session_result = {
            "TrafficMirrorSession": {
                "TrafficMirrorSessionId": "tms-5050"
            }
        }

        ec2_client = Mock()
        ec2_client.describe_traffic_mirror_targets = Mock(
            return_value=describe_mirror_targets_result)
        ec2_client.create_traffic_mirror_target = Mock(
            return_value=create_traffic_mirror_target_result)
        ec2_client.create_traffic_mirror_filter = Mock(
            return_value=create_filter_result)
        ec2_client.create_traffic_mirror_session = Mock(
            return_value=create_traffic_mirror_session_result)

        cancellation_context = Mock()
        cancellation_context.is_cancelled = False
        logger = Mock()
        cloudshell = Mock()
        checkout_result = Mock()
        checkout_result.Items = [5]
        cloudshell.CheckoutFromPool = Mock(return_value=checkout_result)

        action = CreateTrafficMirroring()
        action.actionId = str(uuid4())
        action.actionParams = CreateTrafficMirroringParams()
        action.actionParams.sessionNumber = "5"
        action.actionParams.sourceNicId = "a"
        action.actionParams.targetNicId = "b"
        actions = [action]

        op = TrafficMirrorOperation(
            tag_service,
            session_number_service,
            traffic_mirror_service,
            cancellation_service,
        )

        results = op.create(
            ec2_client=ec2_client,
            reservation=reservation,
            actions=actions,
            cancellation_context=cancellation_context,
            logger=logger,
            cloudshell=cloudshell,
        )

        self.assertTrue([x for x in results if x.success])