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
import (
"io/ioutil"
"io"
"strings"
"testing"
@ -11,14 +11,14 @@ import (
func TestLimitReaderReadAll(t *testing.T) {
r := strings.NewReader("Reader")
_, err := ioutil.ReadAll(r)
_, err := io.ReadAll(r)
assert.NilError(t, err)
r = strings.NewReader("Test")
_, err = ioutil.ReadAll(&LimitedReader{R: r, N: 4})
_, err = io.ReadAll(&LimitedReader{R: r, N: 4})
assert.NilError(t, err)
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")
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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