From 9f19ac2e5fd3381313dbd92b3ff31366266efff6 Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Mon, 18 Sep 2023 13:23:22 +0800 Subject: [PATCH] chore: remove refs to deprecated io/ioutil Signed-off-by: guoguangwu --- modules/docker/build.go | 3 +-- modules/files/files.go | 15 +++++++-------- modules/helm/format_test.go | 3 +-- modules/http-helper/http_helper.go | 5 ++--- modules/http-helper/http_helper_test.go | 12 ++++++------ modules/k8s/config.go | 3 +-- modules/k8s/kubectl.go | 3 +-- modules/k8s/tunnel.go | 3 +-- modules/logger/parser/store_test.go | 7 +++---- modules/opa/download_policy.go | 3 +-- modules/opa/download_policy_test.go | 5 ++--- modules/packer/packer.go | 3 +-- modules/ssh/agent.go | 3 +-- modules/terraform/opa_check.go | 7 +++---- modules/terraform/plan.go | 3 +-- modules/terraform/var-file.go | 4 ++-- modules/terraform/var-file_test.go | 3 +-- modules/test-structure/save_test_data.go | 7 +++---- modules/test-structure/save_test_data_test.go | 4 ++-- test/packer_basic_example_test.go | 3 +-- test/terraform_remote_exec_example_test.go | 3 +-- test/terraform_scp_example_test.go | 7 +++---- 22 files changed, 45 insertions(+), 64 deletions(-) diff --git a/modules/docker/build.go b/modules/docker/build.go index 3ffb1ebfd..82c29508e 100644 --- a/modules/docker/build.go +++ b/modules/docker/build.go @@ -1,7 +1,6 @@ package docker import ( - "io/ioutil" "os" "path/filepath" "strings" @@ -139,7 +138,7 @@ func GitCloneAndBuildE( path string, dockerBuildOpts *BuildOptions, ) error { - workingDir, err := ioutil.TempDir("", "") + workingDir, err := os.MkdirTemp("", "") if err != nil { return err } diff --git a/modules/files/files.go b/modules/files/files.go index 2eed76e4e..c4b58b372 100644 --- a/modules/files/files.go +++ b/modules/files/files.go @@ -3,7 +3,6 @@ package files import ( "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -110,7 +109,7 @@ func CopyFolderToDest(folderPath string, destRootFolder string, tempFolderPrefix return "", DirNotFoundError{Directory: folderPath} } - tmpDir, err := ioutil.TempDir(destRootFolder, tempFolderPrefix) + tmpDir, err := os.MkdirTemp(destRootFolder, tempFolderPrefix) if err != nil { return "", err } @@ -149,7 +148,7 @@ func CopyFolderContents(source string, destination string) error { // CopyFolderContentsWithFilter copies the files and folders within the given source folder that pass the given filter (return true) to the // destination folder. func CopyFolderContentsWithFilter(source string, destination string, filter func(path string) bool) error { - files, err := ioutil.ReadDir(source) + files, err := os.ReadDir(source) if err != nil { return err } @@ -157,11 +156,11 @@ func CopyFolderContentsWithFilter(source string, destination string, filter func for _, file := range files { src := filepath.Join(source, file.Name()) dest := filepath.Join(destination, file.Name()) - + f, _ := file.Info() if !filter(src) { continue } else if file.IsDir() { - if err := os.MkdirAll(dest, file.Mode()); err != nil { + if err := os.MkdirAll(dest, f.Mode()); err != nil { return err } @@ -169,7 +168,7 @@ func CopyFolderContentsWithFilter(source string, destination string, filter func return err } - } else if isSymLink(file) { + } else if isSymLink(f) { if err := copySymLink(src, dest); err != nil { return err } @@ -218,7 +217,7 @@ func PathIsTerraformLockFile(path string) bool { // CopyFile copies a file from source to destination. func CopyFile(source string, destination string) error { - contents, err := ioutil.ReadFile(source) + contents, err := os.ReadFile(source) if err != nil { return err } @@ -233,7 +232,7 @@ func WriteFileWithSamePermissions(source string, destination string, contents [] return err } - return ioutil.WriteFile(destination, contents, fileInfo.Mode()) + return os.WriteFile(destination, contents, fileInfo.Mode()) } // isSymLink returns true if the given file is a symbolic link diff --git a/modules/helm/format_test.go b/modules/helm/format_test.go index ce03e6454..0f97f79d4 100644 --- a/modules/helm/format_test.go +++ b/modules/helm/format_test.go @@ -2,7 +2,6 @@ package helm import ( "fmt" - "io/ioutil" "os" "path/filepath" "testing" @@ -189,7 +188,7 @@ func TestFormatValuesFilesAsArgs(t *testing.T) { func createTempFiles(numFiles int) ([]string, error) { paths := []string{} for i := 0; i < numFiles; i++ { - tmpFile, err := ioutil.TempFile("", "") + tmpFile, err := os.CreateTemp("", "") defer tmpFile.Close() // We don't use require or t.Fatal here so that we give a chance to delete any temp files that were created // before this error diff --git a/modules/http-helper/http_helper.go b/modules/http-helper/http_helper.go index 7d9e1361a..06efc2ce1 100644 --- a/modules/http-helper/http_helper.go +++ b/modules/http-helper/http_helper.go @@ -6,7 +6,6 @@ import ( "crypto/tls" "fmt" "io" - "io/ioutil" "net/http" "strings" "time" @@ -75,7 +74,7 @@ func HttpGetWithOptionsE(t testing.TestingT, options HttpGetOptions) (int, strin } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return -1, "", err @@ -280,7 +279,7 @@ func HTTPDoWithOptionsE( } defer resp.Body.Close() - respBody, err := ioutil.ReadAll(resp.Body) + respBody, err := io.ReadAll(resp.Body) if err != nil { return -1, "", err diff --git a/modules/http-helper/http_helper_test.go b/modules/http-helper/http_helper_test.go index 9df2c212c..8fbc2d2c4 100644 --- a/modules/http-helper/http_helper_test.go +++ b/modules/http-helper/http_helper_test.go @@ -3,7 +3,7 @@ package http_helper import ( "bytes" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "regexp" @@ -141,7 +141,7 @@ func TestErrorWithRetry(t *testing.T) { func bodyCopyHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) - body, _ := ioutil.ReadAll(r.Body) + body, _ := io.ReadAll(r.Body) w.Write(body) } @@ -168,10 +168,10 @@ func retryHandler(w http.ResponseWriter, r *http.Request) { if counter > 0 { counter-- w.WriteHeader(http.StatusServiceUnavailable) - ioutil.ReadAll(r.Body) + io.ReadAll(r.Body) } else { w.WriteHeader(http.StatusOK) - bytes, _ := ioutil.ReadAll(r.Body) + bytes, _ := io.ReadAll(r.Body) w.Write(bytes) } } @@ -182,10 +182,10 @@ func failRetryHandler(w http.ResponseWriter, r *http.Request) { if failCounter > 0 { failCounter-- w.WriteHeader(http.StatusServiceUnavailable) - ioutil.ReadAll(r.Body) + io.ReadAll(r.Body) } else { w.WriteHeader(http.StatusOK) - bytes, _ := ioutil.ReadAll(r.Body) + bytes, _ := io.ReadAll(r.Body) w.Write(bytes) } } diff --git a/modules/k8s/config.go b/modules/k8s/config.go index 66d377f6a..248e4aa82 100644 --- a/modules/k8s/config.go +++ b/modules/k8s/config.go @@ -2,7 +2,6 @@ package k8s import ( "errors" - "io/ioutil" "os" "path/filepath" "sort" @@ -167,7 +166,7 @@ func CopyHomeKubeConfigToTempE(t testing.TestingT) (string, error) { if err != nil { return "", err } - tmpConfig, err := ioutil.TempFile("", "") + tmpConfig, err := os.CreateTemp("", "") if err != nil { return "", gwErrors.WithStackTrace(err) } diff --git a/modules/k8s/kubectl.go b/modules/k8s/kubectl.go index 9ed5601e2..a2647017b 100644 --- a/modules/k8s/kubectl.go +++ b/modules/k8s/kubectl.go @@ -1,7 +1,6 @@ package k8s import ( - "io/ioutil" "net/url" "os" @@ -135,7 +134,7 @@ func StoreConfigToTempFile(t testing.TestingT, configData string) string { // filename, or error. func StoreConfigToTempFileE(t testing.TestingT, configData string) (string, error) { escapedTestName := url.PathEscape(t.Name()) - tmpfile, err := ioutil.TempFile("", escapedTestName) + tmpfile, err := os.CreateTemp("", escapedTestName) if err != nil { return "", err } diff --git a/modules/k8s/tunnel.go b/modules/k8s/tunnel.go index 0b5a550c4..49c017aa8 100644 --- a/modules/k8s/tunnel.go +++ b/modules/k8s/tunnel.go @@ -8,7 +8,6 @@ package k8s import ( "fmt" "io" - "io/ioutil" "net" "net/http" "strconv" @@ -92,7 +91,7 @@ func NewTunnelWithLogger( logger logger.TestLogger, ) *Tunnel { return &Tunnel{ - out: ioutil.Discard, + out: io.Discard, localPort: local, remotePort: remote, kubectlOptions: kubectlOptions, diff --git a/modules/logger/parser/store_test.go b/modules/logger/parser/store_test.go index 9c21a7edb..fe008f433 100644 --- a/modules/logger/parser/store_test.go +++ b/modules/logger/parser/store_test.go @@ -1,7 +1,6 @@ package parser import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -153,10 +152,10 @@ func TestWriteLogWritesToCorrectLogFile(t *testing.T) { err = logWriter.writeLog(logger, alternativeTestName, alternativeRandomString) assert.Nil(t, err) - buf, err := ioutil.ReadFile(testFileName) + buf, err := os.ReadFile(testFileName) assert.Nil(t, err) assert.Equal(t, string(buf), randomString+"\n") - buf, err = ioutil.ReadFile(alternativeTestFileName) + buf, err = os.ReadFile(alternativeTestFileName) assert.Nil(t, err) assert.Equal(t, string(buf), alternativeRandomString+"\n") } @@ -175,7 +174,7 @@ func TestWriteLogCreatesLogFileIfNotExists(t *testing.T) { assert.Nil(t, err) assert.True(t, files.FileExists(testFileName)) - buf, err := ioutil.ReadFile(testFileName) + buf, err := os.ReadFile(testFileName) assert.Nil(t, err) assert.Equal(t, string(buf), randomString+"\n") } diff --git a/modules/opa/download_policy.go b/modules/opa/download_policy.go index c06d8ff9a..a232ea3df 100644 --- a/modules/opa/download_policy.go +++ b/modules/opa/download_policy.go @@ -1,7 +1,6 @@ package opa import ( - "io/ioutil" "os" "path/filepath" "strings" @@ -58,7 +57,7 @@ func DownloadPolicyE(t testing.TestingT, rulePath string) (string, error) { } // Not downloaded, so use go-getter to download the remote source to a temp dir. - tempDir, err := ioutil.TempDir("", "terratest-opa-policy-*") + tempDir, err := os.MkdirTemp("", "terratest-opa-policy-*") if err != nil { return "", err } diff --git a/modules/opa/download_policy_test.go b/modules/opa/download_policy_test.go index eb0430c59..7958b46d0 100644 --- a/modules/opa/download_policy_test.go +++ b/modules/opa/download_policy_test.go @@ -2,7 +2,6 @@ package opa import ( "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -53,9 +52,9 @@ func TestDownloadPolicyDownloadsRemote(t *testing.T) { require.NoError(t, err) assert.NotEqual(t, absPath, path) - localContents, err := ioutil.ReadFile(localPath) + localContents, err := os.ReadFile(localPath) require.NoError(t, err) - remoteContents, err := ioutil.ReadFile(path) + remoteContents, err := os.ReadFile(path) require.NoError(t, err) assert.Equal(t, localContents, remoteContents) } diff --git a/modules/packer/packer.go b/modules/packer/packer.go index 8f11b4c4a..4045c0f99 100644 --- a/modules/packer/packer.go +++ b/modules/packer/packer.go @@ -4,7 +4,6 @@ package packer import ( "errors" "fmt" - "io/ioutil" "os" "path/filepath" "regexp" @@ -106,7 +105,7 @@ func BuildArtifactE(t testing.TestingT, options *Options) (string, error) { // The built-in env variable defining where plugins are downloaded const packerPluginPathEnvVar = "PACKER_PLUGIN_PATH" options.Logger.Logf(t, "Creating a temporary directory for Packer plugins") - pluginDir, err := ioutil.TempDir("", "terratest-packer-") + pluginDir, err := os.MkdirTemp("", "terratest-packer-") require.NoError(t, err) if len(options.Env) == 0 { options.Env = make(map[string]string) diff --git a/modules/ssh/agent.go b/modules/ssh/agent.go index ae2bb6a39..fcc1f6a0b 100644 --- a/modules/ssh/agent.go +++ b/modules/ssh/agent.go @@ -4,7 +4,6 @@ import ( "crypto/x509" "encoding/pem" "io" - "io/ioutil" "net" "os" "path/filepath" @@ -115,7 +114,7 @@ func SshAgentWithKeyPairsE(t testing.TestingT, keyPairs []*KeyPair) (*SshAgent, logger.Logf(t, "Generating SSH Agent with given KeyPair(s)") // Instantiate a temporary SSH agent - socketDir, err := ioutil.TempDir("", "ssh-agent-") + socketDir, err := os.MkdirTemp("", "ssh-agent-") if err != nil { return nil, err } diff --git a/modules/terraform/opa_check.go b/modules/terraform/opa_check.go index d5afd126d..cd7791ca1 100644 --- a/modules/terraform/opa_check.go +++ b/modules/terraform/opa_check.go @@ -1,7 +1,6 @@ package terraform import ( - "io/ioutil" "os" "path/filepath" "strings" @@ -45,7 +44,7 @@ func OPAEvalE( } // Create a temporary dir to store all the json files - tmpDir, err := ioutil.TempDir("", "terratest-opa-hcl2json-*") + tmpDir, err := os.MkdirTemp("", "terratest-opa-hcl2json-*") if err != nil { return err } @@ -78,7 +77,7 @@ func OPAEvalE( // HCLFileToJSONFile is a function that takes a path containing HCL code, and converts it to JSON representation and // writes out the contents to the given path. func HCLFileToJSONFile(hclPath, jsonOutPath string) error { - fileBytes, err := ioutil.ReadFile(hclPath) + fileBytes, err := os.ReadFile(hclPath) if err != nil { return err } @@ -86,5 +85,5 @@ func HCLFileToJSONFile(hclPath, jsonOutPath string) error { if err != nil { return err } - return ioutil.WriteFile(jsonOutPath, converted, 0600) + return os.WriteFile(jsonOutPath, converted, 0600) } diff --git a/modules/terraform/plan.go b/modules/terraform/plan.go index 4eb18f1ce..c5ea13442 100644 --- a/modules/terraform/plan.go +++ b/modules/terraform/plan.go @@ -2,7 +2,6 @@ package terraform import ( "fmt" - "io/ioutil" "os" "github.com/gruntwork-io/terratest/modules/logger" @@ -70,7 +69,7 @@ func InitAndPlanAndShowWithStructNoLogTempPlanFile(t testing.TestingT, options * options.Logger = logger.Discard defer func() { options.Logger = oldLogger }() - tmpFile, err := ioutil.TempFile("", "terratest-plan-file-") + tmpFile, err := os.CreateTemp("", "terratest-plan-file-") require.NoError(t, err) require.NoError(t, tmpFile.Close()) defer require.NoError(t, os.Remove(tmpFile.Name())) diff --git a/modules/terraform/var-file.go b/modules/terraform/var-file.go index 09f3a2d3b..ab5413e6d 100644 --- a/modules/terraform/var-file.go +++ b/modules/terraform/var-file.go @@ -3,7 +3,7 @@ package terraform import ( "encoding/json" "fmt" - "io/ioutil" + "os" "reflect" "strings" @@ -127,7 +127,7 @@ func GetAllVariablesFromVarFile(t testing.TestingT, fileName string, out interfa // the value pointed to by out. Returns an error if the specified file does not exist, the specified file is not // readable, or the specified file cannot be decoded from HCL. func GetAllVariablesFromVarFileE(t testing.TestingT, fileName string, out interface{}) error { - fileContents, err := ioutil.ReadFile(fileName) + fileContents, err := os.ReadFile(fileName) if err != nil { return err } diff --git a/modules/terraform/var-file_test.go b/modules/terraform/var-file_test.go index 3fca42147..c9e748af8 100644 --- a/modules/terraform/var-file_test.go +++ b/modules/terraform/var-file_test.go @@ -2,7 +2,6 @@ package terraform import ( "fmt" - "io/ioutil" "os" "testing" @@ -470,7 +469,7 @@ func TestGetAllVariablesFromVarFileStructOutJSON(t *testing.T) { // Helper function to write a file to the filesystem // Will immediately fail the test if it could not write the file func WriteFile(t *testing.T, fileName string, bytes []byte) { - err := ioutil.WriteFile(fileName, bytes, 0644) + err := os.WriteFile(fileName, bytes, 0644) require.NoError(t, err) } diff --git a/modules/test-structure/save_test_data.go b/modules/test-structure/save_test_data.go index 02005b9a9..6c13a1eeb 100644 --- a/modules/test-structure/save_test_data.go +++ b/modules/test-structure/save_test_data.go @@ -3,7 +3,6 @@ package test_structure import ( "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" @@ -217,7 +216,7 @@ func SaveTestData(t testing.TestingT, path string, overwrite bool, value interfa t.Fatalf("Failed to create folder %s: %v", parentDir, err) } - if err := ioutil.WriteFile(path, bytes, 0644); err != nil { + if err := os.WriteFile(path, bytes, 0644); err != nil { t.Fatalf("Failed to save value %s: %v", path, err) } } @@ -228,7 +227,7 @@ func SaveTestData(t testing.TestingT, path string, overwrite bool, value interfa func LoadTestData(t testing.TestingT, path string, value interface{}) { logger.Logf(t, "Loading test data from %s", path) - bytes, err := ioutil.ReadFile(path) + bytes, err := os.ReadFile(path) if err != nil { t.Fatalf("Failed to load value from %s: %v", path, err) } @@ -248,7 +247,7 @@ func IsTestDataPresent(t testing.TestingT, path string) bool { return false } - bytes, err := ioutil.ReadFile(path) + bytes, err := os.ReadFile(path) if err != nil { t.Fatalf("Failed to load test data from %s due to unexpected error: %v", path, err) diff --git a/modules/test-structure/save_test_data_test.go b/modules/test-structure/save_test_data_test.go index 7f2d8120e..5e93ca0b6 100644 --- a/modules/test-structure/save_test_data_test.go +++ b/modules/test-structure/save_test_data_test.go @@ -1,7 +1,7 @@ package test_structure import ( - "io/ioutil" + "os" "testing" "github.com/gruntwork-io/terratest/modules/files" @@ -22,7 +22,7 @@ func TestSaveAndLoadTestData(t *testing.T) { isTestDataPresent := IsTestDataPresent(t, "/file/that/does/not/exist") assert.False(t, isTestDataPresent, "Expected no test data would be present because no test data file exists.") - tmpFile, err := ioutil.TempFile("", "save-and-load-test-data") + tmpFile, err := os.CreateTemp("", "save-and-load-test-data") if err != nil { t.Fatalf("Failed to create temp dir: %v", err) } diff --git a/test/packer_basic_example_test.go b/test/packer_basic_example_test.go index cf501a37e..c4f4060a9 100644 --- a/test/packer_basic_example_test.go +++ b/test/packer_basic_example_test.go @@ -2,7 +2,6 @@ package test import ( "fmt" - "io/ioutil" "os" "testing" "time" @@ -95,7 +94,7 @@ func TestPackerBasicExampleWithVarFile(t *testing.T) { instanceType := terratest_aws.GetRecommendedInstanceType(t, awsRegion, []string{"t2.micro, t3.micro", "t2.small", "t3.small"}) // Create temporary packer variable file to store aws region - varFile, err := ioutil.TempFile("", "*.json") + varFile, err := os.CreateTemp("", "*.json") require.NoError(t, err, "Did not expect temp file creation to cause error") // Be sure to clean up temp file diff --git a/test/terraform_remote_exec_example_test.go b/test/terraform_remote_exec_example_test.go index b99a871ca..b9fd7d552 100644 --- a/test/terraform_remote_exec_example_test.go +++ b/test/terraform_remote_exec_example_test.go @@ -2,7 +2,6 @@ package test import ( "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -106,7 +105,7 @@ func TestTerraformRemoteExecExample(t *testing.T) { assert.FileExists(t, testFile) // Check that public IP from output matches public IP generated by script on the server - b, err := ioutil.ReadFile(testFile) + b, err := os.ReadFile(testFile) if err != nil { fmt.Print(err) } diff --git a/test/terraform_scp_example_test.go b/test/terraform_scp_example_test.go index c6bdc7a86..4f9049ac1 100644 --- a/test/terraform_scp_example_test.go +++ b/test/terraform_scp_example_test.go @@ -2,7 +2,6 @@ package test import ( "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -165,7 +164,7 @@ func testScpDirFromHost(t *testing.T, terraformOptions *terraform.Options, keyPa expectedNumFiles := testCase.expectedFiles - fileInfos, err := ioutil.ReadDir(testCase.options.LocalDir) + fileInfos, err := os.ReadDir(testCase.options.LocalDir) if err != nil { t.Fatalf("Error reading from local dir: %s, due to: %s", testCase.options.LocalDir, err.Error()) @@ -215,7 +214,7 @@ func testScpFromHost(t *testing.T, terraformOptions *terraform.Options, keyPair ssh.ScpFileFromE(t, publicHost, remoteTempFilePath, localFile, false) - buf, err := ioutil.ReadFile(localTempFileName) + buf, err := os.ReadFile(localTempFileName) if err != nil { t.Fatalf("Error: Unable to read local file from disk: %s", err.Error()) @@ -267,7 +266,7 @@ func testScpFromAsg(t *testing.T, terraformOptions *terraform.Options, keyPair * defer os.RemoveAll(localDestinationDirectory) //Read the locally copied syslog - buf, err := ioutil.ReadFile(localSyslogLocation) + buf, err := os.ReadFile(localSyslogLocation) if err != nil { t.Fatalf("Error: Unable to read local file from disk: %s", err.Error())