| 1 |
// Copyright 2018 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 |
"encoding/json" |
| 19 |
"net" |
| 20 |
"reflect" |
| 21 |
|
| 22 |
. "github.com/onsi/ginkgo/v2" |
| 23 |
. "github.com/onsi/gomega" |
| 24 |
|
| 25 |
"github.com/containernetworking/cni/pkg/types" |
| 26 |
cniv1 "github.com/containernetworking/cni/pkg/types/100" |
| 27 |
"github.com/containernetworking/cni/pkg/version" |
| 28 |
) |
| 29 |
|
| 30 |
var _ = Describe("Version operations", func() { |
| 31 |
It("computes a list of versions correctly", func() { |
| 32 |
actual := version.VersionsStartingFrom("0.3.1") |
| 33 |
Expect(actual.SupportedVersions()).To(Equal([]string{"0.3.1", "0.4.0", "1.0.0", "1.1.0"})) |
| 34 |
}) |
| 35 |
|
| 36 |
Context("when a prevResult is available", func() { |
| 37 |
It("parses the prevResult", func() { |
| 38 |
rawBytes := []byte(`{ |
| 39 |
"cniVersion": "1.0.0", |
| 40 |
"interfaces": [ |
| 41 |
{ |
| 42 |
"name": "eth0", |
| 43 |
"mac": "00:11:22:33:44:55", |
| 44 |
"sandbox": "/proc/3553/ns/net", |
| 45 |
"pciID": "8086:9a01", |
| 46 |
"socketPath": "/path/to/vhost/fd" |
| 47 |
} |
| 48 |
], |
| 49 |
"ips": [ |
| 50 |
{ |
| 51 |
"version": "4", |
| 52 |
"interface": 0, |
| 53 |
"address": "1.2.3.30/24", |
| 54 |
"gateway": "1.2.3.1" |
| 55 |
} |
| 56 |
] |
| 57 |
}`) |
| 58 |
var raw map[string]interface{} |
| 59 |
err := json.Unmarshal(rawBytes, &raw) |
| 60 |
Expect(err).NotTo(HaveOccurred()) |
| 61 |
|
| 62 |
conf := &types.PluginConf{ |
| 63 |
CNIVersion: "1.0.0", |
| 64 |
Name: "foobar", |
| 65 |
Type: "baz", |
| 66 |
RawPrevResult: raw, |
| 67 |
} |
| 68 |
|
| 69 |
err = version.ParsePrevResult(conf) |
| 70 |
Expect(err).NotTo(HaveOccurred()) |
| 71 |
expectedResult := &cniv1.Result{ |
| 72 |
CNIVersion: "1.0.0", |
| 73 |
Interfaces: []*cniv1.Interface{ |
| 74 |
{ |
| 75 |
Name: "eth0", |
| 76 |
Mac: "00:11:22:33:44:55", |
| 77 |
Sandbox: "/proc/3553/ns/net", |
| 78 |
PciID: "8086:9a01", |
| 79 |
SocketPath: "/path/to/vhost/fd", |
| 80 |
}, |
| 81 |
}, |
| 82 |
IPs: []*cniv1.IPConfig{ |
| 83 |
{ |
| 84 |
Interface: cniv1.Int(0), |
| 85 |
Address: net.IPNet{ |
| 86 |
IP: net.ParseIP("1.2.3.30"), |
| 87 |
Mask: net.IPv4Mask(255, 255, 255, 0), |
| 88 |
}, |
| 89 |
Gateway: net.ParseIP("1.2.3.1"), |
| 90 |
}, |
| 91 |
}, |
| 92 |
} |
| 93 |
Expect(reflect.DeepEqual(conf.PrevResult, expectedResult)).To(BeTrue()) |
| 94 |
}) |
| 95 |
|
| 96 |
It("fails if the prevResult version is unknown", func() { |
| 97 |
conf := &types.PluginConf{ |
| 98 |
CNIVersion: version.Current(), |
| 99 |
Name: "foobar", |
| 100 |
Type: "baz", |
| 101 |
RawPrevResult: map[string]interface{}{ |
| 102 |
"cniVersion": "5678.456", |
| 103 |
}, |
| 104 |
} |
| 105 |
|
| 106 |
err := version.ParsePrevResult(conf) |
| 107 |
Expect(err).To(MatchError(`could not parse prevResult: result type supports [1.0.0 1.1.0] but unmarshalled CNIVersion is "5678.456"`)) |
| 108 |
}) |
| 109 |
|
| 110 |
It("fails if the prevResult version does not match the prevResult version", func() { |
| 111 |
conf := &types.PluginConf{ |
| 112 |
CNIVersion: version.Current(), |
| 113 |
Name: "foobar", |
| 114 |
Type: "baz", |
| 115 |
RawPrevResult: map[string]interface{}{ |
| 116 |
"cniVersion": "0.2.0", |
| 117 |
"ip4": map[string]interface{}{ |
| 118 |
"ip": "1.2.3.30/24", |
| 119 |
"gateway": "1.2.3.1", |
| 120 |
}, |
| 121 |
}, |
| 122 |
} |
| 123 |
|
| 124 |
err := version.ParsePrevResult(conf) |
| 125 |
Expect(err).To(MatchError("could not parse prevResult: result type supports [1.0.0 1.1.0] but unmarshalled CNIVersion is \"0.2.0\"")) |
| 126 |
}) |
| 127 |
}) |
| 128 |
|
| 129 |
Context("when a prevResult is not available", func() { |
| 130 |
It("does not fail", func() { |
| 131 |
conf := &types.PluginConf{ |
| 132 |
CNIVersion: version.Current(), |
| 133 |
Name: "foobar", |
| 134 |
Type: "baz", |
| 135 |
} |
| 136 |
|
| 137 |
err := version.ParsePrevResult(conf) |
| 138 |
Expect(err).NotTo(HaveOccurred()) |
| 139 |
Expect(conf.PrevResult).To(BeNil()) |
| 140 |
}) |
| 141 |
}) |
| 142 |
|
| 143 |
Context("version parsing", func() { |
| 144 |
It("parses versions correctly", func() { |
| 145 |
v1 := "1.1.0" |
| 146 |
v2 := "1.1.1" |
| 147 |
|
| 148 |
check := func(a, b string, want bool) { |
| 149 |
GinkgoHelper() |
| 150 |
gt, err := version.GreaterThan(a, b) |
| 151 |
Expect(err).NotTo(HaveOccurred()) |
| 152 |
Expect(gt).To(Equal(want)) |
| 153 |
} |
| 154 |
|
| 155 |
check(v1, v2, false) |
| 156 |
check(v2, v1, true) |
| 157 |
check(v2, v2, false) |
| 158 |
}) |
| 159 |
}) |
| 160 |
}) |