Cleanup push_test CheckPushPermission unit tests

This commit is contained in:
Sam Stoelinga 2020-03-22 17:11:08 -07:00
parent c56f16b163
commit c2393df7c3
1 changed files with 38 additions and 43 deletions

View File

@ -310,10 +310,16 @@ func Test_makeTransport(t *testing.T) {
} }
} }
var called = false var calledExecCommand = false
var calledCheckPushPermission = false
func setCalledFalse() {
calledExecCommand = false
calledCheckPushPermission = false
}
func fakeExecCommand(command string, args ...string) *exec.Cmd { func fakeExecCommand(command string, args ...string) *exec.Cmd {
called = true 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...)
@ -322,52 +328,41 @@ func fakeExecCommand(command string, args ...string) *exec.Cmd {
} }
func fakeCheckPushPermission(ref name.Reference, kc authn.Keychain, t http.RoundTripper) error { func fakeCheckPushPermission(ref name.Reference, kc authn.Keychain, t http.RoundTripper) error {
calledCheckPushPermission = true
return nil return nil
} }
func TestCheckPushPermissionsGCR(t *testing.T) { func TestCheckPushPermissions(t *testing.T) {
tests := []struct {
Destination string
ShouldCallExecCommand bool
ExistingConfig bool
}{
{"gcr.io/test-image", true, false},
{"gcr.io/test-image", false, true},
{"localhost:5000/test-image", false, false},
{"localhost:5000/test-image", false, true},
}
execCommand = fakeExecCommand execCommand = fakeExecCommand
checkRemotePushPermission = fakeCheckPushPermission checkRemotePushPermission = fakeCheckPushPermission
defer func() { called = false }() for _, test := range tests {
testName := fmt.Sprintf("%s_ExistingDockerConf_%v", test.Destination, test.ExistingConfig)
opts := config.KanikoOptions{ t.Run(testName, func(t *testing.T) {
Destinations: []string{"gcr.io/test-image"}, fs = afero.NewMemMapFs()
} opts := config.KanikoOptions{
fs = afero.NewMemMapFs() Destinations: []string{test.Destination},
CheckPushPermissions(&opts) }
if called != true { if test.ExistingConfig {
t.Error("execCommand should have been called") afero.WriteFile(fs, DockerConfLocation, []byte(""), os.FileMode(0644))
} }
} CheckPushPermissions(&opts)
if test.ShouldCallExecCommand != calledExecCommand {
func TestCheckPushPermissionsGCRExistingDockerConf(t *testing.T) { t.Errorf("Expected calledExecCommand to be %v however it was %v",
execCommand = fakeExecCommand calledExecCommand, test.ShouldCallExecCommand)
checkRemotePushPermission = fakeCheckPushPermission }
defer func() { called = false }() setCalledFalse()
})
opts := config.KanikoOptions{
Destinations: []string{"gcr.io/test-image"},
}
fs = afero.NewMemMapFs()
afero.WriteFile(fs, DockerConfLocation, []byte(""), os.FileMode(0644))
CheckPushPermissions(&opts)
if called != false {
t.Error("execCommand should not have been called")
}
}
func TestCheckPushPermissionsLocalRegistry(t *testing.T) {
execCommand = fakeExecCommand
checkRemotePushPermission = fakeCheckPushPermission
defer func() { called = false }()
opts := config.KanikoOptions{
Destinations: []string{"localhost:5000/test-image"},
}
fs = afero.NewMemMapFs()
CheckPushPermissions(&opts)
if called != false {
t.Error("execCommand should not have been called")
} }
} }