Skip to content

Commit

Permalink
Add JobService Dashboard API test cases (goharbor#18234)
Browse files Browse the repository at this point in the history
Added test cases for the following APIs:
1. GET /jobservice/pools/{pool_id}/workers Get workers
2. PUT /jobservice/jobs/{job_id} Stop running jc
3. PUT /jobservice/queues/{job_type} stop and clean, pause, resume pending jobs in the queue
4. GET /jobservice/queues list job queues
5. GET /jobservice/pools Get worker pools
6. GET /schedules List schedules
7. GET /schedules/{job_type}/paused Get scheduler paused status

Signed-off-by: Yang Jiao <[email protected]>
  • Loading branch information
YangJiao0817 authored and sebglon committed Mar 6, 2023
1 parent 36ab5e1 commit 6ad35a5
Show file tree
Hide file tree
Showing 10 changed files with 514 additions and 13 deletions.
6 changes: 4 additions & 2 deletions tests/apitests/python/library/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _create_client(server, credential, debug, api_type="products"):
if api_type in ('projectv2', 'artifact', 'repository', 'scanner', 'scan', 'scanall', 'preheat', 'quota',
'replication', 'registry', 'robot', 'gc', 'retention', 'immutable', 'system_cve_allowlist',
'configure', 'user', 'member', 'health', 'label', 'webhook', 'purge', 'audit_log', 'scan_data_export',
'statistic', "system_info"):
'statistic', "system_info", "jobservice", "schedule"):
cfg = v2_swagger_client.Configuration()
else:
cfg = swagger_client.Configuration()
Expand Down Expand Up @@ -81,7 +81,9 @@ def _create_client(server, credential, debug, api_type="products"):
"audit_log": v2_swagger_client.AuditlogApi(v2_swagger_client.ApiClient(cfg)),
"scan_data_export": v2_swagger_client.ScanDataExportApi(v2_swagger_client.ApiClient(cfg)),
"statistic": v2_swagger_client.StatisticApi(v2_swagger_client.ApiClient(cfg)),
"system_info": v2_swagger_client.SysteminfoApi(v2_swagger_client.ApiClient(cfg))
"system_info": v2_swagger_client.SysteminfoApi(v2_swagger_client.ApiClient(cfg)),
"jobservice": v2_swagger_client.JobserviceApi(v2_swagger_client.ApiClient(cfg)),
"schedule": v2_swagger_client.ScheduleApi(v2_swagger_client.ApiClient(cfg)),
}.get(api_type,'Error: Wrong API type')

def _assert_status_code(expect_code, return_code, err_msg = r"HTTPS status code s not as we expected. Expected {}, while actual HTTPS status code is {}."):
Expand Down
22 changes: 22 additions & 0 deletions tests/apitests/python/library/gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,28 @@ def create_gc_schedule(self, schedule_type, is_delete_untagged, cron = None, exp
base._assert_status_code(expect_status_code, status_code)
return base._get_id_from_header(header)

def update_gc_schedule(self, schedule_type, cron = None, expect_status_code = 200, expect_response_body = None, **kwargs):
gc_schedule = v2_swagger_client.ScheduleObj()
gc_schedule.type = schedule_type
if cron is not None:
gc_schedule.cron = cron

gc_job = v2_swagger_client.Schedule()
gc_job.schedule = gc_schedule

try:
_, status_code, header = self._get_client(**kwargs).update_gc_schedule_with_http_info(gc_job)
except ApiException as e:
if e.status == expect_status_code:
if expect_response_body is not None and e.body.strip() != expect_response_body.strip():
raise Exception(r"Update GC schedule response body is not as expected {} actual status is {}.".format(expect_response_body.strip(), e.body.strip()))
else:
return e.reason, e.body
else:
raise Exception(r"Update GC schedule result is not as expected {} actual status is {}.".format(expect_status_code, e.status))
base._assert_status_code(expect_status_code, status_code)
return base._get_id_from_header(header)

def gc_now(self, is_delete_untagged=False, **kwargs):
gc_id = self.create_gc_schedule('Manual', is_delete_untagged, **kwargs)
return gc_id
Expand Down
67 changes: 67 additions & 0 deletions tests/apitests/python/library/jobservice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-

import base
import v2_swagger_client
from v2_swagger_client.rest import ApiException


class Jobservice(base.Base):

def __init__(self):
super(Jobservice, self).__init__(api_type="jobservice")

def get_job_queues(self, expect_status_code=200, expect_response_body=None, **kwargs):
try:
return_data, status_code, _ = self._get_client(**kwargs).list_job_queues_with_http_info()
except ApiException as e:
base._assert_status_code(expect_status_code, e.status)
if expect_response_body is not None:
base._assert_status_body(expect_response_body, e.body)
return
base._assert_status_code(expect_status_code, status_code)
return dict(zip([job_queue.job_type for job_queue in return_data], return_data))

def action_pending_jobs(self, job_type, action, expect_status_code=200, expect_response_body=None, **kwargs):
try:
action_request = v2_swagger_client.ActionRequest(action=action)
return_data, status_code, _ = self._get_client(**kwargs).action_pending_jobs_with_http_info(job_type, action_request)
except ApiException as e:
base._assert_status_code(expect_status_code, e.status)
if expect_response_body is not None:
base._assert_status_body(expect_response_body, e.body)
return
base._assert_status_code(expect_status_code, status_code)
return return_data

def get_worker_pools(self, expect_status_code=200, expect_response_body=None, **kwargs):
try:
return_data, status_code, _ = self._get_client(**kwargs).get_worker_pools_with_http_info()
except ApiException as e:
base._assert_status_code(expect_status_code, e.status)
if expect_response_body is not None:
base._assert_status_body(expect_response_body, e.body)
return
base._assert_status_code(expect_status_code, status_code)
return return_data

def get_workers(self, pool_id, expect_status_code=200, expect_response_body=None, **kwargs):
try:
return_data, status_code, _ = self._get_client(**kwargs).get_workers_with_http_info(pool_id)
except ApiException as e:
base._assert_status_code(expect_status_code, e.status)
if expect_response_body is not None:
base._assert_status_body(expect_response_body, e.body)
return
base._assert_status_code(expect_status_code, status_code)
return return_data

def stop_running_job(self, job_id, expect_status_code=200, expect_response_body=None, **kwargs):
try:
return_data, status_code, _ = self._get_client(**kwargs).stop_running_job_with_http_info(job_id)
except ApiException as e:
base._assert_status_code(expect_status_code, e.status)
if expect_response_body is not None:
base._assert_status_body(expect_response_body, e.body)
return
base._assert_status_code(expect_status_code, status_code)
return return_data
2 changes: 0 additions & 2 deletions tests/apitests/python/library/preheat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def create_instance(self, name = None, description="It's a dragonfly instance",
name = base._random_name("instance")
instance = v2_swagger_client.Instance(name=name, description=description,vendor=vendor,
endpoint=endpoint_url, auth_mode=auth_mode, enabled=enabled)
print("instance:",instance)
try:
_, status_code, header = self._get_client(**kwargs).create_instance_with_http_info(instance)
except ApiException as e:
Expand All @@ -36,7 +35,6 @@ def create_policy(self, project_name, project_id, provider_id, name = None, desc
policy = v2_swagger_client.PreheatPolicy(name=name, project_id=project_id, provider_id=provider_id,
description=description,filters=filters,
trigger=trigger, enabled=enabled)
print("policy:",policy)
try:
data, status_code, header = self._get_client(**kwargs).create_policy_with_http_info(project_name, policy)
except ApiException as e:
Expand Down
6 changes: 3 additions & 3 deletions tests/apitests/python/library/purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@


class Purge(base.Base):

def __init__(self):
super(Purge, self).__init__(api_type="purge")

def create_purge_schedule(self, type, cron, dry_run, audit_retention_hour=24, include_operations="create,delete,pull", expect_status_code=201, expect_response_body=None, **kwargs):
def create_purge_schedule(self, type, cron, dry_run=True, audit_retention_hour=24, include_operations="create,delete,pull", expect_status_code=201, expect_response_body=None, **kwargs):
scheduleObj = v2_swagger_client.ScheduleObj(type=type)
if cron is not None:
scheduleObj.cron = cron
Expand Down Expand Up @@ -102,4 +102,4 @@ def get_purge_schedule(self, expect_status_code=200, expect_response_body=None,
base._assert_status_body(expect_response_body, e.body)
return
base._assert_status_code(expect_status_code, status_code)
return return_data
return return_data
8 changes: 3 additions & 5 deletions tests/apitests/python/library/retention.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def get_retention_policy(self, retention_id, expect_status_code = 200, **kwargs)
base._assert_status_code(expect_status_code, status_code)
return policy

def update_retention_policy(self, retention_id, selector_repository="**", selector_tag="**", expect_status_code = 200, **kwargs):
def update_retention_policy(self, retention_id, project_id, selector_repository="**", selector_tag="**", cron="", expect_status_code = 200, **kwargs):
policy=v2_swagger_client.RetentionPolicy(
id=retention_id,
algorithm='or',
Expand All @@ -72,9 +72,7 @@ def update_retention_policy(self, retention_id, selector_repository="**", select
disabled=False,
action="retain",
template="always",
params= {

},
params= {},
scope_selectors={
"repository": [
{
Expand All @@ -96,7 +94,7 @@ def update_retention_policy(self, retention_id, selector_repository="**", select
trigger= {
"kind": "Schedule",
"settings": {
"cron": ""
"cron": cron
},
"references": {
}
Expand Down
21 changes: 21 additions & 0 deletions tests/apitests/python/library/scan_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ def create_scan_all_schedule(self, schedule_type, cron=None, expect_status_code=
raise Exception(r"Create scan all schedule result is not as expected {} actual status is {}.".format(expect_status_code, e.status))
base._assert_status_code(expect_status_code, status_code)

def update_scan_all_schedule(self, schedule_type, cron=None, expect_status_code=200, expect_response_body=None, **kwargs):
schedule_obj = v2_swagger_client.ScheduleObj()
schedule_obj.type = schedule_type
if cron is not None:
schedule_obj.cron = cron

schedule = v2_swagger_client.Schedule()
schedule.schedule = schedule_obj

try:
_, status_code, _ = self._get_client(**kwargs).update_scan_all_schedule_with_http_info(schedule)
except ApiException as e:
if e.status == expect_status_code:
if expect_response_body is not None and e.body.strip() != expect_response_body.strip():
raise Exception(r"Update scan all schedule response body is not as expected {} actual status is {}.".format(expect_response_body.strip(), e.body.strip()))
else:
return e.reason, e.body
else:
raise Exception(r"Update scan all schedule result is not as expected {} actual status is {}.".format(expect_status_code, e.status))
base._assert_status_code(expect_status_code, status_code)

def scan_all_now(self, **kwargs):
self.create_scan_all_schedule('Manual', **kwargs)

Expand Down
38 changes: 38 additions & 0 deletions tests/apitests/python/library/schedule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-

import base
from v2_swagger_client.rest import ApiException


class Schedule(base.Base):

def __init__(self):
super(Schedule, self).__init__(api_type="schedule")

def get_schedule_paused(self, job_type, expect_status_code=200, expect_response_body=None, **kwargs):
try:
return_data, status_code, _ = self._get_client(**kwargs).get_schedule_paused_with_http_info(job_type)
except ApiException as e:
base._assert_status_code(expect_status_code, e.status)
if expect_response_body is not None:
base._assert_status_body(expect_response_body, e.body)
return
base._assert_status_code(expect_status_code, status_code)
return return_data

def list_schedules(self, page_size=50, page=1, expect_status_code=200, expect_response_body=None, **kwargs):
try:
schedules, status_code, _ = self._get_client(**kwargs).list_schedules_with_http_info(page_size=50, page=1)
except ApiException as e:
base._assert_status_code(expect_status_code, e.status)
if expect_response_body is not None:
base._assert_status_body(expect_response_body, e.body)
return
base._assert_status_code(expect_status_code, status_code)
schedule_dict = {}
for schedule in schedules:
if schedule.vendor_id in [ None, -1]:
schedule_dict[schedule.vendor_type] = schedule
else:
schedule_dict["%s-%d" % (schedule.vendor_type, schedule.vendor_id)] = schedule
return schedule_dict
Loading

0 comments on commit 6ad35a5

Please sign in to comment.