diff --git a/benchmark_test.go b/benchmark_test.go index 87ca1faf..d6b51f4b 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -16,7 +16,6 @@ import ( "bufio" "context" "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -24,13 +23,13 @@ import ( "github.com/sirupsen/logrus" - models "github.com/firecracker-microvm/firecracker-go-sdk/client/models" + "github.com/firecracker-microvm/firecracker-go-sdk/client/models" ) const numberOfVMs = 200 func createMachine(ctx context.Context, name string, forwardSignals []os.Signal) (*Machine, func(), error) { - dir, err := ioutil.TempDir("", name) + dir, err := os.MkdirTemp("", name) if err != nil { return nil, nil, err } @@ -50,9 +49,9 @@ func createMachine(ctx context.Context, name string, forwardSignals []os.Signal) MetricsFifo: metrics, LogLevel: "Info", MachineCfg: models.MachineConfiguration{ - VcpuCount: Int64(1), - MemSizeMib: Int64(256), - Smt: Bool(false), + VcpuCount: Int64(1), + MemSizeMib: Int64(256), + Smt: Bool(false), }, Drives: []models.Drive{ { diff --git a/examples/cmd/snapshotting/example_demo.go b/examples/cmd/snapshotting/example_demo.go index 976bf140..73690416 100644 --- a/examples/cmd/snapshotting/example_demo.go +++ b/examples/cmd/snapshotting/example_demo.go @@ -18,7 +18,6 @@ import ( "context" "errors" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -45,7 +44,7 @@ const ( ) func writeCNIConfWithHostLocalSubnet(path, networkName, subnet string) error { - return ioutil.WriteFile(path, []byte(fmt.Sprintf(`{ + return os.WriteFile(path, []byte(fmt.Sprintf(`{ "cniVersion": "0.3.1", "name": "%s", "plugins": [ @@ -60,7 +59,7 @@ func writeCNIConfWithHostLocalSubnet(path, networkName, subnet string) error { "type": "tc-redirect-tap" } ] - }`, networkName, subnet)), 0644) + }`, networkName, subnet)), 0644) } type configOpt func(*sdk.Config) @@ -111,7 +110,7 @@ func createNewConfig(socketPath string, opts ...configOpt) sdk.Config { } func connectToVM(m *sdk.Machine, sshKeyPath string) (*ssh.Client, error) { - key, err := ioutil.ReadFile(sshKeyPath) + key, err := os.ReadFile(sshKeyPath) if err != nil { return nil, err } @@ -405,7 +404,7 @@ func main() { defer os.Remove(cniConfDir) // Setup socket and snapshot + memory paths - tempdir, err := ioutil.TempDir("", "FCGoSDKSnapshotExample") + tempdir, err := os.MkdirTemp("", "FCGoSDKSnapshotExample") if err != nil { log.Fatal(err) } diff --git a/jailer.go b/jailer.go index e261fae6..208de670 100644 --- a/jailer.go +++ b/jailer.go @@ -17,7 +17,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -124,7 +123,7 @@ func NewJailerCommandBuilder() JailerCommandBuilder { // getNumaCpuset returns the CPU list assigned to a NUMA node func getNumaCpuset(node int) string { - if cpus, err := ioutil.ReadFile(fmt.Sprintf("/sys/devices/system/node/node%d/cpulist", node)); err == nil { + if cpus, err := os.ReadFile(fmt.Sprintf("/sys/devices/system/node/node%d/cpulist", node)); err == nil { return strings.TrimSuffix(string(cpus), "\n") } return "" diff --git a/machine_test.go b/machine_test.go index b8d7b502..ef536468 100644 --- a/machine_test.go +++ b/machine_test.go @@ -20,7 +20,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "net" "os" "os/exec" @@ -95,7 +94,7 @@ var fsSafeTestName = strings.NewReplacer("/", "_") func makeSocketPath(tb testing.TB) (string, func()) { tb.Helper() - dir, err := ioutil.TempDir("", fsSafeTestName.Replace(tb.Name())) + dir, err := os.MkdirTemp("", fsSafeTestName.Replace(tb.Name())) require.NoError(tb, err) return filepath.Join(dir, "fc.sock"), func() { os.RemoveAll(dir) } @@ -157,7 +156,7 @@ func TestJailerMicroVMExecution(t *testing.T) { // uses temp directory due to testdata's path being too long which causes a // SUN_LEN error. - tmpDir, err := ioutil.TempDir(os.TempDir(), "jailer-test") + tmpDir, err := os.MkdirTemp(os.TempDir(), "jailer-test") if err != nil { t.Fatalf("Failed to create temporary directory: %v", err) } @@ -298,7 +297,7 @@ func TestMicroVMExecution(t *testing.T) { var nCpus int64 = 2 var memSz int64 = 256 - dir, err := ioutil.TempDir("", t.Name()) + dir, err := os.MkdirTemp("", t.Name()) require.NoError(t, err) defer os.RemoveAll(dir) @@ -484,7 +483,7 @@ func TestLogAndMetrics(t *testing.T) { func testLogAndMetrics(t *testing.T, logLevel string) string { const vmID = "UserSuppliedVMID" - dir, err := ioutil.TempDir("", strings.Replace(t.Name(), "/", "_", -1)) + dir, err := os.MkdirTemp("", strings.Replace(t.Name(), "/", "_", -1)) require.NoError(t, err) defer os.RemoveAll(dir) @@ -534,7 +533,7 @@ func testLogAndMetrics(t *testing.T, logLevel string) string { require.NoError(t, err) assert.NotEqual(t, 0, log.Size()) - content, err := ioutil.ReadFile(cfg.LogPath) + content, err := os.ReadFile(cfg.LogPath) require.NoError(t, err) return string(content) } @@ -806,7 +805,7 @@ func TestStopVMMCleanup(t *testing.T) { socketPath, cleanup := makeSocketPath(t) defer cleanup() - dir, err := ioutil.TempDir("", t.Name()) + dir, err := os.MkdirTemp("", t.Name()) require.NoError(t, err) defer os.RemoveAll(dir) @@ -939,7 +938,7 @@ func TestMicroVMExecutionWithMmdsV2(t *testing.T) { var nCpus int64 = 2 var memSz int64 = 256 - dir, err := ioutil.TempDir("", t.Name()) + dir, err := os.MkdirTemp("", t.Name()) require.NoError(t, err) defer os.RemoveAll(dir) @@ -1156,7 +1155,7 @@ func TestLogFiles(t *testing.T) { } func TestCaptureFifoToFile(t *testing.T) { - dir, err := ioutil.TempDir("", t.Name()) + dir, err := os.MkdirTemp("", t.Name()) require.NoError(t, err) defer os.RemoveAll(dir) @@ -1207,13 +1206,13 @@ func TestCaptureFifoToFile(t *testing.T) { wg.Wait() _, err = os.Stat(logPath) assert.NoError(t, err, "failed to stat file") - b, err := ioutil.ReadFile(logPath) + b, err := os.ReadFile(logPath) assert.NoError(t, err, "failed to read logPath") assert.Equal(t, expectedBytes, b) } func TestCaptureFifoToFile_nonblock(t *testing.T) { - dir, err := ioutil.TempDir("", t.Name()) + dir, err := os.MkdirTemp("", t.Name()) require.NoError(t, err) defer os.RemoveAll(dir) @@ -1270,7 +1269,7 @@ func TestCaptureFifoToFile_nonblock(t *testing.T) { wg.Wait() _, err = os.Stat(logPath) assert.NoError(t, err, "failed to stat file") - b, err := ioutil.ReadFile(logPath) + b, err := os.ReadFile(logPath) assert.NoError(t, err, "failed to read logPath") assert.Equal(t, expectedBytes, b) } @@ -1335,7 +1334,7 @@ func TestPID(t *testing.T) { t.Errorf("expected an error, but received none") } - dir, err := ioutil.TempDir("", t.Name()) + dir, err := os.MkdirTemp("", t.Name()) require.NoError(t, err) defer os.RemoveAll(dir) @@ -1346,10 +1345,10 @@ func TestPID(t *testing.T) { vmlinuxPath := getVmlinuxPath(t) - rootfsBytes, err := ioutil.ReadFile(testRootfs) + rootfsBytes, err := os.ReadFile(testRootfs) require.NoError(t, err, "failed to read rootfs file") rootfsPath := filepath.Join(dir, "TestPID.img") - err = ioutil.WriteFile(rootfsPath, rootfsBytes, 0666) + err = os.WriteFile(rootfsPath, rootfsBytes, 0666) require.NoError(t, err, "failed to copy vm rootfs to %s", rootfsPath) cfg := Config{ @@ -1414,7 +1413,7 @@ func TestCaptureFifoToFile_leak(t *testing.T) { exitCh: make(chan struct{}), } - dir, err := ioutil.TempDir("", t.Name()) + dir, err := os.MkdirTemp("", t.Name()) require.NoError(t, err) defer os.RemoveAll(dir) @@ -1799,7 +1798,7 @@ func TestPauseResume(t *testing.T) { fctesting.RequiresKVM(t) fctesting.RequiresRoot(t) - dir, err := ioutil.TempDir("", t.Name()) + dir, err := os.MkdirTemp("", t.Name()) require.NoError(t, err) defer os.RemoveAll(dir) @@ -1911,7 +1910,7 @@ func TestCreateSnapshot(t *testing.T) { fctesting.RequiresKVM(t) fctesting.RequiresRoot(t) - dir, err := ioutil.TempDir("", t.Name()) + dir, err := os.MkdirTemp("", t.Name()) require.NoError(t, err) defer os.RemoveAll(dir) @@ -1975,7 +1974,7 @@ func TestCreateSnapshot(t *testing.T) { } func connectToVM(m *Machine, sshKeyPath string) (*ssh.Client, error) { - key, err := ioutil.ReadFile(sshKeyPath) + key, err := os.ReadFile(sshKeyPath) if err != nil { return nil, err } @@ -2004,7 +2003,7 @@ func connectToVM(m *Machine, sshKeyPath string) (*ssh.Client, error) { } func writeCNIConfWithHostLocalSubnet(path, networkName, subnet string) error { - return ioutil.WriteFile(path, []byte(fmt.Sprintf(`{ + return os.WriteFile(path, []byte(fmt.Sprintf(`{ "cniVersion": "0.3.1", "name": "%s", "plugins": [ @@ -2026,7 +2025,7 @@ func TestLoadSnapshot(t *testing.T) { fctesting.RequiresKVM(t) fctesting.RequiresRoot(t) - dir, err := ioutil.TempDir("", t.Name()) + dir, err := os.MkdirTemp("", t.Name()) require.NoError(t, err) defer os.RemoveAll(dir) @@ -2036,16 +2035,16 @@ func TestLoadSnapshot(t *testing.T) { cniBinPath := []string{testDataBin} - rootfsBytes, err := ioutil.ReadFile(testRootfsWithSSH) + rootfsBytes, err := os.ReadFile(testRootfsWithSSH) require.NoError(t, err) rootfsPath := filepath.Join(dir, "rootfs.img") - err = ioutil.WriteFile(rootfsPath, rootfsBytes, 0666) + err = os.WriteFile(rootfsPath, rootfsBytes, 0666) require.NoError(t, err) - sshKeyBytes, err := ioutil.ReadFile(testSSHKey) + sshKeyBytes, err := os.ReadFile(testSSHKey) require.NoError(t, err) sshKeyPath := filepath.Join(dir, "id_rsa") - err = ioutil.WriteFile(sshKeyPath, sshKeyBytes, 0600) + err = os.WriteFile(sshKeyPath, sshKeyBytes, 0600) require.NoError(t, err) // Using default cache directory to ensure collision avoidance on IP allocations diff --git a/network_test.go b/network_test.go index 346b5cf3..146dec25 100644 --- a/network_test.go +++ b/network_test.go @@ -16,7 +16,6 @@ package firecracker import ( "context" "fmt" - "io/ioutil" "net" "os" "path/filepath" @@ -26,7 +25,7 @@ import ( "time" "github.com/containernetworking/cni/libcni" - models "github.com/firecracker-microvm/firecracker-go-sdk/client/models" + "github.com/firecracker-microvm/firecracker-go-sdk/client/models" "github.com/firecracker-microvm/firecracker-go-sdk/fctesting" "github.com/go-ping/ping" "github.com/stretchr/testify/assert" @@ -256,7 +255,7 @@ func testNetworkMachineCNI(t *testing.T, useConfFile bool) { cniBinPath := []string{testDataBin, "/opt/cni/bin"} - dir, err := ioutil.TempDir("", fsSafeTestName.Replace(t.Name())) + dir, err := os.MkdirTemp("", fsSafeTestName.Replace(t.Name())) require.NoError(t, err) defer os.RemoveAll(dir) @@ -300,7 +299,7 @@ func testNetworkMachineCNI(t *testing.T, useConfFile bool) { cniConfPath := filepath.Join(cniConfDir, fmt.Sprintf("%s.conflist", networkName)) if useConfFile { require.NoError(t, - ioutil.WriteFile(cniConfPath, []byte(cniConf), 0666), // broad permissions for tests + os.WriteFile(cniConfPath, []byte(cniConf), 0666), // broad permissions for tests "failed to write cni conf file") } else { // make sure config file doesn't exist @@ -389,9 +388,9 @@ func newCNIMachine(t *testing.T, cniBinPath []string, networkConf *libcni.NetworkConfigList, ) *Machine { - rootfsBytes, err := ioutil.ReadFile(testRootfs) + rootfsBytes, err := os.ReadFile(testRootfs) require.NoError(t, err, "failed to read rootfs file") - err = ioutil.WriteFile(rootfsPath, rootfsBytes, 0666) + err = os.WriteFile(rootfsPath, rootfsBytes, 0666) require.NoError(t, err, "failed to copy vm rootfs to %s", rootfsPath) if networkConf != nil { diff --git a/vsock/dial.go b/vsock/dial.go index a3dda36a..2e1e210d 100644 --- a/vsock/dial.go +++ b/vsock/dial.go @@ -18,7 +18,7 @@ import ( "context" "errors" "fmt" - "io/ioutil" + "io" "net" "strings" "time" @@ -37,7 +37,7 @@ type config struct { func defaultConfig() config { noop := logrus.New() - noop.Out = ioutil.Discard + noop.Out = io.Discard return config{ DialTimeout: 100 * time.Millisecond,