Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions client/alloc_watcher_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package client_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -102,7 +102,7 @@ func TestPrevAlloc_StreamAllocDir_TLS(t *testing.T) {
// Save a file into alloc dir
contents := []byte("123\n456")
allocFn := filepath.Join(client1.DataDir, "alloc", origAlloc, "alloc", "data", "bar")
require.NoError(ioutil.WriteFile(allocFn, contents, 0666))
require.NoError(os.WriteFile(allocFn, contents, 0666))
t.Logf("[TEST] Wrote initial file: %s", allocFn)

// Migrate alloc to other node
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestPrevAlloc_StreamAllocDir_TLS(t *testing.T) {
allocFn2 := filepath.Join(client2.DataDir, "alloc", newAlloc.ID, "alloc", "data", "bar")
t.Logf("[TEST] Comparing against file: %s", allocFn2)
testutil.WaitForResult(func() (bool, error) {
found, err := ioutil.ReadFile(allocFn2)
found, err := os.ReadFile(allocFn2)
if err != nil {
return false, err
}
Expand Down
6 changes: 3 additions & 3 deletions client/allocdir/alloc_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -357,12 +356,13 @@ 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, _ := file.Info()
lhaig marked this conversation as resolved.
Show resolved Hide resolved
files[idx] = &cstructs.AllocFileInfo{
Name: info.Name(),
IsDir: info.IsDir(),
Expand Down
15 changes: 7 additions & 8 deletions client/allocdir/alloc_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions client/allocdir/task_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package allocdir

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -184,12 +183,13 @@ 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, _ := fileEntry.Info()
lhaig marked this conversation as resolved.
Show resolved Hide resolved
hostEntry := filepath.Join(source, entry.Name())
taskEntry := filepath.Join(destDir, filepath.Base(hostEntry))
if entry.IsDir() {
Expand Down
5 changes: 2 additions & 3 deletions client/allocdir/task_dir_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package allocdir

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -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)
}

Expand Down
3 changes: 1 addition & 2 deletions client/allocdir/testing.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package allocdir

import (
"io/ioutil"
"os"

hclog "github.com/hashicorp/go-hclog"
Expand All @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions client/allocrunner/alloc_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package allocrunner
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync/atomic"
Expand Down Expand Up @@ -1680,10 +1679,10 @@ func TestAllocRunner_MoveAllocDir(t *testing.T) {
// Step 2. Modify its directory
task := alloc.Job.TaskGroups[0].Tasks[0]
dataFile := filepath.Join(ar.allocDir.SharedDir, "data", "data_file")
ioutil.WriteFile(dataFile, []byte("hello world"), os.ModePerm)
os.WriteFile(dataFile, []byte("hello world"), os.ModePerm)
taskDir := ar.allocDir.TaskDirs[task.Name]
taskLocalFile := filepath.Join(taskDir.LocalDir, "local_file")
ioutil.WriteFile(taskLocalFile, []byte("good bye world"), os.ModePerm)
os.WriteFile(taskLocalFile, []byte("good bye world"), os.ModePerm)

// Step 3. Start a new alloc
alloc2 := mock.BatchAlloc()
Expand Down
23 changes: 11 additions & 12 deletions client/allocrunner/taskrunner/artifact_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package taskrunner
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -83,7 +82,7 @@ func TestTaskRunner_ArtifactHook_PartialDone(t *testing.T) {

// Only create one of the 2 artifacts to cause an error on first run.
file1 := filepath.Join(srcdir, "foo.txt")
require.NoError(t, ioutil.WriteFile(file1, []byte{'1'}, 0644))
require.NoError(t, os.WriteFile(file1, []byte{'1'}, 0644))

// Test server to serve the artifacts
ts := httptest.NewServer(http.FileServer(http.Dir(srcdir)))
Expand Down Expand Up @@ -127,7 +126,7 @@ func TestTaskRunner_ArtifactHook_PartialDone(t *testing.T) {

// Write file2 so artifacts can download successfully
file2 := filepath.Join(srcdir, "bar.txt")
require.NoError(t, ioutil.WriteFile(file2, []byte{'1'}, 0644))
require.NoError(t, os.WriteFile(file2, []byte{'1'}, 0644))

// Mock TaskRunner by copying state from resp to req and reset resp.
req.PreviousState = maps.Clone(resp.State)
Expand Down Expand Up @@ -174,7 +173,7 @@ func TestTaskRunner_ArtifactHook_ConcurrentDownloadSuccess(t *testing.T) {
numOfFiles := 7
for i := 0; i < numOfFiles; i++ {
file := filepath.Join(srcdir, fmt.Sprintf("file%d.txt", i))
require.NoError(t, ioutil.WriteFile(file, []byte{byte(i)}, 0644))
require.NoError(t, os.WriteFile(file, []byte{byte(i)}, 0644))
}

// Test server to serve the artifacts
Expand Down Expand Up @@ -260,13 +259,13 @@ func TestTaskRunner_ArtifactHook_ConcurrentDownloadFailure(t *testing.T) {
srcdir := t.TempDir()

file1 := filepath.Join(srcdir, "file1.txt")
require.NoError(t, ioutil.WriteFile(file1, []byte{'1'}, 0644))
require.NoError(t, os.WriteFile(file1, []byte{'1'}, 0644))

file2 := filepath.Join(srcdir, "file2.txt")
require.NoError(t, ioutil.WriteFile(file2, []byte{'2'}, 0644))
require.NoError(t, os.WriteFile(file2, []byte{'2'}, 0644))

file3 := filepath.Join(srcdir, "file3.txt")
require.NoError(t, ioutil.WriteFile(file3, []byte{'3'}, 0644))
require.NoError(t, os.WriteFile(file3, []byte{'3'}, 0644))

// Test server to serve the artifacts
ts := httptest.NewServer(http.FileServer(http.Dir(srcdir)))
Expand Down Expand Up @@ -319,7 +318,7 @@ func TestTaskRunner_ArtifactHook_ConcurrentDownloadFailure(t *testing.T) {

// create the missing file
file0 := filepath.Join(srcdir, "file0.txt")
require.NoError(t, ioutil.WriteFile(file0, []byte{'0'}, 0644))
require.NoError(t, os.WriteFile(file0, []byte{'0'}, 0644))

// Mock TaskRunner by copying state from resp to req and reset resp.
req.PreviousState = maps.Clone(resp.State)
Expand All @@ -342,19 +341,19 @@ func TestTaskRunner_ArtifactHook_ConcurrentDownloadFailure(t *testing.T) {
require.Contains(t, files[3], "file3.txt")

// verify the file contents too, since files will also be created for failed downloads
data0, err := ioutil.ReadFile(files[0])
data0, err := os.ReadFile(files[0])
require.NoError(t, err)
require.Equal(t, data0, []byte{'0'})

data1, err := ioutil.ReadFile(files[1])
data1, err := os.ReadFile(files[1])
require.NoError(t, err)
require.Equal(t, data1, []byte{'1'})

data2, err := ioutil.ReadFile(files[2])
data2, err := os.ReadFile(files[2])
require.NoError(t, err)
require.Equal(t, data2, []byte{'2'})

data3, err := ioutil.ReadFile(files[3])
data3, err := os.ReadFile(files[3])
require.NoError(t, err)
require.Equal(t, data3, []byte{'3'})

Expand Down
3 changes: 1 addition & 2 deletions client/allocrunner/taskrunner/connect_native_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -270,7 +269,7 @@ func (h *connectNativeHook) maybeSetSITokenEnv(dir, task string, env map[string]
return nil
}

token, err := ioutil.ReadFile(filepath.Join(dir, sidsTokenFile))
token, err := os.ReadFile(filepath.Join(dir, sidsTokenFile))
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("failed to load SI token for native task %s: %w", task, err)
Expand Down
17 changes: 9 additions & 8 deletions client/allocrunner/taskrunner/connect_native_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package taskrunner

import (
"context"
"io/ioutil"
"io"
"os"
"path/filepath"
"testing"

Expand All @@ -27,8 +28,8 @@ func getTestConsul(t *testing.T) *consultest.TestServer {
testConsul, err := consultest.NewTestServerConfigT(t, func(c *consultest.TestServerConfig) {
c.Peering = nil // fix for older versions of Consul (<1.13.0) that don't support peering
if !testing.Verbose() { // disable consul logging if -v not set
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
c.Stdout = io.Discard
c.Stderr = io.Discard
}
})
require.NoError(t, err, "failed to start test consul server")
Expand All @@ -42,7 +43,7 @@ func TestConnectNativeHook_Name(t *testing.T) {
}

func setupCertDirs(t *testing.T) (string, string) {
fd, err := ioutil.TempFile(t.TempDir(), "connect_native_testcert")
fd, err := os.CreateTemp(t.TempDir(), "connect_native_testcert")
require.NoError(t, err)
_, err = fd.WriteString("ABCDEF")
require.NoError(t, err)
Expand All @@ -65,7 +66,7 @@ func TestConnectNativeHook_copyCertificate(t *testing.T) {
t.Run("normal", func(t *testing.T) {
err := new(connectNativeHook).copyCertificate(f, d, "out.pem")
require.NoError(t, err)
b, err := ioutil.ReadFile(filepath.Join(d, "out.pem"))
b, err := os.ReadFile(filepath.Join(d, "out.pem"))
require.NoError(t, err)
require.Equal(t, "ABCDEF", string(b))
})
Expand All @@ -83,7 +84,7 @@ func TestConnectNativeHook_copyCertificates(t *testing.T) {
KeyFile: f,
}, d)
require.NoError(t, err)
ls, err := ioutil.ReadDir(d)
ls, err := os.ReadDir(d)
require.NoError(t, err)
require.Equal(t, 3, len(ls))
})
Expand Down Expand Up @@ -411,7 +412,7 @@ func TestTaskRunner_ConnectNativeHook_with_SI_token(t *testing.T) {
// Insert service identity token in the secrets directory
token := uuid.Generate()
siTokenFile := filepath.Join(request.TaskDir.SecretsDir, sidsTokenFile)
err = ioutil.WriteFile(siTokenFile, []byte(token), 0440)
err = os.WriteFile(siTokenFile, []byte(token), 0440)
require.NoError(t, err)

response := new(interfaces.TaskPrestartResponse)
Expand Down Expand Up @@ -538,7 +539,7 @@ func TestTaskRunner_ConnectNativeHook_shareTLS(t *testing.T) {
}

func checkFilesInDir(t *testing.T, dir string, includes, excludes []string) {
ls, err := ioutil.ReadDir(dir)
ls, err := os.ReadDir(dir)
require.NoError(t, err)

var present []string
Expand Down
3 changes: 1 addition & 2 deletions client/allocrunner/taskrunner/dispatch_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package taskrunner

import (
"context"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -69,5 +68,5 @@ func writeDispatchPayload(base, filename string, payload []byte) error {
return err
}

return ioutil.WriteFile(renderTo, decoded, 0777)
return os.WriteFile(renderTo, decoded, 0777)
}
Loading