Add missing tests for bucket util (#565)

* Add CheckDeepEqual test util

* Fix for srcContext with slash suffix

* Add tests for bucket_util.go
This commit is contained in:
Shuhei Kitagawa 2019-02-20 00:05:23 +09:00 committed by dlorenc
parent 4feed0ff35
commit e8564f0d28
3 changed files with 76 additions and 1 deletions

View File

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

View File

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

View File

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