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
This commit is contained in:
Manish Giri
2023-11-28 13:02:53 -08:00
committed by GitHub
parent 2b3d3c7eff
commit 7bfc73c3ad
32 changed files with 173 additions and 143 deletions
+4 -5
View File
@@ -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)
}
+6 -7
View File
@@ -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)
}
+28 -10
View File
@@ -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
+1 -2
View File
@@ -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
+3 -4
View File
@@ -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
}
+13 -14
View File
@@ -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 {