fix(retention) swagger test case

Signed-off-by: Ziming Zhang <zziming@vmware.com>
This commit is contained in:
Ziming Zhang 2021-01-08 19:05:42 +08:00 committed by Ziming
parent efa63d905a
commit be58c1d3ff
5 changed files with 47 additions and 45 deletions

View File

@ -2443,9 +2443,9 @@ paths:
/retentions/{id}/executions:
post:
summary: Trigger a Retention job
operationId: triggerRetentionJob
description: Trigger a Retention job, if dry_run is True, nothing would be deleted actually.
summary: Trigger a Retention Execution
operationId: triggerRetentionExecution
description: Trigger a Retention Execution, if dry_run is True, nothing would be deleted actually.
tags:
- Retention
produces:
@ -2469,11 +2469,7 @@ paths:
'200':
description: Trigger a Retention job successfully.
'201':
description: Retention job created successfully.
headers:
Location:
type: string
description: The URL of the created resource
$ref: '#/responses/201'
'401':
$ref: '#/responses/401'
'403':
@ -2481,9 +2477,9 @@ paths:
'500':
$ref: '#/responses/500'
get:
summary: Get Retention jobs
operationId: listRetentionJob
description: Get Retention jobs, job status may be delayed before job service schedule it up.
summary: Get Retention executions
operationId: listRetentionExecutions
description: Get Retention executions, execution status may be delayed before job service schedule it up.
tags:
- Retention
parameters:
@ -2507,7 +2503,7 @@ paths:
description: The size of per page.
responses:
'200':
description: Get a Retention job successfully.
description: Get a Retention execution successfully.
schema:
type: array
items:
@ -2529,9 +2525,9 @@ paths:
/retentions/{id}/executions/{eid}:
patch:
summary: Stop a Retention job
operationId: operateRetentionJob
description: Stop a Retention job, only support "stop" action now.
summary: Stop a Retention execution
operationId: operateRetentionExecution
description: Stop a Retention execution, only support "stop" action now.
tags:
- Retention
parameters:
@ -2568,9 +2564,9 @@ paths:
/retentions/{id}/executions/{eid}/tasks:
get:
summary: Get Retention job tasks
summary: Get Retention tasks
operationId: listRetentionTasks
description: Get Retention job tasks, each repository as a task.
description: Get Retention tasks, each repository as a task.
tags:
- Retention
parameters:

View File

@ -12,7 +12,6 @@ import (
projectCtl "github.com/goharbor/harbor/src/controller/project"
retentionCtl "github.com/goharbor/harbor/src/controller/retention"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/pkg/project/metadata"
"github.com/goharbor/harbor/src/pkg/retention/policy"
"github.com/goharbor/harbor/src/pkg/task"
@ -29,7 +28,6 @@ func newRetentionAPI() *retentionAPI {
}
}
// RetentionAPI ...
type retentionAPI struct {
BaseAPI
proMetaMgr metadata.Manager
@ -229,7 +227,7 @@ func (r *retentionAPI) checkRuleConflict(p *policy.Metadata) error {
return nil
}
func (r *retentionAPI) TriggerRetentionJob(ctx context.Context, params operation.TriggerRetentionJobParams) middleware.Responder {
func (r *retentionAPI) TriggerRetentionExecution(ctx context.Context, params operation.TriggerRetentionExecutionParams) middleware.Responder {
p, err := r.retentionCtl.GetRetention(ctx, params.ID)
if err != nil {
return r.SendError(ctx, errors.BadRequestError((err)))
@ -245,10 +243,10 @@ func (r *retentionAPI) TriggerRetentionJob(ctx context.Context, params operation
}
location := fmt.Sprintf("%s/%d", strings.TrimSuffix(params.HTTPRequest.URL.Path, "/"), eid)
return operation.NewTriggerRetentionJobCreated().WithLocation(location)
return operation.NewTriggerRetentionExecutionCreated().WithLocation(location)
}
func (r *retentionAPI) OperateRetentionJob(ctx context.Context, params operation.OperateRetentionJobParams) middleware.Responder {
func (r *retentionAPI) OperateRetentionExecution(ctx context.Context, params operation.OperateRetentionExecutionParams) middleware.Responder {
if params.Body.Action != "stop" {
return r.SendError(ctx, errors.BadRequestError((fmt.Errorf("action should be 'stop'"))))
}
@ -263,13 +261,13 @@ func (r *retentionAPI) OperateRetentionJob(ctx context.Context, params operation
if err = r.retentionCtl.OperateRetentionExec(ctx, params.Eid, params.Body.Action); err != nil {
return r.SendError(ctx, err)
}
return operation.NewOperateRetentionJobOK()
return operation.NewOperateRetentionExecutionOK()
}
func (r *retentionAPI) ListRetentionJob(ctx context.Context, params operation.ListRetentionJobParams) middleware.Responder {
query := &q.Query{
PageNumber: *params.Page,
PageSize: *params.PageSize,
func (r *retentionAPI) ListRetentionExecutions(ctx context.Context, params operation.ListRetentionExecutionsParams) middleware.Responder {
query, err := r.BuildQuery(ctx, nil, params.Page, params.PageSize)
if err != nil {
return r.SendError(ctx, err)
}
p, err := r.retentionCtl.GetRetention(ctx, params.ID)
if err != nil {
@ -291,15 +289,15 @@ func (r *retentionAPI) ListRetentionJob(ctx context.Context, params operation.Li
for _, e := range execs {
payload = append(payload, model.NewRetentionExec(e).ToSwagger())
}
return operation.NewListRetentionJobOK().WithXTotalCount(total).
return operation.NewListRetentionExecutionsOK().WithXTotalCount(total).
WithLink(r.Links(ctx, params.HTTPRequest.URL, total, query.PageNumber, query.PageSize).String()).
WithPayload(payload)
}
func (r *retentionAPI) ListRetentionTasks(ctx context.Context, params operation.ListRetentionTasksParams) middleware.Responder {
query := &q.Query{
PageNumber: *params.Page,
PageSize: *params.PageSize,
query, err := r.BuildQuery(ctx, nil, params.Page, params.PageSize)
if err != nil {
return r.SendError(ctx, err)
}
p, err := r.retentionCtl.GetRetention(ctx, params.ID)
if err != nil {

View File

@ -28,7 +28,7 @@ def get_endpoint():
def _create_client(server, credential, debug, api_type="products"):
cfg = None
if api_type in ('projectv2', 'artifact', 'repository', 'scan', 'scanall', 'preheat', 'replication', 'robot', 'gc'):
if api_type in ('projectv2', 'artifact', 'repository', 'scan', 'scanall', 'preheat', 'replication', 'robot', 'gc', 'retention'):
cfg = v2_swagger_client.Configuration()
else:
cfg = swagger_client.Configuration()
@ -63,6 +63,7 @@ def _create_client(server, credential, debug, api_type="products"):
"replication": v2_swagger_client.ReplicationApi(v2_swagger_client.ApiClient(cfg)),
"robot": v2_swagger_client.RobotApi(v2_swagger_client.ApiClient(cfg)),
"gc": v2_swagger_client.GcApi(v2_swagger_client.ApiClient(cfg)),
"retention": v2_swagger_client.RetentionApi(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 {}."):

View File

@ -1,12 +1,15 @@
# -*- coding: utf-8 -*-
import base
import swagger_client
import v2_swagger_client as swagger_client
class Retention(base.Base):
def __init__(self):
super(Retention,self).__init__(api_type="retention")
def get_metadatas(self, expect_status_code = 200, **kwargs):
client = self._get_client(**kwargs)
metadatas, status_code, _ = client.retentions_metadatas_get_with_http_info()
metadatas, status_code, _ = client.get_rentenition_metadata_with_http_info()
base._assert_status_code(expect_status_code, status_code)
return metadatas
@ -53,13 +56,13 @@ class Retention(base.Base):
},
)
client = self._get_client(**kwargs)
_, status_code, header = client.retentions_post_with_http_info(policy)
_, status_code, header = client.create_retention_with_http_info(policy)
base._assert_status_code(expect_status_code, status_code)
return base._get_id_from_header(header)
def get_retention_policy(self, retention_id, expect_status_code = 200, **kwargs):
client = self._get_client(**kwargs)
policy, status_code, _ = client.retentions_id_get_with_http_info(retention_id)
policy, status_code, _ = client.get_retention_with_http_info(retention_id)
base._assert_status_code(expect_status_code, status_code)
return policy
@ -107,12 +110,12 @@ class Retention(base.Base):
},
)
client = self._get_client(**kwargs)
_, status_code, _ = client.retentions_id_put_with_http_info(retention_id, policy)
_, status_code, _ = client.update_retention_with_http_info(retention_id, policy)
base._assert_status_code(expect_status_code, status_code)
def update_retention_add_rule(self, retention_id, selector_repository="**", selector_tag="**", with_untag="True", expect_status_code = 200, **kwargs):
client = self._get_client(**kwargs)
policy, status_code, _ = client.retentions_id_get_with_http_info(retention_id)
policy, status_code, _ = client.get_retention_with_http_info(retention_id)
base._assert_status_code(200, status_code)
policy.rules.append(swagger_client.RetentionRule(
disabled=False,
@ -139,46 +142,46 @@ class Retention(base.Base):
}
]
))
_, status_code, _ = client.retentions_id_put_with_http_info(retention_id, policy)
_, status_code, _ = client.update_retention_with_http_info(retention_id, policy)
base._assert_status_code(expect_status_code, status_code)
def trigger_retention_policy(self, retention_id, dry_run=False, expect_status_code = 201, **kwargs):
client = self._get_client(**kwargs)
_, status_code, _ = client.retentions_id_executions_post_with_http_info(retention_id, {"dry_run":dry_run})
_, status_code, _ = client.trigger_retention_execution_with_http_info(retention_id, {"dry_run":dry_run})
base._assert_status_code(expect_status_code, status_code)
def stop_retention_execution(self, retention_id, exec_id, expect_status_code = 200, **kwargs):
client = self._get_client(**kwargs)
r, status_code, _ = client.retentions_id_executions_eid_patch(retention_id, exec_id, {"action":"stop"})
r, status_code, _ = client.operate_retention_execution_with_http_info(retention_id, exec_id, {"action":"stop"})
base._assert_status_code(expect_status_code, status_code)
return r
def get_retention_executions(self, retention_id, expect_status_code = 200, **kwargs):
client = self._get_client(**kwargs)
r, status_code, _ = client.retentions_id_executions_get_with_http_info(retention_id)
r, status_code, _ = client.list_retention_executions_with_http_info(retention_id)
base._assert_status_code(expect_status_code, status_code)
return r
def get_retention_exec_tasks(self, retention_id, exec_id, expect_status_code = 200, **kwargs):
client = self._get_client(**kwargs)
r, status_code, _ = client.retentions_id_executions_eid_tasks_get_with_http_info(retention_id, exec_id)
r, status_code, _ = client.list_retention_tasks_with_http_info(retention_id, exec_id)
base._assert_status_code(expect_status_code, status_code)
return r
def get_retention_exec_task_log(self, retention_id, exec_id, task_id, expect_status_code = 200, **kwargs):
client = self._get_client(**kwargs)
r, status_code, _ = client.retentions_id_executions_eid_tasks_tid_get_with_http_info(retention_id, exec_id, task_id)
r, status_code, _ = client.get_retention_task_log_with_http_info(retention_id, exec_id, task_id)
base._assert_status_code(expect_status_code, status_code)
return r
def get_retention_metadatas(self, expect_status_code = 200, **kwargs):
client = self._get_client(**kwargs)
r, status_code, _ = client.retentions_metadatas_get_with_http_info()
r, status_code, _ = client.get_rentenition_metadata_with_http_info()
base._assert_status_code(expect_status_code, status_code)
return r

View File

@ -85,6 +85,10 @@ Test Case - Project Level CVE Allowlist
[Tags] pro_cve
Harbor API Test ./tests/apitests/python/test_project_level_cve_allowlist.py
Test Case - Tag Retention
[Tags] tag_retention
Harbor API Test ./tests/apitests/python/test_retention.py
Test Case - Health Check
[Tags] health
Harbor API Test ./tests/apitests/python/test_health_check.py