def test_elb_informer_init(self): '''Test initialization of elb informers.''' informer = aws_informer.ELBInformer(self.elb_resource, mediator=GLOBAL_MEDIATOR) self.assertIsNotNone(informer) self.assertIsNotNone(informer.mediator) self.assertTrue(isinstance(informer, aws_informer.AWSInformer)) self.assertEqual(informer.entity_type, 'elb') self.assertIsNotNone(informer.region_name) self.assertEqual(informer.region_name, GLOBAL_MEDIATOR.region_name) self.assertEqual(informer.identifier, informer.resource['LoadBalancerName']) 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.ELBInformer(self.elb_resource, mediator=GLOBAL_MEDIATOR) self.assertTrue(informer2 is informer) self.assertEqual(len(informer.mediator.informer_cache), informer_cache_length)
def test_elb_informer_expand(self): '''Test expansion of elb informers.''' GLOBAL_MEDIATOR.flush() informer = aws_informer.ELBInformer(self.elb_resource, mediator=GLOBAL_MEDIATOR) self.assertEqual(informer.expansions, {}) self.assertFalse(informer.is_expanded) informer.expand() self.assertTrue(informer.is_expanded) # Confirming that cached informers don't get re-initialized. # We don't test this for all classes, just once here. informer2 = aws_informer.ELBInformer(self.elb_resource, mediator=GLOBAL_MEDIATOR) self.assertTrue(informer2.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']]) # - - - - - - - - - - - - - - - - - - - - # Make sure we get as many entities in the expanded # informer's to_dict() as we have in the basic resource. # - - - - - - - - - - - - - - - - - - - - self.assertTrue(informer.is_expanded) self.assertEqual(len(informer.resource['SecurityGroups']), len(informer.to_dict()['SecurityGroups'])) self.assertEqual(len(informer.resource['Subnets']), len(informer.to_dict()['Subnets'])) self.assertEqual(len(informer.resource['Instances']), len(informer.to_dict()['Instances']))
def test_elb_informer_to_dict(self): '''Test elb resource to_dict output .''' # We don't know what a particular resource might look like, and # different resource configurations can hit different issues. So # we'll try a few and hope for the best. # - - - - - - - - - - - - - - - - - - - - resource = GLOBAL_MEDIATOR.entities('elb')[-1] informer = aws_informer.ELBInformer(resource, mediator=GLOBAL_MEDIATOR) self.assertTrue(isinstance(informer.to_dict(), dict)) informer.expand() self.assertTrue(isinstance(informer.to_dict(), dict)) if 'Vpcs' in informer.expansions: self.assertEqual(informer.to_dict()['Vpcs'], informer.expansions['Vpcs'].to_dict()) # - - - - - - - - - - - - - - - - - - - - r_index = random.randint(1, len(GLOBAL_MEDIATOR.entities('elb'))) resource = GLOBAL_MEDIATOR.entities('elb')[r_index] informer = aws_informer.ELBInformer(resource, mediator=GLOBAL_MEDIATOR) self.assertTrue(isinstance(informer.to_dict(), dict)) informer.expand() self.assertTrue(isinstance(informer.to_dict(), dict)) if 'Vpcs' in informer.expansions: self.assertEqual(informer.to_dict()['Vpcs'], informer.expansions['Vpcs'].to_dict()) # - - - - - - - - - - - - - - - - - - - - r_index = random.randint(1, len(GLOBAL_MEDIATOR.entities('elb'))) resource = GLOBAL_MEDIATOR.entities('elb')[r_index] informer = aws_informer.ELBInformer(resource, mediator=GLOBAL_MEDIATOR) self.assertTrue(isinstance(informer.to_dict(), dict)) informer.expand() self.assertTrue(isinstance(informer.to_dict(), dict)) if 'Vpcs' in informer.expansions: self.assertEqual(informer.to_dict()['Vpcs'], informer.expansions['Vpcs'].to_dict())
def test_elb_informer_supplementals(self): '''Test supplemental data of elb informers.''' informer = aws_informer.ELBInformer(self.elb_resource, mediator=GLOBAL_MEDIATOR) informer.expand() self.assertIn('DNSIpAddress', informer.supplementals) self.assertIn('DNSIpAddress', informer.to_dict()) self.assertIn('INET', informer.supplementals['DNSIpAddress']) self.assertIn('INET', informer.to_dict()['DNSIpAddress'])
def test_elb_informer_site_init(self): '''Test Load Balancer informer site customizations.''' # - - - - - - - - - - - - - - - - # Test set_elb_informer_name_supplementals(). # - - - - - - - - - - - - - - - - elb_resource = GLOBAL_MEDIATOR.entities('elb')[0] elb_informer = aws_informer.ELBInformer(elb_resource, mediator=GLOBAL_MEDIATOR) self.assertIn('site-specific', elb_informer.supplementals) for field in site_boogio.ELB_NAME_FIELDS: self.assertIn(field, elb_informer.supplementals['site-specific'])
def test_set_elb_informer_instances_keyname_environments(self): '''Test ELB assigned EC2 environment supplementals.''' mediator = aws_informer.AWSMediator( region_name=site_boogio.test_region_name, profile_name=site_boogio.test_profile_name) mediator.flush() self.assertEqual(mediator.informer_cache, {}) elb_resource = mediator.entities('elb')[0] elb_informer = aws_informer.ELBInformer(elb_resource, mediator=mediator) # Without EC2 entities already fetched, this fails. with self.assertRaises(RuntimeError): site_boogio.set_elb_informer_instances_keyname_environments( elb_informer) # Now fetch EC2 entities. self.assertTrue(len(mediator.entities('ec2')) > 0) site_boogio.set_elb_informer_instances_keyname_environments( elb_informer) self.assertIn('instance_environments', elb_informer.supplementals)
def test_informer_to_dict_entity_identifier(self): '''Test the entity_identifier option to to_dict().''' vpc_informers = [ aws_informer.VPCInformer(resource, mediator=GLOBAL_MEDIATOR) for resource in GLOBAL_MEDIATOR.entities('vpc') ] elb_informers = [ aws_informer.ELBInformer(resource, mediator=GLOBAL_MEDIATOR) for resource in GLOBAL_MEDIATOR.entities('elb') ] network_interface_informers = [ aws_informer.NetworkInterfaceInformer(resource, mediator=GLOBAL_MEDIATOR) for resource in GLOBAL_MEDIATOR.entities('network_interface') ] eip_informers = [ aws_informer.EIPInformer(resource, mediator=GLOBAL_MEDIATOR) for resource in GLOBAL_MEDIATOR.entities('eip') ] # - - - - - - - - - - - - - - - - vpc_dicts = [(i, i.to_dict(entity_identifier=True)) for i in vpc_informers] for entity, entity_dict in vpc_dicts: self.assertIn('entity_identifier', entity_dict) self.assertEqual(entity.identifier, entity_dict['entity_identifier']) self.assertEqual( dict(entity.to_dict(), **{'entity_identifier': entity.identifier}), entity_dict) # - - - - - - - - - - - - - - - - elb_dicts = [(i, i.to_dict(entity_identifier=True)) for i in elb_informers] for entity, entity_dict in elb_dicts: self.assertIn('entity_identifier', entity_dict) self.assertEqual(entity.identifier, entity_dict['entity_identifier']) self.assertEqual( dict(entity.to_dict(), **{'entity_identifier': entity.identifier}), entity_dict) # - - - - - - - - - - - - - - - - network_interface_dicts = [(i, i.to_dict(entity_identifier=True)) for i in network_interface_informers] for entity, entity_dict in network_interface_dicts: self.assertIn('entity_identifier', entity_dict) self.assertEqual(entity.identifier, entity_dict['entity_identifier']) self.assertEqual( dict(entity.to_dict(), **{'entity_identifier': entity.identifier}), entity_dict) # - - - - - - - - - - - - - - - - eip_dicts = [(i, i.to_dict(entity_identifier=True)) for i in eip_informers] for entity, entity_dict in eip_dicts: self.assertIn('entity_identifier', entity_dict) self.assertEqual(entity.identifier, entity_dict['entity_identifier']) self.assertEqual( dict(entity.to_dict(), **{'entity_identifier': entity.identifier}), entity_dict)
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)