cli/context: 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:37:07 +01:00
parent 71575ab3b5
commit cca80cdddd
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
8 changed files with 47 additions and 102 deletions

View File

@ -1,7 +1,7 @@
package store package store
import ( import (
"io/ioutil" "io"
"strings" "strings"
"testing" "testing"
@ -11,14 +11,14 @@ import (
func TestLimitReaderReadAll(t *testing.T) { func TestLimitReaderReadAll(t *testing.T) {
r := strings.NewReader("Reader") r := strings.NewReader("Reader")
_, err := ioutil.ReadAll(r) _, err := io.ReadAll(r)
assert.NilError(t, err) assert.NilError(t, err)
r = strings.NewReader("Test") r = strings.NewReader("Test")
_, err = ioutil.ReadAll(&LimitedReader{R: r, N: 4}) _, err = io.ReadAll(&LimitedReader{R: r, N: 4})
assert.NilError(t, err) assert.NilError(t, err)
r = strings.NewReader("Test") r = strings.NewReader("Test")
_, err = ioutil.ReadAll(&LimitedReader{R: r, N: 2}) _, err = io.ReadAll(&LimitedReader{R: r, N: 2})
assert.Error(t, err, "read exceeds the defined limit") assert.Error(t, err, "read exceeds the defined limit")
} }

View File

@ -1,7 +1,6 @@
package store package store
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -21,18 +20,13 @@ func testMetadata(name string) Metadata {
} }
func TestMetadataGetNotExisting(t *testing.T) { func TestMetadataGetNotExisting(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name()) testee := metadataStore{root: t.TempDir(), config: testCfg}
assert.NilError(t, err) _, err := testee.get("noexist")
defer os.RemoveAll(testDir)
testee := metadataStore{root: testDir, config: testCfg}
_, err = testee.get("noexist")
assert.Assert(t, IsErrContextDoesNotExist(err)) assert.Assert(t, IsErrContextDoesNotExist(err))
} }
func TestMetadataCreateGetRemove(t *testing.T) { func TestMetadataCreateGetRemove(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name()) testDir := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(testDir)
testee := metadataStore{root: testDir, config: testCfg} testee := metadataStore{root: testDir, config: testCfg}
expected2 := Metadata{ expected2 := Metadata{
Endpoints: map[string]interface{}{ Endpoints: map[string]interface{}{
@ -43,7 +37,7 @@ func TestMetadataCreateGetRemove(t *testing.T) {
Name: "test-context", Name: "test-context",
} }
testMeta := testMetadata("test-context") testMeta := testMetadata("test-context")
err = testee.createOrUpdate(testMeta) err := testee.createOrUpdate(testMeta)
assert.NilError(t, err) assert.NilError(t, err)
// create a new instance to check it does not depend on some sort of state // create a new instance to check it does not depend on some sort of state
testee = metadataStore{root: testDir, config: testCfg} testee = metadataStore{root: testDir, config: testCfg}
@ -66,22 +60,17 @@ func TestMetadataCreateGetRemove(t *testing.T) {
} }
func TestMetadataRespectJsonAnnotation(t *testing.T) { func TestMetadataRespectJsonAnnotation(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name()) testDir := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(testDir)
testee := metadataStore{root: testDir, config: testCfg} testee := metadataStore{root: testDir, config: testCfg}
assert.NilError(t, testee.createOrUpdate(testMetadata("test"))) assert.NilError(t, testee.createOrUpdate(testMetadata("test")))
bytes, err := ioutil.ReadFile(filepath.Join(testDir, string(contextdirOf("test")), "meta.json")) bytes, err := os.ReadFile(filepath.Join(testDir, string(contextdirOf("test")), "meta.json"))
assert.NilError(t, err) assert.NilError(t, err)
assert.Assert(t, cmp.Contains(string(bytes), "a_very_recognizable_field_name")) assert.Assert(t, cmp.Contains(string(bytes), "a_very_recognizable_field_name"))
assert.Assert(t, cmp.Contains(string(bytes), "another_very_recognizable_field_name")) assert.Assert(t, cmp.Contains(string(bytes), "another_very_recognizable_field_name"))
} }
func TestMetadataList(t *testing.T) { func TestMetadataList(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name()) testee := metadataStore{root: t.TempDir(), config: testCfg}
assert.NilError(t, err)
defer os.RemoveAll(testDir)
testee := metadataStore{root: testDir, config: testCfg}
wholeData := []Metadata{ wholeData := []Metadata{
testMetadata("context1"), testMetadata("context1"),
testMetadata("context2"), testMetadata("context2"),
@ -89,8 +78,7 @@ func TestMetadataList(t *testing.T) {
} }
for _, s := range wholeData { for _, s := range wholeData {
err = testee.createOrUpdate(s) assert.NilError(t, testee.createOrUpdate(s))
assert.NilError(t, err)
} }
data, err := testee.list() data, err := testee.list()
@ -99,10 +87,7 @@ func TestMetadataList(t *testing.T) {
} }
func TestEmptyConfig(t *testing.T) { func TestEmptyConfig(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name()) testee := metadataStore{root: t.TempDir()}
assert.NilError(t, err)
defer os.RemoveAll(testDir)
testee := metadataStore{root: testDir}
wholeData := []Metadata{ wholeData := []Metadata{
testMetadata("context1"), testMetadata("context1"),
testMetadata("context2"), testMetadata("context2"),
@ -110,8 +95,7 @@ func TestEmptyConfig(t *testing.T) {
} }
for _, s := range wholeData { for _, s := range wholeData {
err = testee.createOrUpdate(s) assert.NilError(t, testee.createOrUpdate(s))
assert.NilError(t, err)
} }
data, err := testee.list() data, err := testee.list()
@ -127,10 +111,10 @@ type embeddedStruct struct {
} }
func TestWithEmbedding(t *testing.T) { func TestWithEmbedding(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name()) testee := metadataStore{
assert.NilError(t, err) root: t.TempDir(),
defer os.RemoveAll(testDir) config: NewConfig(func() interface{} { return &contextWithEmbedding{} }),
testee := metadataStore{root: testDir, config: NewConfig(func() interface{} { return &contextWithEmbedding{} })} }
testCtxMeta := contextWithEmbedding{ testCtxMeta := contextWithEmbedding{
embeddedStruct: embeddedStruct{ embeddedStruct: embeddedStruct{
Val: "Hello", Val: "Hello",

View File

@ -3,7 +3,6 @@ package store
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -35,7 +34,7 @@ func (s *metadataStore) createOrUpdate(meta Metadata) error {
if err != nil { if err != nil {
return err return err
} }
return ioutil.WriteFile(filepath.Join(contextDir, metaFile), bytes, 0644) return os.WriteFile(filepath.Join(contextDir, metaFile), bytes, 0644)
} }
func parseTypedOrMap(payload []byte, getter TypeGetter) (interface{}, error) { func parseTypedOrMap(payload []byte, getter TypeGetter) (interface{}, error) {
@ -58,7 +57,7 @@ func parseTypedOrMap(payload []byte, getter TypeGetter) (interface{}, error) {
func (s *metadataStore) get(id contextdir) (Metadata, error) { func (s *metadataStore) get(id contextdir) (Metadata, error) {
contextDir := s.contextDir(id) contextDir := s.contextDir(id)
bytes, err := ioutil.ReadFile(filepath.Join(contextDir, metaFile)) bytes, err := os.ReadFile(filepath.Join(contextDir, metaFile))
if err != nil { if err != nil {
return Metadata{}, convertContextDoesNotExist(err) return Metadata{}, convertContextDoesNotExist(err)
} }
@ -117,7 +116,7 @@ func isContextDir(path string) bool {
} }
func listRecursivelyMetadataDirs(root string) ([]string, error) { func listRecursivelyMetadataDirs(root string) ([]string, error) {
fis, err := ioutil.ReadDir(root) fis, err := os.ReadDir(root)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -9,7 +9,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"path" "path"
"path/filepath" "path/filepath"
@ -349,7 +348,7 @@ func importTar(name string, s Writer, reader io.Reader) error {
return errors.Wrap(err, hdr.Name) return errors.Wrap(err, hdr.Name)
} }
if hdr.Name == metaFile { if hdr.Name == metaFile {
data, err := ioutil.ReadAll(tr) data, err := io.ReadAll(tr)
if err != nil { if err != nil {
return err return err
} }
@ -362,7 +361,7 @@ func importTar(name string, s Writer, reader io.Reader) error {
} }
importedMetaFile = true importedMetaFile = true
} else if strings.HasPrefix(hdr.Name, "tls/") { } else if strings.HasPrefix(hdr.Name, "tls/") {
data, err := ioutil.ReadAll(tr) data, err := io.ReadAll(tr)
if err != nil { if err != nil {
return err return err
} }
@ -378,7 +377,7 @@ func importTar(name string, s Writer, reader io.Reader) error {
} }
func importZip(name string, s Writer, reader io.Reader) error { func importZip(name string, s Writer, reader io.Reader) error {
body, err := ioutil.ReadAll(&LimitedReader{R: reader, N: maxAllowedFileSizeToImport}) body, err := io.ReadAll(&LimitedReader{R: reader, N: maxAllowedFileSizeToImport})
if err != nil { if err != nil {
return err return err
} }
@ -406,7 +405,7 @@ func importZip(name string, s Writer, reader io.Reader) error {
return err return err
} }
data, err := ioutil.ReadAll(&LimitedReader{R: f, N: maxAllowedFileSizeToImport}) data, err := io.ReadAll(&LimitedReader{R: f, N: maxAllowedFileSizeToImport})
defer f.Close() defer f.Close()
if err != nil { if err != nil {
return err return err
@ -424,7 +423,7 @@ func importZip(name string, s Writer, reader io.Reader) error {
if err != nil { if err != nil {
return err return err
} }
data, err := ioutil.ReadAll(f) data, err := io.ReadAll(f)
defer f.Close() defer f.Close()
if err != nil { if err != nil {
return err return err

View File

@ -8,7 +8,6 @@ import (
"crypto/rand" "crypto/rand"
"encoding/json" "encoding/json"
"io" "io"
"io/ioutil"
"os" "os"
"path" "path"
"testing" "testing"
@ -30,11 +29,8 @@ var testCfg = NewConfig(func() interface{} { return &context{} },
) )
func TestExportImport(t *testing.T) { func TestExportImport(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name()) s := New(t.TempDir(), testCfg)
assert.NilError(t, err) err := s.CreateOrUpdate(
defer os.RemoveAll(testDir)
s := New(testDir, testCfg)
err = s.CreateOrUpdate(
Metadata{ Metadata{
Endpoints: map[string]interface{}{ Endpoints: map[string]interface{}{
"ep1": endpoint{Foo: "bar"}, "ep1": endpoint{Foo: "bar"},
@ -87,11 +83,8 @@ func TestExportImport(t *testing.T) {
} }
func TestRemove(t *testing.T) { func TestRemove(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name()) s := New(t.TempDir(), testCfg)
assert.NilError(t, err) err := s.CreateOrUpdate(
defer os.RemoveAll(testDir)
s := New(testDir, testCfg)
err = s.CreateOrUpdate(
Metadata{ Metadata{
Endpoints: map[string]interface{}{ Endpoints: map[string]interface{}{
"ep1": endpoint{Foo: "bar"}, "ep1": endpoint{Foo: "bar"},
@ -114,30 +107,18 @@ func TestRemove(t *testing.T) {
} }
func TestListEmptyStore(t *testing.T) { func TestListEmptyStore(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name()) result, err := New(t.TempDir(), testCfg).List()
assert.NilError(t, err)
defer os.RemoveAll(testDir)
store := New(testDir, testCfg)
result, err := store.List()
assert.NilError(t, err) assert.NilError(t, err)
assert.Check(t, len(result) == 0) assert.Check(t, len(result) == 0)
} }
func TestErrHasCorrectContext(t *testing.T) { func TestErrHasCorrectContext(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name()) _, err := New(t.TempDir(), testCfg).GetMetadata("no-exists")
assert.NilError(t, err)
defer os.RemoveAll(testDir)
store := New(testDir, testCfg)
_, err = store.GetMetadata("no-exists")
assert.ErrorContains(t, err, "no-exists") assert.ErrorContains(t, err, "no-exists")
assert.Check(t, IsErrContextDoesNotExist(err)) assert.Check(t, IsErrContextDoesNotExist(err))
} }
func TestDetectImportContentType(t *testing.T) { func TestDetectImportContentType(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name())
assert.NilError(t, err)
defer os.RemoveAll(testDir)
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
r := bufio.NewReader(buf) r := bufio.NewReader(buf)
ct, err := getImportContentType(r) ct, err := getImportContentType(r)
@ -146,10 +127,7 @@ func TestDetectImportContentType(t *testing.T) {
} }
func TestImportTarInvalid(t *testing.T) { func TestImportTarInvalid(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name()) testDir := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(testDir)
tf := path.Join(testDir, "test.context") tf := path.Join(testDir, "test.context")
f, err := os.Create(tf) f, err := os.Create(tf)
@ -179,10 +157,7 @@ func TestImportTarInvalid(t *testing.T) {
} }
func TestImportZip(t *testing.T) { func TestImportZip(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name()) testDir := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(testDir)
zf := path.Join(testDir, "test.zip") zf := path.Join(testDir, "test.zip")
f, err := os.Create(zf) f, err := os.Create(zf)
@ -230,10 +205,7 @@ func TestImportZip(t *testing.T) {
} }
func TestImportZipInvalid(t *testing.T) { func TestImportZipInvalid(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name()) testDir := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(testDir)
zf := path.Join(testDir, "test.zip") zf := path.Join(testDir, "test.zip")
f, err := os.Create(zf) f, err := os.Create(zf)

View File

@ -1,7 +1,6 @@
package store package store
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
) )
@ -33,11 +32,11 @@ func (s *tlsStore) createOrUpdate(contextID contextdir, endpointName, filename s
if err := os.MkdirAll(epdir, 0700); err != nil { if err := os.MkdirAll(epdir, 0700); err != nil {
return err return err
} }
return ioutil.WriteFile(s.filePath(contextID, endpointName, filename), data, 0600) return os.WriteFile(s.filePath(contextID, endpointName, filename), data, 0600)
} }
func (s *tlsStore) getData(contextID contextdir, endpointName, filename string) ([]byte, error) { func (s *tlsStore) getData(contextID contextdir, endpointName, filename string) ([]byte, error) {
data, err := ioutil.ReadFile(s.filePath(contextID, endpointName, filename)) data, err := os.ReadFile(s.filePath(contextID, endpointName, filename))
if err != nil { if err != nil {
return nil, convertTLSDataDoesNotExist(endpointName, filename, err) return nil, convertTLSDataDoesNotExist(endpointName, filename, err)
} }
@ -61,7 +60,7 @@ func (s *tlsStore) removeAllContextData(contextID contextdir) error {
} }
func (s *tlsStore) listContextData(contextID contextdir) (map[string]EndpointFiles, error) { func (s *tlsStore) listContextData(contextID contextdir) (map[string]EndpointFiles, error) {
epFSs, err := ioutil.ReadDir(s.contextDir(contextID)) epFSs, err := os.ReadDir(s.contextDir(contextID))
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return map[string]EndpointFiles{}, nil return map[string]EndpointFiles{}, nil
@ -72,7 +71,7 @@ func (s *tlsStore) listContextData(contextID contextdir) (map[string]EndpointFil
for _, epFS := range epFSs { for _, epFS := range epFSs {
if epFS.IsDir() { if epFS.IsDir() {
epDir := s.endpointDir(contextID, epFS.Name()) epDir := s.endpointDir(contextID, epFS.Name())
fss, err := ioutil.ReadDir(epDir) fss, err := os.ReadDir(epDir)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -1,19 +1,14 @@
package store package store
import ( import (
"io/ioutil"
"os"
"testing" "testing"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
) )
func TestTlsCreateUpdateGetRemove(t *testing.T) { func TestTlsCreateUpdateGetRemove(t *testing.T) {
testDir, err := ioutil.TempDir("", "TestTlsCreateUpdateGetRemove") testee := tlsStore{root: t.TempDir()}
assert.NilError(t, err) _, err := testee.getData("test-ctx", "test-ep", "test-data")
defer os.RemoveAll(testDir)
testee := tlsStore{root: testDir}
_, err = testee.getData("test-ctx", "test-ep", "test-data")
assert.Equal(t, true, IsErrTLSDataDoesNotExist(err)) assert.Equal(t, true, IsErrTLSDataDoesNotExist(err))
err = testee.createOrUpdate("test-ctx", "test-ep", "test-data", []byte("data")) err = testee.createOrUpdate("test-ctx", "test-ep", "test-data", []byte("data"))
@ -37,10 +32,7 @@ func TestTlsCreateUpdateGetRemove(t *testing.T) {
} }
func TestTlsListAndBatchRemove(t *testing.T) { func TestTlsListAndBatchRemove(t *testing.T) {
testDir, err := ioutil.TempDir("", "TestTlsListAndBatchRemove") testee := tlsStore{root: t.TempDir()}
assert.NilError(t, err)
defer os.RemoveAll(testDir)
testee := tlsStore{root: testDir}
all := map[string]EndpointFiles{ all := map[string]EndpointFiles{
"ep1": {"f1", "f2", "f3"}, "ep1": {"f1", "f2", "f3"},
@ -55,7 +47,7 @@ func TestTlsListAndBatchRemove(t *testing.T) {
for name, files := range all { for name, files := range all {
for _, file := range files { for _, file := range files {
err = testee.createOrUpdate("test-ctx", name, file, []byte("data")) err := testee.createOrUpdate("test-ctx", name, file, []byte("data"))
assert.NilError(t, err) assert.NilError(t, err)
} }
} }

View File

@ -1,7 +1,7 @@
package context package context
import ( import (
"io/ioutil" "os"
"github.com/docker/cli/cli/context/store" "github.com/docker/cli/cli/context/store"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -77,17 +77,17 @@ func TLSDataFromFiles(caPath, certPath, keyPath string) (*TLSData, error) {
err error err error
) )
if caPath != "" { if caPath != "" {
if ca, err = ioutil.ReadFile(caPath); err != nil { if ca, err = os.ReadFile(caPath); err != nil {
return nil, err return nil, err
} }
} }
if certPath != "" { if certPath != "" {
if cert, err = ioutil.ReadFile(certPath); err != nil { if cert, err = os.ReadFile(certPath); err != nil {
return nil, err return nil, err
} }
} }
if keyPath != "" { if keyPath != "" {
if key, err = ioutil.ReadFile(keyPath); err != nil { if key, err = os.ReadFile(keyPath); err != nil {
return nil, err return nil, err
} }
} }