def test_execute_sets_tags(self):
        # Arrange
        def return_public_tag_with_value(*args, **kwargs):
            return {"Key": TagNames.IsPublic, "Value": args[0]}

        prepare_subnet = PrepareSubnet()
        prepare_subnet.actionId = "1"
        prepare_subnet.actionParams = PrepareSubnetParams()
        prepare_subnet.actionParams.cidr = "1.2.3.4/24"
        prepare_subnet.actionParams.alias = "MySubnet"
        actions = [prepare_subnet]

        self.reservation.reservation_id = "123"
        subnet = Mock()
        self.subnet_service.get_first_or_none_subnet_from_vpc = Mock(
            return_value=subnet)
        is_public_tag = Mock()
        self.tag_service.get_is_public_tag = Mock(return_value=is_public_tag)
        default_tags = [Mock()]
        self.tag_service.get_default_tags = Mock(return_value=default_tags)

        # Act
        self.executor.execute(actions)

        # Assert
        default_tags.append(is_public_tag)
        self.tag_service.set_ec2_resource_tags.assert_called_once_with(
            subnet, default_tags)
 def test_execute_sets_private_subnet_to_private_routing_table(self):
     # Arrange
     prepare_subnet = PrepareSubnet()
     prepare_subnet.actionId = "1"
     prepare_subnet.actionParams = PrepareSubnetParams()
     prepare_subnet.actionParams.cidr = "1.2.3.4/24"
     prepare_subnet.actionParams.isPublic = False
     actions = [prepare_subnet]
     # Act
     self.executor.execute(actions)
     # Assert
     self.subnet_service.set_subnet_route_table.assert_called_once()
 def test_prepare_conn_execute_the_network_action_first(self):
     # Arrage
     actions = []
     prepare_subnet_sub_a = PrepareSubnet()
     prepare_subnet_sub_a.actionId = "SubA"
     prepare_subnet_sub_a.actionParams = PrepareSubnetParams()
     actions.append(prepare_subnet_sub_a)
     prepare_cloud_infra = PrepareCloudInfra()
     prepare_cloud_infra.actionId = "Net"
     prepare_cloud_infra.actionParams = PrepareCloudInfraParams()
     actions.append(prepare_cloud_infra)
     prepare_subnet_sub_b = PrepareSubnet()
     prepare_subnet_sub_b.actionId = "SubB"
     prepare_subnet_sub_b.actionParams = PrepareSubnetParams()
     actions.append(prepare_subnet_sub_b)
     prepare_create_key = CreateKeys()
     prepare_create_key.actionId = "CreateKeys"
     actions.append(prepare_create_key)
     # Act
     results = self.prepare_conn.prepare_connectivity(
         ec2_client=self.ec2_client,
         ec2_session=self.ec2_session,
         s3_session=self.s3_session,
         reservation=self.reservation,
         aws_ec2_datamodel=self.aws_dm,
         actions=actions,
         cancellation_context=self.cancellation_context,
         logger=Mock(),
     )
     # Assert
     self.assertEqual(len(results), 4)
     self.assertEqual(results[0].actionId, "Net")
     self.assertEqual(results[1].actionId, "CreateKeys")
     self.assertEqual(results[2].actionId, "SubA")
     self.assertEqual(results[3].actionId, "SubB")
    def test_prepare_conn_error_no_vpc(self):
        self.vpc_serv.find_vpc_for_reservation = Mock(return_value=None)
        self.vpc_serv.get_active_vpcs_count = Mock(return_value=None)

        # Arrage
        actions = []
        prepare_subnet_sub_a = PrepareSubnet()
        prepare_subnet_sub_a.actionId = "SubA"
        prepare_subnet_sub_a.actionParams = PrepareSubnetParams()
        actions.append(prepare_subnet_sub_a)
        prepare_cloud_infra = PrepareCloudInfra()
        prepare_cloud_infra.actionId = "Net"
        prepare_cloud_infra.actionParams = PrepareCloudInfraParams()
        actions.append(prepare_cloud_infra)
        prepare_subnet_sub_b = PrepareSubnet()
        prepare_subnet_sub_b.actionId = "SubB"
        prepare_subnet_sub_b.actionParams = PrepareSubnetParams()
        actions.append(prepare_subnet_sub_b)

        # Assert
        self.assertRaisesRegexp(
            ValueError,
            "^((?!limit).)*$",
            self.prepare_conn.prepare_connectivity,
            ec2_client=self.ec2_client,
            ec2_session=self.ec2_session,
            s3_session=self.s3_session,
            reservation=self.reservation,
            aws_ec2_datamodel=self.aws_dm,
            actions=actions,
            cancellation_context=self.cancellation_context,
            logger=Mock(),
        )
    def test_execute_with_no_vpc(self):
        # Arrange
        prepare_subnet = PrepareSubnet()
        prepare_subnet.actionId = "1"
        prepare_subnet.actionParams = PrepareSubnetParams()
        actions = [prepare_subnet]
        self.vpc_service.find_vpc_for_reservation = Mock(return_value=None)
        self.vpc_service.get_active_vpcs_count = Mock(return_value=None)

        self.reservation.reservation_id = "123"
        # Act
        with self.assertRaises(Exception,
                               msg="VPC for reservation 123 not found."):
            self.executor.execute(actions)
 def test_execute_gets_existing_subnet_and_no_wait(self):
     # Arrange
     prepare_subnet = PrepareSubnet()
     prepare_subnet.actionId = "1"
     prepare_subnet.actionParams = PrepareSubnetParams()
     prepare_subnet.actionParams.cidr = "1.2.3.4/24"
     actions = [prepare_subnet]
     subnet = Mock()
     subnet.subnet_id = "123"
     self.subnet_service.get_first_or_none_subnet_from_vpc = Mock(
         return_value=subnet)
     # Act
     result = self.executor.execute(actions)[0]
     # Assert
     self.assertEqual(result.subnetId, "123")
     self.subnet_waiter.wait.assert_not_called()
    def test_execute_creates_new_subnet_and_wait(self):
        # Arrange
        prepare_subnet = PrepareSubnet()
        prepare_subnet.actionId = "1"
        prepare_subnet.actionParams = PrepareSubnetParams()
        prepare_subnet.actionParams.cidr = "1.2.3.4/24"

        actions = [prepare_subnet]
        self.subnet_service.get_first_or_none_subnet_from_vpc = Mock(
            return_value=None)
        subnet = Mock()
        subnet.subnet_id = "456"
        self.subnet_service.create_subnet_nowait = Mock(return_value=subnet)
        # Act
        result = self.executor.execute(actions)[0]
        # Assert
        self.assertEqual(result.subnetId, "456")
        self.subnet_waiter.wait.assert_called_once()
 def test_prepare_conn_execute_the_subnet_actions(self):
     # Arrage
     actions = []
     prepare_subnet_sub_a = PrepareSubnet();
     prepare_subnet_sub_a.actionId = "SubA"
     prepare_subnet_sub_a.actionParams = PrepareSubnetParams()
     actions.append(prepare_subnet_sub_a)
     prepare_cloud_infra = PrepareCloudInfra()
     prepare_cloud_infra.actionId = "Net"
     prepare_cloud_infra.actionParams = PrepareCloudInfraParams()
     actions.append(prepare_cloud_infra)
     prepare_subnet_sub_b = PrepareSubnet();
     prepare_subnet_sub_b.actionId = "SubB"
     prepare_subnet_sub_b.actionParams = PrepareSubnetParams()
     actions.append(prepare_subnet_sub_b)
     prepare_create_key = CreateKeys();
     prepare_create_key.actionId = "CreateKeys"
     actions.append(prepare_create_key)
     # Act
     with patch('cloudshell.cp.aws.domain.conncetivity.operations.prepare.PrepareSubnetExecutor') as ctor:
         obj = Mock()
         obj.execute = Mock(return_value=["ResA", "ResB"])
         ctor.return_value = obj
         results = self.prepare_conn.prepare_connectivity(ec2_client=self.ec2_client,
                                                          ec2_session=self.ec2_session,
                                                          s3_session=self.s3_session,
                                                          reservation=self.reservation,
                                                          aws_ec2_datamodel=self.aws_dm,
                                                          actions=actions,
                                                          cancellation_context=self.cancellation_context,
                                                          logger=Mock())
     # Assert
     self.assertEqual(len(results), 4)
     self.assertEqual(results[2], "ResA")
     self.assertEqual(results[3], "ResB")
 def test_prepare_conn_execute_the_network_action_first(self):
     # Arrage
     actions = []
     prepare_subnet_sub_a = PrepareSubnet();
     prepare_subnet_sub_a.actionId = "SubA"
     prepare_subnet_sub_a.actionParams = PrepareSubnetParams()
     actions.append(prepare_subnet_sub_a)
     prepare_cloud_infra = PrepareCloudInfra()
     prepare_cloud_infra.actionId = "Net"
     prepare_cloud_infra.actionParams = PrepareCloudInfraParams()
     actions.append(prepare_cloud_infra)
     prepare_subnet_sub_b = PrepareSubnet();
     prepare_subnet_sub_b.actionId = "SubB"
     prepare_subnet_sub_b.actionParams = PrepareSubnetParams()
     actions.append(prepare_subnet_sub_b)
     prepare_create_key = CreateKeys();
     prepare_create_key.actionId = "CreateKeys"
     actions.append(prepare_create_key)
     # Act
     results = self.prepare_conn.prepare_connectivity(ec2_client=self.ec2_client,
                                                      ec2_session=self.ec2_session,
                                                      s3_session=self.s3_session,
                                                      reservation=self.reservation,
                                                      aws_ec2_datamodel=self.aws_dm,
                                                      actions=actions,
                                                      cancellation_context=self.cancellation_context,
                                                      logger=Mock())
     # Assert
     self.assertEqual(len(results), 4)
     self.assertEqual(results[0].actionId, "Net")
     self.assertEqual(results[1].actionId, "CreateKeys")
     self.assertEqual(results[2].actionId, "SubA")
     self.assertEqual(results[3].actionId, "SubB")
    def test_prepare_conn_error_no_vpc(self):
        self.vpc_serv.find_vpc_for_reservation = Mock(return_value=None)
        self.vpc_serv.get_active_vpcs_count = Mock(return_value=None)

        # Arrage
        actions = []
        prepare_subnet_sub_a = PrepareSubnet();
        prepare_subnet_sub_a.actionId = "SubA"
        prepare_subnet_sub_a.actionParams = PrepareSubnetParams()
        actions.append(prepare_subnet_sub_a)
        prepare_cloud_infra = PrepareCloudInfra()
        prepare_cloud_infra.actionId = "Net"
        prepare_cloud_infra.actionParams = PrepareCloudInfraParams()
        actions.append(prepare_cloud_infra)
        prepare_subnet_sub_b = PrepareSubnet();
        prepare_subnet_sub_b.actionId = "SubB"
        prepare_subnet_sub_b.actionParams = PrepareSubnetParams()
        actions.append(prepare_subnet_sub_b)

        # Assert
        self.assertRaisesRegexp(ValueError,
                                '^((?!limit).)*$',
                                self.prepare_conn.prepare_connectivity,
                                ec2_client=self.ec2_client,
                                ec2_session=self.ec2_session,
                                s3_session=self.s3_session,
                                reservation=self.reservation,
                                aws_ec2_datamodel=self.aws_dm,
                                actions=actions,
                                cancellation_context=self.cancellation_context,
                                logger=Mock())
 def test_prepare_conn_must_receive_network_action(self):
     with self.assertRaises(
         Exception, msg="Actions list must contain a PrepareCloudInfraAction."
     ):
         self.prepare_conn.prepare_connectivity(
             ec2_client=self.ec2_client,
             ec2_session=self.ec2_session,
             s3_session=self.s3_session,
             reservation=self.reservation,
             aws_ec2_datamodel=self.aws_dm,
             actions=[PrepareSubnet()],
             cancellation_context=self.cancellation_context,
             logger=Mock(),
         )
 def test_prepare_conn_execute_the_subnet_actions(self):
     # Arrage
     actions = []
     prepare_subnet_sub_a = PrepareSubnet()
     prepare_subnet_sub_a.actionId = "SubA"
     prepare_subnet_sub_a.actionParams = PrepareSubnetParams()
     actions.append(prepare_subnet_sub_a)
     prepare_cloud_infra = PrepareCloudInfra()
     prepare_cloud_infra.actionId = "Net"
     prepare_cloud_infra.actionParams = PrepareCloudInfraParams()
     actions.append(prepare_cloud_infra)
     prepare_subnet_sub_b = PrepareSubnet()
     prepare_subnet_sub_b.actionId = "SubB"
     prepare_subnet_sub_b.actionParams = PrepareSubnetParams()
     actions.append(prepare_subnet_sub_b)
     prepare_create_key = CreateKeys()
     prepare_create_key.actionId = "CreateKeys"
     actions.append(prepare_create_key)
     # Act
     with patch(
         "cloudshell.cp.aws.domain.conncetivity.operations.prepare."
         "PrepareSubnetExecutor"
     ) as ctor:
         obj = Mock()
         obj.execute = Mock(return_value=["ResA", "ResB"])
         ctor.return_value = obj
         results = self.prepare_conn.prepare_connectivity(
             ec2_client=self.ec2_client,
             ec2_session=self.ec2_session,
             s3_session=self.s3_session,
             reservation=self.reservation,
             aws_ec2_datamodel=self.aws_dm,
             actions=actions,
             cancellation_context=self.cancellation_context,
             logger=Mock(),
         )
     # Assert
     self.assertEqual(len(results), 4)
     self.assertEqual(results[2], "ResA")
     self.assertEqual(results[3], "ResB")
Exemplo n.º 13
0
    def test_prepare_conn_error_no_vpc_with_vpc_count(self):
        self.vpc_serv.find_vpc_for_reservation = Mock(return_value=None)

        vpc_count = 50
        self.vpc_serv.get_active_vpcs_count = Mock(return_value=vpc_count)

        # Arrage
        actions = []
        prepare_subnet_sub_a = PrepareSubnet()
        prepare_subnet_sub_a.actionId = "SubA"
        prepare_subnet_sub_a.actionParams = PrepareSubnetParams()
        actions.append(prepare_subnet_sub_a)
        prepare_cloud_infra = PrepareCloudInfra()
        prepare_cloud_infra.actionId = "Net"
        prepare_cloud_infra.actionParams = PrepareCloudInfraParams()
        actions.append(prepare_cloud_infra)
        prepare_subnet_sub_b = PrepareSubnet()
        prepare_subnet_sub_b.actionId = "SubB"
        prepare_subnet_sub_b.actionParams = PrepareSubnetParams()
        actions.append(prepare_subnet_sub_b)
        prepare_create_key = CreateKeys()
        prepare_create_key.actionId = "CreateKeys"
        actions.append(prepare_create_key)

        # Assert
        self.assertRaisesRegexp(ValueError,
                                '(/{0}/)|(limit.$)'.format(vpc_count),
                                self.prepare_conn.prepare_connectivity,
                                ec2_client=self.ec2_client,
                                ec2_session=self.ec2_session,
                                s3_session=self.s3_session,
                                reservation=self.reservation,
                                aws_ec2_datamodel=self.aws_dm,
                                actions=actions,
                                cancellation_context=self.cancellation_context,
                                logger=Mock())
    def test_prepare_conn_error_no_vpc_with_vpc_count(self):
        self.vpc_serv.find_vpc_for_reservation = Mock(return_value=None)

        vpc_count = 50
        self.vpc_serv.get_active_vpcs_count = Mock(return_value=vpc_count)

        # Arrage
        actions = []
        prepare_subnet_sub_a = PrepareSubnet();
        prepare_subnet_sub_a.actionId = "SubA"
        prepare_subnet_sub_a.actionParams = PrepareSubnetParams()
        actions.append(prepare_subnet_sub_a)
        prepare_cloud_infra = PrepareCloudInfra()
        prepare_cloud_infra.actionId = "Net"
        prepare_cloud_infra.actionParams = PrepareCloudInfraParams()
        actions.append(prepare_cloud_infra)
        prepare_subnet_sub_b = PrepareSubnet();
        prepare_subnet_sub_b.actionId = "SubB"
        prepare_subnet_sub_b.actionParams = PrepareSubnetParams()
        actions.append(prepare_subnet_sub_b)
        prepare_create_key = CreateKeys();
        prepare_create_key.actionId = "CreateKeys"
        actions.append(prepare_create_key)

        # Assert
        self.assertRaisesRegexp(ValueError,
                                '(/{0}/)|(limit.$)'.format(vpc_count),
                                self.prepare_conn.prepare_connectivity,
                                ec2_client=self.ec2_client,
                                ec2_session=self.ec2_session,
                                s3_session=self.s3_session,
                                reservation=self.reservation,
                                aws_ec2_datamodel=self.aws_dm,
                                actions=actions,
                                cancellation_context=self.cancellation_context,
                                logger=Mock())