Consolidate creation of SystemContext with auth.json into a helper

Should not change (test) behavior.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
This commit is contained in:
Miloslav Trmač
2021-10-21 20:44:15 +02:00
parent fe1230ef70
commit 0e29b89753

@ -29,6 +29,22 @@ var largeAuthFileValues = map[string]types.DockerAuthConfig{
"quay.io": {Username: "quay", Password: "top"}, "quay.io": {Username: "quay", Password: "top"},
} }
// tempAuthFilePath returns a non-empty path pointing
// to a temporary file with fileContents, or "" if fileContents is empty; and a cleanup
// function the caller must arrange to call.
func tempAuthFilePath(t *testing.T, fileContents string) (string, func()) {
if fileContents == "" {
return "", func() {}
}
f, err := ioutil.TempFile("", "auth.json")
require.NoError(t, err)
path := f.Name()
err = ioutil.WriteFile(path, []byte(fileContents), 0700)
require.NoError(t, err)
return path, func() { os.Remove(path) }
}
// Test that GetCredentials() correctly parses what MakeXRegistryConfigHeader() produces // Test that GetCredentials() correctly parses what MakeXRegistryConfigHeader() produces
func TestMakeXRegistryConfigHeaderGetCredentialsRoundtrip(t *testing.T) { func TestMakeXRegistryConfigHeaderGetCredentialsRoundtrip(t *testing.T) {
for _, tc := range []struct { for _, tc := range []struct {
@ -64,16 +80,8 @@ func TestMakeXRegistryConfigHeaderGetCredentialsRoundtrip(t *testing.T) {
}, },
} { } {
name := tc.name name := tc.name
inputAuthFile := "" inputAuthFile, cleanup := tempAuthFilePath(t, tc.fileContents)
if tc.fileContents != "" { defer cleanup()
f, err := ioutil.TempFile("", "auth.json")
require.NoError(t, err, name)
defer os.Remove(f.Name())
inputAuthFile = f.Name()
err = ioutil.WriteFile(inputAuthFile, []byte(tc.fileContents), 0700)
require.NoError(t, err, name)
}
headers, err := MakeXRegistryConfigHeader(nil, inputAuthFile, tc.username, tc.password) headers, err := MakeXRegistryConfigHeader(nil, inputAuthFile, tc.username, tc.password)
require.NoError(t, err) require.NoError(t, err)
req, err := http.NewRequest(http.MethodPost, "/", nil) req, err := http.NewRequest(http.MethodPost, "/", nil)
@ -125,16 +133,8 @@ func TestMakeXRegistryAuthHeaderGetCredentialsRoundtrip(t *testing.T) {
}, },
} { } {
name := tc.name name := tc.name
inputAuthFile := "" inputAuthFile, cleanup := tempAuthFilePath(t, tc.fileContents)
if tc.fileContents != "" { defer cleanup()
f, err := ioutil.TempFile("", "auth.json")
require.NoError(t, err, name)
defer os.Remove(f.Name())
inputAuthFile = f.Name()
err = ioutil.WriteFile(inputAuthFile, []byte(tc.fileContents), 0700)
require.NoError(t, err, name)
}
headers, err := MakeXRegistryAuthHeader(nil, inputAuthFile, tc.username, tc.password) headers, err := MakeXRegistryAuthHeader(nil, inputAuthFile, tc.username, tc.password)
require.NoError(t, err) require.NoError(t, err)
req, err := http.NewRequest(http.MethodPost, "/", nil) req, err := http.NewRequest(http.MethodPost, "/", nil)
@ -209,16 +209,8 @@ func TestMakeXRegistryConfigHeader(t *testing.T) {
}, },
} { } {
name := tc.name name := tc.name
authFile := "" authFile, cleanup := tempAuthFilePath(t, tc.fileContents)
if tc.fileContents != "" { defer cleanup()
f, err := ioutil.TempFile("", "auth.json")
require.NoError(t, err, name)
defer os.Remove(f.Name())
authFile = f.Name()
err = ioutil.WriteFile(authFile, []byte(tc.fileContents), 0700)
require.NoError(t, err, name)
}
res, err := MakeXRegistryConfigHeader(nil, authFile, tc.username, tc.password) res, err := MakeXRegistryConfigHeader(nil, authFile, tc.username, tc.password)
if tc.shouldErr { if tc.shouldErr {
assert.Error(t, err, name) assert.Error(t, err, name)
@ -281,16 +273,8 @@ func TestMakeXRegistryAuthHeader(t *testing.T) {
}, },
} { } {
name := tc.name name := tc.name
authFile := "" authFile, cleanup := tempAuthFilePath(t, tc.fileContents)
if tc.fileContents != "" { defer cleanup()
f, err := ioutil.TempFile("", "auth.json")
require.NoError(t, err, name)
defer os.Remove(f.Name())
authFile = f.Name()
err = ioutil.WriteFile(authFile, []byte(tc.fileContents), 0700)
require.NoError(t, err, name)
}
res, err := MakeXRegistryAuthHeader(nil, authFile, tc.username, tc.password) res, err := MakeXRegistryAuthHeader(nil, authFile, tc.username, tc.password)
if tc.shouldErr { if tc.shouldErr {
assert.Error(t, err, name) assert.Error(t, err, name)