cni_test.go 11 KB

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