Enabling flatenning on empty namespace

Signed-off-by: NicoForce <github.hhedq@simplelogin.com>
This commit is contained in:
NicoForce 2023-09-06 16:37:00 -04:00
parent 7f191630e4
commit 21281d0be7
2 changed files with 55 additions and 17 deletions

View File

@ -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], "/")
}

View File

@ -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) {