From 07d79a8d8564c7dcdff96ace14aa5e3c561af070 Mon Sep 17 00:00:00 2001 From: NicoForce Date: Wed, 6 Sep 2023 16:37:00 -0400 Subject: [PATCH] Enabling flatenning on empty namespace --- src/controller/replication/flow/stage.go | 6 +- src/controller/replication/flow/stage_test.go | 64 +++++++++++++++---- 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/src/controller/replication/flow/stage.go b/src/controller/replication/flow/stage.go index 803d6cc26007..6290218e556d 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 7e9536d626c9..824645252ffc 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 + // replace count <0, repository contains no "/" var ( - repository string = "c" - namespace string = "" - replaceCount int8 = 0 + repository = "c" + namespace = "n" + replaceCount = -1 ) - 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 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) {