From 1054f8ead7e5239ba09653e6fdc05a9ce8084ad7 Mon Sep 17 00:00:00 2001 From: Casey Callendrello Date: Wed, 27 Apr 2022 21:41:40 +0200 Subject: [PATCH] libcni: handle empty version when parsing version Without this Delete failed for empty-version configs. Update the tests to catch this case. Signed-off-by: Casey Callendrello --- libcni/backwards_compatibility_test.go | 3 +++ pkg/version/plugin.go | 4 ++-- pkg/version/plugin_test.go | 10 +++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/libcni/backwards_compatibility_test.go b/libcni/backwards_compatibility_test.go index 0bba36fa..dabd8721 100644 --- a/libcni/backwards_compatibility_test.go +++ b/libcni/backwards_compatibility_test.go @@ -66,6 +66,9 @@ var _ = Describe("Backwards compatibility", func() { Expect(result).To(Equal(legacy_examples.ExpectedResult)) + err = cniConfig.DelNetwork(context.TODO(), netConf, runtimeConf) + Expect(err).NotTo(HaveOccurred()) + Expect(os.RemoveAll(pluginPath)).To(Succeed()) }) diff --git a/pkg/version/plugin.go b/pkg/version/plugin.go index d4bc9d16..17b22b6b 100644 --- a/pkg/version/plugin.go +++ b/pkg/version/plugin.go @@ -86,8 +86,8 @@ func (*PluginDecoder) Decode(jsonBytes []byte) (PluginInfo, error) { // minor, and micro numbers or returns an error func ParseVersion(version string) (int, int, int, error) { var major, minor, micro int - if version == "" { - return -1, -1, -1, fmt.Errorf("invalid version %q: the version is empty", version) + if version == "" { // special case: no version declared == v0.1.0 + return 0, 1, 0, nil } parts := strings.Split(version, ".") diff --git a/pkg/version/plugin_test.go b/pkg/version/plugin_test.go index 2d1da2d4..99bf6cad 100644 --- a/pkg/version/plugin_test.go +++ b/pkg/version/plugin_test.go @@ -91,8 +91,16 @@ var _ = Describe("Decoding versions reported by a plugin", func() { Expect(micro).To(Equal(3)) }) + It("parses an empty string as v0.1.0", func() { + major, minor, micro, err := version.ParseVersion("") + Expect(err).NotTo(HaveOccurred()) + Expect(major).To(Equal(0)) + Expect(minor).To(Equal(1)) + Expect(micro).To(Equal(0)) + }) + It("returns an error for malformed versions", func() { - badVersions := []string{"asdfasdf", "asdf.", ".asdfas", "asdf.adsf.", "0.", "..", "1.2.3.4.5", ""} + badVersions := []string{"asdfasdf", "asdf.", ".asdfas", "asdf.adsf.", "0.", "..", "1.2.3.4.5"} for _, v := range badVersions { _, _, _, err := version.ParseVersion(v) Expect(err).To(HaveOccurred())