2019-07-23 18:00:31 +02:00
|
|
|
// Copyright Project Harbor Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package retention
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-07-20 05:33:08 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2019-07-24 07:10:10 +02:00
|
|
|
"github.com/goharbor/harbor/src/jobservice/job"
|
|
|
|
"github.com/goharbor/harbor/src/jobservice/logger"
|
2022-07-20 05:33:08 +02:00
|
|
|
"github.com/goharbor/harbor/src/lib/selector"
|
2020-03-24 13:45:45 +01:00
|
|
|
"github.com/goharbor/harbor/src/lib/selector/selectors/doublestar"
|
2019-07-24 07:10:10 +02:00
|
|
|
"github.com/goharbor/harbor/src/pkg/retention/dep"
|
|
|
|
"github.com/goharbor/harbor/src/pkg/retention/policy"
|
|
|
|
"github.com/goharbor/harbor/src/pkg/retention/policy/action"
|
|
|
|
"github.com/goharbor/harbor/src/pkg/retention/policy/lwp"
|
|
|
|
"github.com/goharbor/harbor/src/pkg/retention/policy/rule"
|
2019-07-29 04:33:17 +02:00
|
|
|
"github.com/goharbor/harbor/src/pkg/retention/policy/rule/latestps"
|
2019-07-23 18:00:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// JobTestSuite is test suite for testing job
|
|
|
|
type JobTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
oldClient dep.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestJob is entry of running JobTestSuite
|
|
|
|
func TestJob(t *testing.T) {
|
|
|
|
suite.Run(t, new(JobTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetupSuite ...
|
|
|
|
func (suite *JobTestSuite) SetupSuite() {
|
|
|
|
suite.oldClient = dep.DefaultClient
|
|
|
|
dep.DefaultClient = &fakeRetentionClient{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TearDownSuite ...
|
|
|
|
func (suite *JobTestSuite) TearDownSuite() {
|
|
|
|
dep.DefaultClient = suite.oldClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *JobTestSuite) TestRunSuccess() {
|
|
|
|
params := make(job.Parameters)
|
2019-07-25 05:32:56 +02:00
|
|
|
params[ParamDryRun] = false
|
2020-03-09 14:15:53 +01:00
|
|
|
repository := &selector.Repository{
|
2019-07-23 18:00:31 +02:00
|
|
|
Namespace: "library",
|
|
|
|
Name: "harbor",
|
2020-03-09 14:15:53 +01:00
|
|
|
Kind: selector.Image,
|
2019-07-23 18:00:31 +02:00
|
|
|
}
|
2019-07-29 04:33:17 +02:00
|
|
|
repoJSON, err := repository.ToJSON()
|
2019-07-27 05:39:17 +02:00
|
|
|
require.Nil(suite.T(), err)
|
2019-07-29 04:33:17 +02:00
|
|
|
params[ParamRepo] = repoJSON
|
2019-07-23 18:00:31 +02:00
|
|
|
|
|
|
|
scopeSelectors := make(map[string][]*rule.Selector)
|
|
|
|
scopeSelectors["project"] = []*rule.Selector{{
|
|
|
|
Kind: doublestar.Kind,
|
|
|
|
Decoration: doublestar.RepoMatches,
|
|
|
|
Pattern: "{harbor}",
|
|
|
|
}}
|
|
|
|
|
|
|
|
ruleParams := make(rule.Parameters)
|
2019-07-29 04:33:17 +02:00
|
|
|
ruleParams[latestps.ParameterK] = 10
|
2019-07-23 18:00:31 +02:00
|
|
|
|
2019-07-27 05:39:17 +02:00
|
|
|
meta := &lwp.Metadata{
|
2019-07-23 18:00:31 +02:00
|
|
|
Algorithm: policy.AlgorithmOR,
|
|
|
|
Rules: []*rule.Metadata{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
Priority: 999,
|
|
|
|
Action: action.Retain,
|
2019-07-29 04:33:17 +02:00
|
|
|
Template: latestps.TemplateID,
|
2019-07-23 18:00:31 +02:00
|
|
|
Parameters: ruleParams,
|
|
|
|
TagSelectors: []*rule.Selector{{
|
|
|
|
Kind: doublestar.Kind,
|
|
|
|
Decoration: doublestar.Matches,
|
|
|
|
Pattern: "**",
|
|
|
|
}},
|
|
|
|
ScopeSelectors: scopeSelectors,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2019-07-29 04:33:17 +02:00
|
|
|
metaJSON, err := meta.ToJSON()
|
2019-07-27 05:39:17 +02:00
|
|
|
require.Nil(suite.T(), err)
|
2019-07-29 04:33:17 +02:00
|
|
|
params[ParamMeta] = metaJSON
|
2019-07-23 18:00:31 +02:00
|
|
|
|
2019-07-25 05:32:56 +02:00
|
|
|
j := &Job{}
|
2019-07-27 05:39:17 +02:00
|
|
|
err = j.Validate(params)
|
2019-07-23 18:00:31 +02:00
|
|
|
require.NoError(suite.T(), err)
|
|
|
|
|
|
|
|
err = j.Run(&fakeJobContext{}, params)
|
|
|
|
require.NoError(suite.T(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeRetentionClient struct{}
|
|
|
|
|
|
|
|
// GetCandidates ...
|
2020-03-09 14:15:53 +01:00
|
|
|
func (frc *fakeRetentionClient) GetCandidates(repo *selector.Repository) ([]*selector.Candidate, error) {
|
|
|
|
return []*selector.Candidate{
|
2019-07-23 18:00:31 +02:00
|
|
|
{
|
|
|
|
Namespace: "library",
|
|
|
|
Repository: "harbor",
|
|
|
|
Kind: "image",
|
2020-02-17 03:53:09 +01:00
|
|
|
Tags: []string{"latest"},
|
2019-08-28 12:27:35 +02:00
|
|
|
Digest: "latest",
|
2019-07-23 18:00:31 +02:00
|
|
|
PushedTime: time.Now().Unix() - 11,
|
|
|
|
PulledTime: time.Now().Unix() - 2,
|
|
|
|
CreationTime: time.Now().Unix() - 10,
|
|
|
|
Labels: []string{"L1", "L2"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Namespace: "library",
|
|
|
|
Repository: "harbor",
|
|
|
|
Kind: "image",
|
2020-02-17 03:53:09 +01:00
|
|
|
Tags: []string{"dev", "test"},
|
2019-08-28 12:27:35 +02:00
|
|
|
Digest: "dev",
|
2019-07-23 18:00:31 +02:00
|
|
|
PushedTime: time.Now().Unix() - 10,
|
|
|
|
PulledTime: time.Now().Unix() - 3,
|
|
|
|
CreationTime: time.Now().Unix() - 20,
|
|
|
|
Labels: []string{"L3"},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete ...
|
2020-03-09 14:15:53 +01:00
|
|
|
func (frc *fakeRetentionClient) Delete(candidate *selector.Candidate) error {
|
2019-07-23 18:00:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubmitTask ...
|
2020-03-09 14:15:53 +01:00
|
|
|
func (frc *fakeRetentionClient) DeleteRepository(repo *selector.Repository) error {
|
2019-07-30 02:21:30 +02:00
|
|
|
return nil
|
2019-07-23 18:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type fakeLogger struct{}
|
|
|
|
|
|
|
|
// For debuging
|
|
|
|
func (l *fakeLogger) Debug(v ...interface{}) {}
|
|
|
|
|
|
|
|
// For debuging with format
|
|
|
|
func (l *fakeLogger) Debugf(format string, v ...interface{}) {}
|
|
|
|
|
|
|
|
// For logging info
|
|
|
|
func (l *fakeLogger) Info(v ...interface{}) {
|
|
|
|
fmt.Println(v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For logging info with format
|
|
|
|
func (l *fakeLogger) Infof(format string, v ...interface{}) {
|
|
|
|
fmt.Printf(format+"\n", v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For warning
|
|
|
|
func (l *fakeLogger) Warning(v ...interface{}) {}
|
|
|
|
|
|
|
|
// For warning with format
|
|
|
|
func (l *fakeLogger) Warningf(format string, v ...interface{}) {}
|
|
|
|
|
|
|
|
// For logging error
|
|
|
|
func (l *fakeLogger) Error(v ...interface{}) {
|
|
|
|
fmt.Println(v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For logging error with format
|
|
|
|
func (l *fakeLogger) Errorf(format string, v ...interface{}) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// For fatal error
|
|
|
|
func (l *fakeLogger) Fatal(v ...interface{}) {}
|
|
|
|
|
|
|
|
// For fatal error with error
|
|
|
|
func (l *fakeLogger) Fatalf(format string, v ...interface{}) {}
|
|
|
|
|
|
|
|
type fakeJobContext struct{}
|
|
|
|
|
|
|
|
func (c *fakeJobContext) Build(tracker job.Tracker) (job.Context, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeJobContext) Get(prop string) (interface{}, bool) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeJobContext) SystemContext() context.Context {
|
|
|
|
return context.TODO()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeJobContext) Checkin(status string) error {
|
|
|
|
fmt.Printf("Check in: %s\n", status)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeJobContext) OPCommand() (job.OPCommand, bool) {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeJobContext) GetLogger() logger.Interface {
|
|
|
|
return &fakeLogger{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeJobContext) Tracker() job.Tracker {
|
|
|
|
return nil
|
|
|
|
}
|