| 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 types020_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"fmt" |
| 20 |
"net" |
| 21 |
|
| 22 |
. "github.com/onsi/ginkgo/v2" |
| 23 |
. "github.com/onsi/gomega" |
| 24 |
|
| 25 |
"github.com/containernetworking/cni/pkg/types" |
| 26 |
"github.com/containernetworking/cni/pkg/types/020" |
| 27 |
"github.com/containernetworking/cni/pkg/types/create" |
| 28 |
) |
| 29 |
|
| 30 |
func testResult(resultCNIVersion, jsonCNIVersion string) (*types020.Result, string) { |
| 31 |
ipv4, err := types.ParseCIDR("1.2.3.30/24") |
| 32 |
Expect(err).NotTo(HaveOccurred()) |
| 33 |
Expect(ipv4).NotTo(BeNil()) |
| 34 |
|
| 35 |
routegwv4, routev4, err := net.ParseCIDR("15.5.6.8/24") |
| 36 |
Expect(err).NotTo(HaveOccurred()) |
| 37 |
Expect(routev4).NotTo(BeNil()) |
| 38 |
Expect(routegwv4).NotTo(BeNil()) |
| 39 |
|
| 40 |
ipv6, err := types.ParseCIDR("abcd:1234:ffff::cdde/64") |
| 41 |
Expect(err).NotTo(HaveOccurred()) |
| 42 |
Expect(ipv6).NotTo(BeNil()) |
| 43 |
|
| 44 |
routegwv6, routev6, err := net.ParseCIDR("1111:dddd::aaaa/80") |
| 45 |
Expect(err).NotTo(HaveOccurred()) |
| 46 |
Expect(routev6).NotTo(BeNil()) |
| 47 |
Expect(routegwv6).NotTo(BeNil()) |
| 48 |
|
| 49 |
// Set every field of the struct to ensure source compatibility |
| 50 |
res := &types020.Result{ |
| 51 |
CNIVersion: resultCNIVersion, |
| 52 |
IP4: &types020.IPConfig{ |
| 53 |
IP: *ipv4, |
| 54 |
Gateway: net.ParseIP("1.2.3.1"), |
| 55 |
Routes: []types.Route{ |
| 56 |
{Dst: *routev4, GW: routegwv4}, |
| 57 |
}, |
| 58 |
}, |
| 59 |
IP6: &types020.IPConfig{ |
| 60 |
IP: *ipv6, |
| 61 |
Gateway: net.ParseIP("abcd:1234:ffff::1"), |
| 62 |
Routes: []types.Route{ |
| 63 |
{Dst: *routev6, GW: routegwv6}, |
| 64 |
}, |
| 65 |
}, |
| 66 |
DNS: types.DNS{ |
| 67 |
Nameservers: []string{"1.2.3.4", "1::cafe"}, |
| 68 |
Domain: "acompany.com", |
| 69 |
Search: []string{"somedomain.com", "otherdomain.net"}, |
| 70 |
Options: []string{"foo", "bar"}, |
| 71 |
}, |
| 72 |
} |
| 73 |
|
| 74 |
json := fmt.Sprintf(`{ |
| 75 |
"cniVersion": "%s", |
| 76 |
"ip4": { |
| 77 |
"ip": "1.2.3.30/24", |
| 78 |
"gateway": "1.2.3.1", |
| 79 |
"routes": [ |
| 80 |
{ |
| 81 |
"dst": "15.5.6.0/24", |
| 82 |
"gw": "15.5.6.8" |
| 83 |
} |
| 84 |
] |
| 85 |
}, |
| 86 |
"ip6": { |
| 87 |
"ip": "abcd:1234:ffff::cdde/64", |
| 88 |
"gateway": "abcd:1234:ffff::1", |
| 89 |
"routes": [ |
| 90 |
{ |
| 91 |
"dst": "1111:dddd::/80", |
| 92 |
"gw": "1111:dddd::aaaa" |
| 93 |
} |
| 94 |
] |
| 95 |
}, |
| 96 |
"dns": { |
| 97 |
"nameservers": [ |
| 98 |
"1.2.3.4", |
| 99 |
"1::cafe" |
| 100 |
], |
| 101 |
"domain": "acompany.com", |
| 102 |
"search": [ |
| 103 |
"somedomain.com", |
| 104 |
"otherdomain.net" |
| 105 |
], |
| 106 |
"options": [ |
| 107 |
"foo", |
| 108 |
"bar" |
| 109 |
] |
| 110 |
} |
| 111 |
}`, jsonCNIVersion) |
| 112 |
|
| 113 |
return res, json |
| 114 |
} |
| 115 |
|
| 116 |
var _ = Describe("Ensures compatibility with the 0.1.0/0.2.0 spec", func() { |
| 117 |
It("correctly encodes a 0.2.0 Result", func() { |
| 118 |
res, expectedJSON := testResult(types020.ImplementedSpecVersion, types020.ImplementedSpecVersion) |
| 119 |
out, err := json.Marshal(res) |
| 120 |
Expect(err).NotTo(HaveOccurred()) |
| 121 |
Expect(out).To(MatchJSON(expectedJSON)) |
| 122 |
}) |
| 123 |
|
| 124 |
It("correctly encodes a 0.1.0 Result", func() { |
| 125 |
res, expectedJSON := testResult("0.1.0", "0.1.0") |
| 126 |
out, err := json.Marshal(res) |
| 127 |
Expect(err).NotTo(HaveOccurred()) |
| 128 |
Expect(out).To(MatchJSON(expectedJSON)) |
| 129 |
}) |
| 130 |
|
| 131 |
It("converts a 0.2.0 result to 0.1.0", func() { |
| 132 |
res, expectedJSON := testResult(types020.ImplementedSpecVersion, "0.1.0") |
| 133 |
res010, err := res.GetAsVersion("0.1.0") |
| 134 |
Expect(err).NotTo(HaveOccurred()) |
| 135 |
out, err := json.Marshal(res010) |
| 136 |
Expect(err).NotTo(HaveOccurred()) |
| 137 |
Expect(out).To(MatchJSON(expectedJSON)) |
| 138 |
}) |
| 139 |
|
| 140 |
It("converts a 0.1.0 result to 0.2.0", func() { |
| 141 |
res, expectedJSON := testResult("0.1.0", types020.ImplementedSpecVersion) |
| 142 |
res020, err := res.GetAsVersion(types020.ImplementedSpecVersion) |
| 143 |
Expect(err).NotTo(HaveOccurred()) |
| 144 |
out, err := json.Marshal(res020) |
| 145 |
Expect(err).NotTo(HaveOccurred()) |
| 146 |
Expect(out).To(MatchJSON(expectedJSON)) |
| 147 |
}) |
| 148 |
|
| 149 |
It("creates a 0.1.0 result passing CNIVersion ''", func() { |
| 150 |
_, expectedJSON := testResult("", "") |
| 151 |
resT, err := create.Create("", []byte(expectedJSON)) |
| 152 |
Expect(err).NotTo(HaveOccurred()) |
| 153 |
res010, ok := resT.(*types020.Result) |
| 154 |
Expect(ok).To(BeTrue()) |
| 155 |
Expect(res010.CNIVersion).To(Equal("0.1.0")) |
| 156 |
}) |
| 157 |
}) |