def setUp(self): global customer FunctionalTest.setUp(self) customer_logic.DataManager = get_mock_datamanager customer_logic.pecan = mock.MagicMock() customer_logic.utils = mock.MagicMock() customer_logic.utils.make_transid.return_value = 'some_trans_id' customer_logic.utils.audit_trail.return_value = None customer_logic.utils.make_uuid.return_value = 'some_uuid' customer_logic.utils.get_time_human.return_value = '1337' customer_logic.RdsProxy = mock.MagicMock() customer_logic.RdsProxy.send_customer.return_value = None customer_logic.RdsProxy.get_status.return_value = RdsStatus() customer_logic.build_response = mock.MagicMock() customer = models.Customer() user = models.User() customer.users = [user, models.User()] user.role = ['user', 'admin'] global flow_type flow_type = 0
def get_mock_customer_logic(): global customer_logic_mock customer_logic_mock = mock.MagicMock() if root.CustomerLogic.return_error == 0: res = Models.CustomerResultWrapper(transaction_id='1', id='1', links={}, updated=None, created='1') list_res = Models.CustomerSummaryResponse() list_res.customers.append(Models.CustomerSummary(name='1', id='1', description='1')) customer_logic_mock.create_customer.return_value = res customer_logic_mock.update_customer.return_value = res customer_logic_mock.get_customer.return_value = Models.Customer(**RET_CUSTOMER_JSON) customer_logic_mock.get_customer_list_by_criteria.return_value = list_res elif root.CustomerLogic.return_error == 1: customer_logic_mock.create_customer.side_effect = SystemError() customer_logic_mock.update_customer.side_effect = SystemError() customer_logic_mock.get_customer.side_effect = SystemError() customer_logic_mock.delete_customer_by_uuid.side_effect = SystemError() customer_logic_mock.get_customer_list_by_criteria.side_effect = SystemError() else: customer_logic_mock.create_customer.side_effect = ErrorStatus(status_code=404) customer_logic_mock.update_customer.side_effect = ErrorStatus(status_code=404) customer_logic_mock.get_customer.side_effect = ErrorStatus(status_code=404) customer_logic_mock.delete_customer_by_uuid.side_effect = ErrorStatus( status_code=409) customer_logic_mock.get_customer_list_by_criteria.side_effect = ErrorStatus(status_code=404) return customer_logic_mock
def test_add_users_success(self): logic = customer_logic.CustomerLogic() users = [models.User(), models.User()] logic.add_users('some_uuid', 'some_region', users, 'some_transaction') assert data_manager_mock.add_user.call_count == 2 assert data_manager_mock.commit.called
def test_add_regions_success(self): regions = [models.Region(), models.Region()] logic = customer_logic.CustomerLogic() logic.add_regions('customer_uuid', regions, 'transaction_id') assert data_manager_mock.commit.called assert customer_logic.RdsProxy.send_customer_dict.called
def test_replace_regions_success(self): regions = [models.Region(), models.Region()] logic = customer_logic.CustomerLogic() logic.replace_regions('customer_uuid', regions, 'transaction_id') assert data_manager_mock.commit.called assert customer_logic.RdsProxy.send_customer_dict.called assert record_mock.delete_all_regions_for_customer.called
def test_replace_regions_fail(self): global mock_returns_error mock_returns_error = True regions = [models.Region(), models.Region()] logic = customer_logic.CustomerLogic() self.assertRaises(SystemError, logic.replace_regions, 'customer_uuid', regions, 'transaction_id') assert data_manager_mock.rollback.called
def test_replace_default_users_fail(self): global mock_returns_error mock_returns_error = True users = [models.User(), models.User()] logic = customer_logic.CustomerLogic() self.assertRaises(SystemError, logic.replace_default_users, 'id', users, 'trans_id') assert data_manager_mock.rollback.called
def test_replace_users_success(self): logic = customer_logic.CustomerLogic() users = [models.User(), models.User()] logic.replace_users('some_uuid', 'some_region', users, 'some_transaction') assert record_mock.delete_all_users_from_region.called assert data_manager_mock.add_user.called assert data_manager_mock.commit.called
def test_add_default_users_with_regions_success(self): user = models.User() user.role = ['user', 'admin'] users = [user, models.User()] logic = customer_logic.CustomerLogic() logic.add_default_users('customer_uuid', users, 'transaction_id') assert data_manager_mock.commit.called assert data_manager_mock.add_user.call_count == 2 assert data_manager_mock.add_role.call_count == 2
def test_replace_default_users_no_regions_success(self): user = models.User() user.role = ['user', 'admin'] users = [user, models.User()] logic = customer_logic.CustomerLogic() logic.replace_default_users('customer_uuid', users, 'transaction_id') assert data_manager_mock.commit.called assert record_mock.delete_all_users_from_region.called assert not customer_logic.RdsProxy.send_customer.called assert data_manager_mock.add_user.call_count == 2 assert data_manager_mock.add_role.call_count == 2
def get_mock_customer_logic(): global metadata_logic_mock metadata_logic_mock = mock.MagicMock() if metadata.logic.return_error == 0: res = Models.CustomerResultWrapper(transaction_id='1', id='1', links={}, updated=None, created='1') metadata_logic_mock.add_customer_metadata.return_value = res metadata_logic_mock.update_customer_metadata.return_value = res elif metadata.logic.return_error == 1: metadata_logic_mock.add_customer_metadata.side_effect = SystemError() metadata_logic_mock.update_customer_metadata.side_effect = SystemError() elif metadata.logic.return_error == 2: metadata_logic_mock.add_customer_metadata.side_effect = ErrorStatus(status_code=404) metadata_logic_mock.update_customer_metadata.side_effect = ErrorStatus(status_code=404) elif metadata.logic.return_error == 3: metadata_logic_mock.add_customer_metadata.side_effect = LookupError() metadata_logic_mock.update_customer_metadata.side_effect = LookupError() elif metadata.logic.return_error == 4: metadata_logic_mock.add_customer_metadata.side_effect = AttributeError() metadata_logic_mock.update_customer_metadata.side_effect = AttributeError() elif metadata.logic.return_error == 5: metadata_logic_mock.add_customer_metadata.side_effect = ValueError() metadata_logic_mock.update_customer_metadata.side_effect = ValueError() return metadata_logic_mock
def get_mock_customer_logic(): global customer_logic_mock customer_logic_mock = mock.MagicMock() if users.CustomerLogic.return_error == 0: res = Models.UserResultWrapper(transaction_id='1', users=[]) customer_logic_mock.add_default_users.return_value = res customer_logic_mock.add_users.return_value = res customer_logic_mock.replace_default_users.return_value = res customer_logic_mock.replace_users.return_value = res elif users.CustomerLogic.return_error == 1: customer_logic_mock.add_users.side_effect = SystemError() customer_logic_mock.add_default_users.side_effect = SystemError() customer_logic_mock.replace_users.side_effect = SystemError() customer_logic_mock.replace_default_users.side_effect = SystemError() customer_logic_mock.delete_users.side_effect = SystemError() customer_logic_mock.delete_default_users.side_effect = SystemError() else: customer_logic_mock.add_users.side_effect = ErrorStatus( status_code=404) customer_logic_mock.add_default_users.side_effect = ErrorStatus( status_code=404) customer_logic_mock.replace_users.side_effect = ErrorStatus( status_code=404) customer_logic_mock.replace_default_users.side_effect = ErrorStatus( status_code=404) customer_logic_mock.delete_users.side_effect = ErrorStatus( status_code=404) customer_logic_mock.delete_default_users.side_effect = ErrorStatus( status_code=404) return customer_logic_mock
def to_wsme(self): name = self.name description = self.description enabled = True if self.enabled else False regions = [ customer_region.to_wsme() for customer_region in self.customer_customer_regions if customer_region.region_id != -1 ] defaultRegion = [ customer_region.to_wsme() for customer_region in self.customer_customer_regions if customer_region.region_id == -1 ] metadata = {} for metadata1 in self.customer_metadata: metadata[metadata1.field_key] = metadata1.field_value result = WsmeModels.Customer( description=description, enabled=enabled, name=name, regions=regions, users=defaultRegion[0].users if defaultRegion else [], metadata=metadata, defaultQuotas=defaultRegion[0].quotas if defaultRegion else [], custId=self.uuid, uuid=self.uuid) return result
def test_add_customer_with_default_users(self): default_quota = models.Quota() customer.defaultQuotas = [default_quota] default_region = models.Region() customer.regions = [default_region] default_user = models.User() default_user.role = ['user', 'admin'] default_region.users = [default_user] logic = customer_logic.CustomerLogic() logic.create_customer(customer, 'some_uuid', 'some_trans_id') assert data_manager_mock.add_user.called
def test_enable_error(self): global mock_returns_error mock_returns_error = True logic = customer_logic.CustomerLogic() self.assertRaises(SystemError, logic.enable, 'id', models.Enabled(True), 'trans_id') self.assertTrue(data_manager_mock.rollback.called)
def test_replace_users_error(self): global mock_returns_error mock_returns_error = True logic = customer_logic.CustomerLogic() users = [models.User()] self.assertRaises(SystemError, logic.replace_users, 'id', 'region', users, 'trans_id')
def test_create_customer_success_with_regions(self): customer.regions = [models.Region(name="a")] logic = customer_logic.CustomerLogic() logic.create_customer(customer, 'some_uuid', 'some_trans_id') # sql_customer, trans_id = customer_logic.RdsProxy.send_customer.call_args_list[0][0] # assert trans_id is 'some_trans_id' # assert type(sql_customer) is sql_models.Customer assert data_manager_mock.commit.called assert not data_manager_mock.rollback.called
def to_wsme(self): compute = {} storage = {} network = {} for quota_field in self.quota_field_details: if self.quota_type == "compute": if not quota_field.field_value: quota_field.field_value = wsme.Unset compute[quota_field.field_key] = quota_field.field_value elif self.quota_type == "storage": if not quota_field.field_value: quota_field.field_value = wsme.Unset storage[quota_field.field_key] = quota_field.field_value elif self.quota_type == "network": if not quota_field.field_value: quota_field.field_value = wsme.Unset network[quota_field.field_key] = quota_field.field_value quota = WsmeModels.Quota(compute=[WsmeModels.Compute(**compute)], storage=[WsmeModels.Storage(**storage)], network=[WsmeModels.Network(**network)]) return quota
def to_wsme(self): name = self.region.name type = self.region.type quota = [] quotas = {} for region_quota in self.customer_region_quotas: quotas[region_quota.quota_type] = {} for quota_field in region_quota.quota_field_details: quotas[region_quota.quota_type][ quota_field. field_key] = quota_field.field_value or wsme.Unset if quotas: compute = None storage = None network = None if 'compute' in quotas: compute = [WsmeModels.Compute(**quotas['compute'])] if 'storage' in quotas: storage = [WsmeModels.Storage(**quotas['storage'])] if 'network' in quotas: network = [WsmeModels.Network(**quotas['network'])] quota = [ WsmeModels.Quota(compute=compute, storage=storage, network=network) ] users = [] user = None for user_role in self.customer_region_user_roles: if user and user.id != user_role.user.name: users.append(user) user = WsmeModels.User(id=user_role.user.name, role=[user_role.role.name]) elif user is None: user = WsmeModels.User(id=user_role.user.name, role=[user_role.role.name]) else: user.role.append(user_role.role.name) if user: users.append(user) region = WsmeModels.Region(name=name, type=type, quotas=quota, users=users) return region
def get_mock_customer_logic(): global customer_logic_mock customer_logic_mock = mock.MagicMock() if enabled.CustomerLogic.return_error == 0: res = Models.CustomerResultWrapper(transaction_id='1', id='1', links={}, updated=None, created='1') customer_logic_mock.enable.return_value = res elif enabled.CustomerLogic.return_error == 1: customer_logic_mock.enable.side_effect = SystemError() elif enabled.CustomerLogic.return_error == 2: customer_logic_mock.enable.side_effect = ErrorStatus(status_code=404) return customer_logic_mock
def get_cust_model(): """this function create a customer model object for testing :return: new customer object """ cust = models.Customer(enabled=False, name='a', metadata={'a': 'b'}, regions=[ models.Region( name='r1', type='group', quotas=[models.Quota()], users=[models.User(id='a', role=['admin'])]) ], users=[models.User(id='b', role=['admin'])], defaultQuotas=[models.Quota()]) return cust
def test_add_regions_action(self): regions = [models.Region(), models.Region()] logic = customer_logic.CustomerLogic() logic.add_regions('some_uuid', regions, 'some_trans_id') res = customer_logic.RdsProxy.send_customer.call_args_list
def mock_to_wsme(): return models.Customer( regions=[models.Region(name='DPK', status='Success')])
def test_enable_success(self): logic = customer_logic.CustomerLogic() logic.enable('customer_id', models.Enabled(True), 'transaction_is') self.assertTrue(record_mock.read_customer_by_uuid.called) self.assertTrue(customer_logic.RdsProxy.send_customer.called)
def to_wsme(self): id = "" role = [] user = WsmeModels.User(id=id, role=role) return user
] } RET_CUSTOMER_JSON = { "description": "Customer description", "enabled": True, "name": "myDomain", "metadata": { "my_server_name": "Apache1", "ocx_cust": "123456889" }, "regions": [Models.Region(**{"name": "SAN1", "type": "single", "quotas": [Models.Quota(**{ "compute": [Models.Compute(instances='1', injected_files='1', key_pairs='1', ram='1', vcpus='1', metadata_items='1', injected_file_content_bytes='1', floating_ips='1', fixed_ips='1', injected_file_path_bytes='1', server_groups='1', server_group_members='1')], "storage": [Models.Storage(gigabytes='1', snapshots='1', volumes='1')], "network": [Models.Network(floating_ips='1', networks='1', ports='1', routers='1', subnets='1', security_groups='1', security_group_rules='1', health_monitor='1', member='1', pool='1', nat_instance='1', route_table='1', vip='1')] })]})], "users": [Models.User(** {"id": "userId1", "role": ["admin", "other"]}) ], "defaultQuotas": [Models.Quota(**{ "compute": [Models.Compute(instances='1', injected_files='1', key_pairs='1', ram='1', vcpus='1', metadata_items='1', injected_file_content_bytes='1', floating_ips='1', fixed_ips='1', injected_file_path_bytes='1', server_groups='1', server_group_members='1')], "storage": [Models.Storage(gigabytes='1', snapshots='1', volumes='1')], "network": [Models.Network(floating_ips='1', networks='1', ports='1', routers='1', subnets='1', security_groups='1', security_group_rules='1', health_monitor='1',