예제 #1
0
  def testInternetGatewayLifecycle(self, mock_cmd):
    region = 'us-west-1'
    gateway_id = 'igw-123'
    create_response = {'InternetGateway': {'InternetGatewayId': gateway_id}}
    exists_response = {'InternetGateways': [{'InternetGatewayId': gateway_id}]}
    exists_response_no_resources = {'InternetGateways': []}
    delete_response = {}
    create_cmd = _AwsCommand(region, 'create-internet-gateway')
    describe_cmd = _AwsCommand(
        region, 'describe-internet-gateways',
        '--filter=Name=internet-gateway-id,Values={}'.format(gateway_id))
    delete_cmd = _AwsCommand(region, 'delete-internet-gateway',
                             '--internet-gateway-id={}'.format(gateway_id))

    # Test the Create() command
    mock_cmd.side_effect = [(json.dumps(create_response), '', 0)]
    util.IssueRetryableCommand.side_effect = [(json.dumps(exists_response), '')]
    gateway = aws_network.AwsInternetGateway(region)
    gateway.Create()
    mock_cmd.assert_called_with(create_cmd)
    util.IssueRetryableCommand.assert_called_with(describe_cmd)

    # Reset the mocks and call the Delete() command.
    mock_cmd.reset_mock()
    util.IssueRetryableCommand.reset_mock()
    # Confirms that _Delete() is called twice as the first describe response
    # still shows the gateway id as active.
    mock_cmd.side_effect = [(json.dumps(delete_response), '', 0)] * 2
    util.IssueRetryableCommand.side_effect = [
        (json.dumps(exists_response), ''),
        (json.dumps(exists_response_no_resources), '')
    ]
    gateway.Delete()
    mock_cmd.assert_called_with(delete_cmd, raise_on_failure=False)
    util.IssueRetryableCommand.assert_called_with(describe_cmd)
 def testCreateWithVpcButNotFound(self):
     # the query does not find an attached gateway
     self.SetExpectedCommands(GATEWAY_QUERY_NONE)
     gw = aws_network.AwsInternetGateway(REGION, VPC_ID)
     self.assertCommandsCalled()
     self.assertFalse(gw.attached)
     self.assertIsNone(gw.id)
 def testCreateNoVpc(self):
     self.SetExpectedCommands(CREATE_GATEWAY, GATEWAY_QUERY_ONE)
     gw = aws_network.AwsInternetGateway(REGION)
     gw.Create()
     self.assertFalse(gw.attached)
     self.assertEqual(GATEWAY_ID, gw.id)
     self.assertCommandsCalled()
 def testDetach(self):
     self.SetExpectedCommands(CREATE_GATEWAY, GATEWAY_QUERY_ONE)
     gw = aws_network.AwsInternetGateway(REGION)
     gw.Create()
     self.SetExpectedCommands(ATTACH_GATEWAY)
     gw.Attach(VPC_ID)
     self.SetExpectedCommands(DETACH_GATEWAY)
     gw.Detach()
     self.assertCommandsCalled()
     self.SetExpectedCommands(GATEWAY_QUERY_ONE)
     self.assertTrue(gw._Exists())
     self.assertQueryCommand(True)
 def testCreateWithVpc(self):
     # the query does find an attached gateway
     self.SetExpectedCommands(GATEWAY_QUERY_ONE)
     gw = aws_network.AwsInternetGateway(REGION, VPC_ID)
     self.assertCommandsCalled()
     self.assertTrue(gw.attached)
     self.assertEqual(GATEWAY_ID, gw.id)
     self.assertQueryCommand(
         False)  # should do a query with attached.vpc-id
     self.mock_aws.reset_mock()
     gw.Create()
     self.mock_aws.assert_not_called()
 def testNoVpcNoIdThrowsExeception(self):
     gw = aws_network.AwsInternetGateway(REGION)
     with self.assertRaises(errors.Error):
         gw.GetDict()