From c2af294271b96d938e12cef71d96ebfa3cdbe461 Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Thu, 30 Jun 2022 13:36:59 -0400 Subject: [PATCH] version: don't wipe if filename is empty Signed-off-by: Peter Hunt (cherry picked from commit 935652c90c9f87e001f9453a5b102d538edc06f7) --- internal/version/version.go | 15 +++++++-------- internal/version/version_test.go | 6 +++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/internal/version/version.go b/internal/version/version.go index 14244ed445f..55d92e4f791 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -1,9 +1,7 @@ package version import ( - "bufio" "fmt" - "io/ioutil" "os" "path/filepath" "reflect" @@ -56,14 +54,12 @@ func ShouldCrioWipe(versionFileName string) (bool, error) { // shouldCrioWipe is an internal function for testing purposes func shouldCrioWipe(versionFileName, versionString string) (bool, error) { - f, err := os.Open(versionFileName) - if err != nil { - return true, errors.Errorf("version file %s not found: %v", versionFileName, err) + if versionFileName == "" { + return false, nil } - r := bufio.NewReader(f) - versionBytes, err := ioutil.ReadAll(r) + versionBytes, err := os.ReadFile(versionFileName) if err != nil { - return true, errors.Errorf("reading version file %s failed: %v", versionFileName, err) + return true, errors.Errorf("version file %s not found: %v", versionFileName, err) } // parse the version that was laid down by a previous invocation of crio @@ -99,6 +95,9 @@ func LogVersion() { // writeVersionFile is an internal function for testing purposes func writeVersionFile(file, gitCommit, version string) error { + if file == "" { + return nil + } current, err := parseVersionConstant(version, gitCommit) // Sanity check-this should never happen if err != nil { diff --git a/internal/version/version_test.go b/internal/version/version_test.go index ec031167df3..bb71d7604ad 100644 --- a/internal/version/version_test.go +++ b/internal/version/version_test.go @@ -74,10 +74,10 @@ var _ = t.Describe("Version", func() { _, err = ioutil.ReadFile(filename) Expect(err).To(BeNil()) }) - It("should fail to upgrade with unspecified version", func() { + It("should not wipe with empty version file", func() { upgrade, err := shouldCrioWipe("", tempVersion) - Expect(upgrade).To(BeTrue()) - Expect(err).ToNot(BeNil()) + Expect(upgrade).To(BeFalse()) + Expect(err).To(BeNil()) }) It("should fail to upgrade with empty version file", func() { tempFileName := tempFileName