cli/config: remove deprecated io/ioutil and use t.TempDir()

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-02-25 14:36:33 +01:00
parent b9f0340b68
commit 71575ab3b5
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
4 changed files with 48 additions and 89 deletions

View File

@ -3,7 +3,6 @@ package config
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -27,21 +26,18 @@ func init() {
} }
} }
func setupConfigDir(t *testing.T) (string, func()) { func setupConfigDir(t *testing.T) string {
tmpdir, err := ioutil.TempDir("", "config-test") tmpdir := t.TempDir()
assert.NilError(t, err)
oldDir := Dir() oldDir := Dir()
SetDir(tmpdir) SetDir(tmpdir)
t.Cleanup(func() {
return tmpdir, func() {
SetDir(oldDir) SetDir(oldDir)
os.RemoveAll(tmpdir) })
} return tmpdir
} }
func TestEmptyConfigDir(t *testing.T) { func TestEmptyConfigDir(t *testing.T) {
tmpHome, cleanup := setupConfigDir(t) tmpHome := setupConfigDir(t)
defer cleanup()
config, err := Load("") config, err := Load("")
assert.NilError(t, err) assert.NilError(t, err)
@ -54,9 +50,7 @@ func TestEmptyConfigDir(t *testing.T) {
} }
func TestMissingFile(t *testing.T) { func TestMissingFile(t *testing.T) {
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
config, err := Load(tmpHome) config, err := Load(tmpHome)
assert.NilError(t, err) assert.NilError(t, err)
@ -66,9 +60,7 @@ func TestMissingFile(t *testing.T) {
} }
func TestSaveFileToDirs(t *testing.T) { func TestSaveFileToDirs(t *testing.T) {
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
tmpHome += "/.docker" tmpHome += "/.docker"
@ -80,12 +72,10 @@ func TestSaveFileToDirs(t *testing.T) {
} }
func TestEmptyFile(t *testing.T) { func TestEmptyFile(t *testing.T) {
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
fn := filepath.Join(tmpHome, ConfigFileName) fn := filepath.Join(tmpHome, ConfigFileName)
err = ioutil.WriteFile(fn, []byte(""), 0600) err := os.WriteFile(fn, []byte(""), 0600)
assert.NilError(t, err) assert.NilError(t, err)
_, err = Load(tmpHome) _, err = Load(tmpHome)
@ -93,12 +83,10 @@ func TestEmptyFile(t *testing.T) {
} }
func TestEmptyJSON(t *testing.T) { func TestEmptyJSON(t *testing.T) {
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
fn := filepath.Join(tmpHome, ConfigFileName) fn := filepath.Join(tmpHome, ConfigFileName)
err = ioutil.WriteFile(fn, []byte("{}"), 0600) err := os.WriteFile(fn, []byte("{}"), 0600)
assert.NilError(t, err) assert.NilError(t, err)
config, err := Load(tmpHome) config, err := Load(tmpHome)
@ -118,14 +106,12 @@ email`: "Invalid auth configuration file",
} }
resetHomeDir() resetHomeDir()
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
defer env.Patch(t, homeKey, tmpHome)() defer env.Patch(t, homeKey, tmpHome)()
for content, expectedError := range invalids { for content, expectedError := range invalids {
fn := filepath.Join(tmpHome, oldConfigfile) fn := filepath.Join(tmpHome, oldConfigfile)
err := ioutil.WriteFile(fn, []byte(content), 0600) err := os.WriteFile(fn, []byte(content), 0600)
assert.NilError(t, err) assert.NilError(t, err)
_, err = Load(tmpHome) _, err = Load(tmpHome)
@ -135,15 +121,13 @@ email`: "Invalid auth configuration file",
func TestOldValidAuth(t *testing.T) { func TestOldValidAuth(t *testing.T) {
resetHomeDir() resetHomeDir()
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
defer env.Patch(t, homeKey, tmpHome)() defer env.Patch(t, homeKey, tmpHome)()
fn := filepath.Join(tmpHome, oldConfigfile) fn := filepath.Join(tmpHome, oldConfigfile)
js := `username = am9lam9lOmhlbGxv js := `username = am9lam9lOmhlbGxv
email = user@example.com` email = user@example.com`
err = ioutil.WriteFile(fn, []byte(js), 0600) err := os.WriteFile(fn, []byte(js), 0600)
assert.NilError(t, err) assert.NilError(t, err)
config, err := Load(tmpHome) config, err := Load(tmpHome)
@ -170,14 +154,12 @@ func TestOldValidAuth(t *testing.T) {
func TestOldJSONInvalid(t *testing.T) { func TestOldJSONInvalid(t *testing.T) {
resetHomeDir() resetHomeDir()
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
defer env.Patch(t, homeKey, tmpHome)() defer env.Patch(t, homeKey, tmpHome)()
fn := filepath.Join(tmpHome, oldConfigfile) fn := filepath.Join(tmpHome, oldConfigfile)
js := `{"https://index.docker.io/v1/":{"auth":"test","email":"user@example.com"}}` js := `{"https://index.docker.io/v1/":{"auth":"test","email":"user@example.com"}}`
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil { if err := os.WriteFile(fn, []byte(js), 0600); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -190,14 +172,12 @@ func TestOldJSONInvalid(t *testing.T) {
func TestOldJSON(t *testing.T) { func TestOldJSON(t *testing.T) {
resetHomeDir() resetHomeDir()
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
defer env.Patch(t, homeKey, tmpHome)() defer env.Patch(t, homeKey, tmpHome)()
fn := filepath.Join(tmpHome, oldConfigfile) fn := filepath.Join(tmpHome, oldConfigfile)
js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}` js := `{"https://index.docker.io/v1/":{"auth":"am9lam9lOmhlbGxv","email":"user@example.com"}}`
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil { if err := os.WriteFile(fn, []byte(js), 0600); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -251,13 +231,11 @@ func TestOldJSONFallbackDeprecationWarning(t *testing.T) {
} }
func TestNewJSON(t *testing.T) { func TestNewJSON(t *testing.T) {
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
fn := filepath.Join(tmpHome, ConfigFileName) fn := filepath.Join(tmpHome, ConfigFileName)
js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv" } } }` js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv" } } }`
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil { if err := os.WriteFile(fn, []byte(js), 0600); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -285,13 +263,11 @@ func TestNewJSON(t *testing.T) {
} }
func TestNewJSONNoEmail(t *testing.T) { func TestNewJSONNoEmail(t *testing.T) {
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
fn := filepath.Join(tmpHome, ConfigFileName) fn := filepath.Join(tmpHome, ConfigFileName)
js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv" } } }` js := ` { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv" } } }`
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil { if err := os.WriteFile(fn, []byte(js), 0600); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -319,16 +295,14 @@ func TestNewJSONNoEmail(t *testing.T) {
} }
func TestJSONWithPsFormat(t *testing.T) { func TestJSONWithPsFormat(t *testing.T) {
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
fn := filepath.Join(tmpHome, ConfigFileName) fn := filepath.Join(tmpHome, ConfigFileName)
js := `{ js := `{
"auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } }, "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } },
"psFormat": "table {{.ID}}\\t{{.Label \"com.docker.label.cpu\"}}" "psFormat": "table {{.ID}}\\t{{.Label \"com.docker.label.cpu\"}}"
}` }`
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil { if err := os.WriteFile(fn, []byte(js), 0600); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -348,16 +322,14 @@ func TestJSONWithPsFormat(t *testing.T) {
} }
func TestJSONWithCredentialStore(t *testing.T) { func TestJSONWithCredentialStore(t *testing.T) {
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
fn := filepath.Join(tmpHome, ConfigFileName) fn := filepath.Join(tmpHome, ConfigFileName)
js := `{ js := `{
"auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } }, "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } },
"credsStore": "crazy-secure-storage" "credsStore": "crazy-secure-storage"
}` }`
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil { if err := os.WriteFile(fn, []byte(js), 0600); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -377,16 +349,14 @@ func TestJSONWithCredentialStore(t *testing.T) {
} }
func TestJSONWithCredentialHelpers(t *testing.T) { func TestJSONWithCredentialHelpers(t *testing.T) {
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
fn := filepath.Join(tmpHome, ConfigFileName) fn := filepath.Join(tmpHome, ConfigFileName)
js := `{ js := `{
"auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } }, "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv", "email": "user@example.com" } },
"credHelpers": { "images.io": "images-io", "containers.com": "crazy-secure-storage" } "credHelpers": { "images.io": "images-io", "containers.com": "crazy-secure-storage" }
}` }`
if err := ioutil.WriteFile(fn, []byte(js), 0600); err != nil { if err := os.WriteFile(fn, []byte(js), 0600); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -416,16 +386,14 @@ func saveConfigAndValidateNewFormat(t *testing.T, config *configfile.ConfigFile,
t.Helper() t.Helper()
assert.NilError(t, config.Save()) assert.NilError(t, config.Save())
buf, err := ioutil.ReadFile(filepath.Join(configDir, ConfigFileName)) buf, err := os.ReadFile(filepath.Join(configDir, ConfigFileName))
assert.NilError(t, err) assert.NilError(t, err)
assert.Check(t, is.Contains(string(buf), `"auths":`)) assert.Check(t, is.Contains(string(buf), `"auths":`))
return string(buf) return string(buf)
} }
func TestConfigDir(t *testing.T) { func TestConfigDir(t *testing.T) {
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
if Dir() == tmpHome { if Dir() == tmpHome {
t.Fatalf("Expected ConfigDir to be different than %s by default, but was the same", tmpHome) t.Fatalf("Expected ConfigDir to be different than %s by default, but was the same", tmpHome)
@ -484,16 +452,14 @@ func TestJSONSaveWithNoFile(t *testing.T) {
err = config.Save() err = config.Save()
assert.ErrorContains(t, err, "with empty filename") assert.ErrorContains(t, err, "with empty filename")
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
fn := filepath.Join(tmpHome, ConfigFileName) fn := filepath.Join(tmpHome, ConfigFileName)
f, _ := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) f, _ := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
defer f.Close() defer f.Close()
assert.NilError(t, config.SaveToWriter(f)) assert.NilError(t, config.SaveToWriter(f))
buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName)) buf, err := os.ReadFile(filepath.Join(tmpHome, ConfigFileName))
assert.NilError(t, err) assert.NilError(t, err)
expConfStr := `{ expConfStr := `{
"auths": { "auths": {
@ -515,16 +481,13 @@ func TestLegacyJSONSaveWithNoFile(t *testing.T) {
err = config.Save() err = config.Save()
assert.ErrorContains(t, err, "with empty filename") assert.ErrorContains(t, err, "with empty filename")
tmpHome, err := ioutil.TempDir("", "config-test") tmpHome := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(tmpHome)
fn := filepath.Join(tmpHome, ConfigFileName) fn := filepath.Join(tmpHome, ConfigFileName)
f, _ := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) f, _ := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
defer f.Close() defer f.Close()
assert.NilError(t, config.SaveToWriter(f)) assert.NilError(t, config.SaveToWriter(f))
buf, err := ioutil.ReadFile(filepath.Join(tmpHome, ConfigFileName)) buf, err := os.ReadFile(filepath.Join(tmpHome, ConfigFileName))
assert.NilError(t, err) assert.NilError(t, err)
expConfStr := `{ expConfStr := `{
@ -542,13 +505,12 @@ func TestLegacyJSONSaveWithNoFile(t *testing.T) {
} }
func TestLoadDefaultConfigFile(t *testing.T) { func TestLoadDefaultConfigFile(t *testing.T) {
dir, cleanup := setupConfigDir(t) dir := setupConfigDir(t)
defer cleanup()
buffer := new(bytes.Buffer) buffer := new(bytes.Buffer)
filename := filepath.Join(dir, ConfigFileName) filename := filepath.Join(dir, ConfigFileName)
content := []byte(`{"PsFormat": "format"}`) content := []byte(`{"PsFormat": "format"}`)
err := ioutil.WriteFile(filename, content, 0644) err := os.WriteFile(filename, content, 0644)
assert.NilError(t, err) assert.NilError(t, err)
configFile := LoadDefaultConfigFile(buffer) configFile := LoadDefaultConfigFile(buffer)

View File

@ -4,7 +4,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -75,7 +74,7 @@ func New(fn string) *ConfigFile {
// LegacyLoadFromReader reads the non-nested configuration data given and sets up the // LegacyLoadFromReader reads the non-nested configuration data given and sets up the
// auth config information with given directory and populates the receiver object // auth config information with given directory and populates the receiver object
func (configFile *ConfigFile) LegacyLoadFromReader(configData io.Reader) error { func (configFile *ConfigFile) LegacyLoadFromReader(configData io.Reader) error {
b, err := ioutil.ReadAll(configData) b, err := io.ReadAll(configData)
if err != nil { if err != nil {
return err return err
} }
@ -188,7 +187,7 @@ func (configFile *ConfigFile) Save() (retErr error) {
if err := os.MkdirAll(dir, 0700); err != nil { if err := os.MkdirAll(dir, 0700); err != nil {
return err return err
} }
temp, err := ioutil.TempFile(dir, filepath.Base(configFile.Filename)) temp, err := os.CreateTemp(dir, filepath.Base(configFile.Filename))
if err != nil { if err != nil {
return err return err
} }

View File

@ -3,7 +3,6 @@ package configfile
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"io/ioutil"
"os" "os"
"testing" "testing"
@ -443,7 +442,7 @@ func TestSave(t *testing.T) {
defer os.Remove("test-save") defer os.Remove("test-save")
err := configFile.Save() err := configFile.Save()
assert.NilError(t, err) assert.NilError(t, err)
cfg, err := ioutil.ReadFile("test-save") cfg, err := os.ReadFile("test-save")
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, string(cfg), `{ assert.Equal(t, string(cfg), `{
"auths": {} "auths": {}
@ -458,7 +457,7 @@ func TestSaveCustomHTTPHeaders(t *testing.T) {
configFile.HTTPHeaders["user-agent"] = "user-agent 2" configFile.HTTPHeaders["user-agent"] = "user-agent 2"
err := configFile.Save() err := configFile.Save()
assert.NilError(t, err) assert.NilError(t, err)
cfg, err := ioutil.ReadFile(t.Name()) cfg, err := os.ReadFile(t.Name())
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, string(cfg), `{ assert.Equal(t, string(cfg), `{
"auths": {}, "auths": {},
@ -486,11 +485,11 @@ func TestSaveWithSymlink(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
assert.Assert(t, fi.Mode()&os.ModeSymlink != 0, "expected %s to be a symlink", symLink) assert.Assert(t, fi.Mode()&os.ModeSymlink != 0, "expected %s to be a symlink", symLink)
cfg, err := ioutil.ReadFile(symLink) cfg, err := os.ReadFile(symLink)
assert.NilError(t, err) assert.NilError(t, err)
assert.Check(t, is.Equal(string(cfg), "{\n \"auths\": {}\n}")) assert.Check(t, is.Equal(string(cfg), "{\n \"auths\": {}\n}"))
cfg, err = ioutil.ReadFile(realFile) cfg, err = os.ReadFile(realFile)
assert.NilError(t, err) assert.NilError(t, err)
assert.Check(t, is.Equal(string(cfg), "{\n \"auths\": {}\n}")) assert.Check(t, is.Equal(string(cfg), "{\n \"auths\": {}\n}"))
} }
@ -509,7 +508,7 @@ func TestPluginConfig(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
// Read it back and check it has the expected content // Read it back and check it has the expected content
cfg, err := ioutil.ReadFile("test-plugin") cfg, err := os.ReadFile("test-plugin")
assert.NilError(t, err) assert.NilError(t, err)
golden.Assert(t, string(cfg), "plugin-config.golden") golden.Assert(t, string(cfg), "plugin-config.golden")
@ -520,7 +519,7 @@ func TestPluginConfig(t *testing.T) {
assert.NilError(t, configFile.LoadFromReader(bytes.NewReader(cfg))) assert.NilError(t, configFile.LoadFromReader(bytes.NewReader(cfg)))
err = configFile.Save() err = configFile.Save()
assert.NilError(t, err) assert.NilError(t, err)
cfg, err = ioutil.ReadFile("test-plugin2") cfg, err = os.ReadFile("test-plugin2")
assert.NilError(t, err) assert.NilError(t, err)
golden.Assert(t, string(cfg), "plugin-config.golden") golden.Assert(t, string(cfg), "plugin-config.golden")
@ -555,7 +554,7 @@ func TestPluginConfig(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
// Read it back and check it has the expected content again // Read it back and check it has the expected content again
cfg, err = ioutil.ReadFile("test-plugin2") cfg, err = os.ReadFile("test-plugin2")
assert.NilError(t, err) assert.NilError(t, err)
golden.Assert(t, string(cfg), "plugin-config-2.golden") golden.Assert(t, string(cfg), "plugin-config-2.golden")
} }

View File

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"strings" "strings"
"testing" "testing"
@ -36,7 +35,7 @@ type mockCommand struct {
// Output returns responses from the remote credentials helper. // Output returns responses from the remote credentials helper.
// It mocks those responses based in the input in the mock. // It mocks those responses based in the input in the mock.
func (m *mockCommand) Output() ([]byte, error) { func (m *mockCommand) Output() ([]byte, error) {
in, err := ioutil.ReadAll(m.input) in, err := io.ReadAll(m.input)
if err != nil { if err != nil {
return nil, err return nil, err
} }