diff --git a/pkg/util/bucket_util.go b/pkg/util/bucket_util.go index e2f4dea6ca..6627339af9 100644 --- a/pkg/util/bucket_util.go +++ b/pkg/util/bucket_util.go @@ -24,7 +24,7 @@ import ( func GetBucketAndItem(context string) (string, string) { split := strings.SplitN(context, "/", 2) - if len(split) == 2 { + if len(split) == 2 && split[1] != "" { return split[0], split[1] } return split[0], constants.ContextTar diff --git a/pkg/util/bucket_util_test.go b/pkg/util/bucket_util_test.go new file mode 100644 index 0000000000..dd0e7626d0 --- /dev/null +++ b/pkg/util/bucket_util_test.go @@ -0,0 +1,67 @@ +/* +Copyright 2018 Google LLC + +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 util + +import ( + "testing" + + "github.com/GoogleContainerTools/kaniko/pkg/constants" + "github.com/GoogleContainerTools/kaniko/testutil" +) + +func Test_GetBucketAndItem(t *testing.T) { + tests := []struct { + name string + context string + expectedBucket string + expectedItem string + }{ + { + name: "three slashes", + context: "test1/test2/test3", + expectedBucket: "test1", + expectedItem: "test2/test3", + }, + { + name: "two slashes", + context: "test1/test2", + expectedBucket: "test1", + expectedItem: "test2", + }, + { + name: "one slash", + context: "test1/", + expectedBucket: "test1", + expectedItem: constants.ContextTar, + }, + { + name: "zero slash", + context: "test1", + expectedBucket: "test1", + expectedItem: constants.ContextTar, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + gotBucket, gotItem := GetBucketAndItem(test.context) + testutil.CheckDeepEqual(t, test.expectedBucket, gotBucket) + testutil.CheckDeepEqual(t, test.expectedItem, gotItem) + }) + } + +} diff --git a/testutil/util.go b/testutil/util.go index b6106fab8d..a1e72dd08c 100644 --- a/testutil/util.go +++ b/testutil/util.go @@ -41,6 +41,14 @@ func SetupFiles(path string, files map[string]string) error { return nil } +func CheckDeepEqual(t *testing.T, expected, actual interface{}) { + t.Helper() + if diff := cmp.Diff(actual, expected); diff != "" { + t.Errorf("%T differ (-got, +want): %s", expected, diff) + return + } +} + func CheckErrorAndDeepEqual(t *testing.T, shouldErr bool, err error, expected, actual interface{}) { t.Helper() if err := checkErr(shouldErr, err); err != nil {