From d6ff3e7be52cd012101b5482e32d1c75c702f315 Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Tue, 26 Sep 2023 11:54:13 +0800 Subject: [PATCH 1/2] chore: remove refs to deprecated io/ioutil Signed-off-by: guoguangwu --- cmd/rebase/main.go | 3 +- hack/lifecycle/main.go | 5 +- pkg/blob/fetch.go | 3 +- pkg/blob/fetch_test.go | 21 +++-- pkg/cnb/build_metadata.go | 4 +- pkg/cnb/env_vars.go | 3 +- pkg/cnb/platform_env_vars_setup_test.go | 5 +- pkg/cnb/project_descriptor_test.go | 83 +++++++++---------- pkg/dockercreds/docker_creds_test.go | 5 +- pkg/dockercreds/parse_secrets.go | 5 +- pkg/dockercreds/parse_secrets_test.go | 25 +++--- .../volume_secret_keychain_test.go | 5 +- pkg/notary/image_signer_test.go | 4 +- pkg/notary/repository.go | 4 +- pkg/registry/fetch.go | 9 +- pkg/registry/fetch_test.go | 27 +++--- pkg/secret/volume_secret_reader.go | 9 +- pkg/secret/volume_secret_reader_test.go | 13 ++- 18 files changed, 109 insertions(+), 124 deletions(-) diff --git a/cmd/rebase/main.go b/cmd/rebase/main.go index f372c8b84..9c538c6bc 100644 --- a/cmd/rebase/main.go +++ b/cmd/rebase/main.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "flag" - "io/ioutil" "log" "os" "path/filepath" @@ -134,7 +133,7 @@ func rebase(tags []string, logger *log.Logger) error { return err } - return ioutil.WriteFile(*reportFilePath, buf.Bytes(), 0777) + return os.WriteFile(*reportFilePath, buf.Bytes(), 0777) } func logLoadingSecrets(logger *log.Logger, secretsSlices ...[]string) { diff --git a/hack/lifecycle/main.go b/hack/lifecycle/main.go index 2f9b67722..be65c7a50 100644 --- a/hack/lifecycle/main.go +++ b/hack/lifecycle/main.go @@ -7,7 +7,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "log" "net/http" "os" @@ -193,7 +192,7 @@ func lifecycleLayer(url, os string) (v1.Layer, error) { return nil, err } - buf, err := ioutil.ReadAll(tr) + buf, err := io.ReadAll(tr) if err != nil { return nil, err } @@ -210,7 +209,7 @@ func lifecycleLayer(url, os string) (v1.Layer, error) { } func lifecycleReader(url string) (io.ReadCloser, error) { - dir, err := ioutil.TempDir("", "lifecycle") + dir, err := os.MkdirTemp("", "lifecycle") if err != nil { return nil, err } diff --git a/pkg/blob/fetch.go b/pkg/blob/fetch.go index 19c86572c..3f2c0add5 100644 --- a/pkg/blob/fetch.go +++ b/pkg/blob/fetch.go @@ -2,7 +2,6 @@ package blob import ( "io" - "io/ioutil" "log" "net/http" "net/url" @@ -74,7 +73,7 @@ func downloadBlob(blobURL string) (*os.File, error) { return nil, errors.Errorf("failed to get blob %s", blobURL) } - file, err := ioutil.TempFile("", "") + file, err := os.CreateTemp("", "") if err != nil { return nil, err } diff --git a/pkg/blob/fetch_test.go b/pkg/blob/fetch_test.go index 2527b7211..961a49f24 100644 --- a/pkg/blob/fetch_test.go +++ b/pkg/blob/fetch_test.go @@ -3,7 +3,6 @@ package blob_test import ( "bytes" "fmt" - "io/ioutil" "log" "net/http" "net/http/httptest" @@ -35,7 +34,7 @@ func testBlobFetcher(t *testing.T, when spec.G, it spec.S) { it.Before(func() { var err error - dir, err = ioutil.TempDir("", "fetch_test") + dir, err = os.MkdirTemp("", "fetch_test") require.NoError(t, err) }) @@ -49,21 +48,21 @@ func testBlobFetcher(t *testing.T, when spec.G, it spec.S) { err := fetcher.Fetch(dir, fmt.Sprintf("%s/%s", server.URL, testFile), 0) require.NoError(t, err) - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) require.NoError(t, err) require.Len(t, files, 1) testDir := files[0] require.Equal(t, "testdir", testDir.Name()) require.True(t, testDir.IsDir()) - files, err = ioutil.ReadDir(filepath.Join(dir, testDir.Name())) + files, err = os.ReadDir(filepath.Join(dir, testDir.Name())) require.NoError(t, err) require.Len(t, files, 1) testFile := files[0] require.Equal(t, "testfile", testFile.Name()) require.False(t, testFile.IsDir()) - file, err := ioutil.ReadFile(filepath.Join(dir, testDir.Name(), testFile.Name())) + file, err := os.ReadFile(filepath.Join(dir, testDir.Name(), testFile.Name())) require.NoError(t, err) require.Equal(t, "test file contents", string(file)) @@ -78,14 +77,14 @@ func testBlobFetcher(t *testing.T, when spec.G, it spec.S) { err := fetcher.Fetch(dir, fmt.Sprintf("%s/%s", server.URL, "fat-zip.zip"), 0) require.NoError(t, err) - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) require.NoError(t, err) require.Len(t, files, 1) testFile := files[0] require.Equal(t, "some-file.txt", testFile.Name()) - - require.Equal(t, os.FileMode(0777).String(), testFile.Mode().String()) + info, _ := testFile.Info() + require.Equal(t, os.FileMode(0777).String(), info.Mode().String()) require.Contains(t, output.String(), "Successfully downloaded") }) @@ -94,7 +93,7 @@ func testBlobFetcher(t *testing.T, when spec.G, it spec.S) { err := fetcher.Fetch(dir, fmt.Sprintf("%s/%s", server.URL, "test-exe.tar"), 0) require.NoError(t, err) - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) require.NoError(t, err) require.Len(t, files, 1) @@ -102,7 +101,7 @@ func testBlobFetcher(t *testing.T, when spec.G, it spec.S) { require.Equal(t, "test-exe", testDir.Name()) require.True(t, testDir.IsDir()) - files, err = ioutil.ReadDir(filepath.Join(dir, testDir.Name())) + files, err = os.ReadDir(filepath.Join(dir, testDir.Name())) require.NoError(t, err) require.Len(t, files, 1) @@ -120,7 +119,7 @@ func testBlobFetcher(t *testing.T, when spec.G, it spec.S) { err := fetcher.Fetch(dir, fmt.Sprintf("%s/%s", server.URL, archiveFile), 1) require.NoError(t, err) - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) require.NoError(t, err) require.Len(t, files, 1) diff --git a/pkg/cnb/build_metadata.go b/pkg/cnb/build_metadata.go index 7b7e52305..44cd4d2c3 100644 --- a/pkg/cnb/build_metadata.go +++ b/pkg/cnb/build_metadata.go @@ -5,7 +5,7 @@ import ( "compress/gzip" "encoding/base64" "encoding/json" - "io/ioutil" + "io" lifecyclebuildpack "github.com/buildpacks/lifecycle/buildpack" "github.com/buildpacks/lifecycle/platform" @@ -176,7 +176,7 @@ func DecompressBuildMetadata(compressedMetadata string) (*BuildMetadata, error) return nil, err } defer zr.Close() - data, err := ioutil.ReadAll(zr) + data, err := io.ReadAll(zr) if err != nil { return nil, err } diff --git a/pkg/cnb/env_vars.go b/pkg/cnb/env_vars.go index 56fd56bc9..ce6577edb 100644 --- a/pkg/cnb/env_vars.go +++ b/pkg/cnb/env_vars.go @@ -1,7 +1,6 @@ package cnb import ( - "io/ioutil" "os" "path" @@ -17,7 +16,7 @@ func serializeEnvVars(envVars []envVariable, platformDir string) error { } for _, envVar := range envVars { - err = ioutil.WriteFile(path.Join(folder, envVar.Name), []byte(envVar.Value), os.ModePerm) + err = os.WriteFile(path.Join(folder, envVar.Name), []byte(envVar.Value), os.ModePerm) if err != nil { return err } diff --git a/pkg/cnb/platform_env_vars_setup_test.go b/pkg/cnb/platform_env_vars_setup_test.go index 81013a29b..b3e7b44dd 100644 --- a/pkg/cnb/platform_env_vars_setup_test.go +++ b/pkg/cnb/platform_env_vars_setup_test.go @@ -1,7 +1,6 @@ package cnb_test import ( - "io/ioutil" "os" "path" "testing" @@ -25,7 +24,7 @@ func testPlatformEnvVarsSetup(t *testing.T, when spec.G, it spec.S) { it.Before(func() { var err error - testVolume, err = ioutil.TempDir("", "permission") + testVolume, err = os.MkdirTemp("", "permission") require.NoError(t, err) platformEnv = map[string]string{ @@ -61,7 +60,7 @@ func testPlatformEnvVarsSetup(t *testing.T, when spec.G, it spec.S) { func checkEnvVar(t *testing.T, testVolume, key, value string) { require.FileExists(t, path.Join(testVolume, "env", key)) - buf, err := ioutil.ReadFile(path.Join(testVolume, "env", key)) + buf, err := os.ReadFile(path.Join(testVolume, "env", key)) require.NoError(t, err) require.Equal(t, value, string(buf)) } diff --git a/pkg/cnb/project_descriptor_test.go b/pkg/cnb/project_descriptor_test.go index 882491754..334ce8727 100644 --- a/pkg/cnb/project_descriptor_test.go +++ b/pkg/cnb/project_descriptor_test.go @@ -3,7 +3,6 @@ package cnb_test import ( "bytes" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -31,9 +30,9 @@ func testProcessProjectDescriptor(t *testing.T, when spec.G, it spec.S) { var err error buf = new(bytes.Buffer) logger = log.New(buf, "", 0) - appDir, err = ioutil.TempDir("", "appDir") + appDir, err = os.MkdirTemp("", "appDir") require.NoError(t, err) - platformDir, err = ioutil.TempDir("", "platform") + platformDir, err = os.MkdirTemp("", "platform") require.NoError(t, err) projectToml = filepath.Join(appDir, "project.toml") }) @@ -45,7 +44,7 @@ func testProcessProjectDescriptor(t *testing.T, when spec.G, it spec.S) { when("unsupported project descriptor version", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [_] schema-version = "0.99" `), 0644) @@ -63,7 +62,7 @@ schema-version = "0.99" when("using descriptor v1", func() { when("the descriptor has build env vars", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [[build.env]] name = "keyA" value = "valueA" @@ -107,38 +106,38 @@ value = "valueAnotherC" err = os.Mkdir(filepath.Join(appDir, "secrets"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "secrets", "api_keys.json"), []byte("{}"), 0755) + err = os.WriteFile(filepath.Join(appDir, "secrets", "api_keys.json"), []byte("{}"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "secrets", "user_token"), []byte("token"), 0755) + err = os.WriteFile(filepath.Join(appDir, "secrets", "user_token"), []byte("token"), 0755) assert.Nil(t, err) err = os.Mkdir(filepath.Join(appDir, "nested"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "nested", "nested-cookie.jar"), []byte("chocolate chip"), 0755) + err = os.WriteFile(filepath.Join(appDir, "nested", "nested-cookie.jar"), []byte("chocolate chip"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "other-cookie.jar"), []byte("chocolate chip"), 0755) + err = os.WriteFile(filepath.Join(appDir, "other-cookie.jar"), []byte("chocolate chip"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "nested-cookie.jar"), []byte("chocolate chip"), 0755) + err = os.WriteFile(filepath.Join(appDir, "nested-cookie.jar"), []byte("chocolate chip"), 0755) assert.Nil(t, err) err = os.Mkdir(filepath.Join(appDir, "media"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "media", "mountain.jpg"), []byte("fake image bytes"), 0755) + err = os.WriteFile(filepath.Join(appDir, "media", "mountain.jpg"), []byte("fake image bytes"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "media", "person.png"), []byte("fake image bytes"), 0755) + err = os.WriteFile(filepath.Join(appDir, "media", "person.png"), []byte("fake image bytes"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "cookie.jar"), []byte("chocolate chip"), 0755) + err = os.WriteFile(filepath.Join(appDir, "cookie.jar"), []byte("chocolate chip"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "test.sh"), []byte("echo test"), 0755) + err = os.WriteFile(filepath.Join(appDir, "test.sh"), []byte("echo test"), 0755) assert.Nil(t, err) }) when("it has excludes", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [build] exclude = ["*.sh", "secrets/", "media/metadata", "/other-cookie.jar" ,"/nested-cookie.jar"] `), 0644) @@ -159,7 +158,7 @@ exclude = ["*.sh", "secrets/", "media/metadata", "/other-cookie.jar" ,"/nested-c when("it has includes", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [build] include = [ "*.jar", "media/mountain.jpg", "/media/person.png", ] `), 0644) @@ -181,7 +180,7 @@ include = [ "*.jar", "media/mountain.jpg", "/media/person.png", ] when("it has both excludes and includes", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [build] include = [ "test", ] exclude = ["test", ] @@ -196,7 +195,7 @@ exclude = ["test", ] when("the descriptor has builder", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [build] builder = "my-super-cool-builder" `), 0644) @@ -209,7 +208,7 @@ builder = "my-super-cool-builder" when("the descriptor has buildpacks", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [[build.buildpacks]] id = "cool-buildpack" version = "v4.2" @@ -229,7 +228,7 @@ uri = "check-this-out.com" when("io.buildpacks.env.build format is used", func() { when("format is valid", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [_] schema-version = "0.2" [[io.buildpacks.env.build]] @@ -257,7 +256,7 @@ value = "valueAnotherC" when("format is invalid", func() { when("'value' is invalid", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [_] schema-version = "0.2" [[io.buildpacks.env.build]] @@ -272,7 +271,7 @@ value = 1 }) when("'name' is invalid", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [_] schema-version = "0.2" [[io.buildpacks.env.build]] @@ -291,7 +290,7 @@ value = "ValueA" when("io.buildpacks.env format is used", func() { when("format is valid", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [_] schema-version = "0.2" [[io.buildpacks.env]] @@ -318,7 +317,7 @@ value = "valueAnotherC"`), 0644) when("format is invalid", func() { when("'value' is invalid", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [_] schema-version = "0.2" [[io.buildpacks.env]] @@ -333,7 +332,7 @@ value = 1 }) when("'name' is invalid", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [_] schema-version = "0.2" [[io.buildpacks.env]] @@ -352,7 +351,7 @@ value = "ValueA" when("vars where set with new v0.2 project.toml", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [_] schema-version = "0.2" [[io.buildpacks.build.env]] @@ -380,7 +379,7 @@ value = "valueAnotherC" when("vars where set with both versions of v0.2 project.toml", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [_] schema-version = "0.2" [[io.buildpacks.env.build]] @@ -419,38 +418,38 @@ value = "newValueA" err = os.Mkdir(filepath.Join(appDir, "secrets"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "secrets", "api_keys.json"), []byte("{}"), 0755) + err = os.WriteFile(filepath.Join(appDir, "secrets", "api_keys.json"), []byte("{}"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "secrets", "user_token"), []byte("token"), 0755) + err = os.WriteFile(filepath.Join(appDir, "secrets", "user_token"), []byte("token"), 0755) assert.Nil(t, err) err = os.Mkdir(filepath.Join(appDir, "nested"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "nested", "nested-cookie.jar"), []byte("chocolate chip"), 0755) + err = os.WriteFile(filepath.Join(appDir, "nested", "nested-cookie.jar"), []byte("chocolate chip"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "other-cookie.jar"), []byte("chocolate chip"), 0755) + err = os.WriteFile(filepath.Join(appDir, "other-cookie.jar"), []byte("chocolate chip"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "nested-cookie.jar"), []byte("chocolate chip"), 0755) + err = os.WriteFile(filepath.Join(appDir, "nested-cookie.jar"), []byte("chocolate chip"), 0755) assert.Nil(t, err) err = os.Mkdir(filepath.Join(appDir, "media"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "media", "mountain.jpg"), []byte("fake image bytes"), 0755) + err = os.WriteFile(filepath.Join(appDir, "media", "mountain.jpg"), []byte("fake image bytes"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "media", "person.png"), []byte("fake image bytes"), 0755) + err = os.WriteFile(filepath.Join(appDir, "media", "person.png"), []byte("fake image bytes"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "cookie.jar"), []byte("chocolate chip"), 0755) + err = os.WriteFile(filepath.Join(appDir, "cookie.jar"), []byte("chocolate chip"), 0755) assert.Nil(t, err) - err = ioutil.WriteFile(filepath.Join(appDir, "test.sh"), []byte("echo test"), 0755) + err = os.WriteFile(filepath.Join(appDir, "test.sh"), []byte("echo test"), 0755) assert.Nil(t, err) }) when("it has excludes", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [_] schema-version = "0.2" [io.buildpacks] @@ -473,7 +472,7 @@ exclude = ["*.sh", "secrets/", "media/metadata", "/other-cookie.jar" ,"/nested-c when("it has includes", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [_] schema-version = "0.2" [io.buildpacks] @@ -497,7 +496,7 @@ include = [ "*.jar", "media/mountain.jpg", "/media/person.png", ] when("it has both excludes and includes", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [_] schema-version = "0.2" [io.buildpacks] @@ -514,7 +513,7 @@ exclude = ["test", ] when("the descriptor has builder", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [_] schema-version = "0.2" [io.buildpacks] @@ -529,7 +528,7 @@ builder = "my-super-cool-builder" when("the descriptor has buildpack groups", func() { it.Before(func() { - ioutil.WriteFile(projectToml, []byte(` + os.WriteFile(projectToml, []byte(` [_] schema-version = "0.2" [[io.buildpacks.group]] @@ -548,7 +547,7 @@ uri = "check-this-out.com" when("the descriptor path is set", func() { it.Before(func() { projectToml = filepath.Join(appDir, "some-project.toml") - err := ioutil.WriteFile(projectToml, []byte(` + err := os.WriteFile(projectToml, []byte(` [[build.env]] name = "keyA" value = "valueA" diff --git a/pkg/dockercreds/docker_creds_test.go b/pkg/dockercreds/docker_creds_test.go index 37da50898..0806c39e8 100644 --- a/pkg/dockercreds/docker_creds_test.go +++ b/pkg/dockercreds/docker_creds_test.go @@ -1,7 +1,6 @@ package dockercreds import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -22,7 +21,7 @@ func testDockerCreds(t *testing.T, when spec.G, it spec.S) { it.Before(func() { var err error - testPullSecretsDir, err = ioutil.TempDir("", "test.append") + testPullSecretsDir, err = os.MkdirTemp("", "test.append") require.NoError(t, err) }) @@ -52,7 +51,7 @@ func testDockerCreds(t *testing.T, when spec.G, it spec.S) { err := creds.Save(filepath.Join(testPullSecretsDir, "config.json")) require.NoError(t, err) - configJsonBytes, err := ioutil.ReadFile(filepath.Join(testPullSecretsDir, "config.json")) + configJsonBytes, err := os.ReadFile(filepath.Join(testPullSecretsDir, "config.json")) require.NoError(t, err) assert.JSONEq(t, expectedConfigJsonContents, string(configJsonBytes)) diff --git a/pkg/dockercreds/parse_secrets.go b/pkg/dockercreds/parse_secrets.go index b6d0e3c90..a61de6ffe 100644 --- a/pkg/dockercreds/parse_secrets.go +++ b/pkg/dockercreds/parse_secrets.go @@ -2,7 +2,6 @@ package dockercreds import ( "encoding/json" - "io/ioutil" "os" "path/filepath" "strings" @@ -59,7 +58,7 @@ func parseDockerCfg(path string) (DockerCreds, error) { if !cfgExists { return creds, nil } - cfgFile, err := ioutil.ReadFile(path) + cfgFile, err := os.ReadFile(path) if err != nil { return nil, err } @@ -84,7 +83,7 @@ func parseDockerConfigJson(path string) (DockerCreds, error) { return config.Auths, nil } - configJsonFile, err := ioutil.ReadFile(path) + configJsonFile, err := os.ReadFile(path) if err != nil { return nil, err } diff --git a/pkg/dockercreds/parse_secrets_test.go b/pkg/dockercreds/parse_secrets_test.go index e5b9b7295..20d0a2aee 100644 --- a/pkg/dockercreds/parse_secrets_test.go +++ b/pkg/dockercreds/parse_secrets_test.go @@ -1,7 +1,6 @@ package dockercreds import ( - "io/ioutil" "os" "path" "path/filepath" @@ -26,7 +25,7 @@ func parseDockerConfigSecret(t *testing.T, when spec.G, it spec.S) { it.Before(func() { var err error - testSecretsDir, err = ioutil.TempDir("", "test.pullsecrets") + testSecretsDir, err = os.MkdirTemp("", "test.pullsecrets") require.NoError(t, err) }) @@ -35,7 +34,7 @@ func parseDockerConfigSecret(t *testing.T, when spec.G, it spec.S) { }) it("parses .dockerconfigjson favoring auth key", func() { - err := ioutil.WriteFile(filepath.Join(testSecretsDir, ".dockerconfigjson"), []byte(`{ + err := os.WriteFile(filepath.Join(testSecretsDir, ".dockerconfigjson"), []byte(`{ "auths": { "https://index.docker.io/v1/": { "auth": "dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZHNpbGxpbmVzcwo=", @@ -61,7 +60,7 @@ func parseDockerConfigSecret(t *testing.T, when spec.G, it spec.S) { }) it("parses .dockerconfigjson setting auth key to username/password if unset", func() { - err := ioutil.WriteFile(filepath.Join(testSecretsDir, ".dockerconfigjson"), []byte(`{ + err := os.WriteFile(filepath.Join(testSecretsDir, ".dockerconfigjson"), []byte(`{ "auths": { "https://index.docker.io/v1/": { "username": "testusername", @@ -86,7 +85,7 @@ func parseDockerConfigSecret(t *testing.T, when spec.G, it spec.S) { }) it("parses .dockercfg favoring auth key", func() { - err := ioutil.WriteFile(filepath.Join(testSecretsDir, ".dockercfg"), []byte(`{ + err := os.WriteFile(filepath.Join(testSecretsDir, ".dockercfg"), []byte(`{ "https://index.docker.io/v1/": { "auth": "dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZHNpbGxpbmVzcwo=", "username": "testusername", @@ -110,7 +109,7 @@ func parseDockerConfigSecret(t *testing.T, when spec.G, it spec.S) { }) it("parses .dockercfg setting auth key to username/password if unset", func() { - err := ioutil.WriteFile(filepath.Join(testSecretsDir, ".dockercfg"), []byte(`{ + err := os.WriteFile(filepath.Join(testSecretsDir, ".dockercfg"), []byte(`{ "https://index.docker.io/v1/": { "username": "testusername", "password": "testpassword" @@ -133,7 +132,7 @@ func parseDockerConfigSecret(t *testing.T, when spec.G, it spec.S) { }) it("parses .dockercfg and .dockerconfigjson", func() { - err := ioutil.WriteFile(filepath.Join(testSecretsDir, ".dockerconfigjson"), []byte(`{ + err := os.WriteFile(filepath.Join(testSecretsDir, ".dockerconfigjson"), []byte(`{ "auths": { "https://index.docker.io/v1/": { "auth": "dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZHNpbGxpbmVzcwo=" @@ -143,7 +142,7 @@ func parseDockerConfigSecret(t *testing.T, when spec.G, it spec.S) { ), os.ModePerm) require.NoError(t, err) - err = ioutil.WriteFile(filepath.Join(testSecretsDir, ".dockercfg"), []byte(`{ + err = os.WriteFile(filepath.Join(testSecretsDir, ".dockercfg"), []byte(`{ "gcr.io": { "auth": "dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA==", "username": "testusername", @@ -178,17 +177,17 @@ func testParseBasicAuthSecrets(t *testing.T, when spec.G, it spec.S) { var testDir string it.Before(func() { var err error - testDir, err = ioutil.TempDir("", "docker-secret-parse-test") + testDir, err = os.MkdirTemp("", "docker-secret-parse-test") require.NoError(t, err) require.NoError(t, os.MkdirAll(path.Join(testDir, "gcr-creds"), 0777)) require.NoError(t, os.MkdirAll(path.Join(testDir, "dockerhub-creds"), 0777)) - require.NoError(t, ioutil.WriteFile(path.Join(testDir, "gcr-creds", corev1.BasicAuthUsernameKey), []byte("gcr-username"), 0600)) - require.NoError(t, ioutil.WriteFile(path.Join(testDir, "gcr-creds", corev1.BasicAuthPasswordKey), []byte("gcr-password"), 0600)) + require.NoError(t, os.WriteFile(path.Join(testDir, "gcr-creds", corev1.BasicAuthUsernameKey), []byte("gcr-username"), 0600)) + require.NoError(t, os.WriteFile(path.Join(testDir, "gcr-creds", corev1.BasicAuthPasswordKey), []byte("gcr-password"), 0600)) - require.NoError(t, ioutil.WriteFile(path.Join(testDir, "dockerhub-creds", corev1.BasicAuthUsernameKey), []byte("dockerhub-username"), 0600)) - require.NoError(t, ioutil.WriteFile(path.Join(testDir, "dockerhub-creds", corev1.BasicAuthPasswordKey), []byte("dockerhub-password"), 0600)) + require.NoError(t, os.WriteFile(path.Join(testDir, "dockerhub-creds", corev1.BasicAuthUsernameKey), []byte("dockerhub-username"), 0600)) + require.NoError(t, os.WriteFile(path.Join(testDir, "dockerhub-creds", corev1.BasicAuthPasswordKey), []byte("dockerhub-password"), 0600)) }) diff --git a/pkg/dockercreds/volume_secret_keychain_test.go b/pkg/dockercreds/volume_secret_keychain_test.go index 648575b71..950bf86a6 100644 --- a/pkg/dockercreds/volume_secret_keychain_test.go +++ b/pkg/dockercreds/volume_secret_keychain_test.go @@ -1,7 +1,6 @@ package dockercreds_test import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -32,7 +31,7 @@ func testVolumeSecretKeychain(t *testing.T, when spec.G, it spec.S) { ) it.Before(func() { var err error - folderDir, err = ioutil.TempDir("", "secret-volume") + folderDir, err = os.MkdirTemp("", "secret-volume") require.NoError(t, err) resource = fakeDockerResource{registryString: "registry.io"} }) @@ -42,7 +41,7 @@ func testVolumeSecretKeychain(t *testing.T, when spec.G, it spec.S) { }) it("retrieves the configuration from file", func() { - require.NoError(t, ioutil.WriteFile(filepath.Join(folderDir, ".dockerconfigjson"), []byte(` + require.NoError(t, os.WriteFile(filepath.Join(folderDir, ".dockerconfigjson"), []byte(` { "auths": { "registry.io": { diff --git a/pkg/notary/image_signer_test.go b/pkg/notary/image_signer_test.go index 7e7f5783e..9db516e38 100644 --- a/pkg/notary/image_signer_test.go +++ b/pkg/notary/image_signer_test.go @@ -2,7 +2,7 @@ package notary import ( "encoding/hex" - "io/ioutil" + "io" "log" "path/filepath" "testing" @@ -30,7 +30,7 @@ func TestImageSigner(t *testing.T) { func testImageSigner(t *testing.T, when spec.G, it spec.S) { var ( - logger = log.New(ioutil.Discard, "", 0) + logger = log.New(io.Discard, "", 0) client = registryfakes.NewFakeClient() diff --git a/pkg/notary/repository.go b/pkg/notary/repository.go index 992326c1b..b02ed52bb 100644 --- a/pkg/notary/repository.go +++ b/pkg/notary/repository.go @@ -1,7 +1,7 @@ package notary import ( - "io/ioutil" + "os" "path" "github.com/pkg/errors" @@ -17,7 +17,7 @@ type RemoteRepositoryFactory struct { } func (r *RemoteRepositoryFactory) GetRepository(url string, gun data.GUN, remoteStore storage.RemoteStore, cryptoService signed.CryptoService) (Repository, error) { - changeListDir, err := ioutil.TempDir("", "") + changeListDir, err := os.MkdirTemp("", "") if err != nil { return nil, err } diff --git a/pkg/registry/fetch.go b/pkg/registry/fetch.go index d20a723f2..0e0299ef6 100644 --- a/pkg/registry/fetch.go +++ b/pkg/registry/fetch.go @@ -1,7 +1,6 @@ package registry import ( - "io/ioutil" "log" "os" "path/filepath" @@ -104,7 +103,7 @@ func handleSource(img v1.Image, dir string) error { } func handleZip(img v1.Image, dir string) error { - tmpDir, err := ioutil.TempDir("", "") + tmpDir, err := os.MkdirTemp("", "") if err != nil { return err } @@ -129,7 +128,7 @@ func handleZip(img v1.Image, dir string) error { } func handleTar(img v1.Image, dir string) error { - tmpDir, err := ioutil.TempDir("", "") + tmpDir, err := os.MkdirTemp("", "") if err != nil { return err } @@ -149,7 +148,7 @@ func handleTar(img v1.Image, dir string) error { } func handleTarGZ(img v1.Image, dir string) error { - tmpDir, err := ioutil.TempDir("", "") + tmpDir, err := os.MkdirTemp("", "") if err != nil { return err } @@ -179,7 +178,7 @@ func getSourceFile(img v1.Image, dir string) (*os.File, error) { return nil, err } - infos, err := ioutil.ReadDir(dir) + infos, err := os.ReadDir(dir) if err != nil { return nil, err } diff --git a/pkg/registry/fetch_test.go b/pkg/registry/fetch_test.go index 0037a6d1d..b66b091a3 100644 --- a/pkg/registry/fetch_test.go +++ b/pkg/registry/fetch_test.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -42,7 +41,7 @@ func testRegistrySourceFetcher(t *testing.T, when spec.G, it spec.S) { it.Before(func() { var err error - dir, err = ioutil.TempDir("", "") + dir, err = os.MkdirTemp("", "") require.NoError(t, err) }) @@ -56,7 +55,7 @@ func testRegistrySourceFetcher(t *testing.T, when spec.G, it spec.S) { testContentType := testCases[i+1] it("handles unexploded file types: "+testFile, func() { - buf, err := ioutil.ReadFile(filepath.Join("testdata", testFile)) + buf, err := os.ReadFile(filepath.Join("testdata", testFile)) require.NoError(t, err) img := createSourceImage(t, buf, testContentType) @@ -67,7 +66,7 @@ func testRegistrySourceFetcher(t *testing.T, when spec.G, it spec.S) { err = fetcher.Fetch(dir, repoName) require.NoError(t, err) - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) require.NoError(t, err) require.Len(t, files, 1) @@ -75,7 +74,7 @@ func testRegistrySourceFetcher(t *testing.T, when spec.G, it spec.S) { require.Equal(t, "testdir", testDir.Name()) require.True(t, testDir.IsDir()) - files, err = ioutil.ReadDir(filepath.Join(dir, testDir.Name())) + files, err = os.ReadDir(filepath.Join(dir, testDir.Name())) require.NoError(t, err) require.Len(t, files, 1) @@ -83,7 +82,7 @@ func testRegistrySourceFetcher(t *testing.T, when spec.G, it spec.S) { require.Equal(t, "testfile", testFile.Name()) require.False(t, testFile.IsDir()) - file, err := ioutil.ReadFile(filepath.Join(dir, testDir.Name(), testFile.Name())) + file, err := os.ReadFile(filepath.Join(dir, testDir.Name(), testFile.Name())) require.NoError(t, err) require.Equal(t, "test file contents", string(file)) @@ -92,7 +91,7 @@ func testRegistrySourceFetcher(t *testing.T, when spec.G, it spec.S) { } it("handles regular source images", func() { - buf, err := ioutil.ReadFile(filepath.Join("testdata", "reg.tar")) + buf, err := os.ReadFile(filepath.Join("testdata", "reg.tar")) require.NoError(t, err) img := createSourceImage(t, buf, "") @@ -103,7 +102,7 @@ func testRegistrySourceFetcher(t *testing.T, when spec.G, it spec.S) { err = fetcher.Fetch(dir, repoName) require.NoError(t, err) - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) require.NoError(t, err) require.Len(t, files, 1) @@ -111,7 +110,7 @@ func testRegistrySourceFetcher(t *testing.T, when spec.G, it spec.S) { require.Equal(t, "test.txt", testFile.Name()) require.False(t, testFile.IsDir()) - file, err := ioutil.ReadFile(filepath.Join(dir, testFile.Name())) + file, err := os.ReadFile(filepath.Join(dir, testFile.Name())) require.NoError(t, err) require.Equal(t, "test file contents\n", string(file)) @@ -119,7 +118,7 @@ func testRegistrySourceFetcher(t *testing.T, when spec.G, it spec.S) { }) it("handles directories with improper headers", func() { - buf, err := ioutil.ReadFile(filepath.Join("testdata", "layer.tar")) + buf, err := os.ReadFile(filepath.Join("testdata", "layer.tar")) require.NoError(t, err) img := createSourceImage(t, buf, "") @@ -131,14 +130,14 @@ func testRegistrySourceFetcher(t *testing.T, when spec.G, it spec.S) { require.NoError(t, err) // the vendor/cache directory doesnt have proper headers - _, err = ioutil.ReadFile(filepath.Join(dir, "/vendor/cache/diff-lcs-1.4.2.gem")) + _, err = os.ReadFile(filepath.Join(dir, "/vendor/cache/diff-lcs-1.4.2.gem")) require.NoError(t, err) require.Contains(t, output.String(), "Successfully pulled") }) it("sets the correct file mode", func() { - buf, err := ioutil.ReadFile(filepath.Join("testdata", "tarexe.tar")) + buf, err := os.ReadFile(filepath.Join("testdata", "tarexe.tar")) require.NoError(t, err) img := createSourceImage(t, buf, "tar") @@ -149,7 +148,7 @@ func testRegistrySourceFetcher(t *testing.T, when spec.G, it spec.S) { err = fetcher.Fetch(dir, repoName) require.NoError(t, err) - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) require.NoError(t, err) require.Len(t, files, 1) @@ -157,7 +156,7 @@ func testRegistrySourceFetcher(t *testing.T, when spec.G, it spec.S) { require.Equal(t, "test-exe", testDir.Name()) require.True(t, testDir.IsDir()) - files, err = ioutil.ReadDir(filepath.Join(dir, testDir.Name())) + files, err = os.ReadDir(filepath.Join(dir, testDir.Name())) require.NoError(t, err) require.Len(t, files, 1) diff --git a/pkg/secret/volume_secret_reader.go b/pkg/secret/volume_secret_reader.go index a1027b927..41275972f 100644 --- a/pkg/secret/volume_secret_reader.go +++ b/pkg/secret/volume_secret_reader.go @@ -2,7 +2,6 @@ package secret import ( "fmt" - "io/ioutil" "os" "path/filepath" @@ -11,13 +10,13 @@ import ( func ReadBasicAuthSecret(secretVolume, secretName string) (BasicAuth, error) { secretPath := volumeName(secretVolume, secretName) - ub, err := ioutil.ReadFile(filepath.Join(secretPath, corev1.BasicAuthUsernameKey)) + ub, err := os.ReadFile(filepath.Join(secretPath, corev1.BasicAuthUsernameKey)) if err != nil { return BasicAuth{}, err } username := string(ub) - pb, err := ioutil.ReadFile(filepath.Join(secretPath, corev1.BasicAuthPasswordKey)) + pb, err := os.ReadFile(filepath.Join(secretPath, corev1.BasicAuthPasswordKey)) if err != nil { return BasicAuth{}, err } @@ -31,7 +30,7 @@ func ReadBasicAuthSecret(secretVolume, secretName string) (BasicAuth, error) { func ReadSshSecret(secretVolume, secretName string) (SSH, error) { secretPath := volumeName(secretVolume, secretName) - privateKey, err := ioutil.ReadFile(filepath.Join(secretPath, corev1.SSHAuthPrivateKey)) + privateKey, err := os.ReadFile(filepath.Join(secretPath, corev1.SSHAuthPrivateKey)) if err != nil { return SSH{}, err } @@ -39,7 +38,7 @@ func ReadSshSecret(secretVolume, secretName string) (SSH, error) { var knownHosts []byte = nil knownHostsPath := filepath.Join(secretPath, SSHAuthKnownHostsKey) if _, err := os.Stat(knownHostsPath); !os.IsNotExist(err) { - knownHosts, err = ioutil.ReadFile(knownHostsPath) + knownHosts, err = os.ReadFile(knownHostsPath) if err != nil { return SSH{}, err } diff --git a/pkg/secret/volume_secret_reader_test.go b/pkg/secret/volume_secret_reader_test.go index 59bd68aec..da4793010 100644 --- a/pkg/secret/volume_secret_reader_test.go +++ b/pkg/secret/volume_secret_reader_test.go @@ -1,7 +1,6 @@ package secret_test import ( - "io/ioutil" "os" "path" "testing" @@ -21,7 +20,7 @@ func TestVolumeSecretReader(t *testing.T) { func testVolumeSecretReader(t *testing.T, when spec.G, it spec.S) { when("#readBasicAuthSecret", func() { it("returns the username and password from the secret", func() { - testDir, err := ioutil.TempDir("", "secret-volume") + testDir, err := os.MkdirTemp("", "secret-volume") require.NoError(t, err) defer func() { @@ -30,8 +29,8 @@ func testVolumeSecretReader(t *testing.T, when spec.G, it spec.S) { require.NoError(t, os.MkdirAll(path.Join(testDir, "creds"), 0777)) - require.NoError(t, ioutil.WriteFile(path.Join(testDir, "creds", corev1.BasicAuthUsernameKey), []byte("saved-username"), 0600)) - require.NoError(t, ioutil.WriteFile(path.Join(testDir, "creds", corev1.BasicAuthPasswordKey), []byte("saved-password"), 0600)) + require.NoError(t, os.WriteFile(path.Join(testDir, "creds", corev1.BasicAuthUsernameKey), []byte("saved-username"), 0600)) + require.NoError(t, os.WriteFile(path.Join(testDir, "creds", corev1.BasicAuthPasswordKey), []byte("saved-password"), 0600)) auth, err := secret.ReadBasicAuthSecret(testDir, "creds") require.NoError(t, err) @@ -45,7 +44,7 @@ func testVolumeSecretReader(t *testing.T, when spec.G, it spec.S) { when("#readSshSecret", func() { it("returns the private key and known hosts from the secret", func() { - testDir, err := ioutil.TempDir("", "secret-volume") + testDir, err := os.MkdirTemp("", "secret-volume") require.NoError(t, err) defer func() { @@ -54,8 +53,8 @@ func testVolumeSecretReader(t *testing.T, when spec.G, it spec.S) { require.NoError(t, os.MkdirAll(path.Join(testDir, "creds"), 0777)) - require.NoError(t, ioutil.WriteFile(path.Join(testDir, "creds", corev1.SSHAuthPrivateKey), []byte("foobar"), 0600)) - require.NoError(t, ioutil.WriteFile(path.Join(testDir, "creds", secret.SSHAuthKnownHostsKey), []byte("ssh-keyscan"), 0600)) + require.NoError(t, os.WriteFile(path.Join(testDir, "creds", corev1.SSHAuthPrivateKey), []byte("foobar"), 0600)) + require.NoError(t, os.WriteFile(path.Join(testDir, "creds", secret.SSHAuthKnownHostsKey), []byte("ssh-keyscan"), 0600)) auth, err := secret.ReadSshSecret(testDir, "creds") require.NoError(t, err) From ebd0a7110f0d6f15541f5ebefa2052a76ca2fca0 Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Wed, 27 Sep 2023 11:21:55 +0800 Subject: [PATCH 2/2] fix: assert no error Signed-off-by: guoguangwu --- pkg/blob/fetch_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/blob/fetch_test.go b/pkg/blob/fetch_test.go index 961a49f24..2688babf2 100644 --- a/pkg/blob/fetch_test.go +++ b/pkg/blob/fetch_test.go @@ -83,7 +83,8 @@ func testBlobFetcher(t *testing.T, when spec.G, it spec.S) { testFile := files[0] require.Equal(t, "some-file.txt", testFile.Name()) - info, _ := testFile.Info() + info, err := testFile.Info() + require.NoError(t, err) require.Equal(t, os.FileMode(0777).String(), info.Mode().String()) require.Contains(t, output.String(), "Successfully downloaded")