cni_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // +build linux
  2. /*
  3. Copyright 2014 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package cni
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "fmt"
  19. "io/ioutil"
  20. "math/rand"
  21. "net"
  22. "os"
  23. "path"
  24. "reflect"
  25. "testing"
  26. "text/template"
  27. types020 "github.com/containernetworking/cni/pkg/types/020"
  28. "github.com/stretchr/testify/mock"
  29. "k8s.io/api/core/v1"
  30. clientset "k8s.io/client-go/kubernetes"
  31. utiltesting "k8s.io/client-go/util/testing"
  32. kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
  33. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  34. containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
  35. "k8s.io/kubernetes/pkg/kubelet/dockershim/network"
  36. "k8s.io/kubernetes/pkg/kubelet/dockershim/network/cni/testing"
  37. "k8s.io/kubernetes/pkg/kubelet/dockershim/network/hostport"
  38. networktest "k8s.io/kubernetes/pkg/kubelet/dockershim/network/testing"
  39. "k8s.io/utils/exec"
  40. fakeexec "k8s.io/utils/exec/testing"
  41. )
  42. // Returns .in file path, .out file path, and .env file path
  43. func installPluginUnderTest(t *testing.T, testBinDir, testConfDir, testDataDir, binName string, confName string) (string, string, string) {
  44. for _, dir := range []string{testBinDir, testConfDir, testDataDir} {
  45. err := os.MkdirAll(dir, 0777)
  46. if err != nil {
  47. t.Fatalf("Failed to create test plugin dir %s: %v", dir, err)
  48. }
  49. }
  50. confFile := path.Join(testConfDir, confName+".conf")
  51. f, err := os.Create(confFile)
  52. if err != nil {
  53. t.Fatalf("Failed to install plugin %s: %v", confFile, err)
  54. }
  55. networkConfig := fmt.Sprintf(`{ "name": "%s", "type": "%s", "capabilities": {"portMappings": true, "bandwidth": true, "ipRanges": true} }`, confName, binName)
  56. _, err = f.WriteString(networkConfig)
  57. if err != nil {
  58. t.Fatalf("Failed to write network config file (%v)", err)
  59. }
  60. f.Close()
  61. pluginExec := path.Join(testBinDir, binName)
  62. f, err = os.Create(pluginExec)
  63. const execScriptTempl = `#!/usr/bin/env bash
  64. cat > {{.InputFile}}
  65. env > {{.OutputEnv}}
  66. echo "%@" >> {{.OutputEnv}}
  67. export $(echo ${CNI_ARGS} | sed 's/;/ /g') &> /dev/null
  68. mkdir -p {{.OutputDir}} &> /dev/null
  69. echo -n "$CNI_COMMAND $CNI_NETNS $K8S_POD_NAMESPACE $K8S_POD_NAME $K8S_POD_INFRA_CONTAINER_ID" >& {{.OutputFile}}
  70. echo -n "{ \"ip4\": { \"ip\": \"10.1.0.23/24\" } }"
  71. `
  72. inputFile := path.Join(testDataDir, binName+".in")
  73. outputFile := path.Join(testDataDir, binName+".out")
  74. envFile := path.Join(testDataDir, binName+".env")
  75. execTemplateData := &map[string]interface{}{
  76. "InputFile": inputFile,
  77. "OutputFile": outputFile,
  78. "OutputEnv": envFile,
  79. "OutputDir": testDataDir,
  80. }
  81. tObj := template.Must(template.New("test").Parse(execScriptTempl))
  82. buf := &bytes.Buffer{}
  83. if err := tObj.Execute(buf, *execTemplateData); err != nil {
  84. t.Fatalf("Error in executing script template - %v", err)
  85. }
  86. execScript := buf.String()
  87. _, err = f.WriteString(execScript)
  88. if err != nil {
  89. t.Fatalf("Failed to write plugin exec - %v", err)
  90. }
  91. err = f.Chmod(0777)
  92. if err != nil {
  93. t.Fatalf("Failed to set exec perms on plugin")
  94. }
  95. f.Close()
  96. return inputFile, outputFile, envFile
  97. }
  98. func tearDownPlugin(tmpDir string) {
  99. err := os.RemoveAll(tmpDir)
  100. if err != nil {
  101. fmt.Printf("Error in cleaning up test: %v", err)
  102. }
  103. }
  104. type fakeNetworkHost struct {
  105. networktest.FakePortMappingGetter
  106. kubeClient clientset.Interface
  107. pods []*containertest.FakePod
  108. }
  109. func NewFakeHost(kubeClient clientset.Interface, pods []*containertest.FakePod, ports map[string][]*hostport.PortMapping) *fakeNetworkHost {
  110. host := &fakeNetworkHost{
  111. networktest.FakePortMappingGetter{PortMaps: ports},
  112. kubeClient,
  113. pods,
  114. }
  115. return host
  116. }
  117. func (fnh *fakeNetworkHost) GetPodByName(name, namespace string) (*v1.Pod, bool) {
  118. return nil, false
  119. }
  120. func (fnh *fakeNetworkHost) GetKubeClient() clientset.Interface {
  121. return fnh.kubeClient
  122. }
  123. func (fnh *fakeNetworkHost) GetNetNS(containerID string) (string, error) {
  124. for _, fp := range fnh.pods {
  125. for _, c := range fp.Pod.Containers {
  126. if c.ID.ID == containerID {
  127. return fp.NetnsPath, nil
  128. }
  129. }
  130. }
  131. return "", fmt.Errorf("container %q not found", containerID)
  132. }
  133. func (fnh *fakeNetworkHost) SupportsLegacyFeatures() bool {
  134. return true
  135. }
  136. func TestCNIPlugin(t *testing.T) {
  137. // install some random plugin
  138. netName := fmt.Sprintf("test%d", rand.Intn(1000))
  139. binName := fmt.Sprintf("test_vendor%d", rand.Intn(1000))
  140. podIP := "10.0.0.2"
  141. podIPOutput := fmt.Sprintf("4: eth0 inet %s/24 scope global dynamic eth0\\ valid_lft forever preferred_lft forever", podIP)
  142. fakeCmds := []fakeexec.FakeCommandAction{
  143. func(cmd string, args ...string) exec.Cmd {
  144. return fakeexec.InitFakeCmd(&fakeexec.FakeCmd{
  145. CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
  146. func() ([]byte, error) {
  147. return []byte(podIPOutput), nil
  148. },
  149. },
  150. }, cmd, args...)
  151. },
  152. }
  153. fexec := &fakeexec.FakeExec{
  154. CommandScript: fakeCmds,
  155. LookPathFunc: func(file string) (string, error) {
  156. return fmt.Sprintf("/fake-bin/%s", file), nil
  157. },
  158. }
  159. mockLoCNI := &mock_cni.MockCNI{}
  160. // TODO mock for the test plugin too
  161. tmpDir := utiltesting.MkTmpdirOrDie("cni-test")
  162. testConfDir := path.Join(tmpDir, "etc", "cni", "net.d")
  163. testBinDir := path.Join(tmpDir, "opt", "cni", "bin")
  164. testDataDir := path.Join(tmpDir, "output")
  165. defer tearDownPlugin(tmpDir)
  166. inputFile, outputFile, outputEnv := installPluginUnderTest(t, testBinDir, testConfDir, testDataDir, binName, netName)
  167. containerID := kubecontainer.ContainerID{Type: "test", ID: "test_infra_container"}
  168. pods := []*containertest.FakePod{{
  169. Pod: &kubecontainer.Pod{
  170. Containers: []*kubecontainer.Container{
  171. {ID: containerID},
  172. },
  173. },
  174. NetnsPath: "/proc/12345/ns/net",
  175. }}
  176. plugins := ProbeNetworkPlugins(testConfDir, []string{testBinDir})
  177. if len(plugins) != 1 {
  178. t.Fatalf("Expected only one network plugin, got %d", len(plugins))
  179. }
  180. if plugins[0].Name() != "cni" {
  181. t.Fatalf("Expected CNI network plugin, got %q", plugins[0].Name())
  182. }
  183. cniPlugin, ok := plugins[0].(*cniNetworkPlugin)
  184. if !ok {
  185. t.Fatalf("Not a CNI network plugin!")
  186. }
  187. cniPlugin.execer = fexec
  188. cniPlugin.loNetwork.CNIConfig = mockLoCNI
  189. mockLoCNI.On("AddNetworkList", cniPlugin.loNetwork.NetworkConfig, mock.AnythingOfType("*libcni.RuntimeConf")).Return(&types020.Result{IP4: &types020.IPConfig{IP: net.IPNet{IP: []byte{127, 0, 0, 1}}}}, nil)
  190. // Check that status returns an error
  191. if err := cniPlugin.Status(); err == nil {
  192. t.Fatalf("cniPlugin returned non-err with no podCidr")
  193. }
  194. cniPlugin.Event(network.NET_PLUGIN_EVENT_POD_CIDR_CHANGE, map[string]interface{}{
  195. network.NET_PLUGIN_EVENT_POD_CIDR_CHANGE_DETAIL_CIDR: "10.0.2.0/24",
  196. })
  197. if err := cniPlugin.Status(); err != nil {
  198. t.Fatalf("unexpected status err: %v", err)
  199. }
  200. ports := map[string][]*hostport.PortMapping{
  201. containerID.ID: {
  202. {
  203. Name: "name",
  204. HostPort: 8008,
  205. ContainerPort: 80,
  206. Protocol: "UDP",
  207. HostIP: "0.0.0.0",
  208. },
  209. },
  210. }
  211. fakeHost := NewFakeHost(nil, pods, ports)
  212. plug, err := network.InitNetworkPlugin(plugins, "cni", fakeHost, kubeletconfig.HairpinNone, "10.0.0.0/8", network.UseDefaultMTU)
  213. if err != nil {
  214. t.Fatalf("Failed to select the desired plugin: %v", err)
  215. }
  216. bandwidthAnnotation := make(map[string]string)
  217. bandwidthAnnotation["kubernetes.io/ingress-bandwidth"] = "1M"
  218. bandwidthAnnotation["kubernetes.io/egress-bandwidth"] = "1M"
  219. // Set up the pod
  220. err = plug.SetUpPod("podNamespace", "podName", containerID, bandwidthAnnotation, nil)
  221. if err != nil {
  222. t.Errorf("Expected nil: %v", err)
  223. }
  224. eo, eerr := ioutil.ReadFile(outputEnv)
  225. output, err := ioutil.ReadFile(outputFile)
  226. if err != nil || eerr != nil {
  227. t.Errorf("Failed to read output file %s: %v (env %s err %v)", outputFile, err, eo, eerr)
  228. }
  229. expectedOutput := "ADD /proc/12345/ns/net podNamespace podName test_infra_container"
  230. if string(output) != expectedOutput {
  231. t.Errorf("Mismatch in expected output for setup hook. Expected '%s', got '%s'", expectedOutput, string(output))
  232. }
  233. // Verify the correct network configuration was passed
  234. inputConfig := struct {
  235. RuntimeConfig struct {
  236. PortMappings []map[string]interface{} `json:"portMappings"`
  237. Bandwidth map[string]interface{} `json:"bandwidth"`
  238. IpRanges [][]map[string]interface{} `json:"ipRanges"`
  239. } `json:"runtimeConfig"`
  240. }{}
  241. inputBytes, inerr := ioutil.ReadFile(inputFile)
  242. parseerr := json.Unmarshal(inputBytes, &inputConfig)
  243. if inerr != nil || parseerr != nil {
  244. t.Errorf("failed to parse reported cni input config %s: (%v %v)", inputFile, inerr, parseerr)
  245. }
  246. expectedMappings := []map[string]interface{}{
  247. // hah, golang always unmarshals unstructured json numbers as float64
  248. {"hostPort": 8008.0, "containerPort": 80.0, "protocol": "udp", "hostIP": "0.0.0.0"},
  249. }
  250. if !reflect.DeepEqual(inputConfig.RuntimeConfig.PortMappings, expectedMappings) {
  251. t.Errorf("mismatch in expected port mappings. expected %v got %v", expectedMappings, inputConfig.RuntimeConfig.PortMappings)
  252. }
  253. expectedBandwidth := map[string]interface{}{
  254. "ingressRate": 1000000.0, "egressRate": 1000000.0,
  255. "ingressBurst": 2147483647.0, "egressBurst": 2147483647.0,
  256. }
  257. if !reflect.DeepEqual(inputConfig.RuntimeConfig.Bandwidth, expectedBandwidth) {
  258. t.Errorf("mismatch in expected bandwidth. expected %v got %v", expectedBandwidth, inputConfig.RuntimeConfig.Bandwidth)
  259. }
  260. expectedIpRange := [][]map[string]interface{}{
  261. {
  262. {"subnet": "10.0.2.0/24"},
  263. },
  264. }
  265. if !reflect.DeepEqual(inputConfig.RuntimeConfig.IpRanges, expectedIpRange) {
  266. t.Errorf("mismatch in expected ipRange. expected %v got %v", expectedIpRange, inputConfig.RuntimeConfig.IpRanges)
  267. }
  268. // Get its IP address
  269. status, err := plug.GetPodNetworkStatus("podNamespace", "podName", containerID)
  270. if err != nil {
  271. t.Errorf("Failed to read pod network status: %v", err)
  272. }
  273. if status.IP.String() != podIP {
  274. t.Errorf("Expected pod IP %q but got %q", podIP, status.IP.String())
  275. }
  276. // Tear it down
  277. err = plug.TearDownPod("podNamespace", "podName", containerID)
  278. if err != nil {
  279. t.Errorf("Expected nil: %v", err)
  280. }
  281. output, err = ioutil.ReadFile(outputFile)
  282. expectedOutput = "DEL /proc/12345/ns/net podNamespace podName test_infra_container"
  283. if string(output) != expectedOutput {
  284. t.Errorf("Mismatch in expected output for setup hook. Expected '%s', got '%s'", expectedOutput, string(output))
  285. }
  286. mockLoCNI.AssertExpectations(t)
  287. }
  288. func TestLoNetNonNil(t *testing.T) {
  289. if conf := getLoNetwork(nil); conf == nil {
  290. t.Error("Expected non-nil lo network")
  291. }
  292. }