add tests (#1426)
This commit is contained in:
parent
63153a3b33
commit
357286fa4b
|
|
@ -333,16 +333,16 @@ func TestImageNameDigestFile(t *testing.T) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var calledExecCommand = false
|
var calledExecCommand = []bool{}
|
||||||
var calledCheckPushPermission = false
|
var calledCheckPushPermission = false
|
||||||
|
|
||||||
func setCalledFalse() {
|
func setCalledFalse() {
|
||||||
calledExecCommand = false
|
calledExecCommand = []bool{}
|
||||||
calledCheckPushPermission = false
|
calledCheckPushPermission = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func fakeExecCommand(command string, args ...string) *exec.Cmd {
|
func fakeExecCommand(command string, args ...string) *exec.Cmd {
|
||||||
calledExecCommand = true
|
calledExecCommand = append(calledExecCommand, true)
|
||||||
cs := []string{"-test.run=TestHelperProcess", "--", command}
|
cs := []string{"-test.run=TestHelperProcess", "--", command}
|
||||||
cs = append(cs, args...)
|
cs = append(cs, args...)
|
||||||
cmd := exec.Command(os.Args[0], cs...)
|
cmd := exec.Command(os.Args[0], cs...)
|
||||||
|
|
@ -357,39 +357,61 @@ func fakeCheckPushPermission(ref name.Reference, kc authn.Keychain, t http.Round
|
||||||
|
|
||||||
func TestCheckPushPermissions(t *testing.T) {
|
func TestCheckPushPermissions(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
Destination string
|
description string
|
||||||
ShouldCallExecCommand bool
|
Destination []string
|
||||||
|
ShouldCallExecCommand []bool
|
||||||
ExistingConfig bool
|
ExistingConfig bool
|
||||||
}{
|
}{
|
||||||
{"gcr.io/test-image", true, false},
|
{"a gcr image without config", []string{"gcr.io/test-image"}, []bool{true}, false},
|
||||||
{"gcr.io/test-image", false, true},
|
{"a gcr image with config", []string{"gcr.io/test-image"}, []bool{false}, true},
|
||||||
{"us-docker.pkg.dev/test-image", true, false},
|
{"a pkg.dev image without config", []string{"us-docker.pkg.dev/test-image"}, []bool{true}, false},
|
||||||
{"us-docker.pkg.dev/test-image", false, true},
|
{"a pkg.dev image with config", []string{"us-docker.pkg.dev/test-image"}, []bool{false}, true},
|
||||||
{"localhost:5000/test-image", false, false},
|
{"localhost registry with config", []string{"localhost:5000/test-image"}, []bool{false}, false},
|
||||||
{"localhost:5000/test-image", false, true},
|
{"localhost registry without config", []string{"localhost:5000/test-image"}, []bool{false}, true},
|
||||||
{"notgcr.io/test-image", false, false},
|
{"any other registry", []string{"notgcr.io/test-image"}, []bool{false}, false},
|
||||||
{"notgcr.io/test-image", false, true},
|
{"multiple destinations pushed to different registry",
|
||||||
|
[]string{
|
||||||
|
"us-central1-docker.pkg.dev/prj/test-image",
|
||||||
|
"us-west-docker.pkg.dev/prj/test-image",
|
||||||
|
},
|
||||||
|
[]bool{true, true}, false,
|
||||||
|
},
|
||||||
|
{"same image names with different tags",
|
||||||
|
[]string{
|
||||||
|
"us-central1-docker.pkg.dev/prj/test-image:tag1",
|
||||||
|
"us-central1-docker.pkg.dev/prj/test-image:tag2",
|
||||||
|
},
|
||||||
|
[]bool{true, true}, false,
|
||||||
|
},
|
||||||
|
{"same destination image multiple times",
|
||||||
|
[]string{
|
||||||
|
"us-central1-docker.pkg.dev/prj/test-image",
|
||||||
|
"us-central1-docker.pkg.dev/prj/test-image",
|
||||||
|
},
|
||||||
|
[]bool{true, false}, false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
execCommand = fakeExecCommand
|
execCommand = fakeExecCommand
|
||||||
checkRemotePushPermission = fakeCheckPushPermission
|
checkRemotePushPermission = fakeCheckPushPermission
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
testName := fmt.Sprintf("%s_ExistingDockerConf_%v", test.Destination, test.ExistingConfig)
|
t.Run(test.description, func(t *testing.T) {
|
||||||
t.Run(testName, func(t *testing.T) {
|
setCalledFalse()
|
||||||
fs = afero.NewMemMapFs()
|
fs = afero.NewMemMapFs()
|
||||||
opts := config.KanikoOptions{
|
opts := config.KanikoOptions{
|
||||||
Destinations: []string{test.Destination},
|
Destinations: test.Destination,
|
||||||
}
|
}
|
||||||
if test.ExistingConfig {
|
if test.ExistingConfig {
|
||||||
afero.WriteFile(fs, DockerConfLocation(), []byte(""), os.FileMode(0644))
|
afero.WriteFile(fs, DockerConfLocation(), []byte(""), os.FileMode(0644))
|
||||||
defer fs.Remove(DockerConfLocation())
|
defer fs.Remove(DockerConfLocation())
|
||||||
}
|
}
|
||||||
CheckPushPermissions(&opts)
|
CheckPushPermissions(&opts)
|
||||||
if test.ShouldCallExecCommand != calledExecCommand {
|
for i, shdCall := range test.ShouldCallExecCommand {
|
||||||
t.Errorf("Expected calledExecCommand to be %v however it was %v",
|
if i < len(calledExecCommand) && shdCall != calledExecCommand[i] {
|
||||||
calledExecCommand, test.ShouldCallExecCommand)
|
t.Errorf("Expected calledExecCommand to be %v however it was %v",
|
||||||
|
calledExecCommand, shdCall)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
setCalledFalse()
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue