2015-09-09 21:00:41 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cni
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
"text/template"
|
|
|
|
|
2016-02-05 21:58:03 +00:00
|
|
|
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
2016-02-01 22:30:47 +00:00
|
|
|
|
2015-10-16 03:00:28 +00:00
|
|
|
cadvisorapi "github.com/google/cadvisor/info/v1"
|
2015-09-09 21:00:41 +00:00
|
|
|
|
2016-03-25 21:45:59 +00:00
|
|
|
"k8s.io/kubernetes/cmd/kubelet/app/options"
|
2015-09-09 21:00:41 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-03-31 22:20:04 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/componentconfig"
|
2015-09-09 21:00:41 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/record"
|
|
|
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
2016-02-25 23:40:44 +00:00
|
|
|
containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
|
2015-09-09 21:00:41 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/dockertools"
|
|
|
|
"k8s.io/kubernetes/pkg/kubelet/network"
|
2016-02-25 23:40:44 +00:00
|
|
|
nettest "k8s.io/kubernetes/pkg/kubelet/network/testing"
|
2015-10-19 22:15:59 +00:00
|
|
|
proberesults "k8s.io/kubernetes/pkg/kubelet/prober/results"
|
2016-01-25 21:57:42 +00:00
|
|
|
utiltesting "k8s.io/kubernetes/pkg/util/testing"
|
2015-09-09 21:00:41 +00:00
|
|
|
)
|
|
|
|
|
2015-10-12 12:28:03 +00:00
|
|
|
func installPluginUnderTest(t *testing.T, testVendorCNIDirPrefix, testNetworkConfigPath, vendorName string, plugName string) {
|
2015-09-09 21:00:41 +00:00
|
|
|
pluginDir := path.Join(testNetworkConfigPath, plugName)
|
|
|
|
err := os.MkdirAll(pluginDir, 0777)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create plugin config dir: %v", err)
|
|
|
|
}
|
|
|
|
pluginConfig := path.Join(pluginDir, plugName+".conf")
|
|
|
|
f, err := os.Create(pluginConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to install plugin")
|
|
|
|
}
|
|
|
|
networkConfig := fmt.Sprintf("{ \"name\": \"%s\", \"type\": \"%s\" }", plugName, vendorName)
|
|
|
|
|
|
|
|
_, err = f.WriteString(networkConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to write network config file (%v)", err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
vendorCNIDir := fmt.Sprintf(VendorCNIDirTemplate, testVendorCNIDirPrefix, vendorName)
|
|
|
|
err = os.MkdirAll(vendorCNIDir, 0777)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create plugin dir: %v", err)
|
|
|
|
}
|
|
|
|
pluginExec := path.Join(vendorCNIDir, vendorName)
|
|
|
|
f, err = os.Create(pluginExec)
|
|
|
|
|
|
|
|
const execScriptTempl = `#!/bin/bash
|
|
|
|
read ignore
|
2015-10-02 15:19:08 +00:00
|
|
|
env > {{.OutputEnv}}
|
|
|
|
echo "%@" >> {{.OutputEnv}}
|
2015-09-09 21:00:41 +00:00
|
|
|
export $(echo ${CNI_ARGS} | sed 's/;/ /g') &> /dev/null
|
|
|
|
mkdir -p {{.OutputDir}} &> /dev/null
|
|
|
|
echo -n "$CNI_COMMAND $CNI_NETNS $K8S_POD_NAMESPACE $K8S_POD_NAME $K8S_POD_INFRA_CONTAINER_ID" >& {{.OutputFile}}
|
|
|
|
echo -n "{ \"ip4\": { \"ip\": \"10.1.0.23/24\" } }"
|
|
|
|
`
|
|
|
|
execTemplateData := &map[string]interface{}{
|
|
|
|
"OutputFile": path.Join(pluginDir, plugName+".out"),
|
2015-10-02 15:19:08 +00:00
|
|
|
"OutputEnv": path.Join(pluginDir, plugName+".env"),
|
2015-09-09 21:00:41 +00:00
|
|
|
"OutputDir": pluginDir,
|
|
|
|
}
|
|
|
|
|
|
|
|
tObj := template.Must(template.New("test").Parse(execScriptTempl))
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
if err := tObj.Execute(buf, *execTemplateData); err != nil {
|
|
|
|
t.Fatalf("Error in executing script template - %v", err)
|
|
|
|
}
|
|
|
|
execScript := buf.String()
|
|
|
|
_, err = f.WriteString(execScript)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to write plugin exec - %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Chmod(0777)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to set exec perms on plugin")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
2015-10-12 12:28:03 +00:00
|
|
|
func tearDownPlugin(tmpDir string) {
|
|
|
|
err := os.RemoveAll(tmpDir)
|
2015-09-09 21:00:41 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error in cleaning up test: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeNetworkHost struct {
|
2016-02-01 22:30:47 +00:00
|
|
|
kubeClient clientset.Interface
|
2015-09-09 21:00:41 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 22:30:47 +00:00
|
|
|
func NewFakeHost(kubeClient clientset.Interface) *fakeNetworkHost {
|
2015-09-09 21:00:41 +00:00
|
|
|
host := &fakeNetworkHost{kubeClient: kubeClient}
|
|
|
|
return host
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fnh *fakeNetworkHost) GetPodByName(name, namespace string) (*api.Pod, bool) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2016-02-01 22:30:47 +00:00
|
|
|
func (fnh *fakeNetworkHost) GetKubeClient() clientset.Interface {
|
2015-09-09 21:00:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nh *fakeNetworkHost) GetRuntime() kubecontainer.Runtime {
|
|
|
|
dm, fakeDockerClient := newTestDockerManager()
|
2016-04-04 22:27:20 +00:00
|
|
|
fakeDockerClient.SetFakeRunningContainers([]*dockertools.FakeContainer{
|
2015-11-19 21:59:18 +00:00
|
|
|
{
|
2016-04-04 22:27:20 +00:00
|
|
|
ID: "test_infra_container",
|
|
|
|
Pid: 12345,
|
2015-11-19 21:59:18 +00:00
|
|
|
},
|
|
|
|
})
|
2015-09-09 21:00:41 +00:00
|
|
|
return dm
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestDockerManager() (*dockertools.DockerManager, *dockertools.FakeDockerClient) {
|
2015-11-19 21:59:18 +00:00
|
|
|
fakeDocker := dockertools.NewFakeDockerClient()
|
2015-09-09 21:00:41 +00:00
|
|
|
fakeRecorder := &record.FakeRecorder{}
|
|
|
|
containerRefManager := kubecontainer.NewRefManager()
|
2016-06-09 17:32:28 +00:00
|
|
|
networkPlugin, _ := network.InitNetworkPlugin([]network.NetworkPlugin{}, "", nettest.NewFakeHost(nil), componentconfig.HairpinNone, "10.0.0.0/8")
|
2015-09-09 21:00:41 +00:00
|
|
|
dockerManager := dockertools.NewFakeDockerManager(
|
|
|
|
fakeDocker,
|
|
|
|
fakeRecorder,
|
2015-10-19 22:15:59 +00:00
|
|
|
proberesults.NewManager(),
|
2015-09-09 21:00:41 +00:00
|
|
|
containerRefManager,
|
2015-10-16 03:00:28 +00:00
|
|
|
&cadvisorapi.MachineInfo{},
|
2016-03-25 21:45:59 +00:00
|
|
|
options.GetDefaultPodInfraContainerImage(),
|
2015-09-09 21:00:41 +00:00
|
|
|
0, 0, "",
|
2016-04-21 01:21:41 +00:00
|
|
|
&containertest.FakeOS{},
|
2015-09-09 21:00:41 +00:00
|
|
|
networkPlugin,
|
|
|
|
nil,
|
2015-10-02 13:45:46 +00:00
|
|
|
nil,
|
2015-09-09 21:00:41 +00:00
|
|
|
nil)
|
|
|
|
|
|
|
|
return dockerManager, fakeDocker
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCNIPlugin(t *testing.T) {
|
|
|
|
// install some random plugin
|
|
|
|
pluginName := fmt.Sprintf("test%d", rand.Intn(1000))
|
|
|
|
vendorName := fmt.Sprintf("test_vendor%d", rand.Intn(1000))
|
2015-10-12 12:28:03 +00:00
|
|
|
|
2016-01-25 21:57:42 +00:00
|
|
|
tmpDir := utiltesting.MkTmpdirOrDie("cni-test")
|
2015-10-12 12:28:03 +00:00
|
|
|
testNetworkConfigPath := path.Join(tmpDir, "plugins", "net", "cni")
|
|
|
|
testVendorCNIDirPrefix := tmpDir
|
|
|
|
defer tearDownPlugin(tmpDir)
|
|
|
|
installPluginUnderTest(t, testVendorCNIDirPrefix, testNetworkConfigPath, vendorName, pluginName)
|
2015-09-09 21:00:41 +00:00
|
|
|
|
|
|
|
np := probeNetworkPluginsWithVendorCNIDirPrefix(path.Join(testNetworkConfigPath, pluginName), testVendorCNIDirPrefix)
|
2016-06-09 17:32:28 +00:00
|
|
|
plug, err := network.InitNetworkPlugin(np, "cni", NewFakeHost(nil), componentconfig.HairpinNone, "10.0.0.0/8")
|
2015-09-09 21:00:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to select the desired plugin: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-04-15 19:18:27 +00:00
|
|
|
err = plug.SetUpPod("podNamespace", "podName", kubecontainer.ContainerID{Type: "docker", ID: "test_infra_container"})
|
2015-09-09 21:00:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected nil: %v", err)
|
|
|
|
}
|
2015-10-02 15:19:08 +00:00
|
|
|
outputEnv := path.Join(testNetworkConfigPath, pluginName, pluginName+".env")
|
|
|
|
eo, eerr := ioutil.ReadFile(outputEnv)
|
|
|
|
outputFile := path.Join(testNetworkConfigPath, pluginName, pluginName+".out")
|
|
|
|
output, err := ioutil.ReadFile(outputFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to read output file %s: %v (env %s err %v)", outputFile, err, eo, eerr)
|
|
|
|
}
|
2015-11-19 21:59:18 +00:00
|
|
|
expectedOutput := "ADD /proc/12345/ns/net podNamespace podName test_infra_container"
|
2015-09-09 21:00:41 +00:00
|
|
|
if string(output) != expectedOutput {
|
|
|
|
t.Errorf("Mismatch in expected output for setup hook. Expected '%s', got '%s'", expectedOutput, string(output))
|
|
|
|
}
|
2016-04-15 19:18:27 +00:00
|
|
|
err = plug.TearDownPod("podNamespace", "podName", kubecontainer.ContainerID{Type: "docker", ID: "test_infra_container"})
|
2015-09-09 21:00:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected nil: %v", err)
|
|
|
|
}
|
|
|
|
output, err = ioutil.ReadFile(path.Join(testNetworkConfigPath, pluginName, pluginName+".out"))
|
2015-11-19 21:59:18 +00:00
|
|
|
expectedOutput = "DEL /proc/12345/ns/net podNamespace podName test_infra_container"
|
2015-09-09 21:00:41 +00:00
|
|
|
if string(output) != expectedOutput {
|
|
|
|
t.Errorf("Mismatch in expected output for setup hook. Expected '%s', got '%s'", expectedOutput, string(output))
|
|
|
|
}
|
|
|
|
}
|