Skip to content

Commit

Permalink
chore: Replaced deprecated methods in ioutil using io and os packages
Browse files Browse the repository at this point in the history
Signed-off-by: NanamiNakano <[email protected]>
  • Loading branch information
NanamiNakano authored and austinvazquez committed Apr 1, 2024
1 parent 53f5ca4 commit 58cd01a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 46 deletions.
11 changes: 5 additions & 6 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@ import (
"bufio"
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

"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
}
Expand All @@ -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{
{
Expand Down
9 changes: 4 additions & 5 deletions examples/cmd/snapshotting/example_demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand All @@ -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": [
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions jailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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 ""
Expand Down
49 changes: 24 additions & 25 deletions machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
Expand Down Expand Up @@ -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) }
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)

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

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

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

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

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

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

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

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

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

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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": [
Expand All @@ -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)

Expand All @@ -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
Expand Down
11 changes: 5 additions & 6 deletions network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package firecracker
import (
"context"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand All @@ -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"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions vsock/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"strings"
"time"
Expand All @@ -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,
Expand Down

0 comments on commit 58cd01a

Please sign in to comment.