github.com/looshlee/cilium@v1.6.12/plugins/cilium-cni/types/types_test.go (about) 1 // Copyright 2019 Authors of Cilium 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 // +build !privileged_tests 16 17 package types 18 19 import ( 20 "io/ioutil" 21 "os" 22 "path" 23 "testing" 24 25 "github.com/cilium/cilium/pkg/checker" 26 ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2" 27 28 cnitypes "github.com/containernetworking/cni/pkg/types" 29 "gopkg.in/check.v1" 30 ) 31 32 func Test(t *testing.T) { 33 check.TestingT(t) 34 } 35 36 type CNITypesSuite struct{} 37 38 var _ = check.Suite(&CNITypesSuite{}) 39 40 func testConfRead(c *check.C, confContent string, netconf *NetConf) { 41 dir, err := ioutil.TempDir("", "cilium-cnitype-testsuite") 42 c.Assert(err, check.IsNil) 43 defer os.RemoveAll(dir) 44 45 p := path.Join(dir, "conf1") 46 err = ioutil.WriteFile(p, []byte(confContent), 0644) 47 c.Assert(err, check.IsNil) 48 49 netConf, err := ReadNetConf(p) 50 c.Assert(err, check.IsNil) 51 52 c.Assert(netConf, checker.DeepEquals, netconf) 53 } 54 55 func (t *CNITypesSuite) TestReadCNIConf(c *check.C) { 56 confFile1 := ` 57 { 58 "name": "cilium", 59 "type": "cilium-cni" 60 } 61 ` 62 63 netConf1 := NetConf{ 64 NetConf: cnitypes.NetConf{ 65 Name: "cilium", 66 Type: "cilium-cni", 67 }, 68 } 69 testConfRead(c, confFile1, &netConf1) 70 71 confFile2 := ` 72 { 73 "name": "cilium", 74 "type": "cilium-cni", 75 "mtu": 9000 76 } 77 ` 78 79 netConf2 := NetConf{ 80 NetConf: cnitypes.NetConf{ 81 Name: "cilium", 82 Type: "cilium-cni", 83 }, 84 MTU: 9000, 85 } 86 testConfRead(c, confFile2, &netConf2) 87 } 88 89 func (t *CNITypesSuite) TestReadCNIConfENIWithPlugins(c *check.C) { 90 confFile1 := ` 91 { 92 "cniVersion":"0.3.1", 93 "name":"cilium", 94 "plugins": [ 95 { 96 "cniVersion":"0.3.1", 97 "type":"cilium-cni", 98 "eni": { 99 "pre-allocate": 5, 100 "first-interface-index":1, 101 "security-groups":[ 102 "sg-xxx" 103 ], 104 "subnet-tags":{ 105 "foo":"true" 106 } 107 } 108 } 109 ] 110 } 111 ` 112 netConf1 := NetConf{ 113 NetConf: cnitypes.NetConf{ 114 CNIVersion: "0.3.1", 115 Type: "cilium-cni", 116 }, 117 ENI: ciliumv2.ENISpec{ 118 PreAllocate: 5, 119 FirstInterfaceIndex: 1, 120 SecurityGroups: []string{"sg-xxx"}, 121 SubnetTags: map[string]string{ 122 "foo": "true", 123 }, 124 }, 125 } 126 testConfRead(c, confFile1, &netConf1) 127 } 128 129 func (t *CNITypesSuite) TestReadCNIConfENI(c *check.C) { 130 confFile1 := ` 131 { 132 "name": "cilium", 133 "type": "cilium-cni", 134 "eni": { 135 "instance-type": "m4.xlarge", 136 "pre-allocate": 16, 137 "first-interface-index": 2, 138 "security-groups": [ "sg1", "sg2" ], 139 "subnet-tags": { 140 "key1": "val1", 141 "key2": "val2" 142 }, 143 "vpc-id": "vpc-1", 144 "availability-zone": "us-west1" 145 } 146 } 147 ` 148 netConf1 := NetConf{ 149 NetConf: cnitypes.NetConf{ 150 Name: "cilium", 151 Type: "cilium-cni", 152 }, 153 ENI: ciliumv2.ENISpec{ 154 InstanceType: "m4.xlarge", 155 PreAllocate: 16, 156 FirstInterfaceIndex: 2, 157 SecurityGroups: []string{"sg1", "sg2"}, 158 SubnetTags: map[string]string{ 159 "key1": "val1", 160 "key2": "val2", 161 }, 162 VpcID: "vpc-1", 163 AvailabilityZone: "us-west1", 164 }, 165 } 166 testConfRead(c, confFile1, &netConf1) 167 } 168 169 func (t *CNITypesSuite) TestReadCNIConfError(c *check.C) { 170 // Try to read errorneous CNI configuration file with MTU provided as 171 // string instead of int 172 errorConf := ` 173 { 174 "name": "cilium", 175 "type": "cilium-cni", 176 "mtu": "9000" 177 } 178 ` 179 180 dir, err := ioutil.TempDir("", "cilium-cnitype-testsuite") 181 c.Assert(err, check.IsNil) 182 defer os.RemoveAll(dir) 183 184 p := path.Join(dir, "errorconf") 185 err = ioutil.WriteFile(p, []byte(errorConf), 0644) 186 c.Assert(err, check.IsNil) 187 188 _, err = ReadNetConf(p) 189 c.Assert(err, check.Not(check.IsNil)) 190 }