Update ioutil library references to os and io respectively for e2e helper nomad (#16332)

No user facing changes so I assume no change log is required
This commit is contained in:
Lance Haig 2023-03-08 16:39:03 +01:00 committed by GitHub
parent d9e585b965
commit e89c3d3b36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 45 additions and 64 deletions

View File

@ -2,7 +2,6 @@ package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
@ -30,7 +29,7 @@ func main() {
return
}
fh, err := ioutil.TempFile("", "bench")
fh, err := os.CreateTemp("", "bench")
if err != nil {
fmt.Println(err.Error())
return

View File

@ -3,7 +3,7 @@ package clientstate
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"math/rand"
"net/http"
"os"
@ -60,7 +60,7 @@ func getPID(client *api.Client, alloc *api.Allocation, path string) (int, error)
}
defer r.Close()
out, err := ioutil.ReadAll(r)
out, err := io.ReadAll(r)
if err != nil {
return 0, err
}
@ -453,7 +453,7 @@ func (tc *ClientStateTC) TestClientState_Corrupt(f *framework.F) {
}
defer r.Close()
out, err := ioutil.ReadAll(r)
out, err := io.ReadAll(r)
if err != nil {
return false, err
}

View File

@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"regexp"
@ -232,7 +231,7 @@ func volumeRegister(volID, volFilePath, createOrRegister string) error {
return fmt.Errorf("could not open stdin?: %w", err)
}
content, err := ioutil.ReadFile(volFilePath)
content, err := os.ReadFile(volFilePath)
if err != nil {
return fmt.Errorf("could not open vol file: %w", err)
}

View File

@ -4,7 +4,7 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strconv"
@ -50,7 +50,7 @@ func execCmd(jobID, jobFilePath string, cmd *exec.Cmd) error {
return fmt.Errorf("could not open stdin?: %w", err)
}
content, err := ioutil.ReadFile(jobFilePath)
content, err := os.ReadFile(jobFilePath)
if err != nil {
return fmt.Errorf("could not open job file: %w", err)
}

View File

@ -3,7 +3,6 @@ package execagent
import (
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
@ -126,7 +125,7 @@ func NewMixedAgent(bin string) (*NomadAgent, error) {
if err := os.MkdirAll(BaseDir, 0755); err != nil {
return nil, err
}
dir, err := ioutil.TempDir(BaseDir, "agent")
dir, err := os.MkdirTemp(BaseDir, "agent")
if err != nil {
return nil, err
}
@ -161,7 +160,7 @@ func NewClientServerPair(bin string, serverOut, clientOut io.Writer) (
return nil, nil, err
}
sdir, err := ioutil.TempDir(BaseDir, "server")
sdir, err := os.MkdirTemp(BaseDir, "server")
if err != nil {
return nil, nil, err
}
@ -188,7 +187,7 @@ func NewClientServerPair(bin string, serverOut, clientOut io.Writer) (
server.Cmd.Stdout = serverOut
server.Cmd.Stderr = serverOut
cdir, err := ioutil.TempDir(BaseDir, "client")
cdir, err := os.MkdirTemp(BaseDir, "client")
if err != nil {
return nil, nil, err
}

View File

@ -3,7 +3,7 @@ package metrics
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
@ -70,7 +70,7 @@ func (tc *MetricsTest) promQuery(query string) (model.Vector, error) {
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP status: %v", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

View File

@ -7,7 +7,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
@ -233,7 +232,7 @@ func createBinDir(binDir string) error {
// missingVault returns the binaries that must be downloaded. versions key must
// be the Vault version.
func missingVault(binDir string, versions map[string]string) (map[string]string, error) {
files, err := ioutil.ReadDir(binDir)
files, err := os.ReadDir(binDir)
if err != nil {
if os.IsNotExist(err) {
return versions, nil

View File

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"regexp"
@ -220,7 +219,7 @@ func (tc *VaultSecretsTest) TestVaultSecrets(f *framework.F) {
// We need to namespace the keys in the policy, so read it in and replace the
// values of the policy names
func writePolicy(policyID, policyPath, testID string) (string, error) {
raw, err := ioutil.ReadFile(policyPath)
raw, err := os.ReadFile(policyPath)
if err != nil {
return "", err
}
@ -248,7 +247,7 @@ func writePolicy(policyID, policyPath, testID string) (string, error) {
// and replace the values of the template and vault fields
func runJob(jobID, testID string, index int) error {
raw, err := ioutil.ReadFile("./vaultsecrets/input/secrets.nomad")
raw, err := os.ReadFile("./vaultsecrets/input/secrets.nomad")
if err != nil {
return err
}

View File

@ -2,7 +2,6 @@ package escapingfs
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -11,7 +10,7 @@ import (
)
func write(t *testing.T, file, data string) {
err := ioutil.WriteFile(file, []byte(data), 0600)
err := os.WriteFile(file, []byte(data), 0600)
require.NoError(t, err)
}

View File

@ -2,7 +2,6 @@ package loader
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -1214,7 +1213,7 @@ func TestPluginLoader_External_SkipBadFiles(t *testing.T) {
require.NoError(os.Symlink(selfExe, filepath.Join(h.pluginDir(), plugins[0])))
// Create a non-executable file
require.NoError(ioutil.WriteFile(filepath.Join(h.pluginDir(), "some.yaml"), []byte("hcl > yaml"), 0666))
require.NoError(os.WriteFile(filepath.Join(h.pluginDir(), "some.yaml"), []byte("hcl > yaml"), 0666))
logger := testlog.HCLogger(t)
logger.SetLevel(log.Trace)

View File

@ -18,7 +18,6 @@ import (
"fmt"
"hash"
"io"
"io/ioutil"
"time"
"github.com/hashicorp/raft"
@ -202,7 +201,7 @@ func read(in io.Reader, metadata *raft.SnapshotMeta, snap io.Writer) error {
// turn made the snapshot verification fail. By explicitly reading the
// whole thing first we ensure that we calculate the correct hash
// independent of how json.Decode works internally.
buf, err := ioutil.ReadAll(io.TeeReader(archive, metaHash))
buf, err := io.ReadAll(io.TeeReader(archive, metaHash))
if err != nil {
return fmt.Errorf("failed to read snapshot metadata: %v", err)
}

View File

@ -5,7 +5,6 @@ import (
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"strings"
@ -75,7 +74,7 @@ func TestArchive_GoodData(t *testing.T) {
defer f.Close()
var metadata raft.SnapshotMeta
err = read(f, &metadata, ioutil.Discard)
err = read(f, &metadata, io.Discard)
if err != nil {
t.Fatalf("case %d: should've read the snapshot, but didn't: %v", i, err)
}
@ -104,7 +103,7 @@ func TestArchive_BadData(t *testing.T) {
defer f.Close()
var metadata raft.SnapshotMeta
err = read(f, &metadata, ioutil.Discard)
err = read(f, &metadata, io.Discard)
if err == nil || !strings.Contains(err.Error(), c.Error) {
t.Fatalf("case %d (%s): %v", i, c.Name, err)
}

View File

@ -9,7 +9,6 @@ import (
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/hashicorp/go-hclog"
@ -50,7 +49,7 @@ func New(logger hclog.Logger, r *raft.Raft) (*Snapshot, error) {
// Make a scratch file to receive the contents so that we don't buffer
// everything in memory. This gets deleted in Close() since we keep it
// around for re-reading.
archive, err := ioutil.TempFile("", "snapshot")
archive, err := os.CreateTemp("", "snapshot")
if err != nil {
return nil, fmt.Errorf("failed to create snapshot file: %v", err)
}
@ -180,7 +179,7 @@ func CopySnapshot(in io.Reader, dest io.WriteCloser) (*raft.SnapshotMeta, error)
// The docs for gzip.Reader say: "Clients should treat data returned by Read as
// tentative until they receive the io.EOF marking the end of the data."
func concludeGzipRead(decomp *gzip.Reader) error {
extra, err := ioutil.ReadAll(decomp) // ReadAll consumes the EOF
extra, err := io.ReadAll(decomp) // ReadAll consumes the EOF
if err != nil {
return err
} else if len(extra) != 0 {
@ -219,7 +218,7 @@ func Restore(logger hclog.Logger, in io.Reader, r *raft.Raft) error {
// Make a scratch file to receive the contents of the snapshot data so
// we can avoid buffering in memory.
snap, err := ioutil.TempFile("", "snapshot")
snap, err := os.CreateTemp("", "snapshot")
if err != nil {
return fmt.Errorf("failed to create temp snapshot file: %v", err)
}

View File

@ -4,7 +4,6 @@ package testtask
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"time"
@ -110,7 +109,7 @@ func execute() {
}
msg := popArg()
file := popArg()
ioutil.WriteFile(file, []byte(msg), 0666)
os.WriteFile(file, []byte(msg), 0666)
case "pgrp":
if len(args) < 1 {
@ -135,7 +134,7 @@ func execute() {
os.Exit(1)
}
if err := ioutil.WriteFile(pidFile, []byte(fmt.Sprintf("%d", cmd.Process.Pid)), 0777); err != nil {
if err := os.WriteFile(pidFile, []byte(fmt.Sprintf("%d", cmd.Process.Pid)), 0777); err != nil {
fmt.Fprintf(os.Stderr, "failed to write pid file: %v\n", err)
os.Exit(1)
}

View File

@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -127,7 +126,7 @@ func decode(c *jobConfig) error {
}
func parseFile(path string) (*hcl.File, hcl.Diagnostics) {
body, err := ioutil.ReadFile(path)
body, err := os.ReadFile(path)
if err != nil {
return nil, hcl.Diagnostics{
&hcl.Diagnostic{

View File

@ -1,7 +1,6 @@
package jobspec2
import (
"io/ioutil"
"os"
"strings"
"testing"
@ -19,7 +18,7 @@ func TestEquivalentToHCL1(t *testing.T) {
ci.Parallel(t)
hclSpecDir := "../jobspec/test-fixtures/"
fis, err := ioutil.ReadDir(hclSpecDir)
fis, err := os.ReadDir(hclSpecDir)
require.NoError(t, err)
for _, fi := range fis {
@ -168,7 +167,7 @@ job "example" {
})
t.Run("set via var-files", func(t *testing.T) {
varFile, err := ioutil.TempFile("", "")
varFile, err := os.CreateTemp("", "")
require.NoError(t, err)
defer os.Remove(varFile.Name())
@ -328,7 +327,7 @@ job "example" {
})
require.NoError(t, err)
expected, err := ioutil.ReadFile("parse_test.go")
expected, err := os.ReadFile("parse_test.go")
require.NoError(t, err)
require.NotNil(t, out.Region)
@ -426,7 +425,7 @@ func TestParse_InvalidHCL(t *testing.T) {
})
t.Run("invalid vars file", func(t *testing.T) {
tmp, err := ioutil.TempFile("", "nomad-jobspec2-")
tmp, err := os.CreateTemp("", "nomad-jobspec2-")
require.NoError(t, err)
defer os.Remove(tmp.Name())

View File

@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
@ -554,7 +553,7 @@ func (a *ACL) fileBootstrapResetIndex() uint64 {
path := filepath.Join(a.srv.config.DataDir, aclBootstrapReset)
// Read the file
raw, err := ioutil.ReadFile(path)
raw, err := os.ReadFile(path)
if err != nil {
if !os.IsNotExist(err) {
a.logger.Error("failed to read bootstrap file", "path", path, "error", err)

View File

@ -2,8 +2,8 @@ package nomad
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"testing"
"time"
@ -1534,7 +1534,7 @@ func TestACLEndpoint_Bootstrap_Reset(t *testing.T) {
// Create the reset file
output := []byte(fmt.Sprintf("%d", resetIdx))
path := filepath.Join(dir, aclBootstrapReset)
assert.Nil(t, ioutil.WriteFile(path, output, 0755))
assert.Nil(t, os.WriteFile(path, output, 0755))
// Try again, should work with reset
if err := msgpackrpc.CallWithCodec(codec, "ACL.Bootstrap", req, &resp); err != nil {

View File

@ -3,7 +3,6 @@ package nomad
import (
"context"
"io"
"io/ioutil"
"time"
"github.com/hashicorp/go-msgpack/codec"
@ -91,7 +90,7 @@ func (e *Event) stream(conn io.ReadWriteCloser) {
defer cancel()
// goroutine to detect remote side closing
go func() {
io.Copy(ioutil.Discard, conn)
io.Copy(io.Discard, conn)
cancel()
}()

View File

@ -5,7 +5,6 @@ import (
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path"
@ -614,7 +613,7 @@ func TestOperator_SnapshotSave(t *testing.T) {
index := resp.Index
snap, err := ioutil.TempFile("", "nomadtests-snapshot-")
snap, err := os.CreateTemp("", "nomadtests-snapshot-")
require.NoError(t, err)
defer os.Remove(snap.Name())
@ -707,7 +706,7 @@ func TestOperator_SnapshotSave_ACL(t *testing.T) {
require.NotEmpty(t, resp.SnapshotChecksum)
require.Contains(t, resp.SnapshotChecksum, "sha-256=")
io.Copy(ioutil.Discard, p1)
io.Copy(io.Discard, p1)
})
}
}
@ -968,7 +967,7 @@ func TestOperator_SnapshotRestore_ACL(t *testing.T) {
require.NotZero(t, resp.Index)
io.Copy(ioutil.Discard, p1)
io.Copy(io.Discard, p1)
})
}
}

View File

@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/rpc"
"os"
@ -1370,7 +1369,7 @@ func newTLSTestHelper(t *testing.T) tlsTestHelper {
h.caPEM, h.pk, err = tlsutil.GenerateCA(tlsutil.CAOpts{Days: 5, Domain: "nomad"})
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(h.dir, "ca.pem"), []byte(h.caPEM), 0600)
err = os.WriteFile(filepath.Join(h.dir, "ca.pem"), []byte(h.caPEM), 0600)
require.NoError(t, err)
// Generate servers and their certificate.
@ -1439,9 +1438,9 @@ func (h tlsTestHelper) newCert(t *testing.T, name string) string {
})
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(h.dir, node+"-"+name+".pem"), []byte(pem), 0600)
err = os.WriteFile(filepath.Join(h.dir, node+"-"+name+".pem"), []byte(pem), 0600)
require.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(h.dir, node+"-"+name+".key"), []byte(key), 0600)
err = os.WriteFile(filepath.Join(h.dir, node+"-"+name+".key"), []byte(key), 0600)
require.NoError(t, err)
return filepath.Join(h.dir, node+"-"+name)

View File

@ -7,7 +7,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/rpc"
"os"
@ -1361,7 +1360,7 @@ func (s *Server) setupRaft() error {
if err := s.checkRaftVersionFile(raftVersionFilePath); err != nil {
return err
}
if err := ioutil.WriteFile(raftVersionFilePath, []byte(raftVersionFileContent), 0644); err != nil {
if err := os.WriteFile(raftVersionFilePath, []byte(raftVersionFileContent), 0644); err != nil {
return fmt.Errorf("failed to write Raft version file: %v", err)
}
@ -1411,7 +1410,7 @@ func (s *Server) setupRaft() error {
peersFile := filepath.Join(path, "peers.json")
peersInfoFile := filepath.Join(path, "peers.info")
if _, err := os.Stat(peersInfoFile); os.IsNotExist(err) {
if err := ioutil.WriteFile(peersInfoFile, []byte(peersInfoContent), 0644); err != nil {
if err := os.WriteFile(peersInfoFile, []byte(peersInfoContent), 0644); err != nil {
return fmt.Errorf("failed to write peers.info file: %v", err)
}
@ -1497,7 +1496,7 @@ func (s *Server) checkRaftVersionFile(path string) error {
return nil
}
v, err := ioutil.ReadFile(path)
v, err := os.ReadFile(path)
if err != nil {
s.logger.Warn(fmt.Sprintf("unable to read Raft version file, %s", baseWarning), "error", err)
return nil

View File

@ -16,7 +16,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
@ -153,12 +152,12 @@ func NewTestServer(t testing.T, cb ServerConfigCallback) *TestServer {
t.Skipf("nomad version failed: %v", err)
}
dataDir, err := ioutil.TempDir("", "nomad")
dataDir, err := os.MkdirTemp("", "nomad")
if err != nil {
t.Fatalf("err: %s", err)
}
configFile, err := ioutil.TempFile(dataDir, "nomad")
configFile, err := os.CreateTemp(dataDir, "nomad")
if err != nil {
defer os.RemoveAll(dataDir)
t.Fatalf("err: %s", err)