def test_sg_informer_init(self):
        '''Test initialization of security group informers.'''

        informer = aws_informer.SecurityGroupInformer(self.sg_resource,
                                                      mediator=GLOBAL_MEDIATOR)
        self.assertIsNotNone(informer)
        self.assertIsNotNone(informer.mediator)
        self.assertTrue(isinstance(informer, aws_informer.AWSInformer))
        self.assertEqual(informer.entity_type, 'security_group')

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

        self.assertEqual(informer.identifier, informer.resource['GroupId'])
        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.SecurityGroupInformer(
            self.sg_resource, mediator=GLOBAL_MEDIATOR)
        self.assertTrue(informer2 is informer)
        self.assertEqual(len(informer.mediator.informer_cache),
                         informer_cache_length)
    def test_sg_informer_expand(self):
        '''Test expansion of security groupinformers.'''

        GLOBAL_MEDIATOR.flush()
        informer = aws_informer.SecurityGroupInformer(self.sg_resource,
                                                      mediator=GLOBAL_MEDIATOR)

        self.assertEqual(informer.expansions, {})

        informer.expand()

        for key in informer.expansions:
            self.assertIn(key, informer.resource)
Beispiel #3
0
    def test_sg_informer_site_init(self):
        '''Test Security Group informer site customizations.'''

        # - - - - - - - - - - - - - - - -
        # Test set_sg_informer_name_supplementals().
        # - - - - - - - - - - - - - - - -
        sg_resource = GLOBAL_MEDIATOR.entities('security_group')[0]
        sg_informer = aws_informer.SecurityGroupInformer(
            sg_resource, mediator=GLOBAL_MEDIATOR)

        self.assertIn('site-specific', sg_informer.supplementals)
        for field in site_boogio.SG_NAME_FIELDS:
            self.assertIn(field, sg_informer.supplementals['site-specific'])
    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))
    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)