def test_get_meta(self): request = unit_utils.get_fake_request(method='POST') fixture = {'meta': {'key': 'key1', 'value': 'value1'}} self.controller.create(request, self.schedule_1['id'], fixture) request = unit_utils.get_fake_request(method='GET') meta = self.controller.get(request, self.schedule_1['id'], 'key1') self.assertEqual(meta['meta']['key'], fixture['meta']['key']) self.assertEqual(meta['meta']['value'], fixture['meta']['value'])
def test_delete_meta(self): request = unit_utils.get_fake_request(method='POST') fixture = {'meta': {'key': 'key1', 'value': 'value1'}} self.controller.create(request, self.schedule_1['id'], fixture) request = unit_utils.get_fake_request(method='DELETE') self.controller.delete(request, self.schedule_1['id'], 'key1') request = unit_utils.get_fake_request(method='GET') self.assertRaises(webob.exc.HTTPNotFound, self.controller.get, request, self.schedule_1['id'], 'key1')
def test_list_meta(self): request = unit_utils.get_fake_request(method='POST') fixture = {'meta': {'key': 'key1', 'value': 'value1'}} self.controller.create(request, self.schedule_1['id'], fixture) fixture2 = {'meta': {'key': 'key2', 'value': 'value2'}} self.controller.create(request, self.schedule_1['id'], fixture2) request = unit_utils.get_fake_request(method='GET') metadata = self.controller.list(request, self.schedule_1['id']) self.assertEqual(2, len(metadata['metadata'])) self.assertMetadataInList(metadata['metadata'], fixture['meta']) self.assertMetadataInList(metadata['metadata'], fixture2['meta'])
def test_update_heartbeat_not_found(self): request = unit_test_utils.get_fake_request(method='PUT') job_id = str(uuid.uuid4()) body = {'heartbeat': '2012-11-16T18:41:43Z'} self.assertRaises(webob.exc.HTTPNotFound, self.controller.update_heartbeat, request, job_id, body)
def test_list(self): request = unit_test_utils.get_fake_request(method='GET') jobs = self.controller.list(request).get('jobs') self.assertEqual(len(jobs), 2) for k in JOB_ATTRS: self.assertEqual(set([s[k] for s in jobs]), set([self.job_1[k], self.job_2[k]]))
def test_create(self): expected_next_run = timeutils.parse_isotime( '1989-01-19T12:00:00Z').replace(tzinfo=None) self._stub_notifications(None, 'qonos.job.create', 'fake-payload', 'INFO') def fake_schedule_to_next_run(_schedule, start_time=None): self.assertEqual(timeutils.utcnow(), start_time) return expected_next_run self.stubs.Set(api_utils, 'schedule_to_next_run', fake_schedule_to_next_run) request = unit_utils.get_fake_request(method='POST') fixture = {'job': {'schedule_id': self.schedule_1['id']}} job = self.controller.create(request, fixture).get('job') self.assertNotEqual(job, None) self.assertNotEqual(job.get('id'), None) self.assertEqual(job['schedule_id'], self.schedule_1['id']) self.assertEqual(job['tenant'], self.schedule_1['tenant']) self.assertEqual(job['action'], self.schedule_1['action']) self.assertEqual(job['status'], 'QUEUED') self.assertEqual(len(job['metadata']), 0) schedule = db_api.schedule_get_by_id(self.schedule_1['id']) self.assertNotEqual(schedule['next_run'], self.schedule_1['next_run']) self.assertEqual(schedule['next_run'], expected_next_run) self.assertNotEqual(schedule['last_scheduled'], self.schedule_1.get('last_scheduled')) self.assertTrue(schedule.get('last_scheduled'))
def test_list_with_status_filter_case_insensitive(self): path = '?status=Error' request = unit_utils.get_fake_request(path=path, method='GET') jobs = self.controller.list(request).get('jobs') self.assertEqual(len(jobs), 1) self.assertEqual(jobs[0]['id'], self.job_2['id'])
def test_list_with_worker_id_filter(self): path = '?worker_id=%s' % unit_utils.WORKER_UUID2 request = unit_utils.get_fake_request(path=path, method='GET') jobs = self.controller.list(request).get('jobs') self.assertEqual(len(jobs), 1) self.assertEqual(jobs[0]['id'], self.job_2['id'])
def test_update_status_without_timeout(self): request = unit_utils.get_fake_request(method='PUT') body = {'status': {'status': 'DONE'}} actual = self.controller.update_status(request, self.job_1['id'], body)['status'] self.assertEqual(actual['status'], body['status']['status'])
def test_update_status_job_not_found(self): request = unit_utils.get_fake_request(method='PUT') job_id = str(uuid.uuid4()) body = {'status': {'status': 'QUEUED'}} self.assertRaises(webob.exc.HTTPNotFound, self.controller.update_status, request, job_id, body)
def test_update_metadata_empty(self): request = unit_utils.get_fake_request(method='PUT') expected = {'metadata': {}} actual = self.controller.update(request, self.schedule_1['id'], expected) self.assertEqual(expected, actual)
def test_update_metadata(self): request = unit_utils.get_fake_request(method='PUT') expected = {'metadata': {'key1': 'value1'}} actual = self.controller.update(request, self.job_1['id'], expected) self.assertEqual(expected, actual)
def test_update_metadata_empty_key(self): request = unit_utils.get_fake_request(method='PUT') schedule_id = self.schedule_1['id'] fixture = {'metadata': {'': 'value'}} self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update, request, schedule_id, fixture)
def test_list_with_action_filter(self): path = '?action=%s' % 'test_action' request = unit_utils.get_fake_request(path=path, method='GET') jobs = self.controller.list(request).get('jobs') self.assertEqual(len(jobs), 1) self.assertEqual(jobs[0]['id'], self.job_4['id'])
def test_list_next_run_before_filtered(self): next_run = self.schedule_3['next_run'] path = '?next_run_before=%s' path = path % next_run request = unit_utils.get_fake_request(path=path, method='GET') schedules = self.controller.list(request).get('schedules') self.assertEqual(len(schedules), 3)
def test_create_zero_hour(self): hour = 0 fixture = {'schedule': { 'id': unit_utils.SCHEDULE_UUID5, 'tenant': unit_utils.TENANT1, 'action': 'snapshot', 'minute': 30, 'hour': hour, }} expected = fixture['schedule'] request = unit_utils.get_fake_request(method='POST') actual = self.controller.create(request, fixture)['schedule'] self.assertNotEqual(actual.get('id'), None) self.assertNotEqual(actual.get('created_at'), None) self.assertNotEqual(actual.get('updated_at'), None) now = timeutils.utcnow() if not (now.hour == hour and now.minute < 30): now = now + datetime.timedelta(days=1) expected_next_run = timeutils.isotime( now.replace(hour=hour, minute=30, second=0, microsecond=0)) self.assertEqual(expected_next_run, actual['next_run']) self.assertEqual(expected['tenant'], actual['tenant']) self.assertEqual(expected['action'], actual['action']) self.assertEqual(expected['minute'], actual['minute']) self.assertEqual(expected['hour'], actual['hour'])
def test_update_with_day_of_month(self): expected_next_run = '1989-01-19T12:00:00Z' def fake_schedule_to_next_run(*args, **kwargs): return timeutils.parse_isotime(expected_next_run) self.stubs.Set(api_utils, 'schedule_to_next_run', fake_schedule_to_next_run) request = unit_utils.get_fake_request(method='PUT') update_fixture = {'schedule': {'day_of_month': '5'}} updated = self.controller.update(request, self.schedule_1['id'], update_fixture)['schedule'] self.assertNotEqual(updated.get('created_at'), None) self.assertNotEqual(updated.get('updated_at'), None) self.assertEqual(self.schedule_1['tenant'], updated['tenant']) self.assertEqual(self.schedule_1['action'], updated['action']) self.assertEqual(update_fixture['schedule']['day_of_month'], updated['day_of_month']) self.assertFalse(updated['minute']) self.assertFalse(updated['hour']) self.assertFalse(updated['month']) self.assertFalse(updated['day_of_week']) self.assertEqual(updated['next_run'], expected_next_run)
def test_update_whitespace_tenant(self): request = unit_utils.get_fake_request(method='PUT') schedule_id = self.schedule_1['id'] update_fixture = {'schedule': {'tenant': ' '}} self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update, request, schedule_id, update_fixture)
def test_list(self): request = unit_utils.get_fake_request(method='GET') schedules = self.controller.list(request).get('schedules') self.assertEqual(len(schedules), 2) for k in SCHEDULE_ATTRS: self.assertEqual(set([s[k] for s in schedules]), set([self.schedule_1[k], self.schedule_2[k]]))
def test_update_metadata_whitespace_key(self): request = unit_utils.get_fake_request(method='PUT') schedule_id = self.schedule_1['id'] fixture = {'metadata': {' ': 'value'}} self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update, request, schedule_id, fixture)
def test_list(self): self.config(limit_param_default=2, api_limit_max=4) request = unit_utils.get_fake_request(method="GET") workers = self.controller.list(request).get("workers") self.assertEqual(len(workers), 2) for k in WORKER_ATTRS: self.assertEqual(set([s[k] for s in workers]), set([self.worker_1[k], self.worker_2[k]]))
def test_get_next_job_for_action(self): request = unit_test_utils.get_fake_request(method='POST') fixture = {'action': 'snapshot'} job = self.controller.get_next_job(request, unit_test_utils.WORKER_UUID1, fixture) self.assertEqual(unit_test_utils.WORKER_UUID1, job['worker_id'])
def test_list(self): request = unit_test_utils.get_fake_request(method='GET') workers = self.controller.list(request).get('workers') self.assertEqual(len(workers), 2) for k in WORKER_ATTRS: self.assertEqual(set([s[k] for s in workers]), set([self.worker_1[k], self.worker_2[k]]))
def test_create_zero_hour(self): hour = 0 fixture = { "schedule": { "id": unit_utils.SCHEDULE_UUID5, "tenant": unit_utils.TENANT1, "action": "snapshot", "minute": 30, "hour": hour, } } expected = fixture["schedule"] request = unit_utils.get_fake_request(method="POST") actual = self.controller.create(request, fixture)["schedule"] self.assertNotEqual(actual.get("id"), None) self.assertNotEqual(actual.get("created_at"), None) self.assertNotEqual(actual.get("updated_at"), None) now = timeutils.utcnow() if not (now.hour == hour and now.minute < 30): now = now + datetime.timedelta(days=1) expected_next_run = timeutils.isotime(now.replace(hour=hour, minute=30, second=0, microsecond=0)) self.assertEqual(expected_next_run, actual["next_run"]) self.assertEqual(expected["tenant"], actual["tenant"]) self.assertEqual(expected["action"], actual["action"]) self.assertEqual(expected["minute"], actual["minute"]) self.assertEqual(expected["hour"], actual["hour"])
def test_update(self): expected_next_run = "1989-01-19T12:00:00Z" def fake_schedule_to_next_run(*args, **kwargs): return timeutils.parse_isotime(expected_next_run) self.stubs.Set(api_utils, "schedule_to_next_run", fake_schedule_to_next_run) request = unit_utils.get_fake_request(method="PUT") update_fixture = { "schedule": { "minute": "55", "hour": "5", "day": "2", "day_of_week": "4", "day_of_month": "23", "action": "new-action", "tenant": "new-tenant", } } updated = self.controller.update(request, self.schedule_1["id"], update_fixture)["schedule"] self.assertEqual(update_fixture["schedule"]["minute"], updated["minute"]) self.assertEqual(update_fixture["schedule"]["hour"], updated["hour"]) self.assertEqual(update_fixture["schedule"]["day"], updated["day"]) self.assertEqual(update_fixture["schedule"]["day_of_week"], updated["day_of_week"]) self.assertEqual(update_fixture["schedule"]["day_of_month"], updated["day_of_month"]) self.assertEqual(update_fixture["schedule"]["action"], updated["action"]) self.assertEqual(update_fixture["schedule"]["tenant"], updated["tenant"]) self.assertEqual(updated["next_run"], expected_next_run)
def test_get_next_job_for_action(self): request = unit_utils.get_fake_request(method='POST') fixture = {'action': 'snapshot'} job = self.controller.get_next_job(request, self.worker_1['id'], fixture) self.assertEqual(self.worker_1['id'], job['job']['worker_id'])
def test_list_next_run_before_filtered(self): next_run = self.schedule_3["next_run"] path = "?next_run_before=%s" path = path % next_run request = unit_utils.get_fake_request(path=path, method="GET") schedules = self.controller.list(request).get("schedules") self.assertEqual(len(schedules), 3)
def test_create_with_next_run(self): expected_next_run = timeutils.parse_isotime('1989-01-19T12:00:00Z') def fake_schedule_to_next_run(_schedule, start_time=None): self.assertEqual(timeutils.utcnow(), start_time) return expected_next_run self.stubs.Set(api_utils, 'schedule_to_next_run', fake_schedule_to_next_run) self._stub_notifications(None, 'qonos.job.create', 'fake-payload', 'INFO') request = unit_utils.get_fake_request(method='POST') fixture = {'job': {'schedule_id': self.schedule_1['id'], 'next_run': timeutils.isotime(self.schedule_1['next_run'])}} job = self.controller.create(request, fixture).get('job') self.assertNotEqual(job, None) self.assertNotEqual(job.get('id'), None) self.assertEqual(job['schedule_id'], self.schedule_1['id']) self.assertEqual(job['tenant'], self.schedule_1['tenant']) self.assertEqual(job['action'], self.schedule_1['action']) self.assertEqual(job['status'], 'QUEUED') self.assertEqual(len(job['metadata']), 0) schedule = db_api.schedule_get_by_id(self.schedule_1['id']) self.assertNotEqual(schedule['next_run'], self.schedule_1['next_run']) self.assertEqual(schedule['next_run'], expected_next_run) self.assertNotEqual(schedule['last_scheduled'], self.schedule_1.get('last_scheduled')) self.assertTrue(schedule.get('last_scheduled'))
def test_get_next_job_none_for_action(self): request = unit_utils.get_fake_request(method='POST') fixture = {'action': 'dummy'} job = self.controller.get_next_job(request, self.worker_1['id'], fixture) self.assertEqual(job['job'], None)
def test_list_with_hard_timeout_filter(self): hard_timeout = timeutils.isotime(self.job_1['hard_timeout']) path = '?hard_timeout=%s' % hard_timeout request = unit_utils.get_fake_request(path=path, method='GET') jobs = self.controller.list(request).get('jobs') self.assertEqual(len(jobs), 1) self.assertEqual(jobs[0]['id'], self.job_1['id'])
def test_list_with_limit_and_marker(self): self.config(limit_param_default=2, api_limit_max=4) path = '?marker=%s&limit=1' % unit_utils.JOB_UUID1 request = unit_utils.get_fake_request(path=path, method='GET') jobs = self.controller.list(request).get('jobs') self.assertEqual(len(jobs), 1) for k in JOB_ATTRS: self.assertEqual(set([s[k] for s in jobs]), set([self.job_2[k]]))
def test_update_invalid_next_run(self): request = unit_utils.get_fake_request(method='PUT') update_fixture = {'schedule': { 'next_run': '12345' }} self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update, request, self.schedule_1['id'], update_fixture)
def test_list(self): self.config(limit_param_default=2, api_limit_max=4) request = unit_utils.get_fake_request(method='GET') workers = self.controller.list(request).get('workers') self.assertEqual(len(workers), 2) for k in WORKER_ATTRS: self.assertEqual(set([s[k] for s in workers]), set([self.worker_1[k], self.worker_2[k]]))
def test_update_heartbeat(self): request = unit_test_utils.get_fake_request(method='PUT') body = {'heartbeat': '2012-11-16T18:41:43Z'} self.controller.update_heartbeat(request, self.job_1['id'], body) expected = timeutils.normalize_time( timeutils.parse_isotime(body['heartbeat'])) actual = db_api.job_get_by_id(self.job_1['id'])['updated_at'] self.assertEqual(actual, expected)
def test_list(self): self.config(api_limit_max=4, limit_param_default=2) request = unit_utils.get_fake_request(method='GET') jobs = self.controller.list(request).get('jobs') self.assertEqual(len(jobs), 2) for k in JOB_ATTRS: self.assertEqual(set([s[k] for s in jobs]), set([self.job_1[k], self.job_2[k]]))
def test_create_next_run_invalid_format(self): expected_next_run = '12345' self._stub_notifications(None, 'qonos.job.create', 'fake-payload', 'INFO') request = unit_utils.get_fake_request(method='POST') fixture = {'job': {'schedule_id': self.schedule_1['id'], 'next_run': expected_next_run}} self.assertRaises(webob.exc.HTTPBadRequest, self.controller.create, request, fixture)
def test_get(self): request = unit_utils.get_fake_request(method='GET') job = self.controller.get(request, self.job_1['id']).get('job') self.assertEqual(job['status'], 'QUEUED') self.assertEqual(job['schedule_id'], self.schedule_1['id']) self.assertEqual(job['worker_id'], unit_utils.WORKER_UUID1) self.assertEqual(job['retry_count'], 0) self.assertNotEqual(job['updated_at'], None) self.assertNotEqual(job['created_at'], None)
def test_list_with_marker(self): self.config(limit_param_default=2, api_limit_max=4) path = '?marker=%s' % unit_utils.WORKER_UUID1 request = unit_utils.get_fake_request(path=path, method='GET') workers = self.controller.list(request).get('workers') self.assertEqual(len(workers), 2) for k in WORKER_ATTRS: self.assertEqual(set([s[k] for s in workers]), set([self.worker_2[k], self.worker_3[k]]))
def test_list_next_run_filtered_before_less_than_after(self): after = self.schedule_3['next_run'] before = timeutils.isotime(after - datetime.timedelta(seconds=1)) path = '?next_run_after=%s&next_run_before=%s' path = path % (after, before) request = unit_utils.get_fake_request(path=path, method='GET') schedules = self.controller.list(request).get('schedules') self.assertEqual(len(schedules), 0)
def test_list_with_limit_and_marker(self): self.config(limit_param_default=2, api_limit_max=4) path = '?marker=%s&limit=1' % unit_utils.SCHEDULE_UUID1 request = unit_utils.get_fake_request(path=path, method='GET') schedules = self.controller.list(request).get('schedules') self.assertEqual(len(schedules), 1) for k in SCHEDULE_ATTRS: self.assertEqual(set([s[k] for s in schedules]), set([self.schedule_2[k]]))
def test_update_no_tenant(self): request = unit_utils.get_fake_request(method='PUT') update_fixture = {'schedule': {'action': 'snapshot'}} old_tenant = self.schedule_1['tenant'] updated = self.controller.update(request, self.schedule_1['id'], update_fixture)['schedule'] self.assertEqual(updated.get('tenant'), old_tenant)
def test_update_status(self): timeout = datetime.datetime(2012, 11, 16, 22, 0) request = unit_utils.get_fake_request(method='PUT') body = {'status': {'status': 'PROCESSING', 'timeout': str(timeout)}} job_status = self.controller.update_status(request, self.job_1['id'], body)['status'] actual_status = job_status['status'] actual_timeout = job_status['timeout'] self.assertEqual(actual_status, body['status']['status']) self.assertEqual(actual_timeout, timeout)
def test_create_next_run_differs(self): expected_next_run = '1989-01-19T12:00:00Z' self._stub_notifications(None, 'qonos.job.create', 'fake-payload', 'INFO') request = unit_utils.get_fake_request(method='POST') fixture = {'job': {'schedule_id': self.schedule_1['id'], 'next_run': expected_next_run}} self.assertRaises(webob.exc.HTTPConflict, self.controller.create, request, fixture)
def test_update_valid_next_run(self): request = unit_utils.get_fake_request(method='PUT') expected_next_run = '1989-01-19T12:00:00Z' update_fixture = {'schedule': { 'next_run': expected_next_run }} updated = self.controller.update(request, self.schedule_1['id'], update_fixture)['schedule'] self.assertEqual(updated['next_run'], expected_next_run)
def test_get_with_metadata(self): request = unit_utils.get_fake_request(method='GET') job = self.controller.get(request, self.job_2['id']).get('job') self.assertEqual(job['status'], 'ERROR') self.assertEqual(job['schedule_id'], self.schedule_2['id']) self.assertEqual(job['worker_id'], unit_utils.WORKER_UUID2) self.assertEqual(job['retry_count'], 1) self.assertNotEqual(job['updated_at'], None) self.assertNotEqual(job['created_at'], None) self.assertEqual(len(job['metadata']), 1) self.assertTrue('instance_id' in job['metadata']) self.assertEqual(job['metadata']['instance_id'], 'my_instance')
def test_list(self): request = unit_utils.get_fake_request(method='GET') schedules = self.controller.list(request).get('schedules') links = self.controller.list(request).get('schedules_links') self.assertEqual(len(schedules), 4) for k in SCHEDULE_ATTRS: self.assertEqual(set([s[k] for s in schedules]), set([self.schedule_1[k], self.schedule_2[k], self.schedule_3[k], self.schedule_4[k]])) for item in links: if item.get('rel') == 'next': self.assertEqual(item.get('href'), None)
def test_create_with_metadata(self): self._stub_notifications(None, 'qonos.job.create', 'fake-payload', 'INFO') request = unit_utils.get_fake_request(method='POST') fixture = {'job': {'schedule_id': self.schedule_2['id']}} job = self.controller.create(request, fixture).get('job') self.assertNotEqual(job, None) self.assertNotEqual(job.get('id'), None) self.assertEqual(job['schedule_id'], self.schedule_2['id']) self.assertEqual(job['tenant'], self.schedule_2['tenant']) self.assertEqual(job['action'], self.schedule_2['action']) self.assertEqual(job['status'], 'QUEUED') self.assertEqual(len(job['metadata']), 1) self.assertTrue('instance_id' in job['metadata']) self.assertEqual(job['metadata']['instance_id'], 'my_instance')
def test_list_schedules_links(self): self.config(limit_param_default=2, api_limit_max=4) path = '?marker=%s' % unit_utils.SCHEDULE_UUID1 request = unit_utils.get_fake_request(path=path, method='GET') schedules = self.controller.list(request).get('schedules') links = self.controller.list(request).get('schedules_links') self.assertEqual(len(schedules), 2) for k in SCHEDULE_ATTRS: self.assertEqual(set([s[k] for s in schedules]), set([self.schedule_2[k], self.schedule_3[k]])) for item in links: if item.get('rel') == 'next': marker = unit_utils.SCHEDULE_UUID3 self.assertEqual(item.get('href'), '/v1/schedules?marker=%s' % marker)
def test_list_jobs_links(self): self.config(limit_param_default=2, api_limit_max=4) path = '?marker=%s' % unit_utils.JOB_UUID1 request = unit_utils.get_fake_request(path=path, method='GET') response = self.controller.list(request) jobs = response.get('jobs') links = response.get('jobs_links') self.assertEqual(len(jobs), 2) for k in JOB_ATTRS: self.assertEqual(set([s[k] for s in jobs]), set([self.job_2[k], self.job_3[k]])) for item in links: if item.get('rel') == 'next': marker = unit_utils.JOB_UUID3 self.assertEqual(item.get('href'), '/v1/jobs?marker=%s' % marker)