cli/manifest: 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 15:29:30 +01:00
parent cca80cdddd
commit 58cf16da45
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 17 additions and 33 deletions

View File

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

View File

@ -1,7 +1,6 @@
package store package store
import ( import (
"io/ioutil"
"os" "os"
"testing" "testing"
@ -29,41 +28,31 @@ func ref(name string) fakeRef {
} }
func sref(t *testing.T, name string) *types.SerializableNamed { func sref(t *testing.T, name string) *types.SerializableNamed {
t.Helper()
named, err := reference.ParseNamed("example.com/" + name) named, err := reference.ParseNamed("example.com/" + name)
assert.NilError(t, err) assert.NilError(t, err)
return &types.SerializableNamed{Named: named} 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) { func TestStoreRemove(t *testing.T) {
store, cleanup := newTestStore(t) tmpDir := t.TempDir()
defer cleanup() store := NewStore(tmpDir)
listRef := ref("list") listRef := ref("list")
data := types.ImageManifest{Ref: sref(t, "abcdef")} data := types.ImageManifest{Ref: sref(t, "abcdef")}
assert.NilError(t, store.Save(listRef, ref("manifest"), data)) 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, 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) { func TestStoreSaveAndGet(t *testing.T) {
store, cleanup := newTestStore(t) store := NewStore(t.TempDir())
defer cleanup()
listRef := ref("list") listRef := ref("list")
data := types.ImageManifest{Ref: sref(t, "abcdef")} data := types.ImageManifest{Ref: sref(t, "abcdef")}
err := store.Save(listRef, ref("exists"), data) 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) { func TestStoreGetList(t *testing.T) {
store, cleanup := newTestStore(t) store := NewStore(t.TempDir())
defer cleanup()
listRef := ref("list") listRef := ref("list")
first := types.ImageManifest{Ref: sref(t, "first")} first := types.ImageManifest{Ref: sref(t, "first")}
assert.NilError(t, store.Save(listRef, ref("first"), first)) assert.NilError(t, store.Save(listRef, ref("first"), first))
@ -127,9 +114,7 @@ func TestStoreGetList(t *testing.T) {
} }
func TestStoreGetListDoesNotExist(t *testing.T) { func TestStoreGetListDoesNotExist(t *testing.T) {
store, cleanup := newTestStore(t) store := NewStore(t.TempDir())
defer cleanup()
listRef := ref("list") listRef := ref("list")
_, err := store.GetList(listRef) _, err := store.GetList(listRef)
assert.Error(t, err, "No such manifest: list") assert.Error(t, err, "No such manifest: list")