From 7bfc73c3ad39a738c6aa4f778312f82eba438c8d Mon Sep 17 00:00:00 2001 From: Manish Giri Date: Tue, 28 Nov 2023 15:02:53 -0600 Subject: [PATCH] fix: Remove references to deprecated io/ioutil pkg (#2867) * Update benchmark_test * Updae tar.go * Update further refs * Commit next set of replacements * Reverting changes in vendor folder * Update integreation_with_context_test.go * Update k8s_test.go * Update remaining usages * Replace conflicting usage of fs local variable --- integration/benchmark_test.go | 6 +- integration/images.go | 3 +- integration/integration_with_context_test.go | 15 ++++- integration/k8s_test.go | 17 ++++-- integration/tar.go | 3 +- pkg/buildcontext/tar_test.go | 3 +- pkg/cache/warm.go | 7 +-- pkg/cache/warm_test.go | 9 ++- pkg/commands/copy_test.go | 63 +++++++++++++------- pkg/commands/fake_commands.go | 3 +- pkg/commands/run_test.go | 5 +- pkg/creds/creds.go | 4 +- pkg/dockerfile/dockerfile.go | 7 ++- pkg/dockerfile/dockerfile_test.go | 3 +- pkg/executor/build_test.go | 9 ++- pkg/executor/composite_cache_test.go | 13 ++-- pkg/executor/copy_multistage_test.go | 38 ++++++++---- pkg/executor/fakes.go | 3 +- pkg/executor/push.go | 7 +-- pkg/executor/push_test.go | 27 ++++----- pkg/filesystem/resolve_test.go | 7 +-- pkg/snapshot/snapshot.go | 5 +- pkg/snapshot/snapshot_test.go | 5 +- pkg/util/fs_util.go | 3 +- pkg/util/fs_util_test.go | 29 +++++---- pkg/util/gcr_util_test.go | 3 +- pkg/util/proc/proc.go | 3 +- pkg/util/tar_util.go | 3 +- pkg/util/tar_util_test.go | 3 +- pkg/util/transport_util.go | 4 +- pkg/util/util.go | 3 +- testutil/util.go | 3 +- 32 files changed, 173 insertions(+), 143 deletions(-) diff --git a/integration/benchmark_test.go b/integration/benchmark_test.go index e5d9c4e10..c6a31430f 100644 --- a/integration/benchmark_test.go +++ b/integration/benchmark_test.go @@ -19,7 +19,7 @@ package integration import ( "encoding/json" "fmt" - "io/ioutil" + "io" "os" "os/exec" "path/filepath" @@ -94,7 +94,7 @@ func newResult(t *testing.T, f string) result { if err != nil { t.Errorf("could not read benchmark file %s", f) } - byteValue, _ := ioutil.ReadAll(jsonFile) + byteValue, _ := io.ReadAll(jsonFile) if err := json.Unmarshal(byteValue, ¤t); err != nil { t.Errorf("could not unmarshal benchmark file") } @@ -160,7 +160,7 @@ func runInGcloud(dir string, num int) (string, error) { } // grab gcs and to temp dir and return - tmpDir, err := ioutil.TempDir("", fmt.Sprintf("%d", num)) + tmpDir, err := os.MkdirTemp("", fmt.Sprintf("%d", num)) if err != nil { return "", err } diff --git a/integration/images.go b/integration/images.go index 58f1737ac..11fca9c8f 100644 --- a/integration/images.go +++ b/integration/images.go @@ -20,7 +20,6 @@ import ( "bytes" "context" "fmt" - "io/ioutil" "os" "os/exec" "path" @@ -459,7 +458,7 @@ func buildKanikoImage( shdUpload bool, ) (string, error) { benchmarkEnv := "BENCHMARK_FILE=false" - benchmarkDir, err := ioutil.TempDir("", "") + benchmarkDir, err := os.MkdirTemp("", "") if err != nil { return "", err } diff --git a/integration/integration_with_context_test.go b/integration/integration_with_context_test.go index 30b17b675..412bf212e 100644 --- a/integration/integration_with_context_test.go +++ b/integration/integration_with_context_test.go @@ -18,7 +18,7 @@ package integration import ( "fmt" - "io/ioutil" + "io/fs" "os" "path/filepath" "testing" @@ -31,12 +31,21 @@ func TestWithContext(t *testing.T) { } dir := filepath.Join(cwd, "dockerfiles-with-context") - - testDirs, err := ioutil.ReadDir(dir) + entries, err := os.ReadDir(dir) if err != nil { t.Fatal(err) } + testDirs := make([]fs.FileInfo, 0, len(entries)) + + for _, entry := range entries { + info, err := entry.Info() + if err != nil { + t.Fatal(err) + } + testDirs = append(testDirs, info) + } + builder := NewDockerFileBuilder() for _, tdInfo := range testDirs { diff --git a/integration/k8s_test.go b/integration/k8s_test.go index 076593162..f05917a32 100644 --- a/integration/k8s_test.go +++ b/integration/k8s_test.go @@ -18,7 +18,7 @@ package integration import ( "fmt" - "io/ioutil" + "io/fs" "log" "os" "os/exec" @@ -41,10 +41,19 @@ func TestK8s(t *testing.T) { dir := filepath.Join(cwd, "dockerfiles-with-context") - testDirs, err := ioutil.ReadDir(dir) + entries, err := os.ReadDir(dir) if err != nil { t.Fatal(err) } + testDirs := make([]fs.FileInfo, 0, len(entries)) + + for _, entry := range entries { + info, err := entry.Info() + if err != nil { + t.Fatal(err) + } + testDirs = append(testDirs, info) + } builder := NewDockerFileBuilder() @@ -64,7 +73,7 @@ func TestK8s(t *testing.T) { dockerImage := GetDockerImage(config.imageRepo, name) kanikoImage := GetKanikoImage(config.imageRepo, name) - tmpfile, err := ioutil.TempFile("", "k8s-job-*.yaml") + tmpfile, err := os.CreateTemp("", "k8s-job-*.yaml") if err != nil { log.Fatal(err) } @@ -77,7 +86,7 @@ func TestK8s(t *testing.T) { t.Logf("Testing K8s based Kaniko building of dockerfile %s and push to %s \n", testDir, kanikoImage) - content, err := ioutil.ReadFile(tmpfile.Name()) + content, err := os.ReadFile(tmpfile.Name()) if err != nil { log.Fatal(err) } diff --git a/integration/tar.go b/integration/tar.go index 3bb51cb93..9e36c55c2 100644 --- a/integration/tar.go +++ b/integration/tar.go @@ -19,7 +19,6 @@ package integration import ( "compress/gzip" "fmt" - "io/ioutil" "log" "os" "time" @@ -35,7 +34,7 @@ func CreateIntegrationTarball() (string, error) { if err != nil { return "nil", fmt.Errorf("Failed find path to integration dir: %w", err) } - tempDir, err := ioutil.TempDir("", "") + tempDir, err := os.MkdirTemp("", "") if err != nil { return "", fmt.Errorf("Failed to create temporary directory to hold tarball: %w", err) } diff --git a/pkg/buildcontext/tar_test.go b/pkg/buildcontext/tar_test.go index 7c16fdb0f..0f881daa7 100644 --- a/pkg/buildcontext/tar_test.go +++ b/pkg/buildcontext/tar_test.go @@ -20,7 +20,6 @@ import ( "bytes" "compress/gzip" "fmt" - "io/ioutil" "os" "path/filepath" "runtime" @@ -145,7 +144,7 @@ func TestBuildWithLocalTar(t *testing.T) { } func getSHAFromFilePath(f string) (string, error) { - data, err := ioutil.ReadFile(f) + data, err := os.ReadFile(f) if err != nil { return "", err } diff --git a/pkg/cache/warm.go b/pkg/cache/warm.go index 02832a312..38b3729e3 100644 --- a/pkg/cache/warm.go +++ b/pkg/cache/warm.go @@ -20,7 +20,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "net/http" "os" "path" @@ -109,7 +108,7 @@ func writeBufsToFile(cachePath string, tarBuf, manifestBuf *bytes.Buffer) error } mfstPath := cachePath + ".json" - if err := ioutil.WriteFile(mfstPath, manifestBuf.Bytes(), 0666); err != nil { + if err := os.WriteFile(mfstPath, manifestBuf.Bytes(), 0666); err != nil { return errors.Wrap(err, "Failed to save manifest to file") } @@ -186,9 +185,9 @@ func ParseDockerfile(opts *config.WarmerOptions) ([]string, error) { if e != nil { return nil, e } - d, err = ioutil.ReadAll(response.Body) + d, err = io.ReadAll(response.Body) } else { - d, err = ioutil.ReadFile(opts.DockerfilePath) + d, err = os.ReadFile(opts.DockerfilePath) } if err != nil { diff --git a/pkg/cache/warm_test.go b/pkg/cache/warm_test.go index f66cef55e..6dd926f8f 100644 --- a/pkg/cache/warm_test.go +++ b/pkg/cache/warm_test.go @@ -18,7 +18,6 @@ package cache import ( "bytes" - "io/ioutil" "os" "testing" @@ -119,7 +118,7 @@ func TestParseDockerfile_SingleStageDockerfile(t *testing.T) { dockerfile := `FROM alpine:latest LABEL maintainer="alexezio" ` - tmpfile, err := ioutil.TempFile("", "example") + tmpfile, err := os.CreateTemp("", "example") if err != nil { t.Fatal(err) } @@ -152,7 +151,7 @@ LABEL maintainer="alexezio" FROM alpine:latest as RUNNER LABEL maintainer="alexezio" ` - tmpfile, err := ioutil.TempFile("", "example") + tmpfile, err := os.CreateTemp("", "example") if err != nil { t.Fatal(err) } @@ -186,7 +185,7 @@ func TestParseDockerfile_ArgsDockerfile(t *testing.T) { dockerfile := `ARG version=latest FROM golang:${version} ` - tmpfile, err := ioutil.TempFile("", "example") + tmpfile, err := os.CreateTemp("", "example") if err != nil { t.Fatal(err) } @@ -225,7 +224,7 @@ func TestParseDockerfile_MissingsDockerfile(t *testing.T) { func TestParseDockerfile_InvalidsDockerfile(t *testing.T) { dockerfile := "This is a invalid dockerfile" - tmpfile, err := ioutil.TempFile("", "example") + tmpfile, err := os.CreateTemp("", "example") if err != nil { t.Fatal(err) } diff --git a/pkg/commands/copy_test.go b/pkg/commands/copy_test.go index ed89b1673..0acbec772 100755 --- a/pkg/commands/copy_test.go +++ b/pkg/commands/copy_test.go @@ -21,7 +21,6 @@ import ( "fmt" "io" "io/fs" - "io/ioutil" "os" "path/filepath" "strings" @@ -126,6 +125,24 @@ func setupTestTemp(t *testing.T) string { return tempDir } +func readDirectory(dirName string) ([]fs.FileInfo, error) { + entries, err := os.ReadDir(dirName) + if err != nil { + return nil, err + } + + testDir := make([]fs.FileInfo, 0, len(entries)) + + for _, entry := range entries { + info, err := entry.Info() + if err != nil { + return nil, err + } + testDir = append(testDir, info) + } + return testDir, err +} + func Test_CachingCopyCommand_ExecuteCommand(t *testing.T) { tempDir := setupTestTemp(t) @@ -320,7 +337,7 @@ func TestCopyExecuteCmd(t *testing.T) { return // Unrecoverable, will segfault in the next line } if fstat.IsDir() { - files, err := ioutil.ReadDir(dest) + files, err := readDirectory(dest) if err != nil { t.Error() } @@ -358,12 +375,12 @@ func Test_resolveIfSymlink(t *testing.T) { tmpDir := t.TempDir() - baseDir, err := ioutil.TempDir(tmpDir, "not-linked") + baseDir, err := os.MkdirTemp(tmpDir, "not-linked") if err != nil { t.Error(err) } - path, err := ioutil.TempFile(baseDir, "foo.txt") + path, err := os.CreateTemp(baseDir, "foo.txt") if err != nil { t.Error(err) } @@ -420,11 +437,11 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { } file := filepath.Join(dir, "bam.txt") - if err := ioutil.WriteFile(file, []byte("meow"), 0777); err != nil { + if err := os.WriteFile(file, []byte("meow"), 0777); err != nil { t.Fatal(err) } targetPath := filepath.Join(dir, "dam.txt") - if err := ioutil.WriteFile(targetPath, []byte("woof"), 0777); err != nil { + if err := os.WriteFile(targetPath, []byte("woof"), 0777); err != nil { t.Fatal(err) } if err := os.Symlink("dam.txt", filepath.Join(dir, "sym.link")); err != nil { @@ -437,7 +454,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { t.Run("copy dir to another dir", func(t *testing.T) { testDir, srcDir := setupDirs(t) defer os.RemoveAll(testDir) - expected, err := ioutil.ReadDir(filepath.Join(testDir, srcDir)) + expected, err := readDirectory(filepath.Join(testDir, srcDir)) if err != nil { t.Fatal(err) } @@ -461,7 +478,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { } testutil.CheckNoError(t, err) // Check if "dest" dir exists with contents of srcDir - actual, err := ioutil.ReadDir(filepath.Join(testDir, "dest")) + actual, err := readDirectory(filepath.Join(testDir, "dest")) if err != nil { t.Fatal(err) } @@ -475,7 +492,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { testDir, srcDir := setupDirs(t) defer os.RemoveAll(testDir) ignoredFile := "bam.txt" - srcFiles, err := ioutil.ReadDir(filepath.Join(testDir, srcDir)) + srcFiles, err := readDirectory(filepath.Join(testDir, srcDir)) if err != nil { t.Fatal(err) } @@ -508,7 +525,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { } testutil.CheckNoError(t, err) // Check if "dest" dir exists with contents of srcDir - actual, err := ioutil.ReadDir(filepath.Join(testDir, "dest")) + actual, err := readDirectory(filepath.Join(testDir, "dest")) if err != nil { t.Fatal(err) } @@ -544,7 +561,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { err := cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{})) testutil.CheckNoError(t, err) // Check if "dest" dir exists with file bam.txt - files, err := ioutil.ReadDir(filepath.Join(testDir, "dest")) + files, err := readDirectory(filepath.Join(testDir, "dest")) if err != nil { t.Fatal(err) } @@ -600,7 +617,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { err := cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{})) testutil.CheckNoError(t, err) // Check if "dest" dir exists with file bam.txt - files, err := ioutil.ReadDir(filepath.Join(testDir, "dest")) + files, err := readDirectory(filepath.Join(testDir, "dest")) if err != nil { t.Fatal(err) } @@ -629,7 +646,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { err := cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{})) testutil.CheckNoError(t, err) // Check if "dest" dir exists with link sym.link - files, err := ioutil.ReadDir(filepath.Join(testDir, "dest")) + files, err := readDirectory(filepath.Join(testDir, "dest")) if err != nil { t.Fatal(err) } @@ -648,7 +665,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { testDir, srcDir := setupDirs(t) defer os.RemoveAll(testDir) doesNotExists := filepath.Join(testDir, "dead.txt") - if err := ioutil.WriteFile(doesNotExists, []byte("remove me"), 0777); err != nil { + if err := os.WriteFile(doesNotExists, []byte("remove me"), 0777); err != nil { t.Fatal(err) } if err := os.Symlink("../dead.txt", filepath.Join(testDir, srcDir, "dead.link")); err != nil { @@ -674,7 +691,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { err := cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{})) testutil.CheckNoError(t, err) // Check if "dest" dir exists with link dead.link - files, err := ioutil.ReadDir(filepath.Join(testDir, "dest")) + files, err := readDirectory(filepath.Join(testDir, "dest")) if err != nil { t.Fatal(err) } @@ -728,7 +745,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { t.Run("copy dir with a symlink to a file outside of current src dir", func(t *testing.T) { testDir, srcDir := setupDirs(t) defer os.RemoveAll(testDir) - expected, err := ioutil.ReadDir(filepath.Join(testDir, srcDir)) + expected, err := readDirectory(filepath.Join(testDir, srcDir)) if err != nil { t.Fatal(err) } @@ -738,7 +755,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { t.Fatal(err) } targetPath := filepath.Join(anotherSrc, "target.txt") - if err := ioutil.WriteFile(targetPath, []byte("woof"), 0777); err != nil { + if err := os.WriteFile(targetPath, []byte("woof"), 0777); err != nil { t.Fatal(err) } if err := os.Symlink(targetPath, filepath.Join(testDir, srcDir, "zSym.link")); err != nil { @@ -762,7 +779,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { testutil.CheckNoError(t, err) // Check if "dest" dir exists contents of srcDir and an extra zSym.link created // in this test - actual, err := ioutil.ReadDir(filepath.Join(testDir, "dest")) + actual, err := readDirectory(filepath.Join(testDir, "dest")) if err != nil { t.Fatal(err) } @@ -818,7 +835,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { t.Run("copy src dir to a dest dir which is a symlink", func(t *testing.T) { testDir, srcDir := setupDirs(t) defer os.RemoveAll(testDir) - expected, err := ioutil.ReadDir(filepath.Join(testDir, srcDir)) + expected, err := readDirectory(filepath.Join(testDir, srcDir)) if err != nil { t.Fatal(err) } @@ -848,7 +865,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { err = cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{})) testutil.CheckNoError(t, err) // Check if "linkdest" dir exists with contents of srcDir - actual, err := ioutil.ReadDir(filepath.Join(testDir, "linkDest")) + actual, err := readDirectory(filepath.Join(testDir, "linkDest")) if err != nil { t.Fatal(err) } @@ -893,12 +910,12 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { err := cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{})) testutil.CheckNoError(t, err) // Check if "linkDest" link is same. - actual, err := ioutil.ReadDir(filepath.Join(testDir, "dest")) + actual, err := readDirectory(filepath.Join(testDir, "dest")) if err != nil { t.Fatal(err) } testutil.CheckDeepEqual(t, "bam.txt", actual[0].Name()) - c, err := ioutil.ReadFile(filepath.Join(testDir, "dest", "bam.txt")) + c, err := os.ReadFile(filepath.Join(testDir, "dest", "bam.txt")) if err != nil { t.Fatal(err) } @@ -942,7 +959,7 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) { err := cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{})) testutil.CheckNoError(t, err) - actual, err := ioutil.ReadDir(filepath.Join(testDir)) + actual, err := readDirectory(filepath.Join(testDir)) if err != nil { t.Fatal(err) } diff --git a/pkg/commands/fake_commands.go b/pkg/commands/fake_commands.go index 8efee7c4d..365650064 100644 --- a/pkg/commands/fake_commands.go +++ b/pkg/commands/fake_commands.go @@ -20,7 +20,6 @@ package commands import ( "bytes" "io" - "io/ioutil" v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/google/go-containerregistry/pkg/v1/types" @@ -40,7 +39,7 @@ func (f fakeLayer) Compressed() (io.ReadCloser, error) { return nil, nil } func (f fakeLayer) Uncompressed() (io.ReadCloser, error) { - return ioutil.NopCloser(bytes.NewReader(f.TarContent)), nil + return io.NopCloser(bytes.NewReader(f.TarContent)), nil } func (f fakeLayer) Size() (int64, error) { return 0, nil diff --git a/pkg/commands/run_test.go b/pkg/commands/run_test.go index baba1d1bb..135d7e2a7 100644 --- a/pkg/commands/run_test.go +++ b/pkg/commands/run_test.go @@ -20,7 +20,6 @@ import ( "archive/tar" "bytes" "io" - "io/ioutil" "log" "os" "os/user" @@ -130,7 +129,7 @@ Meow meow meow meow meow meow meow meow ` for _, name := range fileNames { - if err := ioutil.WriteFile(filepath.Join(dir, name), []byte(content), 0777); err != nil { + if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0777); err != nil { return nil, err } } @@ -153,7 +152,7 @@ meow meow meow meow if err := tw.WriteHeader(hdr); err != nil { log.Fatal(err) } - body, err := ioutil.ReadFile(path) + body, err := os.ReadFile(path) if err != nil { return err } diff --git a/pkg/creds/creds.go b/pkg/creds/creds.go index 5c95eaeeb..294fe85ef 100644 --- a/pkg/creds/creds.go +++ b/pkg/creds/creds.go @@ -17,7 +17,7 @@ limitations under the License. package creds import ( - "io/ioutil" + "io" ecr "github.com/awslabs/amazon-ecr-credential-helper/ecr-login" "github.com/chrismellard/docker-credential-acr-env/pkg/credhelper" @@ -31,7 +31,7 @@ func GetKeychain() authn.Keychain { return authn.NewMultiKeychain( authn.DefaultKeychain, google.Keychain, - authn.NewKeychainFromHelper(ecr.NewECRHelper(ecr.WithLogger(ioutil.Discard))), + authn.NewKeychainFromHelper(ecr.NewECRHelper(ecr.WithLogger(io.Discard))), authn.NewKeychainFromHelper(credhelper.NewACRCredentialsHelper()), authn.NewKeychainFromHelper(gitlab.NewGitLabCredentialsHelper()), ) diff --git a/pkg/dockerfile/dockerfile.go b/pkg/dockerfile/dockerfile.go index aa97ed0bb..fec1cb3f1 100644 --- a/pkg/dockerfile/dockerfile.go +++ b/pkg/dockerfile/dockerfile.go @@ -19,8 +19,9 @@ package dockerfile import ( "bytes" "fmt" - "io/ioutil" + "io" "net/http" + "os" "regexp" "strconv" "strings" @@ -44,9 +45,9 @@ func ParseStages(opts *config.KanikoOptions) ([]instructions.Stage, []instructio if e != nil { return nil, nil, e } - d, err = ioutil.ReadAll(response.Body) + d, err = io.ReadAll(response.Body) } else { - d, err = ioutil.ReadFile(opts.DockerfilePath) + d, err = os.ReadFile(opts.DockerfilePath) } if err != nil { diff --git a/pkg/dockerfile/dockerfile_test.go b/pkg/dockerfile/dockerfile_test.go index 0588636f1..b38fb81ef 100644 --- a/pkg/dockerfile/dockerfile_test.go +++ b/pkg/dockerfile/dockerfile_test.go @@ -18,7 +18,6 @@ package dockerfile import ( "fmt" - "io/ioutil" "os" "reflect" "testing" @@ -45,7 +44,7 @@ func Test_ParseStages_ArgValueWithQuotes(t *testing.T) { FROM scratch COPY --from=second /hi2 /hi3 ` - tmpfile, err := ioutil.TempFile("", "Dockerfile.test") + tmpfile, err := os.CreateTemp("", "Dockerfile.test") if err != nil { t.Fatal(err) } diff --git a/pkg/executor/build_test.go b/pkg/executor/build_test.go index fc205dace..837075aa9 100644 --- a/pkg/executor/build_test.go +++ b/pkg/executor/build_test.go @@ -20,7 +20,6 @@ import ( "archive/tar" "bytes" "fmt" - "io/ioutil" "os" "path/filepath" "reflect" @@ -622,7 +621,7 @@ func Test_stageBuilder_optimize(t *testing.T) { sb := &stageBuilder{opts: tc.opts, cf: cf, snapshotter: snap, layerCache: lc, args: dockerfile.NewBuildArgs([]string{})} ck := CompositeCache{} - file, err := ioutil.TempFile("", "foo") + file, err := os.CreateTemp("", "foo") if err != nil { t.Error(err) } @@ -1245,7 +1244,7 @@ FROM ubuntu:16.04 COPY %s bar.txt RUN foobar `, filename) - f, _ := ioutil.TempFile("", "") + f, _ := os.CreateTemp("", "") os.WriteFile(f.Name(), []byte(dockerFile), 0755) opts := &config.KanikoOptions{ DockerfilePath: f.Name(), @@ -1531,7 +1530,7 @@ func tempDirAndFile(t *testing.T) (string, []string) { dir := t.TempDir() for _, filename := range filenames { filepath := filepath.Join(dir, filename) - err := ioutil.WriteFile(filepath, []byte(`meow`), 0777) + err := os.WriteFile(filepath, []byte(`meow`), 0777) if err != nil { t.Errorf("could not create temp file %v", err) } @@ -1560,7 +1559,7 @@ func generateTar(t *testing.T, dir string, fileNames ...string) []byte { t.Errorf("could not write tar header %v", err) } - content, err := ioutil.ReadFile(filePath) + content, err := os.ReadFile(filePath) if err != nil { t.Errorf("could not read tempfile %v", err) } diff --git a/pkg/executor/composite_cache_test.go b/pkg/executor/composite_cache_test.go index 3f1ee2465..86dc73f14 100644 --- a/pkg/executor/composite_cache_test.go +++ b/pkg/executor/composite_cache_test.go @@ -17,7 +17,6 @@ limitations under the License. package executor import ( - "io/ioutil" "os" "path" "path/filepath" @@ -71,7 +70,7 @@ func Test_CompositeCache_AddPath_dir(t *testing.T) { tmpDir := t.TempDir() content := `meow meow meow` - if err := ioutil.WriteFile(filepath.Join(tmpDir, "foo.txt"), []byte(content), 0777); err != nil { + if err := os.WriteFile(filepath.Join(tmpDir, "foo.txt"), []byte(content), 0777); err != nil { t.Errorf("got error writing temp file %v", err) } @@ -98,7 +97,7 @@ func Test_CompositeCache_AddPath_dir(t *testing.T) { } } func Test_CompositeCache_AddPath_file(t *testing.T) { - tmpfile, err := ioutil.TempFile("/tmp", "foo.txt") + tmpfile, err := os.CreateTemp("/tmp", "foo.txt") if err != nil { t.Errorf("got error setting up test %v", err) } @@ -146,7 +145,7 @@ func createFilesystemStructure(root string, directories, files []string) error { for _, fileName := range files { filePath := path.Join(root, fileName) - err := ioutil.WriteFile(filePath, []byte(fileName), 0644) + err := os.WriteFile(filePath, []byte(fileName), 0644) if err != nil { return err } @@ -158,7 +157,7 @@ func createFilesystemStructure(root string, directories, files []string) error { func setIgnoreContext(t *testing.T, content string) (util.FileContext, error) { var fileContext util.FileContext dockerIgnoreDir := t.TempDir() - err := ioutil.WriteFile(dockerIgnoreDir+".dockerignore", []byte(content), 0644) + err := os.WriteFile(dockerIgnoreDir+".dockerignore", []byte(content), 0644) if err != nil { return fileContext, err } @@ -288,7 +287,7 @@ func Test_CompositeKey_AddPath_WithExtraFile_Works(t *testing.T) { t.Fatalf("Error creating filesytem structure: %s", err) } extraPath := path.Join(testDir2, test.extraFile) - err = ioutil.WriteFile(extraPath, []byte(test.extraFile), 0644) + err = os.WriteFile(extraPath, []byte(test.extraFile), 0644) if err != nil { t.Fatalf("Error creating filesytem structure: %s", err) } @@ -431,7 +430,7 @@ func Test_CompositeKey_AddPath_WithExtraFilIgnored_Works(t *testing.T) { t.Fatalf("Error creating filesytem structure: %s", err) } extraPath := path.Join(testDir2, test.extraFile) - err = ioutil.WriteFile(extraPath, []byte(test.extraFile), 0644) + err = os.WriteFile(extraPath, []byte(test.extraFile), 0644) if err != nil { t.Fatalf("Error creating filesytem structure: %s", err) } diff --git a/pkg/executor/copy_multistage_test.go b/pkg/executor/copy_multistage_test.go index 450b3056f..838cc61d3 100644 --- a/pkg/executor/copy_multistage_test.go +++ b/pkg/executor/copy_multistage_test.go @@ -18,7 +18,7 @@ package executor import ( "fmt" - "io/ioutil" + "io/fs" "os" "path/filepath" "testing" @@ -28,6 +28,24 @@ import ( "github.com/GoogleContainerTools/kaniko/testutil" ) +func readDirectory(dirName string) ([]fs.FileInfo, error) { + entries, err := os.ReadDir(dirName) + if err != nil { + return nil, err + } + + testDir := make([]fs.FileInfo, 0, len(entries)) + + for _, entry := range entries { + info, err := entry.Info() + if err != nil { + return nil, err + } + testDir = append(testDir, info) + } + return testDir, err +} + func TestCopyCommand_Multistage(t *testing.T) { t.Run("copy a file across multistage", func(t *testing.T) { testDir, fn := setupMultistageTests(t) @@ -39,7 +57,7 @@ ENV test test From scratch as second COPY --from=first copied/bam.txt output/bam.txt` - ioutil.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755) + os.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755) opts := &config.KanikoOptions{ DockerfilePath: filepath.Join(testDir, "workspace", "Dockerfile"), SrcContext: filepath.Join(testDir, "workspace"), @@ -48,7 +66,7 @@ COPY --from=first copied/bam.txt output/bam.txt` _, err := DoBuild(opts) testutil.CheckNoError(t, err) // Check Image has one layer bam.txt - files, err := ioutil.ReadDir(filepath.Join(testDir, "output")) + files, err := readDirectory(filepath.Join(testDir, "output")) if err != nil { t.Fatal(err) } @@ -67,7 +85,7 @@ ENV test test From scratch as second COPY --from=first copied/bam.txt output/` - ioutil.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755) + os.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755) opts := &config.KanikoOptions{ DockerfilePath: filepath.Join(testDir, "workspace", "Dockerfile"), SrcContext: filepath.Join(testDir, "workspace"), @@ -75,7 +93,7 @@ COPY --from=first copied/bam.txt output/` } _, err := DoBuild(opts) testutil.CheckNoError(t, err) - files, err := ioutil.ReadDir(filepath.Join(testDir, "output")) + files, err := readDirectory(filepath.Join(testDir, "output")) if err != nil { t.Fatal(err) } @@ -92,7 +110,7 @@ ENV test test From scratch as second COPY --from=first copied another` - ioutil.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755) + os.WriteFile(filepath.Join(testDir, "workspace", "Dockerfile"), []byte(dockerFile), 0755) opts := &config.KanikoOptions{ DockerfilePath: filepath.Join(testDir, "workspace", "Dockerfile"), SrcContext: filepath.Join(testDir, "workspace"), @@ -101,7 +119,7 @@ COPY --from=first copied another` _, err := DoBuild(opts) testutil.CheckNoError(t, err) // Check Image has one layer bam.txt - files, err := ioutil.ReadDir(filepath.Join(testDir, "another")) + files, err := readDirectory(filepath.Join(testDir, "another")) if err != nil { t.Fatal(err) } @@ -145,14 +163,14 @@ func setupMultistageTests(t *testing.T) (string, func()) { t.Fatal(err) } file := filepath.Join(workspace, "foo", "bam.txt") - if err := ioutil.WriteFile(file, []byte("meow"), 0755); err != nil { + if err := os.WriteFile(file, []byte("meow"), 0755); err != nil { t.Fatal(err) } os.Symlink("bam.txt", filepath.Join(workspace, "foo", "bam.link")) // Make a file with contents link file = filepath.Join(workspace, "exec") - if err := ioutil.WriteFile(file, []byte("woof"), 0755); err != nil { + if err := os.WriteFile(file, []byte("woof"), 0755); err != nil { t.Fatal(err) } // Make bin @@ -173,7 +191,7 @@ func setupMultistageTests(t *testing.T) (string, func()) { `36 35 98:0 /kaniko %s/kaniko rw,noatime master:1 - ext3 /dev/root rw,errors=continue 36 35 98:0 /proc %s/proc rw,noatime master:1 - ext3 /dev/root rw,errors=continue `, testDir, testDir) - if err := ioutil.WriteFile(mFile, []byte(mountInfo), 0644); err != nil { + if err := os.WriteFile(mFile, []byte(mountInfo), 0644); err != nil { t.Fatal(err) } config.MountInfoPath = mFile diff --git a/pkg/executor/fakes.go b/pkg/executor/fakes.go index 155212397..b503195c1 100644 --- a/pkg/executor/fakes.go +++ b/pkg/executor/fakes.go @@ -21,7 +21,6 @@ import ( "bytes" "errors" "io" - "io/ioutil" "github.com/GoogleContainerTools/kaniko/pkg/commands" "github.com/GoogleContainerTools/kaniko/pkg/dockerfile" @@ -158,7 +157,7 @@ func (f fakeLayer) Compressed() (io.ReadCloser, error) { return nil, nil } func (f fakeLayer) Uncompressed() (io.ReadCloser, error) { - return ioutil.NopCloser(bytes.NewReader(f.TarContent)), nil + return io.NopCloser(bytes.NewReader(f.TarContent)), nil } func (f fakeLayer) Size() (int64, error) { return 0, nil diff --git a/pkg/executor/push.go b/pkg/executor/push.go index a953d0fef..b6f19c443 100644 --- a/pkg/executor/push.go +++ b/pkg/executor/push.go @@ -20,7 +20,6 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" "net/http" "os" "path/filepath" @@ -74,7 +73,7 @@ func (w *withUserAgent) RoundTrip(r *http.Request) (*http.Response, error) { // for testing var ( - fs = afero.NewOsFs() + newOsFs = afero.NewOsFs() checkRemotePushPermission = remote.CheckPushPermission ) @@ -156,7 +155,7 @@ func writeDigestFile(path string, digestByteArray []byte) error { } logrus.Tracef("Created directory %v", parentDir) } - return ioutil.WriteFile(path, digestByteArray, 0644) + return os.WriteFile(path, digestByteArray, 0644) } // DoPush is responsible for pushing image to the destinations specified in opts. @@ -301,7 +300,7 @@ func writeImageOutputs(image v1.Image, destRefs []name.Tag) error { if dir == "" { return nil } - f, err := fs.Create(filepath.Join(dir, "images")) + f, err := newOsFs.Create(filepath.Join(dir, "images")) if err != nil { return err } diff --git a/pkg/executor/push_test.go b/pkg/executor/push_test.go index d1423de6c..3a67843b1 100644 --- a/pkg/executor/push_test.go +++ b/pkg/executor/push_test.go @@ -20,7 +20,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "net/http" "net/http/httptest" "os" @@ -81,9 +80,9 @@ func TestWriteImageOutputs(t *testing.T) { `, d, d), }} { t.Run(c.desc, func(t *testing.T) { - fs = afero.NewMemMapFs() + newOsFs = afero.NewMemMapFs() if c.want == "" { - fs = afero.NewReadOnlyFs(fs) // No files should be written. + newOsFs = afero.NewReadOnlyFs(newOsFs) // No files should be written. } os.Setenv("BUILDER_OUTPUT", c.env) @@ -95,7 +94,7 @@ func TestWriteImageOutputs(t *testing.T) { return } - b, err := afero.ReadFile(fs, filepath.Join(c.env, "images")) + b, err := afero.ReadFile(newOsFs, filepath.Join(c.env, "images")) if err != nil { t.Fatalf("ReadFile: %v", err) } @@ -136,7 +135,7 @@ func TestHeaderAdded(t *testing.T) { resp, err := rt.RoundTrip(req) testutil.CheckError(t, false, err) defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) testutil.CheckErrorAndDeepEqual(t, false, err, test.expected, string(body)) }) } @@ -148,7 +147,7 @@ type mockRoundTripper struct { func (m *mockRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { ua := r.UserAgent() - return &http.Response{Body: ioutil.NopCloser(bytes.NewBufferString(ua))}, nil + return &http.Response{Body: io.NopCloser(bytes.NewBufferString(ua))}, nil } func TestOCILayoutPath(t *testing.T) { @@ -218,7 +217,7 @@ func TestImageNameDigestFile(t *testing.T) { want := []byte("gcr.io/foo/bar@" + digest.String() + "\nindex.docker.io/bob/image@" + digest.String() + "\n") - got, err := ioutil.ReadFile("tmpFile") + got, err := os.ReadFile("tmpFile") testutil.CheckErrorAndDeepEqual(t, false, err, want, got) @@ -311,7 +310,7 @@ func TestImageNameTagDigestFile(t *testing.T) { want := []byte("gcr.io/foo/bar:123@" + digest.String() + "\nindex.docker.io/bob/image:latest@" + digest.String() + "\n") - got, err := ioutil.ReadFile("tmpFile") + got, err := os.ReadFile("tmpFile") testutil.CheckErrorAndDeepEqual(t, false, err, want, got) } @@ -395,7 +394,7 @@ func TestCheckPushPermissions(t *testing.T) { for _, test := range tests { t.Run(test.description, func(t *testing.T) { resetCalledCount() - fs = afero.NewMemMapFs() + newOsFs = afero.NewMemMapFs() opts := config.KanikoOptions{ CacheRepo: test.cacheRepo, Destinations: test.destinations, @@ -403,8 +402,8 @@ func TestCheckPushPermissions(t *testing.T) { NoPushCache: test.noPushCache, } if test.existingConfig { - afero.WriteFile(fs, util.DockerConfLocation(), []byte(""), os.FileMode(0644)) - defer fs.Remove(util.DockerConfLocation()) + afero.WriteFile(newOsFs, util.DockerConfLocation(), []byte(""), os.FileMode(0644)) + defer newOsFs.Remove(util.DockerConfLocation()) } CheckPushPermissions(&opts) if checkPushPermsCallCount != test.checkPushPermsExpectedCallCount { @@ -433,7 +432,7 @@ func TestSkipPushPermission(t *testing.T) { for _, test := range tests { t.Run(test.description, func(t *testing.T) { resetCalledCount() - fs = afero.NewMemMapFs() + newOsFs = afero.NewMemMapFs() opts := config.KanikoOptions{ CacheRepo: test.cacheRepo, Destinations: test.destinations, @@ -442,8 +441,8 @@ func TestSkipPushPermission(t *testing.T) { SkipPushPermissionCheck: test.skipPushPermission, } if test.existingConfig { - afero.WriteFile(fs, util.DockerConfLocation(), []byte(""), os.FileMode(0644)) - defer fs.Remove(util.DockerConfLocation()) + afero.WriteFile(newOsFs, util.DockerConfLocation(), []byte(""), os.FileMode(0644)) + defer newOsFs.Remove(util.DockerConfLocation()) } CheckPushPermissions(&opts) if checkPushPermsCallCount != test.checkPushPermsExpectedCallCount { diff --git a/pkg/filesystem/resolve_test.go b/pkg/filesystem/resolve_test.go index 751bd2f6c..57f7f47ad 100644 --- a/pkg/filesystem/resolve_test.go +++ b/pkg/filesystem/resolve_test.go @@ -18,7 +18,6 @@ package filesystem import ( "fmt" - "io/ioutil" "os" "path/filepath" "reflect" @@ -67,7 +66,7 @@ func Test_ResolvePaths(t *testing.T) { t.Fatal(err) } - if err := ioutil.WriteFile(fTarget, []byte{}, 0777); err != nil { + if err := os.WriteFile(fTarget, []byte{}, 0777); err != nil { t.Fatal(err) } @@ -143,7 +142,7 @@ func Test_ResolvePaths(t *testing.T) { t.Fatal(err) } - if err := ioutil.WriteFile(filepath.Join(target, "meow.txt"), []byte{}, 0777); err != nil { + if err := os.WriteFile(filepath.Join(target, "meow.txt"), []byte{}, 0777); err != nil { t.Fatal(err) } @@ -192,7 +191,7 @@ func Test_resolveSymlinkAncestor(t *testing.T) { targetPath := filepath.Join(targetDir, "bam.txt") - if err := ioutil.WriteFile(targetPath, []byte("meow"), 0777); err != nil { + if err := os.WriteFile(targetPath, []byte("meow"), 0777); err != nil { t.Fatal(err) } diff --git a/pkg/snapshot/snapshot.go b/pkg/snapshot/snapshot.go index 3da11f427..2a9244693 100644 --- a/pkg/snapshot/snapshot.go +++ b/pkg/snapshot/snapshot.go @@ -19,7 +19,6 @@ package snapshot import ( "errors" "fmt" - "io/ioutil" "os" "path/filepath" "runtime" @@ -65,7 +64,7 @@ func (s *Snapshotter) Key() (string, error) { // TakeSnapshot takes a snapshot of the specified files, avoiding directories in the ignorelist, and creates // a tarball of the changed files. Return contents of the tarball, and whether or not any files were changed func (s *Snapshotter) TakeSnapshot(files []string, shdCheckDelete bool, forceBuildMetadata bool) (string, error) { - f, err := ioutil.TempFile(config.KanikoDir, "") + f, err := os.CreateTemp(config.KanikoDir, "") if err != nil { return "", err } @@ -124,7 +123,7 @@ func (s *Snapshotter) TakeSnapshot(files []string, shdCheckDelete bool, forceBui // TakeSnapshotFS takes a snapshot of the filesystem, avoiding directories in the ignorelist, and creates // a tarball of the changed files. func (s *Snapshotter) TakeSnapshotFS() (string, error) { - f, err := ioutil.TempFile(s.getSnashotPathPrefix(), "") + f, err := os.CreateTemp(s.getSnashotPathPrefix(), "") if err != nil { return "", err } diff --git a/pkg/snapshot/snapshot_test.go b/pkg/snapshot/snapshot_test.go index e343fd3bd..30b882d1b 100644 --- a/pkg/snapshot/snapshot_test.go +++ b/pkg/snapshot/snapshot_test.go @@ -19,7 +19,6 @@ package snapshot import ( "archive/tar" "io" - "io/ioutil" "os" "path/filepath" "sort" @@ -88,7 +87,7 @@ func TestSnapshotFSFileChange(t *testing.T) { if hdr.Typeflag == tar.TypeDir { continue } - contents, _ := ioutil.ReadAll(tr) + contents, _ := io.ReadAll(tr) if string(contents) != snapshotFiles[hdr.Name] { t.Fatalf("Contents of %s incorrect, expected: %s, actual: %s", hdr.Name, snapshotFiles[hdr.Name], string(contents)) } @@ -176,7 +175,7 @@ func TestSnapshotFSChangePermissions(t *testing.T) { if hdr.Typeflag == tar.TypeDir { continue } - contents, _ := ioutil.ReadAll(tr) + contents, _ := io.ReadAll(tr) if string(contents) != snapshotFiles[hdr.Name] { t.Fatalf("Contents of %s incorrect, expected: %s, actual: %s", hdr.Name, snapshotFiles[hdr.Name], string(contents)) } diff --git a/pkg/util/fs_util.go b/pkg/util/fs_util.go index eea6da739..e0bbfb7d2 100644 --- a/pkg/util/fs_util.go +++ b/pkg/util/fs_util.go @@ -22,7 +22,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "math" "net/http" "os" @@ -763,7 +762,7 @@ func getExcludedFiles(dockerfilePath, buildcontext string) ([]string, error) { return nil, nil } logrus.Infof("Using dockerignore file: %v", path) - contents, err := ioutil.ReadFile(path) + contents, err := os.ReadFile(path) if err != nil { return nil, errors.Wrap(err, "parsing .dockerignore") } diff --git a/pkg/util/fs_util_test.go b/pkg/util/fs_util_test.go index bd36dd7cc..d49f32043 100644 --- a/pkg/util/fs_util_test.go +++ b/pkg/util/fs_util_test.go @@ -21,7 +21,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "path/filepath" "reflect" @@ -52,7 +51,7 @@ func Test_DetectFilesystemSkiplist(t *testing.T) { if err := os.MkdirAll(filepath.Dir(path), 0750); err != nil { t.Fatalf("Error creating tempdir: %s", err) } - if err := ioutil.WriteFile(path, []byte(fileContents), 0644); err != nil { + if err := os.WriteFile(path, []byte(fileContents), 0644); err != nil { t.Fatalf("Error writing file contents to %s: %s", path, err) } @@ -492,7 +491,7 @@ func fileExists(p string) checker { func fileMatches(p string, c []byte) checker { return func(root string, t *testing.T) { - actual, err := ioutil.ReadFile(filepath.Join(root, p)) + actual, err := os.ReadFile(filepath.Join(root, p)) if err != nil { t.Fatalf("error reading file: %s", p) } @@ -840,7 +839,7 @@ func TestCopySymlink(t *testing.T) { linkTarget: "/abs/dest", dest: "overwrite_me", beforeLink: func(r string) error { - return ioutil.WriteFile(filepath.Join(r, "overwrite_me"), nil, 0644) + return os.WriteFile(filepath.Join(r, "overwrite_me"), nil, 0644) }, }} @@ -851,7 +850,7 @@ func TestCopySymlink(t *testing.T) { r := t.TempDir() os.MkdirAll(filepath.Join(r, filepath.Dir(tc.linkTarget)), 0777) tc.linkTarget = filepath.Join(r, tc.linkTarget) - ioutil.WriteFile(tc.linkTarget, nil, 0644) + os.WriteFile(tc.linkTarget, nil, 0644) if tc.beforeLink != nil { if err := tc.beforeLink(r); err != nil { @@ -980,7 +979,7 @@ func Test_CopyFile_skips_self(t *testing.T) { tempFile := filepath.Join(tempDir, "foo") expected := "bar" - if err := ioutil.WriteFile( + if err := os.WriteFile( tempFile, []byte(expected), 0755, @@ -998,7 +997,7 @@ func Test_CopyFile_skips_self(t *testing.T) { } // Ensure file has expected contents - actualData, err := ioutil.ReadFile(tempFile) + actualData, err := os.ReadFile(tempFile) if err != nil { t.Fatal(err) } @@ -1130,7 +1129,7 @@ func Test_GetFSFromLayers_with_whiteouts_include_whiteout_disabled(t *testing.T) root := t.TempDir() // Write a whiteout path d1 := []byte("Hello World\n") - if err := ioutil.WriteFile(filepath.Join(root, "foobar"), d1, 0644); err != nil { + if err := os.WriteFile(filepath.Join(root, "foobar"), d1, 0644); err != nil { t.Fatal(err) } @@ -1185,7 +1184,7 @@ func Test_GetFSFromLayers_with_whiteouts_include_whiteout_disabled(t *testing.T) f(layerFiles, tw) - rc := ioutil.NopCloser(buf) + rc := io.NopCloser(buf) mockLayer.EXPECT().Uncompressed().Return(rc, nil) secondLayerFiles := []string{ @@ -1200,7 +1199,7 @@ func Test_GetFSFromLayers_with_whiteouts_include_whiteout_disabled(t *testing.T) mockLayer2 := mockv1.NewMockLayer(ctrl) mockLayer2.EXPECT().MediaType().Return(types.OCILayer, nil) - rc = ioutil.NopCloser(buf) + rc = io.NopCloser(buf) mockLayer2.EXPECT().Uncompressed().Return(rc, nil) layers := []v1.Layer{ @@ -1292,7 +1291,7 @@ func Test_GetFSFromLayers_ignorelist(t *testing.T) { f(layerFiles, tw) - rc := ioutil.NopCloser(buf) + rc := io.NopCloser(buf) mockLayer.EXPECT().Uncompressed().Return(rc, nil) layers := []v1.Layer{ @@ -1346,7 +1345,7 @@ func Test_GetFSFromLayers_ignorelist(t *testing.T) { f(layerFiles, tw) - rc = ioutil.NopCloser(buf) + rc = io.NopCloser(buf) mockLayer.EXPECT().Uncompressed().Return(rc, nil) layers = []v1.Layer{ @@ -1413,7 +1412,7 @@ func Test_GetFSFromLayers(t *testing.T) { mockLayer := mockv1.NewMockLayer(ctrl) mockLayer.EXPECT().MediaType().Return(types.OCILayer, nil) - rc := ioutil.NopCloser(buf) + rc := io.NopCloser(buf) mockLayer.EXPECT().Uncompressed().Return(rc, nil) layers := []v1.Layer{ @@ -1463,7 +1462,7 @@ func TestInitIgnoreList(t *testing.T) { mountInfo := `36 35 98:0 /kaniko /test/kaniko rw,noatime master:1 - ext3 /dev/root rw,errors=continue 36 35 98:0 /proc /test/proc rw,noatime master:1 - ext3 /dev/root rw,errors=continue ` - mFile, err := ioutil.TempFile("", "mountinfo") + mFile, err := os.CreateTemp("", "mountinfo") if err != nil { t.Fatal(err) } @@ -1520,7 +1519,7 @@ func Test_setFileTimes(t *testing.T) { p := filepath.Join(testDir, "foo.txt") - if err := ioutil.WriteFile(p, []byte("meow"), 0777); err != nil { + if err := os.WriteFile(p, []byte("meow"), 0777); err != nil { t.Fatal(err) } diff --git a/pkg/util/gcr_util_test.go b/pkg/util/gcr_util_test.go index 71fac3031..e7144688c 100644 --- a/pkg/util/gcr_util_test.go +++ b/pkg/util/gcr_util_test.go @@ -17,7 +17,6 @@ limitations under the License. package util import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -101,7 +100,7 @@ func TestDockerConfLocationWithFileLocation(t *testing.T) { if err := os.Unsetenv(DockerConfigEnvKey); err != nil { t.Fatalf("Failed to unset DOCKER_CONFIG: %v", err) } - file, err := ioutil.TempFile("", "docker.conf") + file, err := os.CreateTemp("", "docker.conf") if err != nil { t.Fatalf("could not create temp file: %s", err) } diff --git a/pkg/util/proc/proc.go b/pkg/util/proc/proc.go index 0f21e522e..9592e81b3 100644 --- a/pkg/util/proc/proc.go +++ b/pkg/util/proc/proc.go @@ -20,7 +20,6 @@ package proc import ( "fmt" - "io/ioutil" "os" "regexp" "strings" @@ -201,7 +200,7 @@ func readFile(file string) []byte { return nil } - b, _ := ioutil.ReadFile(file) + b, _ := os.ReadFile(file) return b } diff --git a/pkg/util/tar_util.go b/pkg/util/tar_util.go index 9d7655c1a..11a4707ba 100644 --- a/pkg/util/tar_util.go +++ b/pkg/util/tar_util.go @@ -23,7 +23,6 @@ import ( "fmt" "io" "io/fs" - "io/ioutil" "os" "path/filepath" "strings" @@ -266,7 +265,7 @@ func fileIsCompressedTar(src string) (bool, archive.Compression) { return false, -1 } defer r.Close() - buf, err := ioutil.ReadAll(r) + buf, err := io.ReadAll(r) if err != nil { return false, -1 } diff --git a/pkg/util/tar_util_test.go b/pkg/util/tar_util_test.go index 565b2881d..eb58da0fa 100644 --- a/pkg/util/tar_util_test.go +++ b/pkg/util/tar_util_test.go @@ -22,7 +22,6 @@ import ( "compress/gzip" "fmt" "io" - "io/ioutil" "os" "path/filepath" "testing" @@ -61,7 +60,7 @@ func Test_AddFileToTar(t *testing.T) { testDir := t.TempDir() path := filepath.Join(testDir, regularFiles[0]) - if err := ioutil.WriteFile(path, []byte("hello"), os.ModePerm); err != nil { + if err := os.WriteFile(path, []byte("hello"), os.ModePerm); err != nil { t.Fatal(err) } // use a pre-determined time with non-zero microseconds to avoid flakiness diff --git a/pkg/util/transport_util.go b/pkg/util/transport_util.go index 04d05ab27..e6cdf9dd9 100644 --- a/pkg/util/transport_util.go +++ b/pkg/util/transport_util.go @@ -20,9 +20,9 @@ import ( "crypto/tls" "crypto/x509" "fmt" + "os" "strings" - "io/ioutil" "net/http" "github.com/GoogleContainerTools/kaniko/pkg/config" @@ -43,7 +43,7 @@ func (p *X509CertPool) value() *x509.CertPool { } func (p *X509CertPool) append(path string) error { - pem, err := ioutil.ReadFile(path) + pem, err := os.ReadFile(path) if err != nil { return err } diff --git a/pkg/util/util.go b/pkg/util/util.go index 5303b0ea9..d0a2455a9 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -22,7 +22,6 @@ import ( "encoding/hex" "fmt" "io" - "io/ioutil" "math" "os" "strconv" @@ -176,7 +175,7 @@ func SHA256(r io.Reader) (string, error) { // GetInputFrom returns Reader content func GetInputFrom(r io.Reader) ([]byte, error) { - output, err := ioutil.ReadAll(r) + output, err := io.ReadAll(r) if err != nil { return nil, err } diff --git a/testutil/util.go b/testutil/util.go index b94ea10cc..6d876ec37 100644 --- a/testutil/util.go +++ b/testutil/util.go @@ -18,7 +18,6 @@ package testutil import ( "fmt" - "io/ioutil" "os" "os/user" "path/filepath" @@ -35,7 +34,7 @@ func SetupFiles(path string, files map[string]string) error { if err := os.MkdirAll(filepath.Dir(path), 0750); err != nil { return err } - if err := ioutil.WriteFile(path, []byte(c), 0644); err != nil { + if err := os.WriteFile(path, []byte(c), 0644); err != nil { return err } }