def setUp(self):
        self.ec2_session = Mock()
        self.reservation = Mock()

        self.vpc_id = "vpc-id"
        self.vpc = Mock()
        self.vpc.route_tables = Mock()
        self.vpc.id = self.vpc_id
        self.ec2_session.Vpc = Mock(return_value=self.vpc)
        self.tag_service = Mock()

        self.mocked_route_table = Mock()
        self.mocked_route_table.associations_attribute = [{}]

        self.route_table_service = RouteTablesService(self.tag_service)
    def setUp(self):
        self.ec2_session = Mock()
        self.reservation = Mock()

        self.vpc_id = 'vpc-id'
        self.vpc = Mock()
        self.vpc.route_tables = Mock()
        self.vpc.id = self.vpc_id
        self.ec2_session.Vpc = Mock(return_value=self.vpc)
        self.tag_service = Mock()

        self.mocked_route_table = Mock()
        self.mocked_route_table.associations_attribute = [{}]

        self.route_table_service = RouteTablesService(self.tag_service)
    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 TestRouteTableService(TestCase):
    def setUp(self):
        self.ec2_session = Mock()
        self.reservation = Mock()

        self.vpc_id = "vpc-id"
        self.vpc = Mock()
        self.vpc.route_tables = Mock()
        self.vpc.id = self.vpc_id
        self.ec2_session.Vpc = Mock(return_value=self.vpc)
        self.tag_service = Mock()

        self.mocked_route_table = Mock()
        self.mocked_route_table.associations_attribute = [{}]

        self.route_table_service = RouteTablesService(self.tag_service)

    def test_get_main_route_table(self):
        # prepare
        main_route_table = Mock()
        main_route_table.associations_attribute = [{"Main": True}]
        route_tables = [self.mocked_route_table, main_route_table]
        self.vpc.route_tables.all = Mock(return_value=route_tables)

        # act
        main_rt = self.route_table_service.get_main_route_table(
            self.ec2_session, self.vpc_id
        )

        # assert
        self.assertIsNotNone(main_rt)

    def test_get_main_route_table_when_main_route_table_not_available(self):
        # prepare
        route_tables = [self.mocked_route_table]
        self.vpc.route_tables.all = Mock(return_value=route_tables)

        # act
        main_rt = self.route_table_service.get_main_route_table(
            self.ec2_session, self.vpc_id
        )

        # assert
        self.assertIsNone(main_rt)

    def test_add_route_to_peered_vpc(self):
        # prepare
        route_table = Mock()
        target_peering_id = "target_peering_id"
        target_vpc_cidr = "target_vpc_cidr"

        # act
        self.route_table_service.add_route_to_peered_vpc(
            route_table=route_table,
            target_peering_id=target_peering_id,
            target_vpc_cidr=target_vpc_cidr,
        )

        # assert
        route_table.create_route.assert_called_with(
            DestinationCidrBlock=target_vpc_cidr,
            VpcPeeringConnectionId=target_peering_id,
        )

    def test_add_route_to_internet_gateway(self):
        # prepare
        route_table = Mock()
        target_internet_gateway_id = "target_internet_gateway_id"

        # act
        self.route_table_service.add_route_to_internet_gateway(
            route_table=route_table,
            target_internet_gateway_id=target_internet_gateway_id,
        )

        # assert
        route_table.create_route.assert_called_with(
            GatewayId=target_internet_gateway_id, DestinationCidrBlock="0.0.0.0/0"
        )

    def test_delete_blackhole_routes(self):
        # prepare
        active_route = Mock()
        active_route.state = "active"
        blackhole_route = Mock()
        blackhole_route.state = "blackhole"

        route_table = Mock()
        route_table.routes = [active_route, blackhole_route]

        # act
        self.route_table_service.delete_blackhole_routes(route_table=route_table)

        # assert
        self.assertTrue(blackhole_route.delete.called)
        self.assertFalse(active_route.delete.called)

    def test_find_first_route_simple_filter_matched(self):
        # prepare
        target_vpc_cidr = "10.0.0.0/24"
        route = Mock()
        route.destination_cidr_block = target_vpc_cidr

        route_table = Mock()
        route_table.routes = [route]

        # act
        result = self.route_table_service.find_first_route(
            route_table=route_table, filters={"destination_cidr_block": target_vpc_cidr}
        )

        # assert
        self.assertEquals(result, route)

    def test_find_first_route_no_filter_matched(self):
        # prepare
        route = Mock()
        route_table = Mock()
        route_table.routes = [route]

        # act
        result = self.route_table_service.find_first_route(
            route_table=route_table, filters={"destination_cidr_block": "10.0.0.0/24"}
        )

        # assert
        self.assertIsNone(result)

    def test_get_custom_route_tables(self):
        # Arrange
        tables = [Mock(), Mock(), Mock()]
        self.route_table_service.get_main_route_table = Mock(return_value=tables[0])
        self.route_table_service.get_all_route_tables = Mock(return_value=tables)
        tables[0].id = "0"
        tables[1].id = "1"
        tables[2].id = "2"
        # Act
        results = self.route_table_service.get_custom_route_tables(
            ec2_session=self.ec2_session, vpc_id=self.vpc_id
        )
        # Assert
        self.assertEqual(results, [tables[1], tables[2]])

    @pytest.mark.skip(reason="skip for now")
    def test_create_route_table(self):
        # Arrange
        table = Mock()
        tags = Mock()
        self.vpc.create_route_table = Mock(return_value=table)
        self.tag_service.get_default_tags = Mock(return_value=tags)
        # Act
        self.route_table_service.create_route_table(
            ec2_session=self.ec2_session,
            reservation=self.reservation,
            vpc_id=self.vpc_id,
            table_name="MyTable",
        )
        # Assert
        self.vpc.create_route_table.assert_called_once()
        self.tag_service.get_default_tags.assert_called_once_with(
            "MyTable", self.reservation
        )
        self.tag_service.set_ec2_resource_tags.assert_called_once_with(table, tags)

    @pytest.mark.skip(reason="skip for now")
    def test_get_route_table(self):
        # Arrange
        table = Mock()
        table.tags = [{"Key": "Name", "Value": "Table1"}]
        self.route_table_service.get_all_route_tables = Mock(return_value=[table])
        # Act
        table1 = self.route_table_service.get_route_table(
            ec2_session=self.ec2_session, vpc_id=self.vpc_id, table_name="Table1"
        )
        table2 = self.route_table_service.get_route_table(
            ec2_session=self.ec2_session, vpc_id=self.vpc_id, table_name="Table2"
        )
        # Assert
        self.assertEqual(table1, table)
        self.assertEqual(table2, None)

    def test_delete_table(self):
        # Arrange
        table = Mock()
        # Act
        result = self.route_table_service.delete_table(table)
        # Assert
        table.delete.assert_called_once()
        self.assertTrue(result)
class TestRouteTableService(TestCase):
    def setUp(self):
        self.ec2_session = Mock()
        self.reservation = Mock()

        self.vpc_id = 'vpc-id'
        self.vpc = Mock()
        self.vpc.route_tables = Mock()
        self.vpc.id = self.vpc_id
        self.ec2_session.Vpc = Mock(return_value=self.vpc)
        self.tag_service = Mock()

        self.mocked_route_table = Mock()
        self.mocked_route_table.associations_attribute = [{}]

        self.route_table_service = RouteTablesService(self.tag_service)

    def test_get_main_route_table(self):
        # prepare
        main_route_table = Mock()
        main_route_table.associations_attribute = [{'Main': True}]
        route_tables = [self.mocked_route_table, main_route_table]
        self.vpc.route_tables.all = Mock(return_value=route_tables)

        # act
        main_rt = self.route_table_service.get_main_route_table(self.ec2_session, self.vpc_id)

        # assert
        self.assertIsNotNone(main_rt)

    def test_get_main_route_table_when_main_route_table_not_available(self):
        # prepare
        route_tables = [self.mocked_route_table]
        self.vpc.route_tables.all = Mock(return_value=route_tables)

        # act
        main_rt = self.route_table_service.get_main_route_table(self.ec2_session, self.vpc_id)

        # assert
        self.assertIsNone(main_rt)

    def test_add_route_to_peered_vpc(self):
        # prepare
        route_table = Mock()
        target_peering_id = 'target_peering_id'
        target_vpc_cidr = 'target_vpc_cidr'

        # act
        self.route_table_service.add_route_to_peered_vpc(route_table=route_table, target_peering_id=target_peering_id,
                                                         target_vpc_cidr=target_vpc_cidr)

        # assert
        route_table.create_route.assert_called_with(DestinationCidrBlock=target_vpc_cidr,
                                                    VpcPeeringConnectionId=target_peering_id)

    def test_add_route_to_internet_gateway(self):
        # prepare
        route_table = Mock()
        target_internet_gateway_id = 'target_internet_gateway_id'

        # act
        self.route_table_service.add_route_to_internet_gateway(route_table=route_table,
                                                               target_internet_gateway_id=target_internet_gateway_id)

        # assert
        route_table.create_route.assert_called_with(GatewayId=target_internet_gateway_id,
                                                    DestinationCidrBlock='0.0.0.0/0')

    def test_delete_blackhole_routes(self):
        # prepare
        active_route = Mock()
        active_route.state = 'active'
        blackhole_route = Mock()
        blackhole_route.state = 'blackhole'

        route_table = Mock()
        route_table.routes = [active_route, blackhole_route]

        # act
        self.route_table_service.delete_blackhole_routes(route_table=route_table)

        # assert
        self.assertTrue(blackhole_route.delete.called)
        self.assertFalse(active_route.delete.called)

    def test_find_first_route_simple_filter_matched(self):
        # prepare
        target_vpc_cidr = '10.0.0.0/24'
        route = Mock()
        route.destination_cidr_block = target_vpc_cidr

        route_table = Mock()
        route_table.routes = [route]

        # act
        result = self.route_table_service.find_first_route(route_table=route_table,
                                                           filters={'destination_cidr_block': target_vpc_cidr})

        # assert
        self.assertEquals(result, route)

    def test_find_first_route_no_filter_matched(self):
        # prepare
        route = Mock()
        route_table = Mock()
        route_table.routes = [route]

        # act
        result = self.route_table_service.find_first_route(route_table=route_table,
                                                           filters={'destination_cidr_block': '10.0.0.0/24'})

        # assert
        self.assertIsNone(result)

    def test_get_custom_route_tables(self):
        # Arrange
        tables = [Mock(), Mock(), Mock()]
        self.route_table_service.get_main_route_table = Mock(return_value=tables[0])
        self.route_table_service.get_all_route_tables = Mock(return_value=tables)
        tables[0].id = "0"
        tables[1].id = "1"
        tables[2].id = "2"
        # Act
        results = self.route_table_service.get_custom_route_tables(ec2_session=self.ec2_session, vpc_id=self.vpc_id)
        # Assert
        self.assertEqual(results, [tables[1], tables[2]])

    def test_create_route_table(self):
        # Arrange
        table = Mock()
        tags = Mock()
        self.vpc.create_route_table = Mock(return_value=table)
        self.tag_service.get_default_tags = Mock(return_value=tags)
        # Act
        self.route_table_service.create_route_table(ec2_session=self.ec2_session, reservation=self.reservation,
                                                    vpc_id=self.vpc_id, table_name="MyTable")
        # Assert
        self.vpc.create_route_table.assert_called_once()
        self.tag_service.get_default_tags.assert_called_once_with("MyTable", self.reservation)
        self.tag_service.set_ec2_resource_tags.assert_called_once_with(table, tags)

    def test_get_route_table(self):
        # Arrange
        table = Mock()
        table.tags = [{"Key":"Name", "Value":"Table1"}]
        self.route_table_service.get_all_route_tables = Mock(return_value=[table])
        # Act
        table1 = self.route_table_service.get_route_table(ec2_session=self.ec2_session, vpc_id=self.vpc_id, table_name="Table1")
        table2 = self.route_table_service.get_route_table(ec2_session=self.ec2_session, vpc_id=self.vpc_id, table_name="Table2")
        # Assert
        self.assertEqual(table1, table)
        self.assertEqual(table2, None)

    def test_delete_table(self):
        # Arrange
        table = Mock()
        # Act
        result = self.route_table_service.delete_table(table)
        # Assert
        table.delete.assert_called_once()
        self.assertTrue(result)