deps: Update ioutil library references to os and io respectively for drivers package (#16331)

* Update ioutil library references to os and io respectively for drivers package

No user facing changes so I assume no change log is required

* Fix failing tests
This commit is contained in:
Lance Haig 2023-03-08 17:31:09 +01:00 committed by GitHub
parent ae256e28d8
commit 2332d694bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 46 additions and 55 deletions

View File

@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
@ -805,7 +804,7 @@ func parseSecurityOpts(securityOpts []string) ([]string, error) {
}
}
if con[0] == "seccomp" && con[1] != "unconfined" {
f, err := ioutil.ReadFile(con[1])
f, err := os.ReadFile(con[1])
if err != nil {
return securityOpts, fmt.Errorf("opening seccomp profile (%s) failed: %v", con[1], err)
}

View File

@ -4,7 +4,6 @@ package docker
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -25,7 +24,7 @@ func TestDockerDriver_authFromHelper(t *testing.T) {
helperContent := []byte(fmt.Sprintf("#!/bin/sh\ncat > %s/helper-$1.out;echo '%s'", dir, helperPayload))
helperFile := filepath.Join(dir, "docker-credential-testnomad")
err := ioutil.WriteFile(helperFile, helperContent, 0777)
err := os.WriteFile(helperFile, helperContent, 0777)
require.NoError(t, err)
path := os.Getenv("PATH")
@ -41,7 +40,7 @@ func TestDockerDriver_authFromHelper(t *testing.T) {
if _, err := os.Stat(filepath.Join(dir, "helper-get.out")); os.IsNotExist(err) {
t.Fatalf("Expected helper-get.out to exist")
}
content, err := ioutil.ReadFile(filepath.Join(dir, "helper-get.out"))
content, err := os.ReadFile(filepath.Join(dir, "helper-get.out"))
require.NoError(t, err)
require.Equal(t, "registry.local:5000", string(content))
}
@ -87,7 +86,7 @@ func TestDockerDriver_PidsLimit(t *testing.T) {
outputFile := filepath.Join(task.TaskDir().LogDir, "redis-demo.stderr.0")
exp := "can't fork"
tu.WaitForResult(func() (bool, error) {
act, err := ioutil.ReadFile(outputFile)
act, err := os.ReadFile(outputFile)
if err != nil {
return false, err
}

View File

@ -3,8 +3,8 @@ package docker
import (
"context"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"reflect"
"runtime"
@ -397,7 +397,7 @@ func TestDockerDriver_Start_LoadImage(t *testing.T) {
// Check that data was written to the shared alloc directory.
outputFile := filepath.Join(task.TaskDir().LocalDir, "output")
act, err := ioutil.ReadFile(outputFile)
act, err := os.ReadFile(outputFile)
if err != nil {
t.Fatalf("Couldn't read expected output: %v", err)
}
@ -524,7 +524,7 @@ func TestDockerDriver_Start_Wait_AllocDir(t *testing.T) {
// Check that data was written to the shared alloc directory.
outputFile := filepath.Join(task.TaskDir().SharedAllocDir, file)
act, err := ioutil.ReadFile(outputFile)
act, err := os.ReadFile(outputFile)
if err != nil {
t.Fatalf("Couldn't read expected output: %v", err)
}
@ -2143,7 +2143,7 @@ func TestDockerDriver_VolumesDisabled(t *testing.T) {
t.Fatalf("timeout")
}
if _, err := ioutil.ReadFile(filepath.Join(task.TaskDir().Dir, fn)); err != nil {
if _, err := os.ReadFile(filepath.Join(task.TaskDir().Dir, fn)); err != nil {
t.Fatalf("unexpected error reading %s: %v", fn, err)
}
}
@ -2201,7 +2201,7 @@ func TestDockerDriver_VolumesEnabled(t *testing.T) {
t.Fatalf("timeout")
}
if _, err := ioutil.ReadFile(hostpath); err != nil {
if _, err := os.ReadFile(hostpath); err != nil {
t.Fatalf("unexpected error reading %s: %v", hostpath, err)
}
}

View File

@ -5,7 +5,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -353,7 +352,7 @@ func TestExecDriver_NoOrphans(t *testing.T) {
return false, fmt.Errorf("task PID is zero")
}
children, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/task/%d/children", taskState.Pid, taskState.Pid))
children, err := os.ReadFile(fmt.Sprintf("/proc/%d/task/%d/children", taskState.Pid, taskState.Pid))
if err != nil {
return false, fmt.Errorf("error reading /proc for children: %v", err)
}
@ -509,7 +508,7 @@ func TestExecDriver_Start_Wait_AllocDir(t *testing.T) {
// Check that data was written to the shared alloc directory.
outputFile := filepath.Join(task.TaskDir().SharedAllocDir, file)
act, err := ioutil.ReadFile(outputFile)
act, err := os.ReadFile(outputFile)
require.NoError(t, err)
require.Exactly(t, exp, act)
@ -624,7 +623,7 @@ func TestExecDriver_DevicesAndMounts(t *testing.T) {
tmpDir := t.TempDir()
err := ioutil.WriteFile(filepath.Join(tmpDir, "testfile"), []byte("from-host"), 600)
err := os.WriteFile(filepath.Join(tmpDir, "testfile"), []byte("from-host"), 600)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
@ -661,8 +660,8 @@ func TestExecDriver_DevicesAndMounts(t *testing.T) {
},
}
require.NoError(t, ioutil.WriteFile(task.StdoutPath, []byte{}, 660))
require.NoError(t, ioutil.WriteFile(task.StderrPath, []byte{}, 660))
require.NoError(t, os.WriteFile(task.StdoutPath, []byte{}, 660))
require.NoError(t, os.WriteFile(task.StderrPath, []byte{}, 660))
tc := &TaskConfig{
Command: "/bin/bash",
@ -691,7 +690,7 @@ exit 0
result := <-ch
require.NoError(t, harness.DestroyTask(task.ID, true))
stdout, err := ioutil.ReadFile(task.StdoutPath)
stdout, err := os.ReadFile(task.StdoutPath)
require.NoError(t, err)
require.Equal(t, `mounted device /inserted-random: 1:8
reading from ro path: from-host
@ -699,7 +698,7 @@ reading from rw path: from-host
overwriting file in rw succeeded
writing new file in rw succeeded`, strings.TrimSpace(string(stdout)))
stderr, err := ioutil.ReadFile(task.StderrPath)
stderr, err := os.ReadFile(task.StderrPath)
require.NoError(t, err)
require.Equal(t, `touch: cannot touch '/tmp/task-path-ro/testfile': Read-only file system
touch: cannot touch '/tmp/task-path-ro/testfile-from-ro': Read-only file system`, strings.TrimSpace(string(stderr)))
@ -707,7 +706,7 @@ touch: cannot touch '/tmp/task-path-ro/testfile-from-ro': Read-only file system`
// testing exit code last so we can inspect output first
require.Zero(t, result.ExitCode)
fromRWContent, err := ioutil.ReadFile(filepath.Join(tmpDir, "testfile-from-rw"))
fromRWContent, err := os.ReadFile(filepath.Join(tmpDir, "testfile-from-rw"))
require.NoError(t, err)
require.Equal(t, "from-exec", strings.TrimSpace(string(fromRWContent)))
}

View File

@ -4,14 +4,14 @@ import (
"context"
"errors"
"fmt"
"github.com/hashicorp/nomad/client/lib/cgutil"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/hashicorp/nomad/client/lib/cgutil"
"github.com/hashicorp/nomad/ci"
ctestutil "github.com/hashicorp/nomad/client/testutil"
"github.com/hashicorp/nomad/helper/pluginutils/hclutils"
@ -90,7 +90,7 @@ func TestJavaDriver_Jar_Start_Wait(t *testing.T) {
require.Zero(t, result.ExitCode)
// Get the stdout of the process and assert that it's not empty
stdout, err := ioutil.ReadFile(filepath.Join(task.TaskDir().LogDir, "demo-app.stdout.0"))
stdout, err := os.ReadFile(filepath.Join(task.TaskDir().LogDir, "demo-app.stdout.0"))
require.NoError(t, err)
require.Contains(t, string(stdout), "Hello")
@ -190,7 +190,7 @@ func TestJavaDriver_Class_Start_Wait(t *testing.T) {
require.Zero(t, result.ExitCode)
// Get the stdout of the process and assert that it's not empty
stdout, err := ioutil.ReadFile(filepath.Join(task.TaskDir().LogDir, "demo-app.stdout.0"))
stdout, err := os.ReadFile(filepath.Join(task.TaskDir().LogDir, "demo-app.stdout.0"))
require.NoError(t, err)
require.Contains(t, string(stdout), "Hello")

View File

@ -4,7 +4,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"io"
"math/rand"
"strconv"
"strings"
@ -641,7 +641,7 @@ func (d *Driver) ExecTaskStreaming(ctx context.Context, taskID string, execOpts
cmd := *h.execCommand
if len(execOpts.Command) == 1 && execOpts.Command[0] == "showinput" {
stdin, _ := ioutil.ReadAll(execOpts.Stdin)
stdin, _ := io.ReadAll(execOpts.Stdin)
cmd = Command{
RunFor: "1ms",
StdoutString: fmt.Sprintf("TTY: %v\nStdin:\n%s\n",

View File

@ -3,7 +3,6 @@ package rawexec
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -337,7 +336,7 @@ func TestRawExecDriver_Start_Wait_AllocDir(t *testing.T) {
// Check that data was written to the shared alloc directory.
outputFile := filepath.Join(task.TaskDir().SharedAllocDir, file)
act, err := ioutil.ReadFile(outputFile)
act, err := os.ReadFile(outputFile)
require.NoError(err)
require.Exactly(exp, act)
require.NoError(harness.DestroyTask(task.ID, true))
@ -382,7 +381,7 @@ func TestRawExecDriver_Start_Kill_Wait_Cgroup(t *testing.T) {
var pidData []byte
testutil.WaitForResult(func() (bool, error) {
var err error
pidData, err = ioutil.ReadFile(filepath.Join(task.TaskDir().Dir, pidFile))
pidData, err = os.ReadFile(filepath.Join(task.TaskDir().Dir, pidFile))
if err != nil {
return false, err
}

View File

@ -5,7 +5,6 @@ package rawexec
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
@ -95,7 +94,7 @@ while true; do
sleep 1
done
`)
require.NoError(ioutil.WriteFile(testFile, testData, 0777))
require.NoError(os.WriteFile(testFile, testData, 0777))
_, _, err := harness.StartTask(task)
require.NoError(err)
@ -120,7 +119,7 @@ done
outputFile := filepath.Join(task.TaskDir().LogDir, "signal.stdout.0")
exp := "Terminated."
testutil.WaitForResult(func() (bool, error) {
act, err := ioutil.ReadFile(outputFile)
act, err := os.ReadFile(outputFile)
if err != nil {
return false, fmt.Errorf("Couldn't read expected output: %v", err)
}
@ -245,7 +244,7 @@ func TestRawExecDriver_DestroyKillsAll(t *testing.T) {
// Ensure that the task is marked as dead, but account
// for WaitTask() closing channel before internal state is updated
testutil.WaitForResult(func() (bool, error) {
stdout, err := ioutil.ReadFile(filepath.Join(task.TaskDir().LogDir, "test.stdout.0"))
stdout, err := os.ReadFile(filepath.Join(task.TaskDir().LogDir, "test.stdout.0"))
if err != nil {
return false, fmt.Errorf("failed to output pid file: %v", err)
}
@ -389,7 +388,7 @@ func TestRawExecDriver_NoCgroup(t *testing.T) {
ci.Parallel(t)
clienttestutil.RequireLinux(t)
expectedBytes, err := ioutil.ReadFile("/proc/self/cgroup")
expectedBytes, err := os.ReadFile("/proc/self/cgroup")
require.NoError(t, err)
expected := strings.TrimSpace(string(expectedBytes))
@ -430,7 +429,7 @@ func TestRawExecDriver_NoCgroup(t *testing.T) {
// Check the log file to see it exited because of the signal
outputFile := filepath.Join(task.TaskDir().LogDir, "nocgroup.stdout.0")
testutil.WaitForResult(func() (bool, error) {
act, err := ioutil.ReadFile(outputFile)
act, err := os.ReadFile(outputFile)
if err != nil {
return false, fmt.Errorf("Couldn't read expected output: %v", err)
}

View File

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -186,7 +185,7 @@ func (c *ExecCommand) Stdout() (io.WriteCloser, error) {
}
c.stdout = f
} else {
c.stdout = nopCloser{ioutil.Discard}
c.stdout = nopCloser{io.Discard}
}
}
return c.stdout, nil
@ -202,7 +201,7 @@ func (c *ExecCommand) Stderr() (io.WriteCloser, error) {
}
c.stderr = f
} else {
c.stderr = nopCloser{ioutil.Discard}
c.stderr = nopCloser{io.Discard}
}
}
return c.stderr, nil

View File

@ -3,7 +3,6 @@ package executor
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
@ -207,7 +206,7 @@ func TestExecutor_IsolationAndConstraints(t *testing.T) {
r.NoError(err)
memLimits := filepath.Join(state.CgroupPaths["memory"], "memory.limit_in_bytes")
data, err := ioutil.ReadFile(memLimits)
data, err := os.ReadFile(memLimits)
r.NoError(err)
expectedMemLim := strconv.Itoa(int(execCmd.Resources.NomadResources.Memory.MemoryMB * 1024 * 1024))
@ -391,7 +390,7 @@ func TestExecutor_CgroupPathsAreDestroyed(t *testing.T) {
executor.Shutdown("SIGKILL", 0)
// test that the cgroup paths are not visible
tmpFile, err := ioutil.TempFile("", "")
tmpFile, err := os.CreateTemp("", "")
require.NoError(err)
defer os.Remove(tmpFile.Name())
@ -824,7 +823,7 @@ func TestUniversalExecutor_NoCgroup(t *testing.T) {
ci.Parallel(t)
testutil.ExecCompatible(t)
expectedBytes, err := ioutil.ReadFile("/proc/self/cgroup")
expectedBytes, err := os.ReadFile("/proc/self/cgroup")
require.NoError(t, err)
expected := strings.TrimSpace(string(expectedBytes))

View File

@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -383,7 +382,7 @@ func TestExecutor_Shutdown_Exit(t *testing.T) {
func TestUniversalExecutor_MakeExecutable(t *testing.T) {
ci.Parallel(t)
// Create a temp file
f, err := ioutil.TempFile("", "")
f, err := os.CreateTemp("", "")
if err != nil {
t.Fatal(err)
}
@ -422,7 +421,7 @@ func TestUniversalExecutor_LookupPath(t *testing.T) {
// Write a file under foo
filePath := filepath.Join(tmpDir, "foo", "tmp.txt")
err := ioutil.WriteFile(filePath, []byte{1, 2}, os.ModeAppend)
err := os.WriteFile(filePath, []byte{1, 2}, os.ModeAppend)
require.Nil(err)
// Lookup with full path on host to binary
@ -436,7 +435,7 @@ func TestUniversalExecutor_LookupPath(t *testing.T) {
// Write a file under task dir
filePath3 := filepath.Join(tmpDir, "tmp.txt")
ioutil.WriteFile(filePath3, []byte{1, 2}, os.ModeAppend)
os.WriteFile(filePath3, []byte{1, 2}, os.ModeAppend)
// Lookup with file name, should find the one we wrote above
path, err = lookupBin(tmpDir, "tmp.txt")
@ -446,7 +445,7 @@ func TestUniversalExecutor_LookupPath(t *testing.T) {
// Write a file under local subdir
os.MkdirAll(filepath.Join(tmpDir, "local"), 0700)
filePath2 := filepath.Join(tmpDir, "local", "tmp.txt")
ioutil.WriteFile(filePath2, []byte{1, 2}, os.ModeAppend)
os.WriteFile(filePath2, []byte{1, 2}, os.ModeAppend)
// Lookup with file name, should find the one we wrote above
path, err = lookupBin(tmpDir, "tmp.txt")
@ -607,7 +606,7 @@ func TestExecutor_Start_NonExecutableBinaries(t *testing.T) {
tmpDir := t.TempDir()
nonExecutablePath := filepath.Join(tmpDir, "nonexecutablefile")
ioutil.WriteFile(nonExecutablePath,
os.WriteFile(nonExecutablePath,
[]byte("#!/bin/sh\necho hello world"),
0600)

View File

@ -2,7 +2,6 @@ package hostnames
import (
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
@ -61,7 +60,7 @@ ff02::3 ip6-allhosts
// tasks within an alloc should be able to share and modify the file, so
// only write to it if it doesn't exist
if _, err := os.Stat(path); os.IsNotExist(err) {
err := ioutil.WriteFile(path, []byte(content.String()), 0644)
err := os.WriteFile(path, []byte(content.String()), 0644)
if err != nil {
return nil, err
}

View File

@ -4,7 +4,7 @@
package hostnames
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -105,7 +105,7 @@ func TestGenerateEtcHostsMount(t *testing.T) {
} else {
require.NotNil(got)
require.FileExists(dest)
tmpHosts, err := ioutil.ReadFile(dest)
tmpHosts, err := os.ReadFile(dest)
require.NoError(err)
for _, line := range tc.expected {
require.Contains(string(tmpHosts), line)

View File

@ -4,7 +4,7 @@
package resolvconf
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -14,7 +14,7 @@ import (
func Test_copySystemDNS(t *testing.T) {
require := require.New(t)
data, err := ioutil.ReadFile(dresolvconf.Path())
data, err := os.ReadFile(dresolvconf.Path())
require.NoError(err)
dest := filepath.Join(t.TempDir(), "resolv.conf")
@ -22,7 +22,7 @@ func Test_copySystemDNS(t *testing.T) {
require.NoError(copySystemDNS(dest))
require.FileExists(dest)
tmpResolv, err := ioutil.ReadFile(dest)
tmpResolv, err := os.ReadFile(dest)
require.NoError(err)
require.Equal(data, tmpResolv)
}