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

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 58cf16da45)
Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
Sebastiaan van Stijn 2022-02-25 15:29:30 +01:00 committed by Cory Snider
parent cec1b29b81
commit f8211f7f80
2 changed files with 17 additions and 33 deletions

View File

@ -3,7 +3,6 @@ package store
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -47,7 +46,7 @@ func (s *fsStore) Get(listRef reference.Reference, manifest reference.Reference)
}
func (s *fsStore) getFromFilename(ref reference.Reference, filename string) (types.ImageManifest, error) {
bytes, err := ioutil.ReadFile(filename)
bytes, err := os.ReadFile(filename)
switch {
case os.IsNotExist(err):
return types.ImageManifest{}, newNotFoundError(ref.String())
@ -112,7 +111,7 @@ func (s *fsStore) GetList(listRef reference.Reference) ([]types.ImageManifest, e
// listManifests stored in a transaction
func (s *fsStore) listManifests(transaction string) ([]string, error) {
transactionDir := filepath.Join(s.root, makeFilesafeName(transaction))
fileInfos, err := ioutil.ReadDir(transactionDir)
fileInfos, err := os.ReadDir(transactionDir)
switch {
case os.IsNotExist(err):
return nil, nil
@ -120,7 +119,7 @@ func (s *fsStore) listManifests(transaction string) ([]string, error) {
return nil, err
}
filenames := []string{}
filenames := make([]string, 0, len(fileInfos))
for _, info := range fileInfos {
filenames = append(filenames, info.Name())
}
@ -137,7 +136,7 @@ func (s *fsStore) Save(listRef reference.Reference, manifest reference.Reference
if err != nil {
return err
}
return ioutil.WriteFile(filename, bytes, 0644)
return os.WriteFile(filename, bytes, 0644)
}
func (s *fsStore) createManifestListDirectory(transaction string) error {

View File

@ -1,7 +1,6 @@
package store
import (
"io/ioutil"
"os"
"testing"
@ -29,41 +28,31 @@ func ref(name string) fakeRef {
}
func sref(t *testing.T, name string) *types.SerializableNamed {
t.Helper()
named, err := reference.ParseNamed("example.com/" + name)
assert.NilError(t, err)
return &types.SerializableNamed{Named: named}
}
func newTestStore(t *testing.T) (Store, func()) {
tmpdir, err := ioutil.TempDir("", "manifest-store-test")
assert.NilError(t, err)
return NewStore(tmpdir), func() { os.RemoveAll(tmpdir) }
}
func getFiles(t *testing.T, store Store) []os.FileInfo {
infos, err := ioutil.ReadDir(store.(*fsStore).root)
assert.NilError(t, err)
return infos
}
func TestStoreRemove(t *testing.T) {
store, cleanup := newTestStore(t)
defer cleanup()
tmpDir := t.TempDir()
store := NewStore(tmpDir)
listRef := ref("list")
data := types.ImageManifest{Ref: sref(t, "abcdef")}
assert.NilError(t, store.Save(listRef, ref("manifest"), data))
assert.Assert(t, is.Len(getFiles(t, store), 1))
files, err := os.ReadDir(tmpDir)
assert.NilError(t, err)
assert.Assert(t, is.Len(files, 1))
assert.Check(t, store.Remove(listRef))
assert.Check(t, is.Len(getFiles(t, store), 0))
files, err = os.ReadDir(tmpDir)
assert.NilError(t, err)
assert.Check(t, is.Len(files, 0))
}
func TestStoreSaveAndGet(t *testing.T) {
store, cleanup := newTestStore(t)
defer cleanup()
store := NewStore(t.TempDir())
listRef := ref("list")
data := types.ImageManifest{Ref: sref(t, "abcdef")}
err := store.Save(listRef, ref("exists"), data)
@ -112,9 +101,7 @@ var cmpReferenceNamed = cmp.Transformer("namedref", func(r reference.Named) stri
})
func TestStoreGetList(t *testing.T) {
store, cleanup := newTestStore(t)
defer cleanup()
store := NewStore(t.TempDir())
listRef := ref("list")
first := types.ImageManifest{Ref: sref(t, "first")}
assert.NilError(t, store.Save(listRef, ref("first"), first))
@ -127,9 +114,7 @@ func TestStoreGetList(t *testing.T) {
}
func TestStoreGetListDoesNotExist(t *testing.T) {
store, cleanup := newTestStore(t)
defer cleanup()
store := NewStore(t.TempDir())
listRef := ref("list")
_, err := store.GetList(listRef)
assert.Error(t, err, "No such manifest: list")