Skip to content

Commit

Permalink
Merge pull request #893 from squeed/handle-empty-version
Browse files Browse the repository at this point in the history
libcni: handle empty version when parsing version
  • Loading branch information
dcbw authored Apr 27, 2022
2 parents f32e3df + 1054f8e commit 08f8596
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions libcni/backwards_compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/version/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ".")
Expand Down
10 changes: 9 additions & 1 deletion pkg/version/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 08f8596

Please sign in to comment.