[go: nahoru, domu]

Skip to content

Commit

Permalink
fix(retention) swagger test case
Browse files Browse the repository at this point in the history
Signed-off-by: Ziming Zhang <zziming@vmware.com>
  • Loading branch information
bitsf committed Jan 11, 2021
1 parent efa63d9 commit be58c1d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 45 deletions.
30 changes: 13 additions & 17 deletions api/v2.0/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -2469,21 +2469,17 @@ 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':
$ref: '#/responses/403'
'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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
26 changes: 12 additions & 14 deletions src/server/v2.0/handler/retention.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -29,7 +28,6 @@ func newRetentionAPI() *retentionAPI {
}
}

// RetentionAPI ...
type retentionAPI struct {
BaseAPI
proMetaMgr metadata.Manager
Expand Down Expand Up @@ -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)))
Expand All @@ -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'"))))
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion tests/apitests/python/library/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {}."):
Expand Down
29 changes: 16 additions & 13 deletions tests/apitests/python/library/retention.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -53,13 +56,13 @@ def create_retention_policy(self, project_id, selector_repository="**", selector
},
)
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

Expand Down Expand Up @@ -107,12 +110,12 @@ def update_retention_policy(self, retention_id, selector_repository="**", select
},
)
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,
Expand All @@ -139,46 +142,46 @@ def update_retention_add_rule(self, retention_id, selector_repository="**", sele
}
]
))
_, 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
4 changes: 4 additions & 0 deletions tests/robot-cases/Group0-BAT/API_DB.robot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit be58c1d

Please sign in to comment.