deps: Update ioutil deprecated library references to os and io respectively in the client package (#16318)

* Update ioutil deprecated library references to os and io respectively

* Deal with the errors produced.

Add error handling to filEntry info
Add error handling to info
This commit is contained in:
Lance Haig
2023-03-08 20:25:10 +01:00
committed by GitHub
parent 3160c76209
commit 48e7d70fcd
34 changed files with 112 additions and 127 deletions

View File

@@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
@@ -357,12 +356,16 @@ func (d *AllocDir) List(path string) ([]*cstructs.AllocFileInfo, error) {
}
p := filepath.Join(d.AllocDir, path)
finfos, err := ioutil.ReadDir(p)
finfos, err := os.ReadDir(p)
if err != nil {
return []*cstructs.AllocFileInfo{}, err
}
files := make([]*cstructs.AllocFileInfo, len(finfos))
for idx, info := range finfos {
for idx, file := range finfos {
info, err := file.Info()
if err != nil {
return []*cstructs.AllocFileInfo{}, err
}
files[idx] = &cstructs.AllocFileInfo{
Name: info.Name(),
IsDir: info.IsDir(),

View File

@@ -6,7 +6,6 @@ import (
"context"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@@ -120,14 +119,14 @@ func TestAllocDir_MountSharedAlloc(t *testing.T) {
// Write a file to the shared dir.
contents := []byte("foo")
const filename = "bar"
if err := ioutil.WriteFile(filepath.Join(d.SharedDir, filename), contents, 0666); err != nil {
if err := os.WriteFile(filepath.Join(d.SharedDir, filename), contents, 0666); err != nil {
t.Fatalf("Couldn't write file to shared directory: %v", err)
}
// Check that the file exists in the task directories
for _, td := range []*TaskDir{td1, td2} {
taskFile := filepath.Join(td.SharedTaskDir, filename)
act, err := ioutil.ReadFile(taskFile)
act, err := os.ReadFile(taskFile)
if err != nil {
t.Errorf("Failed to read shared alloc file from task dir: %v", err)
continue
@@ -163,7 +162,7 @@ func TestAllocDir_Snapshot(t *testing.T) {
// Write a file to the shared dir.
exp := []byte{'f', 'o', 'o'}
file := "bar"
if err := ioutil.WriteFile(filepath.Join(d.SharedDir, "data", file), exp, 0666); err != nil {
if err := os.WriteFile(filepath.Join(d.SharedDir, "data", file), exp, 0666); err != nil {
t.Fatalf("Couldn't write file to shared directory: %v", err)
}
@@ -176,7 +175,7 @@ func TestAllocDir_Snapshot(t *testing.T) {
// Write a file to the task local
exp = []byte{'b', 'a', 'r'}
file1 := "lol"
if err := ioutil.WriteFile(filepath.Join(td1.LocalDir, file1), exp, 0666); err != nil {
if err := os.WriteFile(filepath.Join(td1.LocalDir, file1), exp, 0666); err != nil {
t.Fatalf("couldn't write file to task local directory: %v", err)
}
@@ -250,14 +249,14 @@ func TestAllocDir_Move(t *testing.T) {
// Write a file to the shared dir.
exp1 := []byte("foo")
file1 := "bar"
if err := ioutil.WriteFile(filepath.Join(dataDir, file1), exp1, 0666); err != nil {
if err := os.WriteFile(filepath.Join(dataDir, file1), exp1, 0666); err != nil {
t.Fatalf("Couldn't write file to shared directory: %v", err)
}
// Write a file to the task local
exp2 := []byte("bar")
file2 := "lol"
if err := ioutil.WriteFile(filepath.Join(td1.LocalDir, file2), exp2, 0666); err != nil {
if err := os.WriteFile(filepath.Join(td1.LocalDir, file2), exp2, 0666); err != nil {
t.Fatalf("couldn't write to task local directory: %v", err)
}
@@ -337,7 +336,7 @@ func TestAllocDir_ReadAt_SecretDir(t *testing.T) {
// create target file in the task secrets dir
full := filepath.Join(d.AllocDir, target)
err = ioutil.WriteFile(full, []byte("hi"), 0600)
err = os.WriteFile(full, []byte("hi"), 0600)
require.NoError(t, err)
// ReadAt of a file in the task secrets dir should fail

View File

@@ -2,7 +2,6 @@ package allocdir
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
@@ -184,12 +183,16 @@ func (t *TaskDir) embedDirs(entries map[string]string) error {
}
// Enumerate the files in source.
dirEntries, err := ioutil.ReadDir(source)
dirEntries, err := os.ReadDir(source)
if err != nil {
return fmt.Errorf("Couldn't read directory %v: %v", source, err)
}
for _, entry := range dirEntries {
for _, fileEntry := range dirEntries {
entry, err := fileEntry.Info()
if err != nil {
return fmt.Errorf("Couldn't read the file information %v: %v", entry, err)
}
hostEntry := filepath.Join(source, entry.Name())
taskEntry := filepath.Join(destDir, filepath.Base(hostEntry))
if entry.IsDir() {

View File

@@ -1,7 +1,6 @@
package allocdir
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -55,11 +54,11 @@ func TestTaskDir_EmbedDirs(t *testing.T) {
file := "foo"
subFile := "bar"
if err := ioutil.WriteFile(filepath.Join(host, file), []byte{'a'}, 0777); err != nil {
if err := os.WriteFile(filepath.Join(host, file), []byte{'a'}, 0777); err != nil {
t.Fatalf("Couldn't create file in host dir %v: %v", host, err)
}
if err := ioutil.WriteFile(filepath.Join(subDir, subFile), []byte{'a'}, 0777); err != nil {
if err := os.WriteFile(filepath.Join(subDir, subFile), []byte{'a'}, 0777); err != nil {
t.Fatalf("Couldn't create file in host subdir %v: %v", subDir, err)
}

View File

@@ -1,7 +1,6 @@
package allocdir
import (
"io/ioutil"
"os"
hclog "github.com/hashicorp/go-hclog"
@@ -11,7 +10,7 @@ import (
// TestAllocDir returns a built alloc dir in a temporary directory and cleanup
// func.
func TestAllocDir(t testing.T, l hclog.Logger, prefix, id string) (*AllocDir, func()) {
dir, err := ioutil.TempDir("", prefix)
dir, err := os.MkdirTemp("", prefix)
if err != nil {
t.Fatalf("Couldn't create temp dir: %v", err)
}