def test_prepare_conn_command_fault_res(self):
        self.aws_dm.is_static_vpc_mode = False

        action = PrepareCloudInfra()
        action.actionId = "1234"
        action.actionParams = PrepareCloudInfraParams()
        action.actionParams.cidr = "1.2.3.4/24"
        action2 = CreateKeys()
        action2.actionId = "123"
        cancellation_context = Mock()

        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=[action, action2],
            cancellation_context=cancellation_context,
            logger=Mock(),
        )

        self.assertFalse(results[0].success)
        self.assertEqual(results[0].infoMessage, "")
        self.assertIsNotNone(results[0].errorMessage)
 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_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_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_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_command(self):
        # Arrange
        action = PrepareCloudInfra()
        action.actionId = "1234"
        action.actionParams = PrepareCloudInfraParams()

        action2 = CreateKeys()
        action2.actionId = "123"

        self.vpc_serv.get_peering_connection_by_reservation_id = Mock(return_value=None)
        access_key = Mock()
        self.prepare_conn._get_or_create_key_pair = Mock(return_value=access_key)
        self.route_table_service.get_all_route_tables = Mock(return_value=MagicMock())

        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=[action,action2],
                                                         cancellation_context=self.cancellation_context,
                                                         logger=Mock())


        self.assertEqual(results[0].actionId, action.actionId)
        self.assertTrue(results[0].success)
        self.assertEqual(results[0].infoMessage, 'PrepareCloudInfra finished successfully')
        self.assertEqual(results[0].errorMessage, '')
        self.assertEqual(results[1].actionId, action2.actionId)
        self.assertTrue(results[1].success)
        self.assertEqual(results[1].infoMessage, 'PrepareCreateKeys finished successfully')
        self.assertEqual(results[1].errorMessage, '')
        self.assertEqual(results[1].accessKey, access_key)
        self.cancellation_service.check_if_cancelled.assert_called()
예제 #8
0
    def test_subnet_connection_params_check_is_public_subnet_true_when_no_attribute(self):
        # arrange
        test_obj = PrepareSubnetParams()
        attr1 = PrepareCloudInfra()
        attr1.name = "Some Attribute"
        attr1.value = "Some Value"
        test_obj.subnetServiceAttributes = [attr1]

        # act
        is_public = test_obj.isPublic

        # assert
        self.assertTrue(is_public)
예제 #9
0
    def test_subnet_connection_params_check_is_public_subnet_true_when_no_attribute(
            self):
        # arrange
        test_obj = PrepareSubnetParams()
        attr1 = PrepareCloudInfra()
        attr1.name = "Some Attribute"
        attr1.value = "Some Value"
        test_obj.subnetServiceAttributes = [attr1]

        # act
        is_public = test_obj.isPublic

        # assert
        self.assertTrue(is_public)
예제 #10
0
    def test_cleanup(self):
        self.route_table_service.get_all_route_tables = Mock(
            return_value=[Mock(), Mock()])
        vpc = self.vpc_serv.find_vpc_for_reservation()

        self.cleanup_operation.cleanup(
            ec2_session=self.ec2_session,
            s3_session=self.s3_session,
            aws_ec2_data_model=self.aws_ec2_data_model,
            reservation_id=self.reservation_id,
            logger=Mock(),
            actions=[PrepareCloudInfra()],
            ec2_client=Mock())

        self.assertTrue(
            self.vpc_serv.find_vpc_for_reservation.called_with(
                self.ec2_session, self.reservation_id))
        self.assertTrue(
            self.key_pair_serv.remove_key_pair_for_reservation_in_s3.
            called_with(self.s3_session, self.aws_ec2_data_model,
                        self.reservation_id))
        self.assertTrue(self.vpc_serv.delete_all_instances.called_with(vpc))
        self.assertTrue(
            self.vpc_serv.remove_all_security_groups.called_with(vpc))
        self.assertTrue(self.vpc_serv.remove_all_subnets.called_with(vpc))
        self.assertTrue(self.vpc_serv.remove_all_peering.called_with(vpc))
        self.assertTrue(self.vpc_serv.delete_vpc.called_with(vpc))
        self.route_table_service.get_all_route_tables.assert_called_once_with(
            ec2_session=self.ec2_session,
            vpc_id=self.aws_ec2_data_model.aws_management_vpc_id)
        self.assertEquals(
            self.route_table_service.delete_blackhole_routes.call_count, 2)
    def test_prepare_conn_command_fault_res(self):
        action = PrepareCloudInfra()
        action.actionId="1234"
        action.actionParams = PrepareCloudInfraParams()
        action.actionParams.cidr = "1.2.3.4/24"
        action2 = CreateKeys()
        action2.actionId="123"
        cancellation_context = Mock()

        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=[action, action2],
                                                         cancellation_context=cancellation_context,
                                                         logger=Mock())

        self.assertFalse(results[0].success)
        self.assertEqual(results[0].infoMessage, '')
        self.assertIsNotNone(results[0].errorMessage)
 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_command(self):
        # Arrange
        action = PrepareCloudInfra()
        action.actionId = "1234"
        action.actionParams = PrepareCloudInfraParams()

        action2 = CreateKeys()
        action2.actionId = "123"

        self.vpc_serv.get_peering_connection_by_reservation_id = Mock(return_value=None)
        access_key = Mock()
        self.prepare_conn._get_or_create_key_pair = Mock(return_value=access_key)
        self.route_table_service.get_all_route_tables = Mock(return_value=MagicMock())

        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=[action, action2],
            cancellation_context=self.cancellation_context,
            logger=Mock(),
        )

        self.assertEqual(results[0].actionId, action.actionId)
        self.assertTrue(results[0].success)
        self.assertEqual(
            results[0].infoMessage, "PrepareCloudInfra finished successfully"
        )
        self.assertEqual(results[0].errorMessage, "")
        self.assertEqual(results[1].actionId, action2.actionId)
        self.assertTrue(results[1].success)
        self.assertEqual(
            results[1].infoMessage, "PrepareCreateKeys finished successfully"
        )
        self.assertEqual(results[1].errorMessage, "")
        self.assertEqual(results[1].accessKey, access_key)
        self.cancellation_service.check_if_cancelled.assert_called()
예제 #14
0
    def test_cleanup_no_vpc(self):
        vpc_serv = Mock()
        vpc_serv.find_vpc_for_reservation = Mock(return_value=None)
        result = CleanupSandboxInfraOperation(vpc_serv, self.key_pair_serv, self.route_table_service) \
            .cleanup(
                ec2_session=self.ec2_session,
                s3_session=self.s3_session,
                aws_ec2_data_model=self.aws_ec2_data_model,
                reservation_id=self.reservation_id,
                ec2_client=Mock(),
                actions=[PrepareCloudInfra()],
                logger=Mock())

        self.assertFalse(result.success)
    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())
예제 #16
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())