Example #1
0
 def find_instances(self, filter_arg, address):
     """
     Searches for AWS instance based on given filter argument and ip address (private or normal).
     """
     f = Filter()
     f.add_filter(
         name=filter_arg,
         value=address
     )
     return self.ec2.find_instances(f)
Example #2
0
    def test_find_instances_by_hostname(self):
        """
        EC2.find_instances_by_hostname correctly locate instances based hostname.
        """
        filter = Filter()
        filter.add_filter('instance-state-name', 'running')
        filter.add_filter('instance-state-name', 'stopped')
        filter.add_tag_filter('Name', self.FAKE_HOSTNAME)

        self._ec2.find_instances_by_hostname(self.FAKE_HOSTNAME)

        self._logger.debug.assert_called_once_with(
            'Filters to use: %s', dict(filter)
        )
Example #3
0
    def convert_args(self):
        """
        Convert options dictionary to use AWS filters as keys instead of CLI options.
        """

        # Dictionary of options and values to put in the Filter
        include_filter = Filter()

        # Add entries to include_filter with key=AWS filters and value=option
        # values for options that filter on inclusion
        for opt_name in Application._OPTS:
            for opt_value in self.options[opt_name]:
                include_filter.add_filter(
                    name=Application._CLI_TO_AWS[opt_name],
                    value=self._EC2_FILTER_VALUE_TEMPLATE.format(value=opt_value)
                )

        return include_filter
Example #4
0
    def test_attach_ebs_volume_id(self):
        """
        EC2.attach_ebs_volume correctly attach an EBS volume to an instance when given the ID
        """
        filter = Filter()
        filter.add_filter('volume-id', self.FAKE_VOLUME.id)
        self._resource.volumes.filter.return_value = [self.FAKE_VOLUME]

        volume = self._ec2.attach_ebs_volume(
            device=self.FAKE_DEVICE,
            instance=self.FAKE_INSTANCE,
            save_on_termination=False,
            volume_id=self.FAKE_VOLUME.id,
        )

        self.assertEqual(self.FAKE_VOLUME, volume)

        self.assertFalse(self.FAKE_VOLUME.reload.called)

        debug_calls = [
            call('Filters to use: %s', dict(filter)),
            call('Waiting for the EBS volume to be attached...'),
        ]
        self.assertEqual(debug_calls, self._logger.debug.call_args_list)
Example #5
0
class FilterTest(unittest.TestCase):
    """
    Test case for Filter
    """
    TEST_TAG_KEY = 'Name'
    TEST_TAG_KEY_FULL = 'tag:' + TEST_TAG_KEY
    TEST_TAG_VALUE = 'example.krxd.net'
    TEST_FILTER_KEY = 'instance-state-name'
    TEST_FILTER_VALUE_1 = 'running'
    TEST_FILTER_VALUE_2 = 'stopped'
    TEST_FILTER_VALUE = [TEST_FILTER_VALUE_1, TEST_FILTER_VALUE_2]
    TEST_FILTER_STR = TEST_FILTER_KEY + '=' + TEST_FILTER_VALUE_1

    def setUp(self):
        self.f = Filter()

    def test_init_dict(self):
        """
        Ensure dict passed to __init__ is initialized.
        """
        dic = {
            self.TEST_TAG_KEY_FULL: [self.TEST_TAG_VALUE],
            self.TEST_FILTER_KEY: self.TEST_FILTER_VALUE,
        }
        self.f = Filter(dic)

        self.assertEqual(dic, self.f)

    def test_init_none(self):
        """
        Ensure that filters are initialized empty.
        """
        self.assertEqual({}, self.f)

    def test_init_keywords(self):
        """
        Ensure that filters can be initialized with keywords.
        """
        dic = {
            self.TEST_TAG_KEY_FULL: [self.TEST_TAG_VALUE],
            self.TEST_FILTER_KEY: self.TEST_FILTER_VALUE,
        }
        self.f = Filter(**dic)

        self.assertEqual(dic, self.f)

    def test_add_filter_new(self):
        """
        Make sure Filter.add_filter mutates filter as expected.
        """
        self.f.add_filter(self.TEST_FILTER_KEY, self.TEST_FILTER_VALUE_1)
        self.assertIn(self.TEST_FILTER_KEY, self.f)
        self.assertEqual([self.TEST_FILTER_VALUE_1], self.f[self.TEST_FILTER_KEY])

    def test_add_filter_existing(self):
        """
        Make sure Filter.add_filter is an append operation.
        """
        self.f.add_filter(self.TEST_FILTER_KEY, self.TEST_FILTER_VALUE_1)
        self.f.add_filter(self.TEST_FILTER_KEY, self.TEST_FILTER_VALUE_2)
        self.assertIn(self.TEST_FILTER_KEY, self.f)
        self.assertEqual(self.TEST_FILTER_VALUE, self.f[self.TEST_FILTER_KEY])

    def test_add_tag_filter(self):
        """
        Make sure Filter.add_tag_filter helper creates the appropriate tag filter.
        """
        self.f.add_tag_filter(self.TEST_TAG_KEY, self.TEST_TAG_VALUE)
        self.assertIn(self.TEST_TAG_KEY_FULL, self.f)
        self.assertEqual([self.TEST_TAG_VALUE], self.f[self.TEST_TAG_KEY_FULL])

    def test_parse_string_name_value(self):
        """
        Make sure Filter.parse_string_name_value parses argument names.
        """
        self.f.parse_string(self.TEST_FILTER_STR)
        self.assertIn(self.TEST_FILTER_KEY, self.f)
        self.assertEqual([self.TEST_FILTER_VALUE_1], self.f[self.TEST_FILTER_KEY])

    def test_parse_string_value(self):
        """
        Make sure Filter.parse_string_name_value parses argument values.
        """
        self.f.parse_string(self.TEST_TAG_VALUE)
        self.assertIn('tag-value', self.f._filter)
        self.assertEqual([self.TEST_TAG_VALUE], self.f._filter['tag-value'])

    def test_set(self):
        """
        Make sure Filter attributes can be set via ['name'] notation
        """
        self.f[self.TEST_FILTER_KEY] = self.TEST_FILTER_VALUE
        self.assertEqual(self.TEST_FILTER_VALUE, self.f[self.TEST_FILTER_KEY])

    def test_del(self):
        """
        Make sure Filter attributes can be deleted via ['name'] notation
        """
        self.f.add_filter(self.TEST_FILTER_KEY, self.TEST_FILTER_VALUE_1)
        del self.f[self.TEST_FILTER_KEY]
        self.assertNotIn(self.TEST_FILTER_KEY, self.f._filter)

    def test_iter(self):
        """
        Make sure Filter attributes can be iterated
        """
        self.f.add_filter(self.TEST_FILTER_KEY, self.TEST_FILTER_VALUE_1)
        self.assertEqual([self.TEST_FILTER_KEY], [key for key in self.f])

    def test_len(self):
        """
        Make sure Filter handles len() properly
        """
        self.f.add_filter(self.TEST_FILTER_KEY, self.TEST_FILTER_VALUE_1)
        self.assertEqual(1, len(self.f))