diff --git a/src/controller/replication/flow/stage.go b/src/controller/replication/flow/stage.go index 803d6cc26..6290218e5 100644 --- a/src/controller/replication/flow/stage.go +++ b/src/controller/replication/flow/stage.go @@ -159,10 +159,6 @@ func getResourceName(res *model.Resource) string { // repository:a/b/c/image namespace:n replaceCount: 3 -> n/image // repository:a/b/c/image namespace:n replaceCount: 4 -> error func replaceNamespace(repository string, namespace string, replaceCount int8, dstRepoComponentPathType string) (string, error) { - if len(namespace) == 0 { - return repository, nil - } - srcRepoPathComponents := strings.Split(repository, "/") srcLength := len(srcRepoPathComponents) @@ -174,6 +170,8 @@ func replaceNamespace(repository string, namespace string, replaceCount int8, ds return "", errors.New(nil).WithCode(errors.BadRequestCode). WithMessage("the source repository %q contains only %d path components %v excepting the last one, but the destination namespace flattening level is %d", repository, srcLength-1, srcRepoPathComponents[:srcLength-1], replaceCount) + case len(namespace) == 0: + dstRepoPrefix = strings.Join(srcRepoPathComponents[replaceCount:srcLength-1], "/") default: dstRepoPrefix = namespace + "/" + strings.Join(srcRepoPathComponents[replaceCount:srcLength-1], "/") } diff --git a/src/controller/replication/flow/stage_test.go b/src/controller/replication/flow/stage_test.go index 7e9536d62..824645252 100644 --- a/src/controller/replication/flow/stage_test.go +++ b/src/controller/replication/flow/stage_test.go @@ -116,20 +116,12 @@ func (s *stageTestSuite) TestAssembleDestinationResources() { } func (s *stageTestSuite) TestReplaceNamespace() { - // empty namespace - var ( - repository string = "c" - namespace string = "" - replaceCount int8 = 0 - ) - result, err := replaceNamespace(repository, namespace, replaceCount, "") - s.Require().Nil(err) - s.Equal("c", result) - // replace count <0, repository contains no "/" - repository = "c" - namespace = "n" - replaceCount = -1 + var ( + repository = "c" + namespace = "n" + replaceCount = -1 + ) result, err = replaceNamespace(repository, namespace, replaceCount, "") s.Require().Nil(err) s.Equal("n/c", result) @@ -195,6 +187,54 @@ func (s *stageTestSuite) TestReplaceNamespace() { result, err = replaceNamespace(repository, namespace, replaceCount, "") s.Require().Nil(err) s.Equal("n/a", result) + + // replace count = 0 with empty namespace + repository = "a/b/c" + namespace = "" + replaceCount = 0 + result, err = replaceNamespace(repository, namespace, replaceCount, "") + s.Require().Nil(err) + s.Equal("a/b/c", result) + + // replace count = 1 with empty namespace + repository = "a/b/c" + namespace = "" + replaceCount = 1 + result, err = replaceNamespace(repository, namespace, replaceCount, "") + s.Require().Nil(err) + s.Equal("b/c", result) + + // replace count = 2 with empty namespace + repository = "a/b/c" + namespace = "" + replaceCount = 2 + result, err = replaceNamespace(repository, namespace, replaceCount, "") + s.Require().Nil(err) + s.Equal("c", result) + + // replace count <0, repository contains no "/" with empty namespace + repository = "c" + namespace = "" + replaceCount = -1 + result, err = replaceNamespace(repository, namespace, replaceCount, "") + s.Require().Nil(err) + s.Equal("c", result) + + // replace count <0, repository contains only one "/" with empty namespace + repository = "b/c" + namespace = "" + replaceCount = -1 + result, err = replaceNamespace(repository, namespace, replaceCount, "") + s.Require().Nil(err) + s.Equal("c", result) + + // replace count <0, repository contains more than one "/" with empty namespace + repository = "a/b/c" + namespace = "" + replaceCount = -1 + result, err = replaceNamespace(repository, namespace, replaceCount, "") + s.Require().Nil(err) + s.Equal("c", result) } func TestStage(t *testing.T) {