Ejemplo n.º 1
0
    def test_ec2instance_informer_init(self):
        '''Test initialization of ec2 informers.'''

        informer = aws_informer.EC2InstanceInformer(self.ec2_resource,
                                                    mediator=GLOBAL_MEDIATOR)
        self.assertIsNotNone(informer)
        self.assertIsNotNone(informer.mediator)
        self.assertTrue(isinstance(informer, aws_informer.AWSInformer))
        self.assertEqual(informer.entity_type, 'ec2')

        self.assertIsNotNone(informer.region_name)
        self.assertEqual(informer.region_name, GLOBAL_MEDIATOR.region_name)

        self.assertEqual(informer.identifier, informer.resource['InstanceId'])
        self.assertIn(informer.identifier,
                      informer.mediator.informer_cache.keys())

        # Let's make sure informer caching works. We shouldn't add any
        # new cache entries if we create another informer for this
        # resource - it should be re-used.
        informer_cache_length = len(informer.mediator.informer_cache)
        informer2 = aws_informer.EC2InstanceInformer(self.ec2_resource,
                                                     mediator=GLOBAL_MEDIATOR)
        self.assertTrue(informer2 is informer)
        self.assertEqual(len(informer.mediator.informer_cache),
                         informer_cache_length)
Ejemplo n.º 2
0
    def test_ec2_informer_site_init(self):
        '''Test EC2 informer site customizations.'''

        # - - - - - - - - - - - - - - - -
        # Test set_ec2_informer_keyname_environment().
        # - - - - - - - - - - - - - - - -
        ec2_entities_with_keyname = [
            x for x in GLOBAL_MEDIATOR.entities('ec2')
            if ('KeyName' in x and x['KeyName'][:len('oncue')] == 'oncue')
        ]

        if not ec2_entities_with_keyname:
            raise RuntimeError('no EC2 entities with an oncue KeyName')

        ec2_keyname_resource = ec2_entities_with_keyname[0]
        keyname_informer = aws_informer.EC2InstanceInformer(
            ec2_keyname_resource, mediator=GLOBAL_MEDIATOR)

        self.assertIn('site-specific', keyname_informer.supplementals)
        self.assertIn('environment',
                      keyname_informer.supplementals['site-specific'])

        # - - - - - - - - - - - - - - - -
        # Test set_ec2_informer_name_supplementals().
        # - - - - - - - - - - - - - - - -
        ec2_entities_with_tags_name = [
            x for x in GLOBAL_MEDIATOR.entities('ec2')
            if ('Tags' in x and 'Name' in [t['Key'] for t in x['Tags']])
        ]

        if not ec2_entities_with_tags_name:
            raise RuntimeError('no EC2 entities with a KeyName')

        ec2_tags_name_resource = ec2_entities_with_tags_name[0]
        tags_name_informer = aws_informer.EC2InstanceInformer(
            ec2_tags_name_resource, mediator=GLOBAL_MEDIATOR)
        for field in site_boogio.EC2_NAME_FIELDS:
            self.assertIn(field,
                          tags_name_informer.supplementals['site-specific'])
Ejemplo n.º 3
0
    def test_ec2instance_informer_expand(self):
        '''Test expansion of ec2 informers.'''

        GLOBAL_MEDIATOR.flush()
        informer = aws_informer.EC2InstanceInformer(self.ec2_resource,
                                                    mediator=GLOBAL_MEDIATOR)

        self.assertEqual(informer.expansions, {})

        informer.expand()

        self.assertTrue(informer.is_expanded)

        for key in informer.expansions:
            self.assertIn(key, informer.resource)

        # - - - - - - - - - - - - - - - - - - - -
        # Security Group resources.
        # - - - - - - - - - - - - - - - - - - - -

        self.assertIn('SecurityGroups', informer.expansions)
        self.assertEqual(len(informer.expansions['SecurityGroups']),
                         len(informer.resource['SecurityGroups']))

        # We need to have retrieved some security groups for the rest
        # of the tests in this section to make sense.
        self.assertTrue(len(informer.expansions['SecurityGroups']) > 0)
        self.assertTrue(
            isinstance(informer.expansions['SecurityGroups'][0],
                       aws_informer.SecurityGroupInformer))
        self.assertIs(informer.expansions['SecurityGroups'][0].mediator,
                      informer.mediator)
        self.assertTrue(
            True in
            [sg.is_expanded for sg in informer.expansions['SecurityGroups']])
        self.assertTrue(
            False not in
            [sg.is_expanded for sg in informer.expansions['SecurityGroups']])
Ejemplo n.º 4
0
    def test_elb_informer_caching(self):
        '''Test informer caching.'''

        # pylint: disable=protected-access

        self.assertEqual(GLOBAL_MEDIATOR.informer_cache, {})

        # - - - - - - - - - - - - - - - - - - - -

        ec2_informers = [
            aws_informer.EC2InstanceInformer(ec2_resource,
                                             mediator=GLOBAL_MEDIATOR)
            for ec2_resource in self.ec2_resources
        ]
        ec2_informers_cached = cached_type('ec2')

        # - - - - - - - - - - - - - - - - - - - -

        elb_informers = [
            aws_informer.ELBInformer(elb_resource, mediator=GLOBAL_MEDIATOR)
            for elb_resource in self.elb_resources
        ]
        elb_informers_cached = cached_type('elb')

        # - - - - - - - - - - - - - - - - - - - -

        vpc_informers = [
            aws_informer.VPCInformer(vpc_resource, mediator=GLOBAL_MEDIATOR)
            for vpc_resource in self.vpc_resources
        ]
        vpc_informers_cached = cached_type('vpc')

        # - - - - - - - - - - - - - - - - - - - -

        sg_informers = [
            aws_informer.SecurityGroupInformer(sg_resource,
                                               mediator=GLOBAL_MEDIATOR)
            for sg_resource in self.sg_resources
        ]
        sg_informers_cached = cached_type('security_group')

        # - - - - - - - - - - - - - - - - - - - -

        self.assertTrue(len(ec2_informers_cached) > 0)
        self.assertEqual(len(ec2_informers), len(ec2_informers_cached))

        self.assertTrue(len(elb_informers_cached) > 0)
        self.assertEqual(len(elb_informers), len(elb_informers_cached))

        self.assertTrue(len(vpc_informers_cached) > 0)
        self.assertEqual(len(vpc_informers), len(vpc_informers_cached))

        self.assertTrue(len(sg_informers_cached) > 0)
        self.assertEqual(len(sg_informers), len(sg_informers_cached))

        # - - - - - - - - - - - - - - - - - - - -

        GLOBAL_MEDIATOR.flush('vpc')

        ec2_informers_cached = cached_type('ec2')
        elb_informers_cached = cached_type('elb')
        vpc_informers_cached = cached_type('vpc')
        sg_informers_cached = cached_type('security_group')

        self.assertTrue(len(vpc_informers_cached) == 0)
        self.assertIsNone(GLOBAL_MEDIATOR._other_entities['vpc'])

        # There may be some unselected entities, so >=, not ==.
        self.assertEqual(len(ec2_informers), len(ec2_informers_cached))
        self.assertTrue(
            len(GLOBAL_MEDIATOR._services['ec2']) >= len(ec2_informers))

        self.assertEqual(len(elb_informers), len(elb_informers_cached))
        self.assertTrue(
            len(GLOBAL_MEDIATOR._services['elb']) >= len(elb_informers))

        self.assertEqual(len(sg_informers), len(sg_informers_cached))
        self.assertTrue(
            len(GLOBAL_MEDIATOR._other_entities['security_group']) >= len(
                sg_informers))

        # - - - - - - - - - - - - - - - - - - - -

        GLOBAL_MEDIATOR.flush('ec2', 'security_group')

        ec2_informers_cached = cached_type('ec2')
        elb_informers_cached = cached_type('elb')
        vpc_informers_cached = cached_type('vpc')
        sg_informers_cached = cached_type('security_group')

        self.assertTrue(len(ec2_informers_cached) == 0)
        self.assertIsNone(GLOBAL_MEDIATOR._services['ec2'])

        self.assertTrue(len(vpc_informers_cached) == 0)
        self.assertIsNone(GLOBAL_MEDIATOR._other_entities['vpc'])

        self.assertTrue(len(sg_informers_cached) == 0)
        self.assertIsNone(GLOBAL_MEDIATOR._other_entities['security_group'])

        # There may be some unselected entities, so >=, not ==.
        self.assertEqual(len(elb_informers), len(elb_informers_cached))
        self.assertTrue(
            len(GLOBAL_MEDIATOR._services['elb']) >= len(elb_informers))
Ejemplo n.º 5
0
    def test_elb_informer_caching_with_expansion(self):
        '''Test expansion of informers with caching.'''

        # pylint: disable=protected-access

        # - - - - - - - - - - - - - - - - - - - -

        ec2_informers_count = len([
            aws_informer.EC2InstanceInformer(ec2_resource,
                                             mediator=GLOBAL_MEDIATOR)
            for ec2_resource in self.ec2_resources
        ])
        ec2_informers_cached_count = len(cached_type('ec2'))

        # - - - - - - - - - - - - - - - - - - - -

        elb_informers_count = len([
            aws_informer.ELBInformer(elb_resource, mediator=GLOBAL_MEDIATOR)
            for elb_resource in self.elb_resources
        ])
        elb_informers_cached_count = len(cached_type('elb'))

        # - - - - - - - - - - - - - - - - - - - -

        vpc_informers_count = len([
            aws_informer.VPCInformer(vpc_resource, mediator=GLOBAL_MEDIATOR)
            for vpc_resource in self.vpc_resources
        ])
        vpc_informers_cached_count = len(cached_type('vpc'))

        # - - - - - - - - - - - - - - - - - - - -

        sg_informers_count = len([
            aws_informer.SecurityGroupInformer(sg_resource,
                                               mediator=GLOBAL_MEDIATOR)
            for sg_resource in self.sg_resources
        ])
        sg_informers_cached_count = len(cached_type('security_group'))

        # - - - - - - - - - - - - - - - - - - - -

        self.assertTrue(ec2_informers_cached_count > 0)
        self.assertEqual(ec2_informers_count, ec2_informers_cached_count)

        self.assertTrue(elb_informers_cached_count > 0)
        self.assertEqual(elb_informers_count, elb_informers_cached_count)

        self.assertTrue(vpc_informers_cached_count > 0)
        self.assertEqual(vpc_informers_count, vpc_informers_cached_count)

        self.assertTrue(sg_informers_cached_count > 0)
        self.assertEqual(sg_informers_count, sg_informers_cached_count)

        # - - - - - - - - - - - - - - - - - - - -

        for informer in GLOBAL_MEDIATOR.informer_cache.values():
            informer.expand()

        new_ec2_informers_cached_count = len(cached_type('ec2'))
        new_elb_informers_cached_count = len(cached_type('elb'))
        new_vpc_informers_cached_count = len(cached_type('vpc'))
        new_sg_informers_cached_count = len(cached_type('security_group'))

        self.assertEqual(new_ec2_informers_cached_count,
                         ec2_informers_cached_count)
        self.assertEqual(new_elb_informers_cached_count,
                         elb_informers_cached_count)
        self.assertEqual(new_vpc_informers_cached_count,
                         vpc_informers_cached_count)
        self.assertEqual(new_sg_informers_cached_count,
                         sg_informers_cached_count)