Skip to content

Commit

Permalink
Remove deprecated io/util
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Kozlovic <[email protected]>
  • Loading branch information
kozlovic committed Apr 17, 2023
1 parent 6e64eeb commit f644961
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 80 deletions.
7 changes: 3 additions & 4 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -177,7 +176,7 @@ func TestLogger(t *testing.T) {
checkLogger("Unable to close logger: dummy error")

// Switch to file log
tmpDir, err := ioutil.TempDir("", "")
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatalf("Unable to create temp dir: %v", err)
}
Expand All @@ -188,7 +187,7 @@ func TestLogger(t *testing.T) {
logger.SetFileSizeLimit(1000)
// Reopen and check file content
logger.ReopenLogFile()
buf, err := ioutil.ReadFile(fname)
buf, err := os.ReadFile(fname)
if err != nil {
t.Fatalf("Error reading file: %v", err)
}
Expand All @@ -201,7 +200,7 @@ func TestLogger(t *testing.T) {
for i := 0; i < 100; i++ {
logger.Noticef(notice)
}
files, err := ioutil.ReadDir(tmpDir)
files, err := os.ReadDir(tmpDir)
if err != nil {
t.Fatalf("Unable to read temp dir: %v", err)
}
Expand Down
9 changes: 4 additions & 5 deletions server/clustering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/http"
Expand Down Expand Up @@ -46,7 +45,7 @@ import (
var defaultRaftLog string

func init() {
tmpDir, err := ioutil.TempDir("", "raft_logs_")
tmpDir, err := os.MkdirTemp("", "raft_logs_")
if err != nil {
panic("Could not create tmp dir")
}
Expand Down Expand Up @@ -3679,7 +3678,7 @@ func TestClusteringRaftLogReplayDoesNotDeleteLatestVersionOfChannel(t *testing.T
follower := removeServer(servers, leader)[0]
follower.Shutdown()
witnessFile := filepath.Join(follower.opts.FilestoreDir, channel, "deleted.txt")
if err := ioutil.WriteFile(witnessFile, []byte("if present, channel has not been deleted then recreated"), 0666); err != nil {
if err := os.WriteFile(witnessFile, []byte("if present, channel has not been deleted then recreated"), 0666); err != nil {
t.Fatalf("Error creating file: %v", err)
}
// Now restart..
Expand Down Expand Up @@ -4551,7 +4550,7 @@ func TestClusteringWithCryptoStore(t *testing.T) {

check := func(t *testing.T, name, fname string) {
t.Helper()
content, err := ioutil.ReadFile(fname)
content, err := os.ReadFile(fname)
if err != nil {
t.Fatalf("Error reading file %q: %v", fname1, err)
}
Expand Down Expand Up @@ -6770,7 +6769,7 @@ func TestClusteringRestoreSnapshotMsgsBailIfNoLeader(t *testing.T) {
return nil
})

snaps, err := ioutil.ReadDir(filepath.Join(defaultRaftLog, "c", clusterName, "snapshots"))
snaps, err := os.ReadDir(filepath.Join(defaultRaftLog, "c", clusterName, "snapshots"))
if err != nil {
t.Fatalf("Error reading snapshots directory: %v", err)
}
Expand Down
23 changes: 11 additions & 12 deletions server/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package server
import (
"bytes"
"flag"
"io/ioutil"
"os"
"reflect"
"runtime"
Expand Down Expand Up @@ -314,11 +313,11 @@ func TestParsePermError(t *testing.T) {
if runtime.GOOS == "windows" {
t.SkipNow()
}
tmpDir, err := ioutil.TempDir("", "streaming")
tmpDir, err := os.MkdirTemp("", "streaming")
if err != nil {
t.Fatalf("Could not create tmp dir: %v", err)
}
file, err := ioutil.TempFile(tmpDir, "config.conf")
file, err := os.CreateTemp(tmpDir, "config.conf")
if err != nil {
t.Fatalf("Could not create tmp file: %v", err)
}
Expand All @@ -339,7 +338,7 @@ func TestParsePermError(t *testing.T) {

func TestParseParserError(t *testing.T) {
confFile := "wrong_config.conf"
if err := ioutil.WriteFile(confFile, []byte("x=."), 0660); err != nil {
if err := os.WriteFile(confFile, []byte("x=."), 0660); err != nil {
t.Fatalf("Unexpected error creating conf file: %v", err)
}
defer os.Remove(confFile)
Expand All @@ -351,7 +350,7 @@ func TestParseParserError(t *testing.T) {

func TestParseStoreType(t *testing.T) {
confFile := "wrong_config.conf"
if err := ioutil.WriteFile(confFile, []byte("store=memory"), 0660); err != nil {
if err := os.WriteFile(confFile, []byte("store=memory"), 0660); err != nil {
t.Fatalf("Unexpected error creating conf file: %v", err)
}
defer os.Remove(confFile)
Expand All @@ -364,7 +363,7 @@ func TestParseStoreType(t *testing.T) {
}
os.Remove(confFile)

if err := ioutil.WriteFile(confFile, []byte("store=xyz"), 0660); err != nil {
if err := os.WriteFile(confFile, []byte("store=xyz"), 0660); err != nil {
t.Fatalf("Unexpected error creating conf file: %v", err)
}
defer os.Remove(confFile)
Expand All @@ -380,7 +379,7 @@ func TestParseStoreType(t *testing.T) {
stores.TypeSQL,
}
for _, gs := range goodStores {
if err := ioutil.WriteFile(confFile, []byte("store="+gs), 0660); err != nil {
if err := os.WriteFile(confFile, []byte("store="+gs), 0660); err != nil {
t.Fatalf("Unexpected error creating conf file: %v", err)
}
defer os.Remove(confFile)
Expand All @@ -398,7 +397,7 @@ func TestParseStoreType(t *testing.T) {
func TestParsePerChannelLimitsSetToZero(t *testing.T) {
confFile := "config.conf"
defer os.Remove(confFile)
if err := ioutil.WriteFile(confFile,
if err := os.WriteFile(confFile,
[]byte("store_limits: {channels: {foo: {max_msgs: 0, max_bytes: 0, max_age: \"0\", max_subs: 0, max_inactivity: \"0\"}}}"), 0660); err != nil {
t.Fatalf("Unexpected error creating conf file: %v", err)
}
Expand Down Expand Up @@ -532,7 +531,7 @@ func TestParseWrongTypes(t *testing.T) {

func expectFailureFor(t *testing.T, content, errorMatch string) {
confFile := "wrong_config.conf"
if err := ioutil.WriteFile(confFile, []byte(content), 0660); err != nil {
if err := os.WriteFile(confFile, []byte(content), 0660); err != nil {
t.Fatalf("Unexpected error creating conf file: %v", err)
}
defer os.Remove(confFile)
Expand Down Expand Up @@ -637,15 +636,15 @@ func TestParseConfigureOptions(t *testing.T) {
streaming: {
cluster_id: my_cluster
}`)
if err := ioutil.WriteFile(sconf, scontent, 0660); err != nil {
if err := os.WriteFile(sconf, scontent, 0660); err != nil {
t.Fatalf("Error creating conf file: %v", err)
}
ncontent := []byte(`
port: 5223
streaming: {
cluster_id: my_cluster_2
}`)
if err := ioutil.WriteFile(nconf, ncontent, 0660); err != nil {
if err := os.WriteFile(nconf, ncontent, 0660); err != nil {
t.Fatalf("Error creating conf file: %v", err)
}
sopts, nopts := mustNotFail([]string{"-sc", sconf, "-c", nconf})
Expand Down Expand Up @@ -700,7 +699,7 @@ func TestParseConfigureOptions(t *testing.T) {
} else {
ncontent = []byte(`logtime: true`)
}
if err := ioutil.WriteFile(nconf, ncontent, 0660); err != nil {
if err := os.WriteFile(nconf, ncontent, 0660); err != nil {
t.Fatalf("Error creating conf file: %v", err)
}
_, nopts = mustNotFail([]string{"-c", nconf})
Expand Down
9 changes: 5 additions & 4 deletions server/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"math/rand"
"net/http"
"os"
"reflect"
"runtime"
"strings"
Expand Down Expand Up @@ -81,7 +82,7 @@ func getBodyEx(t *testing.T, client *http.Client, scheme, endpoint string, mp, e
if ct != expectedContentType {
stackFatalf(t, "Expected %s content-type, got %s\n", expectedContentType, ct)
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
resp.Body.Close()
stackFatalf(t, "Got an error reading the body: %v\n", err)
Expand Down Expand Up @@ -155,7 +156,7 @@ func TestMonitorStartOwnHTTPSServer(t *testing.T) {
defer s.Shutdown()

tlsConfig := &tls.Config{}
caCert, err := ioutil.ReadFile("../test/certs/ca.pem")
caCert, err := os.ReadFile("../test/certs/ca.pem")
if err != nil {
t.Fatalf("Got error reading RootCA file: %s", err)
}
Expand Down Expand Up @@ -1494,7 +1495,7 @@ func TestMonitorNoPanicOnServerRestart(t *testing.T) {
if err != nil {
continue
}
ioutil.ReadAll(resp.Body)
io.ReadAll(resp.Body)
resp.Body.Close()
select {
case <-done:
Expand Down
9 changes: 4 additions & 5 deletions server/server_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"crypto/tls"
"flag"
"fmt"
"io/ioutil"
"os"
"reflect"
"runtime"
Expand Down Expand Up @@ -1283,12 +1282,12 @@ func TestStreamingServerReadyLog(t *testing.T) {
}

func TestReopenLogFileStopsNATSDebugTrace(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "nats-streaming-server")
tmpDir, err := os.MkdirTemp("", "nats-streaming-server")
if err != nil {
t.Fatal("Could not create tmp dir")
}
defer os.RemoveAll(tmpDir)
file, err := ioutil.TempFile(tmpDir, "log_")
file, err := os.CreateTemp(tmpDir, "log_")
if err != nil {
t.Fatalf("Could not create the temp file: %v", err)
}
Expand All @@ -1311,7 +1310,7 @@ func TestReopenLogFileStopsNATSDebugTrace(t *testing.T) {

check := func(str string, expected bool) {
t.Helper()
buf, err := ioutil.ReadFile(nOpts.LogFile)
buf, err := os.ReadFile(nOpts.LogFile)
if err != nil {
t.Fatalf("Error reading file: %v", err)
}
Expand Down Expand Up @@ -1352,7 +1351,7 @@ func TestReopenLogFileStopsNATSDebugTrace(t *testing.T) {
s.log.Noticef(pstr)
}
// Check that size limit has been applied.
files, err := ioutil.ReadDir(tmpDir)
files, err := os.ReadDir(tmpDir)
if err != nil {
t.Fatalf("Error reading directory: %v", err)
}
Expand Down
9 changes: 4 additions & 5 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"reflect"
"regexp"
Expand Down Expand Up @@ -162,7 +161,7 @@ func TestMain(m *testing.M) {
}

func init() {
tmpDir, err := ioutil.TempDir(".", "data_server_")
tmpDir, err := os.MkdirTemp(".", "data_server_")
if err != nil {
panic("Could not create tmp dir")
}
Expand Down Expand Up @@ -409,13 +408,13 @@ func createConnectionWithNatsOpts(t tLogger, clientName string,

func createConfFile(t *testing.T, content []byte) string {
t.Helper()
conf, err := ioutil.TempFile("", "")
conf, err := os.CreateTemp("", "")
if err != nil {
t.Fatalf("Error creating conf file: %v", err)
}
fName := conf.Name()
conf.Close()
if err := ioutil.WriteFile(fName, content, 0666); err != nil {
if err := os.WriteFile(fName, content, 0666); err != nil {
os.Remove(fName)
t.Fatalf("Error writing conf file: %v", err)
}
Expand All @@ -424,7 +423,7 @@ func createConfFile(t *testing.T, content []byte) string {

func changeCurrentConfigContentWithNewContent(t *testing.T, curConfig string, content []byte) {
t.Helper()
if err := ioutil.WriteFile(curConfig, content, 0666); err != nil {
if err := os.WriteFile(curConfig, content, 0666); err != nil {
t.Fatalf("Error writing config: %v", err)
}
}
Expand Down
11 changes: 5 additions & 6 deletions server/signal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package server
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -77,7 +76,7 @@ func TestSignalToReOpenLogFile(t *testing.T) {
// Add a trace
expectedStr := "This is a Notice"
s.log.Noticef(expectedStr)
buf, err := ioutil.ReadFile(logFile)
buf, err := os.ReadFile(logFile)
if err != nil {
t.Fatalf("Error reading file: %v", err)
}
Expand All @@ -92,7 +91,7 @@ func TestSignalToReOpenLogFile(t *testing.T) {
syscall.Kill(syscall.Getpid(), syscall.SIGUSR1)
// Wait a bit for action to be performed
waitFor(t, 2*time.Second, 15*time.Millisecond, func() error {
buf, err = ioutil.ReadFile(logFile)
buf, err = os.ReadFile(logFile)
if err != nil {
return fmt.Errorf("Error reading file: %v", err)
}
Expand All @@ -105,7 +104,7 @@ func TestSignalToReOpenLogFile(t *testing.T) {
// Make sure that new traces are added
expectedStr = "This is a new notice"
s.log.Noticef(expectedStr)
buf, err = ioutil.ReadFile(logFile)
buf, err = os.ReadFile(logFile)
if err != nil {
t.Fatalf("Error reading file: %v", err)
}
Expand Down Expand Up @@ -208,7 +207,7 @@ func TestNATSServerConfigReloadFollowedByReopen(t *testing.T) {
cmd.Start()
// Wait for it to print some startup trace
waitFor(t, 2*time.Second, 50*time.Millisecond, func() error {
content, err := ioutil.ReadFile(lfile)
content, err := os.ReadFile(lfile)
if err != nil {
return err
}
Expand All @@ -230,7 +229,7 @@ func TestNATSServerConfigReloadFollowedByReopen(t *testing.T) {
c.Close()
syscall.Kill(cmd.Process.Pid, syscall.SIGINT)
cmd.Wait()
content, err := ioutil.ReadFile(lfile)
content, err := os.ReadFile(lfile)
if err != nil {
t.Fatalf("Error reading log file: %v", err)
}
Expand Down
7 changes: 3 additions & 4 deletions stores/cryptostore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package stores
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -240,7 +239,7 @@ func TestCryptoStoreUseEnvKey(t *testing.T) {

cs.Close()

datContent, err := ioutil.ReadFile(datFile)
datContent, err := os.ReadFile(datFile)
if err != nil {
t.Fatalf("Error reading file: %v", err)
}
Expand Down Expand Up @@ -399,7 +398,7 @@ func TestCryptoStoreCheckEncryptedStore(t *testing.T) {

s.Close()

datContent, err := ioutil.ReadFile(datFile)
datContent, err := os.ReadFile(datFile)
if err != nil {
t.Fatalf("Error reading file: %v", err)
}
Expand All @@ -411,7 +410,7 @@ func TestCryptoStoreCheckEncryptedStore(t *testing.T) {
}
// Only message store is encrypted, so subscription file
// should be in plain text.
subContent, err := ioutil.ReadFile(subFile)
subContent, err := os.ReadFile(subFile)
if err != nil {
t.Fatalf("Error reading file: %v", err)
}
Expand Down
Loading

0 comments on commit f644961

Please sign in to comment.