| 1 |
// Copyright 2016 CNI authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package version_test |
| 16 |
|
| 17 |
import ( |
| 18 |
. "github.com/onsi/ginkgo/v2" |
| 19 |
. "github.com/onsi/gomega" |
| 20 |
|
| 21 |
"github.com/containernetworking/cni/pkg/version" |
| 22 |
) |
| 23 |
|
| 24 |
var _ = Describe("Decoding versions reported by a plugin", func() { |
| 25 |
var ( |
| 26 |
decoder *version.PluginDecoder |
| 27 |
versionStdout []byte |
| 28 |
) |
| 29 |
|
| 30 |
BeforeEach(func() { |
| 31 |
decoder = &version.PluginDecoder{} |
| 32 |
versionStdout = []byte(`{ |
| 33 |
"cniVersion": "some-library-version", |
| 34 |
"supportedVersions": [ "some-version", "some-other-version" ] |
| 35 |
}`) |
| 36 |
}) |
| 37 |
|
| 38 |
It("returns a PluginInfo that represents the given json bytes", func() { |
| 39 |
pluginInfo, err := decoder.Decode(versionStdout) |
| 40 |
Expect(err).NotTo(HaveOccurred()) |
| 41 |
Expect(pluginInfo).NotTo(BeNil()) |
| 42 |
Expect(pluginInfo.SupportedVersions()).To(Equal([]string{ |
| 43 |
"some-version", |
| 44 |
"some-other-version", |
| 45 |
})) |
| 46 |
}) |
| 47 |
|
| 48 |
Context("when the bytes cannot be decoded as json", func() { |
| 49 |
BeforeEach(func() { |
| 50 |
versionStdout = []byte(`{{{`) |
| 51 |
}) |
| 52 |
|
| 53 |
It("returns a meaningful error", func() { |
| 54 |
_, err := decoder.Decode(versionStdout) |
| 55 |
Expect(err).To(MatchError("decoding version info: invalid character '{' looking for beginning of object key string")) |
| 56 |
}) |
| 57 |
}) |
| 58 |
|
| 59 |
Context("when the json bytes are missing the required CNIVersion field", func() { |
| 60 |
BeforeEach(func() { |
| 61 |
versionStdout = []byte(`{ "supportedVersions": [ "foo" ] }`) |
| 62 |
}) |
| 63 |
|
| 64 |
It("returns a meaningful error", func() { |
| 65 |
_, err := decoder.Decode(versionStdout) |
| 66 |
Expect(err).To(MatchError("decoding version info: missing field cniVersion")) |
| 67 |
}) |
| 68 |
}) |
| 69 |
|
| 70 |
Context("when there are no supported versions", func() { |
| 71 |
BeforeEach(func() { |
| 72 |
versionStdout = []byte(`{ "cniVersion": "0.2.0" }`) |
| 73 |
}) |
| 74 |
|
| 75 |
It("assumes that the supported versions are 0.1.0 and 0.2.0", func() { |
| 76 |
pluginInfo, err := decoder.Decode(versionStdout) |
| 77 |
Expect(err).NotTo(HaveOccurred()) |
| 78 |
Expect(pluginInfo).NotTo(BeNil()) |
| 79 |
Expect(pluginInfo.SupportedVersions()).To(Equal([]string{ |
| 80 |
"0.1.0", |
| 81 |
"0.2.0", |
| 82 |
})) |
| 83 |
}) |
| 84 |
}) |
| 85 |
|
| 86 |
Describe("ParseVersion", func() { |
| 87 |
It("parses a valid version correctly", func() { |
| 88 |
major, minor, micro, err := version.ParseVersion("1.2.3") |
| 89 |
Expect(err).NotTo(HaveOccurred()) |
| 90 |
Expect(major).To(Equal(1)) |
| 91 |
Expect(minor).To(Equal(2)) |
| 92 |
Expect(micro).To(Equal(3)) |
| 93 |
}) |
| 94 |
|
| 95 |
It("parses an empty string as v0.1.0", func() { |
| 96 |
major, minor, micro, err := version.ParseVersion("") |
| 97 |
Expect(err).NotTo(HaveOccurred()) |
| 98 |
Expect(major).To(Equal(0)) |
| 99 |
Expect(minor).To(Equal(1)) |
| 100 |
Expect(micro).To(Equal(0)) |
| 101 |
}) |
| 102 |
|
| 103 |
It("returns an error for malformed versions", func() { |
| 104 |
badVersions := []string{"asdfasdf", "asdf.", ".asdfas", "asdf.adsf.", "0.", "..", "1.2.3.4.5"} |
| 105 |
for _, v := range badVersions { |
| 106 |
_, _, _, err := version.ParseVersion(v) |
| 107 |
Expect(err).To(HaveOccurred()) |
| 108 |
} |
| 109 |
}) |
| 110 |
}) |
| 111 |
|
| 112 |
Describe("GreaterThanOrEqualTo", func() { |
| 113 |
It("correctly compares versions", func() { |
| 114 |
versions := [][2]string{ |
| 115 |
{"1.2.34", "1.2.14"}, |
| 116 |
{"2.5.4", "2.4.4"}, |
| 117 |
{"1.2.3", "0.2.3"}, |
| 118 |
{"0.4.0", "0.3.1"}, |
| 119 |
} |
| 120 |
for _, v := range versions { |
| 121 |
// Make sure the first is greater than the second |
| 122 |
gt, err := version.GreaterThanOrEqualTo(v[0], v[1]) |
| 123 |
Expect(err).NotTo(HaveOccurred()) |
| 124 |
Expect(gt).To(BeTrue()) |
| 125 |
|
| 126 |
// And the opposite |
| 127 |
gt, err = version.GreaterThanOrEqualTo(v[1], v[0]) |
| 128 |
Expect(err).NotTo(HaveOccurred()) |
| 129 |
Expect(gt).To(BeFalse()) |
| 130 |
} |
| 131 |
}) |
| 132 |
|
| 133 |
It("returns true when versions are the same", func() { |
| 134 |
gt, err := version.GreaterThanOrEqualTo("1.2.3", "1.2.3") |
| 135 |
Expect(err).NotTo(HaveOccurred()) |
| 136 |
Expect(gt).To(BeTrue()) |
| 137 |
}) |
| 138 |
|
| 139 |
It("returns an error for malformed versions", func() { |
| 140 |
versions := [][2]string{ |
| 141 |
{"1.2.34", "asdadf"}, |
| 142 |
{"adsfad", "2.5.4"}, |
| 143 |
} |
| 144 |
for _, v := range versions { |
| 145 |
_, err := version.GreaterThanOrEqualTo(v[0], v[1]) |
| 146 |
Expect(err).To(HaveOccurred()) |
| 147 |
} |
| 148 |
}) |
| 149 |
}) |
| 150 |
}) |