def test_list_schedules_by_tag(self, *args): ScheduleFactory(tags=[{ 'key': 'tag_key_1', 'value': 'tag_value_1' }], domain_id=self.domain_id) schedule_vos = ScheduleFactory.build_batch(9, domain_id=self.domain_id) list(map(lambda vo: vo.save(), schedule_vos)) params = { 'query': { 'filter': [{ 'k': 'tags.tag_key_1', 'v': 'tag_value_1', 'o': 'eq' }] }, 'domain_id': self.domain_id } self.transaction.method = 'list' schedule_svc = ScheduleService(transaction=self.transaction) schedule_vos, total_count = schedule_svc.list(params.copy()) SchedulesInfo(schedule_vos, total_count) self.assertEqual(len(schedule_vos), 1) self.assertIsInstance(schedule_vos[0], Schedule) self.assertEqual(total_count, 1)
def test_stat_schedule(self, *args): schedule_vos = ScheduleFactory.build_batch(10, domain_id=self.domain_id) list(map(lambda vo: vo.save(), schedule_vos)) params = { 'domain_id': self.domain_id, 'query': { 'aggregate': [{ 'group': { 'keys': [{ 'key': 'schedule_id', 'name': 'Id' }], 'fields': [{ 'operator': 'count', 'name': 'Count' }] } }, { 'sort': { 'key': 'Count', 'desc': True } }] } } self.transaction.method = 'stat' schedule_svc = ScheduleService(transaction=self.transaction) values = schedule_svc.stat(params) StatisticsInfo(values) print_data(values, 'test_stat_schedule')
def test_update_schedule(self, *args): new_schedule_vo = ScheduleFactory(domain_id=self.domain_id) params = { 'schedule_id': new_schedule_vo.storage_id, 'schedule': { 'cron': '*/5 * * * *' }, 'tags': { 'update_key': 'update_value' }, 'domain_id': self.domain_id } self.transaction.method = 'update' schedule_svc = ScheduleService(transaction=self.transaction) schedule_vo = schedule_svc.update(params.copy()) print_data(schedule_vo.to_dict(), 'test_update_schedule') ScheduleInfo(schedule_vo) self.assertIsInstance(schedule_vo, Schedule) self.assertEqual(new_schedule_vo.storage_id, schedule_vo.schedule_id) self.assertIsInstance(schedule_vo.schedule, Scheduled) self.assertEqual(schedule_vo.schedule.cron, params['schedule']['cron']) self.assertEqual(params['tags'], utils.tags_to_dict(schedule_vo.tags)) self.assertEqual(params['domain_id'], schedule_vo.domain_id)
def test_delete_schedule(self, *args): new_schedule_vo = ScheduleFactory(domain_id=self.domain_id) params = { 'schedule_id': new_schedule_vo.storage_id, 'domain_id': self.domain_id } self.transaction.method = 'delete' schedule_svc = ScheduleService(transaction=self.transaction) result = schedule_svc.delete(params.copy()) self.assertIsNone(result)
def test_list_schedules_by_topic(self, *args): schedule_vos = ScheduleFactory.build_batch(10, domain_id=self.domain_id) list(map(lambda vo: vo.save(), schedule_vos)) params = {'topic': schedule_vos[0].topic, 'domain_id': self.domain_id} self.transaction.method = 'list' schedule_svc = ScheduleService(transaction=self.transaction) schedule_vos, total_count = schedule_svc.list(params.copy()) SchedulesInfo(schedule_vos, total_count) self.assertEqual(len(schedule_vos), 1) self.assertIsInstance(schedule_vos[0], Schedule) self.assertEqual(total_count, 1)
def test_get_schedule(self, *args): new_schedule_vo = ScheduleFactory(domain_id=self.domain_id) params = { 'schedule_id': new_schedule_vo.storage_id, 'domain_id': self.domain_id } self.transaction.method = 'get' schedule_svc = ScheduleService(transaction=self.transaction) schedule_vo = schedule_svc.get(params.copy()) print_data(schedule_vo.to_dict(), 'test_get_schedule') ScheduleInfo(schedule_vo) self.assertIsInstance(schedule_vo, Schedule)
def test_disable_schedule(self, *args): new_schedule_vo = ScheduleFactory(domain_id=self.domain_id, state='ENABLED') params = { 'schedule_id': new_schedule_vo.storage_id, 'domain_id': self.domain_id } self.transaction.method = 'disable' schedule_svc = ScheduleService(transaction=self.transaction) schedule_vo = schedule_svc.disable(params.copy()) print_data(schedule_vo.to_dict(), 'test_disable_schedule') ScheduleInfo(schedule_vo) self.assertIsInstance(schedule_vo, Schedule) self.assertEqual(new_schedule_vo.storage_id, schedule_vo.schedule_id) self.assertEqual('DISABLED', schedule_vo.state)
def test_update_schedule_with_wrong_schedule_option(self, *args): new_schedule_vo = ScheduleFactory(domain_id=self.domain_id) params = { 'schedule_id': new_schedule_vo.storage_id, 'schedule': { 'cron': '*/5 * * * *', 'interval': 5 }, 'tags': { 'update_key': 'update_value' }, 'domain_id': self.domain_id } self.transaction.method = 'update' schedule_svc = ScheduleService(transaction=self.transaction) with self.assertRaises(ERROR_SCHEDULE_OPTION): schedule_vo = schedule_svc.update(params.copy())
def get(self, params): return ScheduleFactory(**params)
def list(self, params): return ScheduleFactory.build_batch(10, **params), 10
def disable(self, params): return ScheduleFactory(**params)
def update(self, params): params = copy.deepcopy(params) if 'tags' in params: params['tags'] = utils.dict_to_tags(params['tags']) return ScheduleFactory(**params)
def test_create_history(self, mock_stat_resource, *args): new_schedule_vo = ScheduleFactory( domain_id=self.domain_id, options={ 'aggregate': [{ 'query': { 'resource_type': 'identity.Project', 'query': { 'aggregate': [{ 'group': { 'keys': [{ 'key': 'project_id', 'name': 'project_id' }, { 'key': 'name', 'name': 'project_name' }] } }] } } }, { 'join': { 'resource_type': 'inventory.Server', 'type': 'LEFT', 'keys': ['project_id'], 'query': { 'aggregate': [{ 'group': { 'keys': [{ 'key': 'project_id', 'name': 'project_id' }], 'fields': [{ 'operator': 'count', 'name': 'server_count' }] } }] } } }, { 'join': { 'resource_type': 'inventory.CloudService', 'type': 'LEFT', 'keys': ['project_id'], 'query': { 'aggregate': [{ 'group': { 'keys': [{ 'key': 'project_id', 'name': 'project_id' }], 'fields': [{ 'operator': 'count', 'name': 'cloud_service_count' }] } }] } } }, { 'formula': { 'eval': 'resource_count = server_count + cloud_service_count' } }, { 'sort': { 'key': 'resource_count', 'desc': True } }], 'page': { 'limit': 5 } }) mock_stat_resource.side_effect = [{ 'results': [{ 'project_id': 'project-123', 'project_name': 'ncsoft' }, { 'project_id': 'project-456', 'project_name': 'nexon' }, { 'project_id': 'project-789', 'project_name': 'netmarble' }] }, { 'results': [{ 'project_id': 'project-123', 'server_count': 100 }, { 'project_id': 'project-456', 'server_count': 65 }, { 'project_id': 'project-789', 'server_count': 77 }] }, { 'results': [{ 'project_id': 'project-123', 'cloud_service_count': 45 }, { 'project_id': 'project-456', 'cloud_service_count': 87 }, { 'project_id': 'project-789', 'cloud_service_count': 104 }] }] params = { 'schedule_id': new_schedule_vo.storage_id, 'domain_id': self.domain_id } self.transaction.method = 'create' history_svc = HistoryService(transaction=self.transaction) result = history_svc.create(params.copy()) self.assertIsNone(result)
def update(self, params): return ScheduleFactory(**params)
def add(self, params): return ScheduleFactory(**params)