def test_user(self): fake_user = users.User(self.fc.users, {'id': '1'}) user.User.keystone().AndReturn(self.fc) self.fc.users.create('test_stack.CfnUser', '', '*****@*****.**', enabled=True, tenant_id='test_tenant').AndReturn(fake_user) fake_role = roles.Role(self.fc.roles, {'id': '123', 'name': 'stack_user_role'}) user.User.keystone().AndReturn(self.fc) self.fc.roles.list().AndReturn([fake_role]) user.User.keystone().AndReturn(self.fc) self.fc.roles.add_user_role('1', '123', 'test_tenant').AndReturn(None) # delete script user.User.keystone().AndReturn(self.fc) self.fc.users.get(user.DummyId('1')).AndRaise(Exception('not found')) eventlet.sleep(1).AndReturn(None) user.User.keystone().AndReturn(self.fc) self.fc.users.get(user.DummyId('1')).AndReturn(fake_user) self.fc.users.delete(fake_user).AndRaise(Exception('delete failed')) self.fc.users.delete(fake_user).AndReturn(None) self.m.ReplayAll() t = self.load_template() stack = self.parse_stack(t) resource = self.create_user(t, stack, 'CfnUser') self.assertEqual('1', resource.instance_id) self.assertEqual('test_stack.CfnUser', resource.FnGetRefId()) self.assertEqual('CREATE_COMPLETE', resource.state) self.assertEqual(user.User.UPDATE_REPLACE, resource.handle_update()) resource.instance_id = None self.assertEqual(None, resource.delete()) self.assertEqual('DELETE_COMPLETE', resource.state) resource.instance_id = '1' resource.state_set('CREATE_COMPLETE') self.assertEqual('CREATE_COMPLETE', resource.state) self.assertEqual(None, resource.delete()) self.assertEqual('DELETE_COMPLETE', resource.state) resource.state_set('CREATE_COMPLETE') self.assertEqual('CREATE_COMPLETE', resource.state) self.assertEqual(None, resource.delete()) self.assertEqual('DELETE_COMPLETE', resource.state) self.m.VerifyAll()
def test_access_key(self): fake_user = users.User(self.fc.users, { 'id': '1', 'name': 'test_stack.CfnUser' }) fake_cred = ec2.EC2( self.fc.ec2, { 'access': '03a4967889d94a9c8f707d267c127a3d', 'secret': 'd5fd0c08f8cc417ead0355c67c529438' }) user.AccessKey.keystone().AndReturn(self.fc) self.fc.users.list(tenant_id='test_tenant').AndReturn([fake_user]) user.AccessKey.keystone().AndReturn(self.fc) self.fc.ec2.create('1', 'test_tenant').AndReturn(fake_cred) # fetch secret key user.AccessKey.keystone().AndReturn(self.fc) self.fc.auth_user_id = '1' user.AccessKey.keystone().AndReturn(self.fc) self.fc.ec2.get( '1', '03a4967889d94a9c8f707d267c127a3d').AndReturn(fake_cred) # delete script user.AccessKey.keystone().AndReturn(self.fc) self.fc.users.list(tenant_id='test_tenant').AndReturn([fake_user]) user.AccessKey.keystone().AndReturn(self.fc) self.fc.ec2.delete('1', '03a4967889d94a9c8f707d267c127a3d').AndReturn(None) self.m.ReplayAll() t = self.load_template() stack = self.parse_stack(t) resource = self.create_access_key(t, stack, 'HostKeys') self.assertEqual(user.AccessKey.UPDATE_REPLACE, resource.handle_update()) self.assertEqual('03a4967889d94a9c8f707d267c127a3d', resource.resource_id) self.assertEqual('d5fd0c08f8cc417ead0355c67c529438', resource._secret) self.assertEqual(resource.FnGetAtt('UserName'), 'test_stack.CfnUser') resource._secret = None self.assertEqual(resource.FnGetAtt('SecretAccessKey'), 'd5fd0c08f8cc417ead0355c67c529438') try: resource.FnGetAtt('Foo') except exception.InvalidTemplateAttribute: pass else: raise Exception('Expected InvalidTemplateAttribute') self.assertEqual(None, resource.delete()) self.m.VerifyAll()
def fake_users_list(*args, **kwargs): tenant_id = kwargs.get("tenant_id", "None") return [ users.User( poller.openstack_client.identity_admin.users, { "id": "id-%s" % tenant_id, "name": "name-%s" % tenant_id, "email": "*****@*****.**" % tenant_id }) ]
def data(TEST): # Make a deep copy of the catalog to avoid persisting side-effects # when tests modify the catalog. TEST.service_catalog = copy.deepcopy(SERVICE_CATALOG) TEST.tokens = utils.TestDataContainer() TEST.domains = utils.TestDataContainer() TEST.users = utils.TestDataContainer() TEST.groups = utils.TestDataContainer() TEST.tenants = utils.TestDataContainer() TEST.role_assignments = utils.TestDataContainer() TEST.roles = utils.TestDataContainer() TEST.ec2 = utils.TestDataContainer() TEST.identity_providers = utils.TestDataContainer() TEST.idp_mappings = utils.TestDataContainer() TEST.idp_protocols = utils.TestDataContainer() admin_role_dict = {'id': '1', 'name': 'admin'} admin_role = roles.Role(roles.RoleManager, admin_role_dict, loaded=True) member_role_dict = {'id': "2", 'name': settings.OPENSTACK_KEYSTONE_DEFAULT_ROLE} member_role = roles.Role(roles.RoleManager, member_role_dict, loaded=True) TEST.roles.add(admin_role, member_role) TEST.roles.admin = admin_role TEST.roles.member = member_role domain_dict = {'id': "1", 'name': 'test_domain', 'description': "a test domain.", 'enabled': True} domain_dict_2 = {'id': "2", 'name': 'disabled_domain', 'description': "a disabled test domain.", 'enabled': False} domain = domains.Domain(domains.DomainManager, domain_dict) disabled_domain = domains.Domain(domains.DomainManager, domain_dict_2) TEST.domains.add(domain, disabled_domain) TEST.domain = domain # Your "current" domain user_dict = {'id': "1", 'name': 'test_user', 'description': 'test_description', 'email': '*****@*****.**', 'password': '******', 'token': 'test_token', 'project_id': '1', 'enabled': True, 'domain_id': "1"} user = users.User(None, user_dict) user_dict = {'id': "2", 'name': 'user_two', 'description': 'test_description', 'email': '*****@*****.**', 'password': '******', 'token': 'test_token', 'project_id': '1', 'enabled': True, 'domain_id': "1"} user2 = users.User(None, user_dict) user_dict = {'id': "3", 'name': 'user_three', 'description': 'test_description', 'email': '*****@*****.**', 'password': '******', 'token': 'test_token', 'project_id': '1', 'enabled': True, 'domain_id': "1"} user3 = users.User(None, user_dict) user_dict = {'id': "4", 'name': 'user_four', 'description': 'test_description', 'email': '*****@*****.**', 'password': '******', 'token': 'test_token', 'project_id': '2', 'enabled': True, 'domain_id': "2"} user4 = users.User(None, user_dict) user_dict = {'id': "5", 'name': 'user_five', 'description': 'test_description', 'email': None, 'password': '******', 'token': 'test_token', 'project_id': '2', 'enabled': True, 'domain_id': "1"} user5 = users.User(None, user_dict) TEST.users.add(user, user2, user3, user4, user5) TEST.user = user # Your "current" user TEST.user.service_catalog = copy.deepcopy(SERVICE_CATALOG) group_dict = {'id': "1", 'name': 'group_one', 'description': 'group one description', 'project_id': '1', 'domain_id': '1'} group = groups.Group(groups.GroupManager(None), group_dict) group_dict = {'id': "2", 'name': 'group_two', 'description': 'group two description', 'project_id': '1', 'domain_id': '1'} group2 = groups.Group(groups.GroupManager(None), group_dict) group_dict = {'id': "3", 'name': 'group_three', 'description': 'group three description', 'project_id': '1', 'domain_id': '1'} group3 = groups.Group(groups.GroupManager(None), group_dict) group_dict = {'id': "4", 'name': 'group_four', 'description': 'group four description', 'project_id': '2', 'domain_id': '2'} group4 = groups.Group(groups.GroupManager(None), group_dict) TEST.groups.add(group, group2, group3, group4) role_assignments_dict = {'user': {'id': '1'}, 'role': {'id': '1'}, 'scope': {'project': {'id': '1'}}} proj_role_assignment1 = role_assignments.RoleAssignment( role_assignments.RoleAssignmentManager, role_assignments_dict) role_assignments_dict = {'user': {'id': '2'}, 'role': {'id': '2'}, 'scope': {'project': {'id': '1'}}} proj_role_assignment2 = role_assignments.RoleAssignment( role_assignments.RoleAssignmentManager, role_assignments_dict) role_assignments_dict = {'group': {'id': '1'}, 'role': {'id': '2'}, 'scope': {'project': {'id': '1'}}} proj_role_assignment3 = role_assignments.RoleAssignment( role_assignments.RoleAssignmentManager, role_assignments_dict) role_assignments_dict = {'user': {'id': '3'}, 'role': {'id': '2'}, 'scope': {'project': {'id': '1'}}} proj_role_assignment4 = role_assignments.RoleAssignment( role_assignments.RoleAssignmentManager, role_assignments_dict) role_assignments_dict = {'user': {'id': '1'}, 'role': {'id': '1'}, 'scope': {'domain': {'id': '1'}}} domain_role_assignment1 = role_assignments.RoleAssignment( role_assignments.RoleAssignmentManager, role_assignments_dict) role_assignments_dict = {'user': {'id': '2'}, 'role': {'id': '2'}, 'scope': {'domain': {'id': '1'}}} domain_role_assignment2 = role_assignments.RoleAssignment( role_assignments.RoleAssignmentManager, role_assignments_dict) role_assignments_dict = {'group': {'id': '1'}, 'role': {'id': '2'}, 'scope': {'domain': {'id': '1'}}} domain_role_assignment3 = role_assignments.RoleAssignment( role_assignments.RoleAssignmentManager, role_assignments_dict) role_assignments_dict = {'user': {'id': '3'}, 'role': {'id': '2'}, 'scope': {'domain': {'id': '1'}}} domain_role_assignment4 = role_assignments.RoleAssignment( role_assignments.RoleAssignmentManager, role_assignments_dict) TEST.role_assignments.add(proj_role_assignment1, proj_role_assignment2, proj_role_assignment3, proj_role_assignment4, domain_role_assignment1, domain_role_assignment2, domain_role_assignment3, domain_role_assignment4) tenant_dict = {'id': "1", 'name': 'test_tenant', 'description': "a test tenant.", 'enabled': True, 'domain_id': '1', 'domain_name': 'test_domain'} tenant_dict_2 = {'id': "2", 'name': 'disabled_tenant', 'description': "a disabled test tenant.", 'enabled': False, 'domain_id': '2', 'domain_name': 'disabled_domain'} tenant_dict_3 = {'id': "3", 'name': u'\u4e91\u89c4\u5219', 'description': "an unicode-named tenant.", 'enabled': True, 'domain_id': '2', 'domain_name': 'disabled_domain'} tenant = tenants.Tenant(tenants.TenantManager, tenant_dict) disabled_tenant = tenants.Tenant(tenants.TenantManager, tenant_dict_2) tenant_unicode = tenants.Tenant(tenants.TenantManager, tenant_dict_3) TEST.tenants.add(tenant, disabled_tenant, tenant_unicode) TEST.tenant = tenant # Your "current" tenant tomorrow = datetime_safe.datetime.now() + timedelta(days=1) expiration = tomorrow.isoformat() scoped_token_dict = { 'access': { 'token': { 'id': "test_token_id", 'expires': expiration, 'tenant': tenant_dict, 'tenants': [tenant_dict]}, 'user': { 'id': "test_user_id", 'name': "test_user", 'roles': [member_role_dict]}, 'serviceCatalog': TEST.service_catalog } } scoped_access_info = access.AccessInfo.factory(resp=None, body=scoped_token_dict) unscoped_token_dict = { 'access': { 'token': { 'id': "test_token_id", 'expires': expiration}, 'user': { 'id': "test_user_id", 'name': "test_user", 'roles': [member_role_dict]}, 'serviceCatalog': TEST.service_catalog } } unscoped_access_info = access.AccessInfo.factory(resp=None, body=unscoped_token_dict) scoped_token = auth_user.Token(scoped_access_info) unscoped_token = auth_user.Token(unscoped_access_info) TEST.tokens.add(scoped_token, unscoped_token) TEST.token = scoped_token # your "current" token. TEST.tokens.scoped_token = scoped_token TEST.tokens.unscoped_token = unscoped_token access_secret = ec2.EC2(ec2.CredentialsManager, {"access": "access", "secret": "secret", "tenant_id": tenant.id}) TEST.ec2.add(access_secret) idp_dict_1 = {'id': 'idp_1', 'description': 'identity provider 1', 'enabled': True, 'remote_ids': ['rid_1', 'rid_2']} idp_1 = identity_providers.IdentityProvider( identity_providers.IdentityProviderManager, idp_dict_1, loaded=True) idp_dict_2 = {'id': 'idp_2', 'description': 'identity provider 2', 'enabled': True, 'remote_ids': ['rid_3', 'rid_4']} idp_2 = identity_providers.IdentityProvider( identity_providers.IdentityProviderManager, idp_dict_2, loaded=True) TEST.identity_providers.add(idp_1, idp_2) idp_mapping_dict = { "id": "mapping_1", "rules": [ { "local": [ { "user": { "name": "{0}" } }, { "group": { "id": "0cd5e9" } } ], "remote": [ { "type": "UserName" }, { "type": "orgPersonType", "not_any_of": [ "Contractor", "Guest" ] } ] } ] } idp_mapping = mappings.Mapping( mappings.MappingManager(None), idp_mapping_dict) TEST.idp_mappings.add(idp_mapping) idp_protocol_dict_1 = {'id': 'protocol_1', 'mapping_id': 'mapping_1'} idp_protocol = protocols.Protocol( protocols.ProtocolManager, idp_protocol_dict_1, loaded=True) TEST.idp_protocols.add(idp_protocol)
def data(TEST): TEST.service_catalog = SERVICE_CATALOG TEST.tokens = TestDataContainer() TEST.domains = TestDataContainer() TEST.users = TestDataContainer() TEST.groups = TestDataContainer() TEST.tenants = TestDataContainer() TEST.roles = TestDataContainer() TEST.ec2 = TestDataContainer() admin_role_dict = {'id': '1', 'name': 'admin'} admin_role = roles.Role(roles.RoleManager, admin_role_dict) member_role_dict = { 'id': "2", 'name': settings.OPENSTACK_KEYSTONE_DEFAULT_ROLE } member_role = roles.Role(roles.RoleManager, member_role_dict) TEST.roles.add(admin_role, member_role) TEST.roles.admin = admin_role TEST.roles.member = member_role domain_dict = { 'id': "1", 'name': 'test_domain', 'description': "a test domain.", 'enabled': True } domain_dict_2 = { 'id': "2", 'name': 'disabled_domain', 'description': "a disabled test domain.", 'enabled': False } domain = domains.Domain(domains.DomainManager, domain_dict) disabled_domain = domains.Domain(domains.DomainManager, domain_dict_2) TEST.domains.add(domain, disabled_domain) TEST.domain = domain # Your "current" domain user_dict = { 'id': "1", 'name': 'test_user', 'email': '*****@*****.**', 'password': '******', 'token': 'test_token', 'project_id': '1', 'enabled': True, 'domain_id': "1" } user = users.User(users.UserManager(None), user_dict) user_dict = { 'id': "2", 'name': 'user_two', 'email': '*****@*****.**', 'password': '******', 'token': 'test_token', 'project_id': '1', 'enabled': True, 'domain_id': "1" } user2 = users.User(users.UserManager(None), user_dict) user_dict = { 'id': "3", 'name': 'user_three', 'email': '*****@*****.**', 'password': '******', 'token': 'test_token', 'project_id': '1', 'enabled': True, 'domain_id': "1" } user3 = users.User(users.UserManager(None), user_dict) user_dict = { 'id': "4", 'name': 'user_four', 'email': '*****@*****.**', 'password': '******', 'token': 'test_token', 'project_id': '2', 'enabled': True, 'domain_id': "2" } user4 = users.User(users.UserManager(None), user_dict) TEST.users.add(user, user2, user3, user4) TEST.user = user # Your "current" user TEST.user.service_catalog = SERVICE_CATALOG group_dict = { 'id': "1", 'name': 'group_one', 'description': 'group one description', 'domain_id': '1' } group = groups.Group(groups.GroupManager(None), group_dict) group_dict = { 'id': "2", 'name': 'group_two', 'description': 'group two description', 'domain_id': '1' } group2 = groups.Group(groups.GroupManager(None), group_dict) group_dict = { 'id': "3", 'name': 'group_three', 'description': 'group three description', 'domain_id': '2' } group3 = groups.Group(groups.GroupManager(None), group_dict) TEST.groups.add(group, group2, group3) tenant_dict = { 'id': "1", 'name': 'test_tenant', 'description': "a test tenant.", 'enabled': True, 'domain_id': '1' } tenant_dict_2 = { 'id': "2", 'name': 'disabled_tenant', 'description': "a disabled test tenant.", 'enabled': False, 'domain_id': '2' } tenant_dict_3 = { 'id': "3", 'name': u'\u4e91\u89c4\u5219', 'description': "an unicode-named tenant.", 'enabled': True, 'domain_id': '2' } tenant = tenants.Tenant(tenants.TenantManager, tenant_dict) disabled_tenant = tenants.Tenant(tenants.TenantManager, tenant_dict_2) tenant_unicode = tenants.Tenant(tenants.TenantManager, tenant_dict_3) TEST.tenants.add(tenant, disabled_tenant, tenant_unicode) TEST.tenant = tenant # Your "current" tenant tomorrow = datetime_safe.datetime.now() + timedelta(days=1) expiration = datetime_safe.datetime.isoformat(tomorrow) scoped_token_dict = { 'access': { 'token': { 'id': "test_token_id", 'expires': expiration, 'tenant': tenant_dict, 'tenants': [tenant_dict] }, 'user': { 'id': "test_user_id", 'name': "test_user", 'roles': [member_role_dict] }, 'serviceCatalog': TEST.service_catalog } } scoped_access_info = AccessInfo.factory(resp=None, body=scoped_token_dict) unscoped_token_dict = { 'access': { 'token': { 'id': "test_token_id", 'expires': expiration }, 'user': { 'id': "test_user_id", 'name': "test_user", 'roles': [member_role_dict] }, 'serviceCatalog': TEST.service_catalog } } unscoped_access_info = AccessInfo.factory(resp=None, body=unscoped_token_dict) scoped_token = Token(scoped_access_info) unscoped_token = Token(unscoped_access_info) TEST.tokens.add(scoped_token, unscoped_token) TEST.token = scoped_token # your "current" token. TEST.tokens.scoped_token = scoped_token TEST.tokens.unscoped_token = unscoped_token access_secret = ec2.EC2(ec2.CredentialsManager, { "access": "access", "secret": "secret" }) TEST.ec2.add(access_secret)
def data(TEST): TEST.ceilometer_users = utils.TestDataContainer() TEST.ceilometer_tenants = utils.TestDataContainer() TEST.resources = utils.TestDataContainer() TEST.samples = utils.TestDataContainer() TEST.meters = utils.TestDataContainer() TEST.statistics = utils.TestDataContainer() TEST.global_disk_usages = utils.TestDataContainer() TEST.global_network_usages = utils.TestDataContainer() TEST.global_network_traffic_usages = utils.TestDataContainer() TEST.global_object_store_usages = utils.TestDataContainer() TEST.statistics_array = utils.TestDataContainer() # users ceilometer_user_dict1 = {'id': "1", 'name': 'user', 'email': '*****@*****.**', 'password': '******', 'token': 'test_token', 'project_id': '1', 'enabled': True, 'domain_id': "1"} ceilometer_user_dict2 = {'id': "2", 'name': 'user2', 'email': '*****@*****.**', 'password': '******', 'token': 'test_token', 'project_id': '2', 'enabled': True, 'domain_id': "2"} TEST.ceilometer_users.add(users.User(users.UserManager(None), ceilometer_user_dict1)) TEST.ceilometer_users.add(users.User(users.UserManager(None), ceilometer_user_dict2)) #tenants tenant_dict = {'id': "1", 'name': 'test_tenant', 'description': "a test tenant.", 'enabled': True, 'domain_id': '1'} tenant_dict_2 = {'id': "2", 'name': 'disabled_tenant', 'description': "a disabled test tenant.", 'enabled': False, 'domain_id': '2'} tenant_dict_3 = {'id': "3", 'name': u'\u4e91\u89c4\u5219', 'description': "an unicode-named tenant.", 'enabled': True, 'domain_id': '2'} ceilometer_tenant = tenants.Tenant(tenants.TenantManager, tenant_dict) ceilometer_disabled_tenant = tenants.Tenant(tenants.TenantManager, tenant_dict_2) ceilometer_tenant_unicode = tenants.Tenant(tenants.TenantManager, tenant_dict_3) TEST.ceilometer_tenants.add(ceilometer_tenant, ceilometer_disabled_tenant, ceilometer_tenant_unicode) # resources resource_dict_1 = dict( resource_id='fake_resource_id', project_id='fake_project_id', user_id="fake_user_id", timestamp='2012-07-02T10:42:00.000000', metadata={'tag': 'self.counter3', 'display_name': 'test-server'}, links=[{'url': 'test_url', 'rel': 'storage.objects'}], ) resource_dict_2 = dict( resource_id='fake_resource_id2', project_id='fake_project_id', user_id="fake_user_id", timestamp='2012-07-02T10:42:00.000000', metadata={'tag': 'self.counter3', 'display_name': 'test-server'}, links=[{'url': 'test_url', 'rel': 'storage.objects'}], ) resource_dict_3 = dict( resource_id='fake_resource_id3', project_id='fake_project_id', user_id="fake_user_id", timestamp='2012-07-02T10:42:00.000000', metadata={'tag': 'self.counter3', 'display_name': 'test-server'}, links=[{'url': 'test_url', 'rel': 'intance'}], ) resource_1 = resources.Resource(resources.ResourceManager(None), resource_dict_1) resource_2 = resources.Resource(resources.ResourceManager(None), resource_dict_2) resource_3 = resources.Resource(resources.ResourceManager(None), resource_dict_3) TEST.resources.add(resource_1) TEST.resources.add(resource_2) TEST.resources.add(resource_3) # samples sample_dict_1 = {'resource_id': 'fake_resource_id', 'project_id': 'fake_project_id', 'user_id': 'fake_user_id', 'counter_name': 'image', 'counter_type': 'gauge', 'counter_unit': 'image', 'counter_volume': 1, 'timestamp': '2012-12-21T11:00:55.000000', 'metadata': {'name1': 'value1', 'name2': 'value2'}, 'message_id': 'fake_message_id'} sample_dict_2 = {'resource_id': 'fake_resource_id2', 'project_id': 'fake_project_id', 'user_id': 'fake_user_id', 'counter_name': 'image', 'counter_type': 'gauge', 'counter_unit': 'image', 'counter_volume': 1, 'timestamp': '2012-12-21T11:00:55.000000', 'metadata': {'name1': 'value1', 'name2': 'value2'}, 'message_id': 'fake_message_id'} sample_1 = samples.Sample(samples.SampleManager(None), sample_dict_1) sample_2 = samples.Sample(samples.SampleManager(None), sample_dict_2) TEST.samples.add(sample_1) TEST.samples.add(sample_2) # meters meter_dict_1 = {'name': 'instance', 'type': 'gauge', 'unit': 'instance', 'resource_id': 'fake_resource_id', 'project_id': 'fake_project_id', 'user_id': 'fake_user_id'} meter_dict_2 = {'name': 'instance', 'type': 'gauge', 'unit': 'instance', 'resource_id': 'fake_resource_id', 'project_id': 'fake_project_id', 'user_id': 'fake_user_id'} meter_dict_3 = {'name': 'disk.read.bytes', 'type': 'gauge', 'unit': 'instance', 'resource_id': 'fake_resource_id', 'project_id': 'fake_project_id', 'user_id': 'fake_user_id'} meter_dict_4 = {'name': 'disk.write.bytes', 'type': 'gauge', 'unit': 'instance', 'resource_id': 'fake_resource_id', 'project_id': 'fake_project_id', 'user_id': 'fake_user_id'} meter_1 = meters.Meter(meters.MeterManager(None), meter_dict_1) meter_2 = meters.Meter(meters.MeterManager(None), meter_dict_2) meter_3 = meters.Meter(meters.MeterManager(None), meter_dict_3) meter_4 = meters.Meter(meters.MeterManager(None), meter_dict_4) TEST.meters.add(meter_1) TEST.meters.add(meter_2) TEST.meters.add(meter_3) TEST.meters.add(meter_4) # statistic statistic_dict_1 = {'min': 1, 'max': 9, 'avg': 4.55, 'sum': 45, 'count': 10, 'duration_start': '2012-12-21T11:00:55.000000', 'duration_end': '2012-12-21T11:00:55.000000', 'period': 7200, 'period_start': '2012-12-21T11:00:55.000000', 'period_end': '2012-12-21T11:00:55.000000'} statistic_1 = statistics.Statistics(statistics.StatisticsManager(None), statistic_dict_1) TEST.statistics.add(statistic_1)
def generate_test_data(): '''Builds a set of test_data data as returned by Keystone V2.''' test_data = TestDataContainer() keystone_service = { 'type': 'identity', 'name': 'keystone', 'endpoints_links': [], 'endpoints': [ { 'region': 'RegionOne', 'adminURL': 'http://admin.localhost:35357/v2.0', 'internalURL': 'http://internal.localhost:5000/v2.0', 'publicURL': 'http://public.localhost:5000/v2.0' } ] } # Users user_dict = {'id': uuid.uuid4().hex, 'name': 'gabriel', 'email': '*****@*****.**', 'password': '******', 'token': '', 'enabled': True} test_data.user = users.User(None, user_dict, loaded=True) # Tenants tenant_dict_1 = {'id': uuid.uuid4().hex, 'name': 'tenant_one', 'description': '', 'enabled': True} tenant_dict_2 = {'id': uuid.uuid4().hex, 'name': 'tenant_two', 'description': '', 'enabled': False} test_data.tenant_one = tenants.Tenant(None, tenant_dict_1, loaded=True) test_data.tenant_two = tenants.Tenant(None, tenant_dict_2, loaded=True) nova_service = { 'type': 'compute', 'name': 'nova', 'endpoint_links': [], 'endpoints': [ { 'region': 'RegionOne', 'adminURL': ('http://nova-admin.localhost:8774/v2.0/%s' % (tenant_dict_1['id'])), 'internalURL': ('http://nova-internal.localhost:8774/v2.0/%s' % (tenant_dict_1['id'])), 'publicURL': ('http://nova-public.localhost:8774/v2.0/%s' % (tenant_dict_1['id'])) }, { 'region': 'RegionTwo', 'adminURL': ('http://nova2-admin.localhost:8774/v2.0/%s' % (tenant_dict_1['id'])), 'internalURL': ('http://nova2-internal.localhost:8774/v2.0/%s' % (tenant_dict_1['id'])), 'publicURL': ('http://nova2-public.localhost:8774/v2.0/%s' % (tenant_dict_1['id'])) } ] } # Roles role_dict = {'id': uuid.uuid4().hex, 'name': 'Member'} test_data.role = roles.Role(roles.RoleManager, role_dict) # Tokens tomorrow = datetime_safe.datetime.now() + datetime.timedelta(days=1) expiration = datetime_safe.datetime.isoformat(tomorrow) scoped_token_dict = { 'access': { 'token': { 'id': uuid.uuid4().hex, 'expires': expiration, 'tenant': tenant_dict_1, 'tenants': [tenant_dict_1, tenant_dict_2]}, 'user': { 'id': user_dict['id'], 'name': user_dict['name'], 'roles': [role_dict]}, 'serviceCatalog': [keystone_service, nova_service] } } test_data.scoped_access_info = access.AccessInfo.factory( resp=None, body=scoped_token_dict) unscoped_token_dict = { 'access': { 'token': { 'id': uuid.uuid4().hex, 'expires': expiration}, 'user': { 'id': user_dict['id'], 'name': user_dict['name'], 'roles': [role_dict]}, 'serviceCatalog': [keystone_service] } } test_data.unscoped_access_info = access.AccessInfo.factory( resp=None, body=unscoped_token_dict) # Service Catalog test_data.service_catalog = service_catalog.ServiceCatalog.factory({ 'serviceCatalog': [keystone_service, nova_service], 'token': { 'id': scoped_token_dict['access']['token']['id'], 'expires': scoped_token_dict['access']['token']['expires'], 'user_id': user_dict['id'], 'tenant_id': tenant_dict_1['id'] } }) return test_data
def data(TEST): TEST.service_catalog = SERVICE_CATALOG TEST.tokens = TestDataContainer() TEST.domains = TestDataContainer() TEST.users = TestDataContainer() TEST.tenants = TestDataContainer() TEST.roles = TestDataContainer() TEST.ec2 = TestDataContainer() admin_role_dict = {'id': '1', 'name': 'admin'} admin_role = roles.Role(roles.RoleManager, admin_role_dict) member_role_dict = {'id': "2", 'name': settings.OPENSTACK_KEYSTONE_DEFAULT_ROLE} member_role = roles.Role(roles.RoleManager, member_role_dict) TEST.roles.add(admin_role, member_role) TEST.roles.admin = admin_role TEST.roles.member = member_role domain_dict = {'id': "1", 'name': 'test_domain', 'description': "a test domain.", 'enabled': True} domain_dict_2 = {'id': "2", 'name': 'disabled_domain', 'description': "a disabled test domain.", 'enabled': False} domain = domains.Domain(domains.DomainManager, domain_dict) disabled_domain = domains.Domain(domains.DomainManager, domain_dict_2) TEST.domains.add(domain, disabled_domain) TEST.domain = domain # Your "current" domain user_dict = {'id': "1", 'name': 'test_user', 'email': '*****@*****.**', 'password': '******', 'token': 'test_token', 'project_id': '1', 'enabled': True} user = users.User(users.UserManager(None), user_dict) user_dict = {'id': "2", 'name': 'user_two', 'email': '*****@*****.**', 'password': '******', 'token': 'test_token', 'project_id': '1', 'enabled': True} user2 = users.User(users.UserManager(None), user_dict) user_dict = {'id': "3", 'name': 'user_three', 'email': '*****@*****.**', 'password': '******', 'token': 'test_token', 'project_id': '1', 'enabled': True} user3 = users.User(users.UserManager(None), user_dict) TEST.users.add(user, user2, user3) TEST.user = user # Your "current" user TEST.user.service_catalog = SERVICE_CATALOG tenant_dict = {'id': "1", 'name': 'test_tenant', 'description': "a test tenant.", 'enabled': True} tenant_dict_2 = {'id': "2", 'name': 'disabled_tenant', 'description': "a disabled test tenant.", 'enabled': False} tenant = tenants.Tenant(tenants.TenantManager, tenant_dict) disabled_tenant = tenants.Tenant(tenants.TenantManager, tenant_dict_2) TEST.tenants.add(tenant, disabled_tenant) TEST.tenant = tenant # Your "current" tenant tomorrow = datetime_safe.datetime.now() + timedelta(days=1) expiration = datetime_safe.datetime.isoformat(tomorrow) scoped_token = tokens.Token(tokens.TokenManager, dict(token={"id": "test_token_id", "expires": expiration, "tenant": tenant_dict, "tenants": [tenant_dict]}, user={"id": "test_user_id", "name": "test_user", "roles": [member_role_dict]}, serviceCatalog=TEST.service_catalog)) unscoped_token = tokens.Token(tokens.TokenManager, dict(token={"id": "test_token_id", "expires": expiration}, user={"id": "test_user_id", "name": "test_user", "roles": [member_role_dict]}, serviceCatalog=TEST.service_catalog)) TEST.tokens.add(scoped_token, unscoped_token) TEST.token = scoped_token # your "current" token. TEST.tokens.scoped_token = scoped_token TEST.tokens.unscoped_token = unscoped_token access_secret = ec2.EC2(ec2.CredentialsManager, {"access": "access", "secret": "secret"}) TEST.ec2.add(access_secret)
def data(TEST): TEST.ceilometer_users = utils.TestDataContainer() TEST.ceilometer_tenants = utils.TestDataContainer() TEST.resources = utils.TestDataContainer() TEST.api_resources = utils.TestDataContainer() TEST.samples = utils.TestDataContainer() TEST.meters = utils.TestDataContainer() TEST.alarms = utils.TestDataContainer() TEST.statistics = utils.TestDataContainer() TEST.global_disk_usages = utils.TestDataContainer() TEST.global_network_usages = utils.TestDataContainer() TEST.global_network_traffic_usages = utils.TestDataContainer() TEST.global_object_store_usages = utils.TestDataContainer() TEST.statistics_array = utils.TestDataContainer() # users ceilometer_user_dict1 = { 'id': "1", 'name': 'user', 'email': '*****@*****.**', 'password': '******', 'token': 'test_token', 'project_id': '1', 'enabled': True, 'domain_id': "1" } ceilometer_user_dict2 = { 'id': "2", 'name': 'user2', 'email': '*****@*****.**', 'password': '******', 'token': 'test_token', 'project_id': '2', 'enabled': True, 'domain_id': "2" } TEST.ceilometer_users.add(users.User(None, ceilometer_user_dict1)) TEST.ceilometer_users.add(users.User(None, ceilometer_user_dict2)) # Tenants. tenant_dict = { 'id': "1", 'name': 'test_tenant', 'description': "a test tenant.", 'enabled': True, 'domain_id': '1' } tenant_dict_2 = { 'id': "2", 'name': 'disabled_tenant', 'description': "a disabled test tenant.", 'enabled': False, 'domain_id': '2' } tenant_dict_3 = { 'id': "3", 'name': u'\u4e91\u89c4\u5219', 'description': "an unicode-named tenant.", 'enabled': True, 'domain_id': '2' } ceilometer_tenant = tenants.Tenant(tenants.TenantManager, tenant_dict) ceilometer_disabled_tenant = tenants.Tenant(tenants.TenantManager, tenant_dict_2) ceilometer_tenant_unicode = tenants.Tenant(tenants.TenantManager, tenant_dict_3) TEST.ceilometer_tenants.add(ceilometer_tenant, ceilometer_disabled_tenant, ceilometer_tenant_unicode) # resources resource_dict_1 = dict( resource_id='fake_resource_id', project_id='fake_project_id', user_id="fake_user_id", timestamp='2012-07-02T10:42:00.000000', metadata={ 'tag': 'self.counter3', 'display_name': 'test-server' }, links=[{ 'url': 'test_url', 'rel': 'storage.objects' }], ) resource_dict_2 = dict( resource_id='fake_resource_id2', project_id='fake_project_id', user_id="fake_user_id", timestamp='2012-07-02T10:42:00.000000', metadata={ 'tag': 'self.counter3', 'display_name': 'test-server' }, links=[{ 'url': 'test_url', 'rel': 'storage.objects' }], ) resource_dict_3 = dict( resource_id='fake_resource_id3', project_id='fake_project_id', user_id="fake_user_id", timestamp='2012-07-02T10:42:00.000000', metadata={ 'tag': 'self.counter3', 'display_name': 'test-server' }, links=[{ 'url': 'test_url', 'rel': 'instance' }], ) resource_dict_4 = dict( resource_id='fake_resource_id3', project_id='fake_project_id', user_id="fake_user_id", timestamp='2012-07-02T10:42:00.000000', metadata={ 'tag': 'self.counter3', 'display_name': 'test-server' }, links=[{ 'url': 'test_url', 'rel': 'memory' }], ) resource_1 = resources.Resource(resources.ResourceManager(None), resource_dict_1) resource_2 = resources.Resource(resources.ResourceManager(None), resource_dict_2) resource_3 = resources.Resource(resources.ResourceManager(None), resource_dict_3) resource_4 = resources.Resource(resources.ResourceManager(None), resource_dict_4) TEST.resources.add(resource_1) TEST.resources.add(resource_2) TEST.resources.add(resource_3) # Having a separate set of fake objects for openstack_dashboard # api Resource class. This is required because of additional methods # defined in openstack_dashboard.api.ceilometer.Resource api_resource_1 = ceilometer.Resource(resource_1) api_resource_2 = ceilometer.Resource(resource_2) api_resource_3 = ceilometer.Resource(resource_3) api_resource_4 = ceilometer.Resource(resource_4) TEST.api_resources.add(api_resource_1) TEST.api_resources.add(api_resource_2) TEST.api_resources.add(api_resource_3) TEST.api_resources.add(api_resource_4) # samples sample_dict_1 = { 'resource_id': 'fake_resource_id', 'project_id': 'fake_project_id', 'user_id': 'fake_user_id', 'counter_name': 'image', 'counter_type': 'gauge', 'counter_unit': 'image', 'counter_volume': 1, 'timestamp': '2012-12-21T11:00:55.000000', 'metadata': { 'name1': 'value1', 'name2': 'value2' }, 'message_id': 'fake_message_id' } sample_dict_2 = { 'resource_id': 'fake_resource_id2', 'project_id': 'fake_project_id', 'user_id': 'fake_user_id', 'counter_name': 'image', 'counter_type': 'gauge', 'counter_unit': 'image', 'counter_volume': 1, 'timestamp': '2012-12-21T11:00:55.000000', 'metadata': { 'name1': 'value1', 'name2': 'value2' }, 'message_id': 'fake_message_id' } sample_1 = samples.Sample(samples.SampleManager(None), sample_dict_1) sample_2 = samples.Sample(samples.SampleManager(None), sample_dict_2) TEST.samples.add(sample_1) TEST.samples.add(sample_2) # alarms alarm_dict_1 = { 'alarm_actions': ['alarm_action1', 'alarm_action2'], 'ok_actions': ['ok_action_1', 'ok_action_2'], 'name': 'fake_alarm_name', 'timestamp': '2015-08-07T05:32:20.970341', 'description': 'fake_random_description', 'time_constraints': [], 'enabled': True, 'state_timestamp': '2015-08-07T17:59:11.351033', 'threshold_rule': { 'meter_name': 'fake_meter_name', 'evaluation_periods': 1, 'period': 300, 'statistic': 'avg', 'threshold': 2.0, 'query': [], 'comparison_operator': 'ge', 'exclude_outliers': False }, 'alarm_id': 'fake_alarm_id', 'state': 'ok', 'insufficient_data_actions': ['fake_action_1', 'fake_action_2'], 'repeat_actions': True, 'user_id': 'fake_user_id', 'project_id': 'fake_project_id', 'type': 'threshold', 'severity': 'low' } alarm_dict_2 = { 'alarm_actions': ['alarm_action1', 'alarm_action2'], 'ok_actions': ['ok_action_1', 'ok_action_2'], 'name': 'fake_alarm_name', 'timestamp': '2015-08-07T05:32:20.970341', 'description': 'New', 'time_constraints': [], 'enabled': True, 'state_timestamp': '2015-08-07T17:59:11.351033', 'threshold_rule': { 'meter_name': 'fake_meter_name', 'evaluation_periods': 1, 'period': 300, 'statistic': 'avg', 'threshold': 2.0, 'query': [], 'comparison_operator': 'ge', 'exclude_outliers': False }, 'alarm_id': 'fake_alarm_id2', 'state': 'ok', 'insufficient_data_actions': ['fake_action_1', 'fake_action_2'], 'repeat_actions': True, 'user_id': 'fake_user_id', 'project_id': 'fake_project_id', 'type': 'threshold', 'severity': 'low' } alarm_dict_3 = { 'alarm_actions': ['alarm_action1', 'alarm_action2'], 'ok_actions': ['ok_action_1', 'ok_action_2'], 'name': 'fake_alarm_name', 'timestamp': '2015-08-07T05:32:20.970341', 'description': 'fake_random_description', 'time_constraints': [], 'enabled': True, 'state_timestamp': '2015-08-07T17:59:11.351033', 'threshold_rule': { 'meter_name': 'fake_meter_name', 'evaluation_periods': 2, 'period': 300, 'statistic': 'avg', 'threshold': 2.0, 'query': [{ 'field': 'resource_id', 'value': '' }], 'comparison_operator': 'ge', 'exclude_outliers': False }, 'alarm_id': 'fake_alarm_id3', 'state': 'ok', 'insufficient_data_actions': ['fake_action_1', 'fake_action_2'], 'repeat_actions': True, 'user_id': '', 'project_id': '', 'type': 'threshold', 'severity': 'low' } alarm_dict_4 = { 'alarm_actions': ['alarm_action1', 'alarm_action2'], 'ok_actions': ['ok_action_1', 'ok_action_2'], 'name': 'fake_alarm_name4', 'timestamp': '2015-08-07T05:32:20.970341', 'description': 'fake_random_description', 'time_constraints': [], 'enabled': True, 'state_timestamp': '2015-08-07T17:59:11.351033', 'threshold_rule': { 'meter_name': '', 'evaluation_periods': -10, 'period': -1, 'statistic': 'avg', 'threshold': '', 'query': [{ 'field': 'resource_id', 'value': '' }], 'comparison_operator': 'ge', 'exclude_outliers': False }, 'alarm_id': 'fake_alarm_id4', 'state': 'ok', 'insufficient_data_actions': ['fake_action_1', 'fake_action_2'], 'repeat_actions': True, 'user_id': 'fake_user_id', 'project_id': 'fake_project_id', 'type': 'threshold', 'severity': 'low' } alarm_1 = alarms.Alarm(alarms.AlarmManager(None), alarm_dict_1) alarm_2 = alarms.Alarm(alarms.AlarmManager(None), alarm_dict_2) alarm_3 = alarms.Alarm(alarms.AlarmManager(None), alarm_dict_3) alarm_4 = alarms.Alarm(alarms.AlarmManager(None), alarm_dict_4) TEST.alarms.add(alarm_1) TEST.alarms.add(alarm_2) TEST.alarms.add(alarm_3) TEST.alarms.add(alarm_4) # meters meter_dict_1 = { 'name': 'instance', 'type': 'gauge', 'unit': 'instance', 'resource_id': 'fake_resource_id', 'project_id': 'fake_project_id', 'user_id': 'fake_user_id' } meter_dict_2 = { 'name': 'instance', 'type': 'gauge', 'unit': 'instance', 'resource_id': 'fake_resource_id', 'project_id': 'fake_project_id', 'user_id': 'fake_user_id' } meter_dict_3 = { 'name': 'disk.read.bytes', 'type': 'gauge', 'unit': 'instance', 'resource_id': 'fake_resource_id', 'project_id': 'fake_project_id', 'user_id': 'fake_user_id' } meter_dict_4 = { 'name': 'disk.write.bytes', 'type': 'gauge', 'unit': 'instance', 'resource_id': 'fake_resource_id', 'project_id': 'fake_project_id', 'user_id': 'fake_user_id' } meter_1 = meters.Meter(meters.MeterManager(None), meter_dict_1) meter_2 = meters.Meter(meters.MeterManager(None), meter_dict_2) meter_3 = meters.Meter(meters.MeterManager(None), meter_dict_3) meter_4 = meters.Meter(meters.MeterManager(None), meter_dict_4) TEST.meters.add(meter_1) TEST.meters.add(meter_2) TEST.meters.add(meter_3) TEST.meters.add(meter_4) # statistic statistic_dict_1 = { 'min': 1, 'max': 9, 'avg': 4.55, 'sum': 45, 'count': 10, 'duration_start': '2012-12-21T11:00:55.000000', 'duration_end': '2012-12-21T11:00:55.000000', 'period': 7200, 'period_start': '2012-12-21T11:00:55.000000', 'period_end': '2012-12-21T11:00:55.000000' } statistic_1 = statistics.Statistics(statistics.StatisticsManager(None), statistic_dict_1) TEST.statistics.add(statistic_1)
def data(TEST): TEST.service_catalog = SERVICE_CATALOG TEST.tokens = TestDataContainer() TEST.users = TestDataContainer() TEST.tenants = TestDataContainer() TEST.roles = TestDataContainer() TEST.ec2 = TestDataContainer() admin_role_dict = {'id': '1', 'name': 'admin'} admin_role = roles.Role(roles.RoleManager, admin_role_dict) member_role_dict = {'id': "2", 'name': settings.OPENSTACK_KEYSTONE_DEFAULT_ROLE} member_role = roles.Role(roles.RoleManager, member_role_dict) TEST.roles.add(member_role, admin_role) TEST.roles.admin = admin_role TEST.roles.member = member_role user_dict = {'id': "1", 'name': 'test_user', 'email': '*****@*****.**', 'password': '******', 'token': 'test_token'} user = users.User(users.UserManager, user_dict) user_dict.update({'id': "2", 'name': 'user_two', 'email': '*****@*****.**'}) user2 = users.User(users.UserManager, user_dict) TEST.users.add(user, user2) TEST.user = user # Your "current" user TEST.user.service_catalog = SERVICE_CATALOG tenant_dict = {'id': "1", 'name': 'test_tenant', 'description': "a test tenant.", 'enabled': True} tenant_dict_2 = {'id': "2", 'name': 'disabled_tenant', 'description': "a disabled test tenant.", 'enabled': False} tenant = tenants.Tenant(tenants.TenantManager, tenant_dict) disabled_tenant = tenants.Tenant(tenants.TenantManager, tenant_dict_2) TEST.tenants.add(tenant, disabled_tenant) TEST.tenant = tenant # Your "current" tenant scoped_token = tokens.Token(tokens.TokenManager, dict(token={"id": "test_token_id", "expires": "#FIXME", "tenant": tenant_dict, "tenants": [tenant_dict]}, user={"id": "test_user_id", "name": "test_user", "roles": [member_role_dict]}, serviceCatalog=TEST.service_catalog)) unscoped_token = tokens.Token(tokens.TokenManager, dict(token={"id": "test_token_id", "expires": "#FIXME"}, user={"id": "test_user_id", "name": "test_user", "roles": [member_role_dict]}, serviceCatalog=TEST.service_catalog)) TEST.tokens.add(scoped_token, unscoped_token) TEST.token = scoped_token # your "current" token. TEST.tokens.scoped_token = scoped_token TEST.tokens.unscoped_token = unscoped_token access_secret = ec2.EC2(ec2.CredentialsManager, {"access": "access", "secret": "secret"}) TEST.ec2.add(access_secret)