github.com/MikeZappa87/cni@v0.8.1/pkg/version/version_test.go (about) 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/containernetworking/cni/pkg/types" 23 "github.com/containernetworking/cni/pkg/types/current" 24 "github.com/containernetworking/cni/pkg/version" 25 26 . "github.com/onsi/ginkgo" 27 . "github.com/onsi/gomega" 28 ) 29 30 var _ = Describe("Version operations", func() { 31 Context("when a prevResult is available", func() { 32 It("parses the prevResult", func() { 33 rawBytes := []byte(`{ 34 "cniVersion": "0.3.0", 35 "interfaces": [ 36 { 37 "name": "eth0", 38 "mac": "00:11:22:33:44:55", 39 "sandbox": "/proc/3553/ns/net" 40 } 41 ], 42 "ips": [ 43 { 44 "version": "4", 45 "interface": 0, 46 "address": "1.2.3.30/24", 47 "gateway": "1.2.3.1" 48 } 49 ] 50 }`) 51 var raw map[string]interface{} 52 err := json.Unmarshal(rawBytes, &raw) 53 Expect(err).NotTo(HaveOccurred()) 54 55 conf := &types.NetConf{ 56 CNIVersion: "0.3.0", 57 Name: "foobar", 58 Type: "baz", 59 RawPrevResult: raw, 60 } 61 62 err = version.ParsePrevResult(conf) 63 Expect(err).NotTo(HaveOccurred()) 64 65 expectedResult := ¤t.Result{ 66 CNIVersion: "0.3.0", 67 Interfaces: []*current.Interface{ 68 { 69 Name: "eth0", 70 Mac: "00:11:22:33:44:55", 71 Sandbox: "/proc/3553/ns/net", 72 }, 73 }, 74 IPs: []*current.IPConfig{ 75 { 76 Version: "4", 77 Interface: current.Int(0), 78 Address: net.IPNet{ 79 IP: net.ParseIP("1.2.3.30"), 80 Mask: net.IPv4Mask(255, 255, 255, 0), 81 }, 82 Gateway: net.ParseIP("1.2.3.1"), 83 }, 84 }, 85 } 86 Expect(reflect.DeepEqual(conf.PrevResult, expectedResult)).To(BeTrue()) 87 }) 88 89 It("fails if the prevResult version is unknown", func() { 90 conf := &types.NetConf{ 91 CNIVersion: "0.3.0", 92 Name: "foobar", 93 Type: "baz", 94 RawPrevResult: map[string]interface{}{ 95 "cniVersion": "5678.456", 96 }, 97 } 98 99 err := version.ParsePrevResult(conf) 100 Expect(err).NotTo(HaveOccurred()) 101 }) 102 103 It("fails if the prevResult is invalid", func() { 104 conf := &types.NetConf{ 105 CNIVersion: "0.3.0", 106 Name: "foobar", 107 Type: "baz", 108 RawPrevResult: map[string]interface{}{ 109 "adsfasdfasdfasdfasdfaf": nil, 110 }, 111 } 112 113 err := version.ParsePrevResult(conf) 114 Expect(err).NotTo(HaveOccurred()) 115 }) 116 }) 117 118 Context("when a prevResult is not available", func() { 119 It("does not fail", func() { 120 conf := &types.NetConf{ 121 CNIVersion: "0.3.0", 122 Name: "foobar", 123 Type: "baz", 124 } 125 126 err := version.ParsePrevResult(conf) 127 Expect(err).NotTo(HaveOccurred()) 128 Expect(conf.PrevResult).To(BeNil()) 129 }) 130 }) 131 })