From 39abcb98ca5534a559be75f101ce26c0c2d19ca2 Mon Sep 17 00:00:00 2001 From: Michael Valdron Date: Thu, 13 Jun 2024 10:57:11 -0400 Subject: [PATCH] refactor deprecated io/ioutil and k8s.io/utils/ptr packages Signed-off-by: Michael Valdron --- pkg/devfile/generator/generators_test.go | 13 +++-- pkg/devfile/generator/utils_test.go | 5 +- pkg/devfile/parser/parse_test.go | 20 +++---- pkg/testingutil/filesystem/default_fs.go | 57 ++++++++++++++----- pkg/testingutil/filesystem/filesystem.go | 4 +- pkg/util/httpcache.go | 15 ++++- pkg/util/util.go | 19 +++++-- tests/v2/integrationTest/integration_test.go | 15 ++++- tests/v2/utils/library/command_test_utils.go | 6 +- .../v2/utils/library/component_test_utils.go | 6 +- tests/v2/utils/library/project_test_utils.go | 10 ++-- 11 files changed, 117 insertions(+), 53 deletions(-) diff --git a/pkg/devfile/generator/generators_test.go b/pkg/devfile/generator/generators_test.go index c6e3ee6..bd0e63d 100644 --- a/pkg/devfile/generator/generators_test.go +++ b/pkg/devfile/generator/generators_test.go @@ -17,14 +17,15 @@ package generator import ( "fmt" + "reflect" + "strings" + "testing" + "github.com/stretchr/testify/assert" appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" - "k8s.io/utils/pointer" - "reflect" - "strings" - "testing" + "k8s.io/utils/ptr" v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" "github.com/devfile/api/v2/pkg/attributes" @@ -1066,7 +1067,7 @@ func TestGetDeployment(t *testing.T) { }, }, Containers: containers, - Replicas: pointer.Int32Ptr(1), + Replicas: ptr.To(int32(1)), }, expected: appsv1.Deployment{ ObjectMeta: objectMetaDedicatedPod, @@ -1083,7 +1084,7 @@ func TestGetDeployment(t *testing.T) { Containers: containers, }, }, - Replicas: pointer.Int32Ptr(1), + Replicas: ptr.To(int32(1)), }, }, }, diff --git a/pkg/devfile/generator/utils_test.go b/pkg/devfile/generator/utils_test.go index 7870ba9..b6d241d 100644 --- a/pkg/devfile/generator/utils_test.go +++ b/pkg/devfile/generator/utils_test.go @@ -16,13 +16,14 @@ package generator import ( - "github.com/hashicorp/go-multierror" - "github.com/stretchr/testify/assert" "path/filepath" "reflect" "strings" "testing" + "github.com/hashicorp/go-multierror" + "github.com/stretchr/testify/assert" + "github.com/devfile/api/v2/pkg/attributes" "github.com/devfile/library/pkg/devfile/parser" "github.com/devfile/library/pkg/devfile/parser/data" diff --git a/pkg/devfile/parser/parse_test.go b/pkg/devfile/parser/parse_test.go index 956e8e0..4c4c456 100644 --- a/pkg/devfile/parser/parse_test.go +++ b/pkg/devfile/parser/parse_test.go @@ -18,6 +18,15 @@ package parser import ( "context" "fmt" + "net" + "net/http" + "net/http/httptest" + "os" + "path" + "reflect" + "strings" + "testing" + v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" "github.com/devfile/api/v2/pkg/attributes" devfilepkg "github.com/devfile/api/v2/pkg/devfile" @@ -28,17 +37,8 @@ import ( "github.com/devfile/library/pkg/testingutil" "github.com/kylelemons/godebug/pretty" "github.com/stretchr/testify/assert" - "io/ioutil" kubev1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "net" - "net/http" - "net/http/httptest" - "os" - "path" - "reflect" "sigs.k8s.io/yaml" - "strings" - "testing" ) const schemaVersion = string(data.APISchemaVersion220) @@ -3861,7 +3861,7 @@ func Test_parseFromURI(t *testing.T) { if err != nil { fmt.Errorf("Test_parseFromURI() error: failed to marshall devfile data: %v", err) } - err = ioutil.WriteFile(localRelativeURI, yamlData, 0644) + err = os.WriteFile(localRelativeURI, yamlData, 0644) if err != nil { fmt.Errorf("Test_parseFromURI() error: fail to write to file: %v", err) } diff --git a/pkg/testingutil/filesystem/default_fs.go b/pkg/testingutil/filesystem/default_fs.go index 9eea63f..4eb9c41 100644 --- a/pkg/testingutil/filesystem/default_fs.go +++ b/pkg/testingutil/filesystem/default_fs.go @@ -22,13 +22,12 @@ limitations under the License. package filesystem import ( - "io/ioutil" "os" "path/filepath" "time" ) -// DefaultFs implements Filesystem using same-named functions from "os" and "io/ioutil" +// DefaultFs implements Filesystem using same-named functions from "os" type DefaultFs struct{} var _ Filesystem = DefaultFs{} @@ -97,33 +96,63 @@ func (DefaultFs) Getwd() (dir string, err error) { return os.Getwd() } -// ReadFile via ioutil.ReadFile +// ReadFile via os.ReadFile func (DefaultFs) ReadFile(filename string) ([]byte, error) { - return ioutil.ReadFile(filename) + return os.ReadFile(filename) } -// WriteFile via ioutil.WriteFile +// WriteFile via os.WriteFile func (DefaultFs) WriteFile(filename string, data []byte, perm os.FileMode) error { - return ioutil.WriteFile(filename, data, perm) + return os.WriteFile(filename, data, perm) } -// TempDir via ioutil.TempDir -func (DefaultFs) TempDir(dir, prefix string) (string, error) { - return ioutil.TempDir(dir, prefix) +// MkdirTemp via os.MkdirTemp +func (DefaultFs) MkdirTemp(dir, prefix string) (string, error) { + return os.MkdirTemp(dir, prefix) } -// TempFile via ioutil.TempFile -func (DefaultFs) TempFile(dir, prefix string) (File, error) { - file, err := ioutil.TempFile(dir, prefix) +// Deprecated: as ioutil.TempDir is deprecated TempDir is replaced by MkdirTemp which uses os.MkdirTemp. +// TempDir now uses MkdirTemp. +func (fs DefaultFs) TempDir(dir, prefix string) (string, error) { + return fs.MkdirTemp(dir, prefix) +} + +// CreateTemp via os.CreateTemp +func (DefaultFs) CreateTemp(dir, prefix string) (File, error) { + file, err := os.CreateTemp(dir, prefix) if err != nil { return nil, err } return &defaultFile{file}, nil } -// ReadDir via ioutil.ReadDir +// TempFile via ioutil.TempFile +// Deprecated: as ioutil.TempFile is deprecated TempFile is replaced by CreateTemp which uses os.CreateTemp. +// TempFile now uses CreateTemp. +func (fs DefaultFs) TempFile(dir, prefix string) (File, error) { + return fs.CreateTemp(dir, prefix) +} + +// ReadDir via os.ReadDir func (DefaultFs) ReadDir(dirname string) ([]os.FileInfo, error) { - return ioutil.ReadDir(dirname) + dirEntries, err := os.ReadDir(dirname) + + if err != nil { + return []os.FileInfo{}, err + } + + dirsInfo := make([]os.FileInfo, 0, len(dirEntries)) + for _, dirEntry := range dirEntries { + info, err := dirEntry.Info() + + if err != nil { + return dirsInfo, err + } + + dirsInfo = append(dirsInfo, info) + } + + return dirsInfo, nil } // Walk via filepath.Walk diff --git a/pkg/testingutil/filesystem/filesystem.go b/pkg/testingutil/filesystem/filesystem.go index e71b6ff..3efb95e 100644 --- a/pkg/testingutil/filesystem/filesystem.go +++ b/pkg/testingutil/filesystem/filesystem.go @@ -41,13 +41,13 @@ type Filesystem interface { Remove(name string) error Chmod(name string, mode os.FileMode) error Getwd() (dir string, err error) - - // from "io/ioutil" ReadFile(filename string) ([]byte, error) WriteFile(filename string, data []byte, perm os.FileMode) error TempDir(dir, prefix string) (string, error) TempFile(dir, prefix string) (File, error) ReadDir(dirname string) ([]os.FileInfo, error) + + // from "filepath" Walk(root string, walkFn filepath.WalkFunc) error } diff --git a/pkg/util/httpcache.go b/pkg/util/httpcache.go index 8efa0a9..67067ee 100644 --- a/pkg/util/httpcache.go +++ b/pkg/util/httpcache.go @@ -16,7 +16,6 @@ package util import ( - "io/ioutil" "os" "path/filepath" "time" @@ -26,11 +25,23 @@ import ( // cleanHttpCache checks cacheDir and deletes all files that were modified more than cacheTime back func cleanHttpCache(cacheDir string, cacheTime time.Duration) error { - cacheFiles, err := ioutil.ReadDir(cacheDir) + dirEntries, err := os.ReadDir(cacheDir) + if err != nil { return err } + cacheFiles := make([]os.FileInfo, 0, len(dirEntries)) + for _, dirEntry := range dirEntries { + info, err := dirEntry.Info() + + if err != nil { + return err + } + + cacheFiles = append(cacheFiles, info) + } + for _, f := range cacheFiles { if f.ModTime().Add(cacheTime).Before(time.Now()) { klog.V(4).Infof("Removing cache file %s, because it is older than %s", f.Name(), cacheTime.String()) diff --git a/pkg/util/util.go b/pkg/util/util.go index 02ad0af..ca8ab69 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -22,7 +22,6 @@ import ( "crypto/rand" "fmt" "io" - "io/ioutil" "math/big" "net" "net/http" @@ -789,7 +788,7 @@ func HTTPGetRequest(request HTTPRequestParams, cacheFor int) ([]byte, error) { } // Process http response - bytes, err := ioutil.ReadAll(resp.Body) + bytes, err := io.ReadAll(resp.Body) if err != nil { return nil, err } @@ -825,11 +824,23 @@ func FilterIgnores(filesChanged, filesDeleted, absIgnoreRules []string) (filesCh // IsValidProjectDir checks that the folder to download the project from devfile is // either empty or only contains the devfile used. func IsValidProjectDir(path string, devfilePath string) error { - files, err := ioutil.ReadDir(path) + dirEntries, err := os.ReadDir(path) + if err != nil { return err } + files := make([]os.FileInfo, 0, len(dirEntries)) + for _, dirEntry := range dirEntries { + info, err := dirEntry.Info() + + if err != nil { + return err + } + + files = append(files, info) + } + if len(files) > 1 { return errors.Errorf("Folder %s is not empty. It can only contain the devfile used.", path) } else if len(files) == 1 { @@ -1050,7 +1061,7 @@ func DownloadFileInMemory(url string) ([]byte, error) { } defer resp.Body.Close() - return ioutil.ReadAll(resp.Body) + return io.ReadAll(resp.Body) } // ValidateK8sResourceName sanitizes kubernetes resource name with the following requirements: diff --git a/tests/v2/integrationTest/integration_test.go b/tests/v2/integrationTest/integration_test.go index 0f4b66e..d001ea8 100644 --- a/tests/v2/integrationTest/integration_test.go +++ b/tests/v2/integrationTest/integration_test.go @@ -3,7 +3,7 @@ package api import ( "context" "fmt" - "io/ioutil" + "os" "regexp" "testing" @@ -44,12 +44,23 @@ func getTestDevfileList(subdir string, testValid bool) ([]string, error) { fileList := make([]string, 0) subdirPath := "../../examples/source/devfiles/" + subdir + "/" - files, err := ioutil.ReadDir(subdirPath) + dirEntries, err := os.ReadDir(subdirPath) if err != nil { commonUtils.LogErrorMessage(fmt.Sprintf("Error in getting file list from the directory: %s : %v", subdirPath, err)) } + files := make([]os.FileInfo, 0, len(dirEntries)) + for _, dirEntry := range dirEntries { + info, err := dirEntry.Info() + + if err != nil { + commonUtils.LogErrorMessage(fmt.Sprintf("Error in getting file information from file list in: %s : %v", subdirPath, err)) + } + + files = append(files, info) + } + for _, file := range files { if !file.IsDir() { r, err := regexp.MatchString("^devfile.+yaml$", file.Name()) diff --git a/tests/v2/utils/library/command_test_utils.go b/tests/v2/utils/library/command_test_utils.go index 020f3a5..f36d36f 100644 --- a/tests/v2/utils/library/command_test_utils.go +++ b/tests/v2/utils/library/command_test_utils.go @@ -18,7 +18,7 @@ package utils import ( "errors" "fmt" - "io/ioutil" + "os" "github.com/google/go-cmp/cmp" "sigs.k8s.io/yaml" @@ -81,7 +81,7 @@ func VerifyCommands(devfile *commonUtils.TestDevfile, parserCommands []schema.Co if err != nil { errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", parserFilename))) } else { - err = ioutil.WriteFile(parserFilename, c, 0644) + err = os.WriteFile(parserFilename, c, 0644) if err != nil { errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......write devfile %s", parserFilename))) } @@ -91,7 +91,7 @@ func VerifyCommands(devfile *commonUtils.TestDevfile, parserCommands []schema.Co if err != nil { errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", testFilename))) } else { - err = ioutil.WriteFile(testFilename, c, 0644) + err = os.WriteFile(testFilename, c, 0644) if err != nil { errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......write devfile %s", testFilename))) } diff --git a/tests/v2/utils/library/component_test_utils.go b/tests/v2/utils/library/component_test_utils.go index 45afc23..f9df63e 100644 --- a/tests/v2/utils/library/component_test_utils.go +++ b/tests/v2/utils/library/component_test_utils.go @@ -18,7 +18,7 @@ package utils import ( "errors" "fmt" - "io/ioutil" + "os" schema "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" commonUtils "github.com/devfile/api/v2/test/v200/utils/common" @@ -87,7 +87,7 @@ func VerifyComponents(devfile *commonUtils.TestDevfile, parserComponents []schem if err != nil { errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", parserFilename))) } else { - err = ioutil.WriteFile(parserFilename, c, 0644) + err = os.WriteFile(parserFilename, c, 0644) if err != nil { errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......write devfile %s", parserFilename))) } @@ -97,7 +97,7 @@ func VerifyComponents(devfile *commonUtils.TestDevfile, parserComponents []schem if err != nil { errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", testFilename))) } else { - err = ioutil.WriteFile(testFilename, c, 0644) + err = os.WriteFile(testFilename, c, 0644) if err != nil { errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......write devfile %s", testFilename))) } diff --git a/tests/v2/utils/library/project_test_utils.go b/tests/v2/utils/library/project_test_utils.go index cac9c7d..aa8bc0b 100644 --- a/tests/v2/utils/library/project_test_utils.go +++ b/tests/v2/utils/library/project_test_utils.go @@ -18,7 +18,7 @@ package utils import ( "errors" "fmt" - "io/ioutil" + "os" "github.com/google/go-cmp/cmp" "sigs.k8s.io/yaml" @@ -105,7 +105,7 @@ func VerifyProjects(devfile *commonUtils.TestDevfile, parserProjects []schema.Pr if err != nil { errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", parserFilename))) } else { - err = ioutil.WriteFile(parserFilename, c, 0644) + err = os.WriteFile(parserFilename, c, 0644) if err != nil { errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......write devfile %s", parserFilename))) } @@ -115,7 +115,7 @@ func VerifyProjects(devfile *commonUtils.TestDevfile, parserProjects []schema.Pr if err != nil { errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", testFilename))) } else { - err = ioutil.WriteFile(testFilename, c, 0644) + err = os.WriteFile(testFilename, c, 0644) if err != nil { errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......write devfile %s", testFilename))) } @@ -166,7 +166,7 @@ func VerifyStarterProjects(devfile *commonUtils.TestDevfile, parserStarterProjec if err != nil { errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", parserFilename))) } else { - err = ioutil.WriteFile(parserFilename, c, 0644) + err = os.WriteFile(parserFilename, c, 0644) if err != nil { errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......write devfile %s", parserFilename))) } @@ -176,7 +176,7 @@ func VerifyStarterProjects(devfile *commonUtils.TestDevfile, parserStarterProjec if err != nil { errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", testFilename))) } else { - err = ioutil.WriteFile(testFilename, c, 0644) + err = os.WriteFile(testFilename, c, 0644) if err != nil { errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......write devfile %s", testFilename))) }