github.com/john-lin/cni@v0.6.0-rc1.0.20170712150331-b69e640cc0e2/pkg/types/current/types_test.go (about) 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 current_test 16 17 import ( 18 "encoding/json" 19 "io/ioutil" 20 "net" 21 "os" 22 23 "github.com/containernetworking/cni/pkg/types" 24 "github.com/containernetworking/cni/pkg/types/current" 25 26 . "github.com/onsi/ginkgo" 27 . "github.com/onsi/gomega" 28 ) 29 30 func testResult() *current.Result { 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 return ¤t.Result{ 51 CNIVersion: "0.3.1", 52 Interfaces: []*current.Interface{ 53 { 54 Name: "eth0", 55 Mac: "00:11:22:33:44:55", 56 Sandbox: "/proc/3553/ns/net", 57 }, 58 }, 59 IPs: []*current.IPConfig{ 60 { 61 Version: "4", 62 Interface: current.Int(0), 63 Address: *ipv4, 64 Gateway: net.ParseIP("1.2.3.1"), 65 }, 66 { 67 Version: "6", 68 Interface: current.Int(0), 69 Address: *ipv6, 70 Gateway: net.ParseIP("abcd:1234:ffff::1"), 71 }, 72 }, 73 Routes: []*types.Route{ 74 {Dst: *routev4, GW: routegwv4}, 75 {Dst: *routev6, GW: routegwv6}, 76 }, 77 DNS: types.DNS{ 78 Nameservers: []string{"1.2.3.4", "1::cafe"}, 79 Domain: "acompany.com", 80 Search: []string{"somedomain.com", "otherdomain.net"}, 81 Options: []string{"foo", "bar"}, 82 }, 83 } 84 } 85 86 var _ = Describe("Current types operations", func() { 87 It("correctly encodes a 0.3.x Result", func() { 88 res := testResult() 89 90 // Redirect stdout to capture JSON result 91 oldStdout := os.Stdout 92 r, w, err := os.Pipe() 93 Expect(err).NotTo(HaveOccurred()) 94 95 os.Stdout = w 96 err = res.Print() 97 w.Close() 98 Expect(err).NotTo(HaveOccurred()) 99 100 // parse the result 101 out, err := ioutil.ReadAll(r) 102 os.Stdout = oldStdout 103 Expect(err).NotTo(HaveOccurred()) 104 105 Expect(string(out)).To(MatchJSON(`{ 106 "cniVersion": "0.3.1", 107 "interfaces": [ 108 { 109 "name": "eth0", 110 "mac": "00:11:22:33:44:55", 111 "sandbox": "/proc/3553/ns/net" 112 } 113 ], 114 "ips": [ 115 { 116 "version": "4", 117 "interface": 0, 118 "address": "1.2.3.30/24", 119 "gateway": "1.2.3.1" 120 }, 121 { 122 "version": "6", 123 "interface": 0, 124 "address": "abcd:1234:ffff::cdde/64", 125 "gateway": "abcd:1234:ffff::1" 126 } 127 ], 128 "routes": [ 129 { 130 "dst": "15.5.6.0/24", 131 "gw": "15.5.6.8" 132 }, 133 { 134 "dst": "1111:dddd::/80", 135 "gw": "1111:dddd::aaaa" 136 } 137 ], 138 "dns": { 139 "nameservers": [ 140 "1.2.3.4", 141 "1::cafe" 142 ], 143 "domain": "acompany.com", 144 "search": [ 145 "somedomain.com", 146 "otherdomain.net" 147 ], 148 "options": [ 149 "foo", 150 "bar" 151 ] 152 } 153 }`)) 154 }) 155 156 It("correctly encodes a 0.1.0 Result", func() { 157 res, err := testResult().GetAsVersion("0.1.0") 158 Expect(err).NotTo(HaveOccurred()) 159 160 Expect(res.String()).To(Equal("IP4:{IP:{IP:1.2.3.30 Mask:ffffff00} Gateway:1.2.3.1 Routes:[{Dst:{IP:15.5.6.0 Mask:ffffff00} GW:15.5.6.8}]}, IP6:{IP:{IP:abcd:1234:ffff::cdde Mask:ffffffffffffffff0000000000000000} Gateway:abcd:1234:ffff::1 Routes:[{Dst:{IP:1111:dddd:: Mask:ffffffffffffffffffff000000000000} GW:1111:dddd::aaaa}]}, DNS:{Nameservers:[1.2.3.4 1::cafe] Domain:acompany.com Search:[somedomain.com otherdomain.net] Options:[foo bar]}")) 161 162 // Redirect stdout to capture JSON result 163 oldStdout := os.Stdout 164 r, w, err := os.Pipe() 165 Expect(err).NotTo(HaveOccurred()) 166 167 os.Stdout = w 168 err = res.Print() 169 w.Close() 170 Expect(err).NotTo(HaveOccurred()) 171 172 // parse the result 173 out, err := ioutil.ReadAll(r) 174 os.Stdout = oldStdout 175 Expect(err).NotTo(HaveOccurred()) 176 177 Expect(string(out)).To(MatchJSON(`{ 178 "cniVersion": "0.2.0", 179 "ip4": { 180 "ip": "1.2.3.30/24", 181 "gateway": "1.2.3.1", 182 "routes": [ 183 { 184 "dst": "15.5.6.0/24", 185 "gw": "15.5.6.8" 186 } 187 ] 188 }, 189 "ip6": { 190 "ip": "abcd:1234:ffff::cdde/64", 191 "gateway": "abcd:1234:ffff::1", 192 "routes": [ 193 { 194 "dst": "1111:dddd::/80", 195 "gw": "1111:dddd::aaaa" 196 } 197 ] 198 }, 199 "dns": { 200 "nameservers": [ 201 "1.2.3.4", 202 "1::cafe" 203 ], 204 "domain": "acompany.com", 205 "search": [ 206 "somedomain.com", 207 "otherdomain.net" 208 ], 209 "options": [ 210 "foo", 211 "bar" 212 ] 213 } 214 }`)) 215 }) 216 217 It("correctly marshals interface index 0", func() { 218 ipc := ¤t.IPConfig{ 219 Version: "4", 220 Interface: current.Int(0), 221 Address: net.IPNet{ 222 IP: net.ParseIP("10.1.2.3"), 223 Mask: net.IPv4Mask(255, 255, 255, 0), 224 }, 225 } 226 227 json, err := json.Marshal(ipc) 228 Expect(err).NotTo(HaveOccurred()) 229 Expect(json).To(MatchJSON(`{ 230 "version": "4", 231 "interface": 0, 232 "address": "10.1.2.3/24" 233 }`)) 234 }) 235 236 It("correctly marshals a missing interface index", func() { 237 ipc := ¤t.IPConfig{ 238 Version: "4", 239 Address: net.IPNet{ 240 IP: net.ParseIP("10.1.2.3"), 241 Mask: net.IPv4Mask(255, 255, 255, 0), 242 }, 243 } 244 245 json, err := json.Marshal(ipc) 246 Expect(err).NotTo(HaveOccurred()) 247 Expect(json).To(MatchJSON(`{ 248 "version": "4", 249 "address": "10.1.2.3/24" 250 }`)) 251 }) 252 })