diff options
author | Miloslav Trmač <mitr@redhat.com> | 2021-10-21 20:44:15 +0200 |
---|---|---|
committer | Miloslav Trmač <mitr@redhat.com> | 2021-12-10 18:16:25 +0100 |
commit | 0e29b89753cc252076d171acdcb5b36aa386ba3e (patch) | |
tree | c55c91044a5ea21ba4de67ce13969658075e315c /pkg | |
parent | fe1230ef7003e89ad9c92fd11e21494ecc64de85 (diff) | |
download | podman-0e29b89753cc252076d171acdcb5b36aa386ba3e.tar.gz podman-0e29b89753cc252076d171acdcb5b36aa386ba3e.tar.bz2 podman-0e29b89753cc252076d171acdcb5b36aa386ba3e.zip |
Consolidate creation of SystemContext with auth.json into a helper
Should not change (test) behavior.
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/auth/auth_test.go | 64 |
1 files changed, 24 insertions, 40 deletions
diff --git a/pkg/auth/auth_test.go b/pkg/auth/auth_test.go index e0d2f1ac6..cbba600a9 100644 --- a/pkg/auth/auth_test.go +++ b/pkg/auth/auth_test.go @@ -29,6 +29,22 @@ var largeAuthFileValues = map[string]types.DockerAuthConfig{ "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 func TestMakeXRegistryConfigHeaderGetCredentialsRoundtrip(t *testing.T) { for _, tc := range []struct { @@ -64,16 +80,8 @@ func TestMakeXRegistryConfigHeaderGetCredentialsRoundtrip(t *testing.T) { }, } { name := tc.name - inputAuthFile := "" - if tc.fileContents != "" { - 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) - } - + inputAuthFile, cleanup := tempAuthFilePath(t, tc.fileContents) + defer cleanup() headers, err := MakeXRegistryConfigHeader(nil, inputAuthFile, tc.username, tc.password) require.NoError(t, err) req, err := http.NewRequest(http.MethodPost, "/", nil) @@ -125,16 +133,8 @@ func TestMakeXRegistryAuthHeaderGetCredentialsRoundtrip(t *testing.T) { }, } { name := tc.name - inputAuthFile := "" - if tc.fileContents != "" { - 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) - } - + inputAuthFile, cleanup := tempAuthFilePath(t, tc.fileContents) + defer cleanup() headers, err := MakeXRegistryAuthHeader(nil, inputAuthFile, tc.username, tc.password) require.NoError(t, err) req, err := http.NewRequest(http.MethodPost, "/", nil) @@ -209,16 +209,8 @@ func TestMakeXRegistryConfigHeader(t *testing.T) { }, } { name := tc.name - authFile := "" - if tc.fileContents != "" { - 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) - } - + authFile, cleanup := tempAuthFilePath(t, tc.fileContents) + defer cleanup() res, err := MakeXRegistryConfigHeader(nil, authFile, tc.username, tc.password) if tc.shouldErr { assert.Error(t, err, name) @@ -281,16 +273,8 @@ func TestMakeXRegistryAuthHeader(t *testing.T) { }, } { name := tc.name - authFile := "" - if tc.fileContents != "" { - 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) - } - + authFile, cleanup := tempAuthFilePath(t, tc.fileContents) + defer cleanup() res, err := MakeXRegistryAuthHeader(nil, authFile, tc.username, tc.password) if tc.shouldErr { assert.Error(t, err, name) |