def test_blank_return_values(self):

        describe_instance_blank_return_value = {
            'Reservations': [
                {
                    'Instances': [
                        {
                            'InstanceId': self.instance_id,
                            'Placement': {
                                'AvailabilityZone': '',
                            },
                        },
                    ],
                },
            ],
        }

        mock_session = mock.Mock()
        mock_boto_client = mock.Mock()
        mock_session.create_client.return_value = mock_boto_client
        mock_boto_client.describe_instances.return_value = describe_instance_blank_return_value

        with self.assertRaises(SystemExit) as context:
            ec2_util.get_instance_data(mock_session, self.instance_id)
            mock_boto_client.describe_instances.assert_called_with(InstanceIds=[self.instance_id])
        self.assertEqual(context.exception.code, 7)
    def test_no_dns_or_ip(self):

        describe_instance_blank_return_value = {
            'Reservations': [
                {
                    'Instances': [
                        {
                            'InstanceId': self.instance_id,
                            'Placement': {
                                'AvailabilityZone': self.availability_zone,
                            },
                            'PublicDnsName': None,
                            'PrivateDnsName': None,
                            'PublicIpAddress': None,
                            'PrivateIpAddress': None
                        },
                    ],
                },
            ],
        }

        mock_session = mock.Mock()
        mock_boto_client = mock.Mock()
        mock_session.create_client.return_value = mock_boto_client
        mock_boto_client.describe_instances.return_value = describe_instance_blank_return_value

        with self.assertRaises(SystemExit) as context:
            ec2_util.get_instance_data(mock_session, self.instance_id)
            mock_boto_client.describe_instances.assert_called_with(InstanceIds=[self.instance_id])
        self.assertEqual(context.exception.code, 8)
    def call_ec2(self):
        """
        Fetches information on the associated EC2 instance
        """

        for bundle in self.instance_bundles:
            session = bundle['session']
            #If bundle['target'] has a value, then use it.
            if bundle['target']:
                bundle['host_info'] = bundle['target']
            else:
                bundle['host_info'] = None

            if (bundle['target'] and bundle['zone']) or len(
                    bundle['instance_id']) == 0:
                # If both are specified or we're not using an instance then we have no reason to call EC2
                self.logger.debug("{0} does not require lookup".format(
                    bundle['target']))
                continue

            instance_info = ec2_util.get_instance_data(session,
                                                       bundle['instance_id'])
            bundle['zone'] = instance_info.availability_zone
            #If host_info is not available, fallback to using public ipaddress and then private ipaddress.
            if not bundle['host_info']:
                bundle[
                    'host_info'] = instance_info.public_ip if instance_info.public_ip else instance_info.private_ip
            self.logger.debug(
                'Successfully got instance information from EC2 API for {0}'.
                format(bundle['instance_id']))
    def test_get_instance_data(self):

        describe_instance_correct_return_value = {
            'Reservations': [
                {
                    'Instances': [
                        {
                            'InstanceId': self.instance_id,
                            'Placement': {
                                'AvailabilityZone': self.availability_zone,
                            },
                            'PublicDnsName': self.public_dns_name,
                            'PrivateDnsName': self.private_dns_name,
                            'PublicIpAddress': self.public_ip,
                            'PrivateIpAddress': self.private_ip
                        },
                    ],
                },
            ],
        }

        mock_session = mock.Mock()
        mock_boto_client = mock.Mock()
        mock_session.create_client.return_value = mock_boto_client
        mock_boto_client.describe_instances.return_value = describe_instance_correct_return_value

        instance_info = ec2_util.get_instance_data(mock_session, self.instance_id)
        mock_boto_client.describe_instances.assert_called_with(InstanceIds=[self.instance_id])

        self.assertEqual(instance_info.public_dns_name, self.public_dns_name)
        self.assertEqual(instance_info.private_dns_name, self.private_dns_name)
        self.assertEqual(instance_info.public_ip, self.public_ip)
        self.assertEqual(instance_info.private_ip, self.private_ip)
        self.assertEqual(instance_info.availability_zone, self.availability_zone)
    def test_invalid_key_in_response(self):

        describe_instance_wrong_return_value = {
            'Reservations': [
                {
                    'Instances': [
                        {
                            'InstanceId': self.instance_id,
                            'WRONG': {
                            },
                        },
                    ],
                },
            ],
        }

        mock_session = mock.Mock()
        mock_boto_client = mock.Mock()
        mock_session.create_client.return_value = mock_boto_client
        mock_boto_client.describe_instances.return_value = describe_instance_wrong_return_value

        with self.assertRaises(SystemExit) as context:
            ec2_util.get_instance_data(mock_session, self.instance_id)
        self.assertEqual(context.exception.code, 1)