mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 18:00:08 +00:00
Merge pull request #17533 from Random-Liu/fix-fake-docker-client
Auto commit by PR queue bot
This commit is contained in:
commit
ff3f240de2
@ -73,7 +73,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
fakeDocker1, fakeDocker2 dockertools.FakeDockerClient
|
fakeDocker1 = dockertools.NewFakeDockerClient()
|
||||||
|
fakeDocker2 = dockertools.NewFakeDockerClient()
|
||||||
// Limit the number of concurrent tests.
|
// Limit the number of concurrent tests.
|
||||||
maxConcurrency int
|
maxConcurrency int
|
||||||
|
|
||||||
@ -220,7 +221,7 @@ func startComponents(firstManifestURL, secondManifestURL string) (string, string
|
|||||||
cm := cm.NewStubContainerManager()
|
cm := cm.NewStubContainerManager()
|
||||||
kcfg := kubeletapp.SimpleKubelet(
|
kcfg := kubeletapp.SimpleKubelet(
|
||||||
cl,
|
cl,
|
||||||
&fakeDocker1,
|
fakeDocker1,
|
||||||
"localhost",
|
"localhost",
|
||||||
testRootDir,
|
testRootDir,
|
||||||
firstManifestURL,
|
firstManifestURL,
|
||||||
@ -252,7 +253,7 @@ func startComponents(firstManifestURL, secondManifestURL string) (string, string
|
|||||||
|
|
||||||
kcfg = kubeletapp.SimpleKubelet(
|
kcfg = kubeletapp.SimpleKubelet(
|
||||||
cl,
|
cl,
|
||||||
&fakeDocker2,
|
fakeDocker2,
|
||||||
"127.0.0.1",
|
"127.0.0.1",
|
||||||
testRootDir,
|
testRootDir,
|
||||||
secondManifestURL,
|
secondManifestURL,
|
||||||
|
@ -96,9 +96,8 @@ func main() {
|
|||||||
cadvisorInterface := new(cadvisor.Fake)
|
cadvisorInterface := new(cadvisor.Fake)
|
||||||
containerManager := cm.NewStubContainerManager()
|
containerManager := cm.NewStubContainerManager()
|
||||||
|
|
||||||
fakeDockerClient := &dockertools.FakeDockerClient{}
|
fakeDockerClient := dockertools.NewFakeDockerClient()
|
||||||
fakeDockerClient.VersionInfo = docker.Env{"ApiVersion=1.18"}
|
fakeDockerClient.VersionInfo = docker.Env{"ApiVersion=1.18"}
|
||||||
fakeDockerClient.ContainerMap = make(map[string]*docker.Container)
|
|
||||||
fakeDockerClient.EnableSleep = true
|
fakeDockerClient.EnableSleep = true
|
||||||
|
|
||||||
hollowKubelet := kubemark.NewHollowKubelet(
|
hollowKubelet := kubemark.NewHollowKubelet(
|
||||||
|
@ -28,6 +28,7 @@ import (
|
|||||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TODO (random-liu) Cleanup the test soon
|
||||||
func newTestContainerGC(t *testing.T) (*containerGC, *FakeDockerClient) {
|
func newTestContainerGC(t *testing.T) (*containerGC, *FakeDockerClient) {
|
||||||
fakeDocker := new(FakeDockerClient)
|
fakeDocker := new(FakeDockerClient)
|
||||||
gc := NewContainerGC(fakeDocker, "")
|
gc := NewContainerGC(fakeDocker, "")
|
||||||
|
@ -79,19 +79,16 @@ func findPodContainer(dockerContainers []*docker.APIContainers, podFullName stri
|
|||||||
|
|
||||||
func TestGetContainerID(t *testing.T) {
|
func TestGetContainerID(t *testing.T) {
|
||||||
fakeDocker := &FakeDockerClient{}
|
fakeDocker := &FakeDockerClient{}
|
||||||
fakeDocker.ContainerList = []docker.APIContainers{
|
fakeDocker.SetFakeRunningContainers([]*docker.Container{
|
||||||
{
|
{
|
||||||
ID: "foobar",
|
ID: "foobar",
|
||||||
Names: []string{"/k8s_foo_qux_ns_1234_42"},
|
Name: "/k8s_foo_qux_ns_1234_42",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID: "barbar",
|
ID: "barbar",
|
||||||
Names: []string{"/k8s_bar_qux_ns_2565_42"},
|
Name: "/k8s_bar_qux_ns_2565_42",
|
||||||
},
|
},
|
||||||
}
|
})
|
||||||
fakeDocker.Container = &docker.Container{
|
|
||||||
ID: "foobar",
|
|
||||||
}
|
|
||||||
|
|
||||||
dockerContainers, err := GetKubeletDockerContainers(fakeDocker, false)
|
dockerContainers, err := GetKubeletDockerContainers(fakeDocker, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -33,27 +33,35 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// FakeDockerClient is a simple fake docker client, so that kubelet can be run for testing without requiring a real docker setup.
|
// FakeDockerClient is a simple fake docker client, so that kubelet can be run for testing without requiring a real docker setup.
|
||||||
// TODO: create a proper constructor for FakeDockerClient, so we won't need to check if ContainerMap is not nil.
|
|
||||||
type FakeDockerClient struct {
|
type FakeDockerClient struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
ContainerList []docker.APIContainers
|
ContainerList []docker.APIContainers
|
||||||
ExitedContainerList []docker.APIContainers
|
ExitedContainerList []docker.APIContainers
|
||||||
Container *docker.Container
|
|
||||||
ContainerMap map[string]*docker.Container
|
ContainerMap map[string]*docker.Container
|
||||||
Image *docker.Image
|
Image *docker.Image
|
||||||
Images []docker.APIImages
|
Images []docker.APIImages
|
||||||
Errors map[string]error
|
Errors map[string]error
|
||||||
called []string
|
called []string
|
||||||
Stopped []string
|
|
||||||
pulled []string
|
pulled []string
|
||||||
Created []string
|
// Created, Stopped and Removed all container docker ID
|
||||||
Removed []string
|
Created []string
|
||||||
RemovedImages sets.String
|
Stopped []string
|
||||||
VersionInfo docker.Env
|
Removed []string
|
||||||
Information docker.Env
|
RemovedImages sets.String
|
||||||
ExecInspect *docker.ExecInspect
|
VersionInfo docker.Env
|
||||||
execCmd []string
|
Information docker.Env
|
||||||
EnableSleep bool
|
ExecInspect *docker.ExecInspect
|
||||||
|
execCmd []string
|
||||||
|
EnableSleep bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFakeDockerClient() *FakeDockerClient {
|
||||||
|
return &FakeDockerClient{
|
||||||
|
VersionInfo: docker.Env{"Version=1.1.3", "ApiVersion=1.15"},
|
||||||
|
Errors: make(map[string]error),
|
||||||
|
RemovedImages: sets.String{},
|
||||||
|
ContainerMap: make(map[string]*docker.Container),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeDockerClient) ClearCalls() {
|
func (f *FakeDockerClient) ClearCalls() {
|
||||||
@ -199,12 +207,10 @@ func (f *FakeDockerClient) InspectContainer(id string) (*docker.Container, error
|
|||||||
defer f.Unlock()
|
defer f.Unlock()
|
||||||
f.called = append(f.called, "inspect_container")
|
f.called = append(f.called, "inspect_container")
|
||||||
err := f.popError("inspect_container")
|
err := f.popError("inspect_container")
|
||||||
if f.ContainerMap != nil {
|
if container, ok := f.ContainerMap[id]; ok {
|
||||||
if container, ok := f.ContainerMap[id]; ok {
|
return container, err
|
||||||
return container, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return f.Container, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// InspectImage is a test-spy implementation of DockerInterface.InspectImage.
|
// InspectImage is a test-spy implementation of DockerInterface.InspectImage.
|
||||||
@ -240,19 +246,17 @@ func (f *FakeDockerClient) CreateContainer(c docker.CreateContainerOptions) (*do
|
|||||||
if err := f.popError("create"); err != nil {
|
if err := f.popError("create"); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
f.Created = append(f.Created, c.Name)
|
|
||||||
// This is not a very good fake. We'll just add this container's name to the list.
|
// This is not a very good fake. We'll just add this container's name to the list.
|
||||||
// Docker likes to add a '/', so copy that behavior.
|
// Docker likes to add a '/', so copy that behavior.
|
||||||
name := "/" + c.Name
|
name := "/" + c.Name
|
||||||
|
f.Created = append(f.Created, name)
|
||||||
// The newest container should be in front, because we assume so in GetPodStatus()
|
// The newest container should be in front, because we assume so in GetPodStatus()
|
||||||
f.ContainerList = append([]docker.APIContainers{
|
f.ContainerList = append([]docker.APIContainers{
|
||||||
{ID: name, Names: []string{name}, Image: c.Config.Image, Labels: c.Config.Labels},
|
{ID: name, Names: []string{name}, Image: c.Config.Image, Labels: c.Config.Labels},
|
||||||
}, f.ContainerList...)
|
}, f.ContainerList...)
|
||||||
container := docker.Container{ID: name, Name: name, Config: c.Config}
|
container := docker.Container{ID: name, Name: name, Config: c.Config}
|
||||||
if f.ContainerMap != nil {
|
containerCopy := container
|
||||||
containerCopy := container
|
f.ContainerMap[name] = &containerCopy
|
||||||
f.ContainerMap[name] = &containerCopy
|
|
||||||
}
|
|
||||||
f.normalSleep(200, 50, 50)
|
f.normalSleep(200, 50, 50)
|
||||||
return &container, nil
|
return &container, nil
|
||||||
}
|
}
|
||||||
@ -266,32 +270,18 @@ func (f *FakeDockerClient) StartContainer(id string, hostConfig *docker.HostConf
|
|||||||
if err := f.popError("start"); err != nil {
|
if err := f.popError("start"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
f.Container = &docker.Container{
|
container, ok := f.ContainerMap[id]
|
||||||
ID: id,
|
if !ok {
|
||||||
Name: id, // For testing purpose, we set name to id
|
container = &docker.Container{ID: id, Name: id}
|
||||||
Config: &docker.Config{Image: "testimage"},
|
|
||||||
HostConfig: hostConfig,
|
|
||||||
State: docker.State{
|
|
||||||
Running: true,
|
|
||||||
Pid: os.Getpid(),
|
|
||||||
StartedAt: time.Now(),
|
|
||||||
},
|
|
||||||
NetworkSettings: &docker.NetworkSettings{IPAddress: "1.2.3.4"},
|
|
||||||
}
|
}
|
||||||
if f.ContainerMap != nil {
|
container.HostConfig = hostConfig
|
||||||
container, ok := f.ContainerMap[id]
|
container.State = docker.State{
|
||||||
if !ok {
|
Running: true,
|
||||||
container = &docker.Container{ID: id, Name: id}
|
Pid: os.Getpid(),
|
||||||
}
|
StartedAt: time.Now(),
|
||||||
container.HostConfig = hostConfig
|
|
||||||
container.State = docker.State{
|
|
||||||
Running: true,
|
|
||||||
Pid: os.Getpid(),
|
|
||||||
StartedAt: time.Now(),
|
|
||||||
}
|
|
||||||
container.NetworkSettings = &docker.NetworkSettings{IPAddress: "2.3.4.5"}
|
|
||||||
f.ContainerMap[id] = container
|
|
||||||
}
|
}
|
||||||
|
container.NetworkSettings = &docker.NetworkSettings{IPAddress: "2.3.4.5"}
|
||||||
|
f.ContainerMap[id] = container
|
||||||
f.normalSleep(200, 50, 50)
|
f.normalSleep(200, 50, 50)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -316,24 +306,22 @@ func (f *FakeDockerClient) StopContainer(id string, timeout uint) error {
|
|||||||
newList = append(newList, container)
|
newList = append(newList, container)
|
||||||
}
|
}
|
||||||
f.ContainerList = newList
|
f.ContainerList = newList
|
||||||
if f.ContainerMap != nil {
|
container, ok := f.ContainerMap[id]
|
||||||
container, ok := f.ContainerMap[id]
|
if !ok {
|
||||||
if !ok {
|
container = &docker.Container{
|
||||||
container = &docker.Container{
|
ID: id,
|
||||||
ID: id,
|
Name: id,
|
||||||
Name: id,
|
State: docker.State{
|
||||||
State: docker.State{
|
Running: false,
|
||||||
Running: false,
|
StartedAt: time.Now().Add(-time.Second),
|
||||||
StartedAt: time.Now().Add(-time.Second),
|
FinishedAt: time.Now(),
|
||||||
FinishedAt: time.Now(),
|
},
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
container.State.FinishedAt = time.Now()
|
|
||||||
container.State.Running = false
|
|
||||||
}
|
}
|
||||||
f.ContainerMap[id] = container
|
} else {
|
||||||
|
container.State.FinishedAt = time.Now()
|
||||||
|
container.State.Running = false
|
||||||
}
|
}
|
||||||
|
f.ContainerMap[id] = container
|
||||||
f.normalSleep(200, 50, 50)
|
f.normalSleep(200, 50, 50)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -346,9 +334,7 @@ func (f *FakeDockerClient) RemoveContainer(opts docker.RemoveContainerOptions) e
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
f.Removed = append(f.Removed, opts.ID)
|
f.Removed = append(f.Removed, opts.ID)
|
||||||
}
|
}
|
||||||
if f.ContainerMap != nil {
|
delete(f.ContainerMap, opts.ID)
|
||||||
delete(f.ContainerMap, opts.ID)
|
|
||||||
}
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ func (*fakeOptionGenerator) GenerateRunContainerOptions(pod *api.Pod, container
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newTestDockerManagerWithHTTPClient(fakeHTTPClient *fakeHTTP) (*DockerManager, *FakeDockerClient) {
|
func newTestDockerManagerWithHTTPClient(fakeHTTPClient *fakeHTTP) (*DockerManager, *FakeDockerClient) {
|
||||||
fakeDocker := &FakeDockerClient{VersionInfo: docker.Env{"Version=1.1.3", "ApiVersion=1.15"}, Errors: make(map[string]error), RemovedImages: sets.String{}}
|
fakeDocker := NewFakeDockerClient()
|
||||||
fakeRecorder := &record.FakeRecorder{}
|
fakeRecorder := &record.FakeRecorder{}
|
||||||
containerRefManager := kubecontainer.NewRefManager()
|
containerRefManager := kubecontainer.NewRefManager()
|
||||||
networkPlugin, _ := network.InitNetworkPlugin([]network.NetworkPlugin{}, "", network.NewFakeHost(nil))
|
networkPlugin, _ := network.InitNetworkPlugin([]network.NetworkPlugin{}, "", network.NewFakeHost(nil))
|
||||||
@ -273,18 +273,18 @@ func verifyPods(a, b []*kubecontainer.Pod) bool {
|
|||||||
|
|
||||||
func TestGetPods(t *testing.T) {
|
func TestGetPods(t *testing.T) {
|
||||||
manager, fakeDocker := newTestDockerManager()
|
manager, fakeDocker := newTestDockerManager()
|
||||||
dockerContainers := []docker.APIContainers{
|
dockerContainers := []*docker.Container{
|
||||||
{
|
{
|
||||||
ID: "1111",
|
ID: "1111",
|
||||||
Names: []string{"/k8s_foo_qux_new_1234_42"},
|
Name: "/k8s_foo_qux_new_1234_42",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID: "2222",
|
ID: "2222",
|
||||||
Names: []string{"/k8s_bar_qux_new_1234_42"},
|
Name: "/k8s_bar_qux_new_1234_42",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID: "3333",
|
ID: "3333",
|
||||||
Names: []string{"/k8s_bar_jlk_wen_5678_42"},
|
Name: "/k8s_bar_jlk_wen_5678_42",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -292,7 +292,10 @@ func TestGetPods(t *testing.T) {
|
|||||||
// because the conversion is tested separately in convert_test.go
|
// because the conversion is tested separately in convert_test.go
|
||||||
containers := make([]*kubecontainer.Container, len(dockerContainers))
|
containers := make([]*kubecontainer.Container, len(dockerContainers))
|
||||||
for i := range containers {
|
for i := range containers {
|
||||||
c, err := toRuntimeContainer(&dockerContainers[i])
|
c, err := toRuntimeContainer(&docker.APIContainers{
|
||||||
|
ID: dockerContainers[i].ID,
|
||||||
|
Names: []string{dockerContainers[i].Name},
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error %v", err)
|
t.Fatalf("unexpected error %v", err)
|
||||||
}
|
}
|
||||||
@ -314,7 +317,7 @@ func TestGetPods(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
fakeDocker.ContainerList = dockerContainers
|
fakeDocker.SetFakeRunningContainers(dockerContainers)
|
||||||
actual, err := manager.GetPods(false)
|
actual, err := manager.GetPods(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error %v", err)
|
t.Fatalf("unexpected error %v", err)
|
||||||
@ -390,19 +393,20 @@ func TestKillContainerInPod(t *testing.T) {
|
|||||||
},
|
},
|
||||||
Spec: api.PodSpec{Containers: []api.Container{{Name: "foo"}, {Name: "bar"}}},
|
Spec: api.PodSpec{Containers: []api.Container{{Name: "foo"}, {Name: "bar"}}},
|
||||||
}
|
}
|
||||||
containers := []docker.APIContainers{
|
containers := []*docker.Container{
|
||||||
{
|
{
|
||||||
ID: "1111",
|
ID: "1111",
|
||||||
Names: []string{"/k8s_foo_qux_new_1234_42"},
|
Name: "/k8s_foo_qux_new_1234_42",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID: "2222",
|
ID: "2222",
|
||||||
Names: []string{"/k8s_bar_qux_new_1234_42"},
|
Name: "/k8s_bar_qux_new_1234_42",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
containerToKill := &containers[0]
|
containerToKill := containers[0]
|
||||||
containerToSpare := &containers[1]
|
containerToSpare := containers[1]
|
||||||
fakeDocker.ContainerList = containers
|
|
||||||
|
fakeDocker.SetFakeRunningContainers(containers)
|
||||||
|
|
||||||
if err := manager.KillContainerInPod(kubecontainer.ContainerID{}, &pod.Spec.Containers[0], pod, "test kill container in pod."); err != nil {
|
if err := manager.KillContainerInPod(kubecontainer.ContainerID{}, &pod.Spec.Containers[0], pod, "test kill container in pod."); err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
@ -448,26 +452,24 @@ func TestKillContainerInPodWithPreStop(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
containers := []docker.APIContainers{
|
containers := []*docker.Container{
|
||||||
{
|
{
|
||||||
ID: "1111",
|
ID: "1111",
|
||||||
Names: []string{"/k8s_foo_qux_new_1234_42"},
|
Name: "/k8s_foo_qux_new_1234_42",
|
||||||
},
|
Config: &docker.Config{
|
||||||
{
|
Labels: map[string]string{
|
||||||
ID: "2222",
|
kubernetesPodLabel: string(podString),
|
||||||
Names: []string{"/k8s_bar_qux_new_1234_42"},
|
kubernetesContainerLabel: "foo",
|
||||||
},
|
},
|
||||||
}
|
|
||||||
containerToKill := &containers[0]
|
|
||||||
fakeDocker.ContainerList = containers
|
|
||||||
fakeDocker.Container = &docker.Container{
|
|
||||||
Config: &docker.Config{
|
|
||||||
Labels: map[string]string{
|
|
||||||
kubernetesPodLabel: string(podString),
|
|
||||||
kubernetesContainerLabel: "foo",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
ID: "2222",
|
||||||
|
Name: "/k8s_bar_qux_new_1234_42",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
containerToKill := containers[0]
|
||||||
|
fakeDocker.SetFakeRunningContainers(containers)
|
||||||
|
|
||||||
if err := manager.KillContainerInPod(kubecontainer.ContainerID{}, &pod.Spec.Containers[0], pod, "test kill container with preStop."); err != nil {
|
if err := manager.KillContainerInPod(kubecontainer.ContainerID{}, &pod.Spec.Containers[0], pod, "test kill container with preStop."); err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
@ -493,17 +495,17 @@ func TestKillContainerInPodWithError(t *testing.T) {
|
|||||||
},
|
},
|
||||||
Spec: api.PodSpec{Containers: []api.Container{{Name: "foo"}, {Name: "bar"}}},
|
Spec: api.PodSpec{Containers: []api.Container{{Name: "foo"}, {Name: "bar"}}},
|
||||||
}
|
}
|
||||||
containers := []docker.APIContainers{
|
containers := []*docker.Container{
|
||||||
{
|
{
|
||||||
ID: "1111",
|
ID: "1111",
|
||||||
Names: []string{"/k8s_foo_qux_new_1234_42"},
|
Name: "/k8s_foo_qux_new_1234_42",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ID: "2222",
|
ID: "2222",
|
||||||
Names: []string{"/k8s_bar_qux_new_1234_42"},
|
Name: "/k8s_bar_qux_new_1234_42",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
fakeDocker.ContainerList = containers
|
fakeDocker.SetFakeRunningContainers(containers)
|
||||||
fakeDocker.Errors["stop"] = fmt.Errorf("sample error")
|
fakeDocker.Errors["stop"] = fmt.Errorf("sample error")
|
||||||
|
|
||||||
if err := manager.KillContainerInPod(kubecontainer.ContainerID{}, &pod.Spec.Containers[0], pod, "test kill container with error."); err == nil {
|
if err := manager.KillContainerInPod(kubecontainer.ContainerID{}, &pod.Spec.Containers[0], pod, "test kill container with error."); err == nil {
|
||||||
@ -565,7 +567,7 @@ func runSyncPod(t *testing.T, dm *DockerManager, fakeDocker *FakeDockerClient, p
|
|||||||
func TestSyncPodCreateNetAndContainer(t *testing.T) {
|
func TestSyncPodCreateNetAndContainer(t *testing.T) {
|
||||||
dm, fakeDocker := newTestDockerManager()
|
dm, fakeDocker := newTestDockerManager()
|
||||||
dm.podInfraContainerImage = "pod_infra_image"
|
dm.podInfraContainerImage = "pod_infra_image"
|
||||||
fakeDocker.ContainerList = []docker.APIContainers{}
|
|
||||||
pod := &api.Pod{
|
pod := &api.Pod{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: api.ObjectMeta{
|
||||||
UID: "12345678",
|
UID: "12345678",
|
||||||
@ -599,8 +601,8 @@ func TestSyncPodCreateNetAndContainer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(fakeDocker.Created) != 2 ||
|
if len(fakeDocker.Created) != 2 ||
|
||||||
!matchString(t, "k8s_POD\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) ||
|
!matchString(t, "/k8s_POD\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) ||
|
||||||
!matchString(t, "k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[1]) {
|
!matchString(t, "/k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[1]) {
|
||||||
t.Errorf("Unexpected containers created %v", fakeDocker.Created)
|
t.Errorf("Unexpected containers created %v", fakeDocker.Created)
|
||||||
}
|
}
|
||||||
fakeDocker.Unlock()
|
fakeDocker.Unlock()
|
||||||
@ -612,7 +614,6 @@ func TestSyncPodCreatesNetAndContainerPullsImage(t *testing.T) {
|
|||||||
puller := dm.dockerPuller.(*FakeDockerPuller)
|
puller := dm.dockerPuller.(*FakeDockerPuller)
|
||||||
puller.HasImages = []string{}
|
puller.HasImages = []string{}
|
||||||
dm.podInfraContainerImage = "pod_infra_image"
|
dm.podInfraContainerImage = "pod_infra_image"
|
||||||
fakeDocker.ContainerList = []docker.APIContainers{}
|
|
||||||
pod := &api.Pod{
|
pod := &api.Pod{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: api.ObjectMeta{
|
||||||
UID: "12345678",
|
UID: "12345678",
|
||||||
@ -642,8 +643,8 @@ func TestSyncPodCreatesNetAndContainerPullsImage(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(fakeDocker.Created) != 2 ||
|
if len(fakeDocker.Created) != 2 ||
|
||||||
!matchString(t, "k8s_POD\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) ||
|
!matchString(t, "/k8s_POD\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) ||
|
||||||
!matchString(t, "k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[1]) {
|
!matchString(t, "/k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[1]) {
|
||||||
t.Errorf("Unexpected containers created %v", fakeDocker.Created)
|
t.Errorf("Unexpected containers created %v", fakeDocker.Created)
|
||||||
}
|
}
|
||||||
fakeDocker.Unlock()
|
fakeDocker.Unlock()
|
||||||
@ -680,7 +681,7 @@ func TestSyncPodWithPodInfraCreatesContainer(t *testing.T) {
|
|||||||
|
|
||||||
fakeDocker.Lock()
|
fakeDocker.Lock()
|
||||||
if len(fakeDocker.Created) != 1 ||
|
if len(fakeDocker.Created) != 1 ||
|
||||||
!matchString(t, "k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) {
|
!matchString(t, "/k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) {
|
||||||
t.Errorf("Unexpected containers created %v", fakeDocker.Created)
|
t.Errorf("Unexpected containers created %v", fakeDocker.Created)
|
||||||
}
|
}
|
||||||
fakeDocker.Unlock()
|
fakeDocker.Unlock()
|
||||||
@ -891,7 +892,6 @@ func TestSyncPodWithPullPolicy(t *testing.T) {
|
|||||||
puller := dm.dockerPuller.(*FakeDockerPuller)
|
puller := dm.dockerPuller.(*FakeDockerPuller)
|
||||||
puller.HasImages = []string{"existing_one", "want:latest"}
|
puller.HasImages = []string{"existing_one", "want:latest"}
|
||||||
dm.podInfraContainerImage = "pod_infra_image"
|
dm.podInfraContainerImage = "pod_infra_image"
|
||||||
fakeDocker.ContainerList = []docker.APIContainers{}
|
|
||||||
|
|
||||||
pod := &api.Pod{
|
pod := &api.Pod{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: api.ObjectMeta{
|
||||||
@ -1354,8 +1354,6 @@ func TestGetRestartCount(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
fakeDocker.ContainerMap = map[string]*docker.Container{}
|
|
||||||
|
|
||||||
// Helper function for verifying the restart count.
|
// Helper function for verifying the restart count.
|
||||||
verifyRestartCount := func(pod *api.Pod, expectedCount int) api.PodStatus {
|
verifyRestartCount := func(pod *api.Pod, expectedCount int) api.PodStatus {
|
||||||
runSyncPod(t, dm, fakeDocker, pod, nil, false)
|
runSyncPod(t, dm, fakeDocker, pod, nil, false)
|
||||||
@ -1433,7 +1431,6 @@ func TestGetTerminationMessagePath(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
fakeDocker.ContainerMap = map[string]*docker.Container{}
|
|
||||||
runSyncPod(t, dm, fakeDocker, pod, nil, false)
|
runSyncPod(t, dm, fakeDocker, pod, nil, false)
|
||||||
|
|
||||||
containerList := fakeDocker.ContainerList
|
containerList := fakeDocker.ContainerList
|
||||||
@ -1498,7 +1495,7 @@ func TestSyncPodWithPodInfraCreatesContainerCallsHandler(t *testing.T) {
|
|||||||
|
|
||||||
fakeDocker.Lock()
|
fakeDocker.Lock()
|
||||||
if len(fakeDocker.Created) != 1 ||
|
if len(fakeDocker.Created) != 1 ||
|
||||||
!matchString(t, "k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) {
|
!matchString(t, "/k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) {
|
||||||
t.Errorf("Unexpected containers created %v", fakeDocker.Created)
|
t.Errorf("Unexpected containers created %v", fakeDocker.Created)
|
||||||
}
|
}
|
||||||
fakeDocker.Unlock()
|
fakeDocker.Unlock()
|
||||||
@ -1599,7 +1596,6 @@ func TestSyncPodWithTerminationLog(t *testing.T) {
|
|||||||
Name: "bar",
|
Name: "bar",
|
||||||
TerminationMessagePath: "/dev/somepath",
|
TerminationMessagePath: "/dev/somepath",
|
||||||
}
|
}
|
||||||
fakeDocker.ContainerList = []docker.APIContainers{}
|
|
||||||
pod := &api.Pod{
|
pod := &api.Pod{
|
||||||
ObjectMeta: api.ObjectMeta{
|
ObjectMeta: api.ObjectMeta{
|
||||||
UID: "12345678",
|
UID: "12345678",
|
||||||
@ -1624,9 +1620,17 @@ func TestSyncPodWithTerminationLog(t *testing.T) {
|
|||||||
defer os.Remove(testPodContainerDir)
|
defer os.Remove(testPodContainerDir)
|
||||||
|
|
||||||
fakeDocker.Lock()
|
fakeDocker.Lock()
|
||||||
defer fakeDocker.Unlock()
|
if len(fakeDocker.Created) != 2 ||
|
||||||
|
!matchString(t, "/k8s_POD\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) ||
|
||||||
parts := strings.Split(fakeDocker.Container.HostConfig.Binds[0], ":")
|
!matchString(t, "/k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[1]) {
|
||||||
|
t.Errorf("Unexpected containers created %v", fakeDocker.Created)
|
||||||
|
}
|
||||||
|
fakeDocker.Unlock()
|
||||||
|
newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1])
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error %v", err)
|
||||||
|
}
|
||||||
|
parts := strings.Split(newContainer.HostConfig.Binds[0], ":")
|
||||||
if !matchString(t, testPodContainerDir+"/k8s_bar\\.[a-f0-9]", parts[0]) {
|
if !matchString(t, testPodContainerDir+"/k8s_bar\\.[a-f0-9]", parts[0]) {
|
||||||
t.Errorf("Unexpected host path: %s", parts[0])
|
t.Errorf("Unexpected host path: %s", parts[0])
|
||||||
}
|
}
|
||||||
@ -1664,17 +1668,20 @@ func TestSyncPodWithHostNetwork(t *testing.T) {
|
|||||||
|
|
||||||
fakeDocker.Lock()
|
fakeDocker.Lock()
|
||||||
if len(fakeDocker.Created) != 2 ||
|
if len(fakeDocker.Created) != 2 ||
|
||||||
!matchString(t, "k8s_POD\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) ||
|
!matchString(t, "/k8s_POD\\.[a-f0-9]+_foo_new_", fakeDocker.Created[0]) ||
|
||||||
!matchString(t, "k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[1]) {
|
!matchString(t, "/k8s_bar\\.[a-f0-9]+_foo_new_", fakeDocker.Created[1]) {
|
||||||
t.Errorf("Unexpected containers created %v", fakeDocker.Created)
|
t.Errorf("Unexpected containers created %v", fakeDocker.Created)
|
||||||
}
|
}
|
||||||
|
fakeDocker.Unlock()
|
||||||
|
|
||||||
utsMode := fakeDocker.Container.HostConfig.UTSMode
|
newContainer, err := fakeDocker.InspectContainer(fakeDocker.Created[1])
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error %v", err)
|
||||||
|
}
|
||||||
|
utsMode := newContainer.HostConfig.UTSMode
|
||||||
if utsMode != "host" {
|
if utsMode != "host" {
|
||||||
t.Errorf("Pod with host network must have \"host\" utsMode, actual: \"%v\"", utsMode)
|
t.Errorf("Pod with host network must have \"host\" utsMode, actual: \"%v\"", utsMode)
|
||||||
}
|
}
|
||||||
|
|
||||||
fakeDocker.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetPodStatusSortedContainers(t *testing.T) {
|
func TestGetPodStatusSortedContainers(t *testing.T) {
|
||||||
|
@ -38,7 +38,6 @@ import (
|
|||||||
"k8s.io/kubernetes/pkg/kubelet/dockertools"
|
"k8s.io/kubernetes/pkg/kubelet/dockertools"
|
||||||
"k8s.io/kubernetes/pkg/kubelet/network"
|
"k8s.io/kubernetes/pkg/kubelet/network"
|
||||||
proberesults "k8s.io/kubernetes/pkg/kubelet/prober/results"
|
proberesults "k8s.io/kubernetes/pkg/kubelet/prober/results"
|
||||||
"k8s.io/kubernetes/pkg/util/sets"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// The temp dir where test plugins will be stored.
|
// The temp dir where test plugins will be stored.
|
||||||
@ -137,15 +136,17 @@ func (fnh *fakeNetworkHost) GetKubeClient() client.Interface {
|
|||||||
|
|
||||||
func (nh *fakeNetworkHost) GetRuntime() kubecontainer.Runtime {
|
func (nh *fakeNetworkHost) GetRuntime() kubecontainer.Runtime {
|
||||||
dm, fakeDockerClient := newTestDockerManager()
|
dm, fakeDockerClient := newTestDockerManager()
|
||||||
fakeDockerClient.Container = &docker.Container{
|
fakeDockerClient.SetFakeRunningContainers([]*docker.Container{
|
||||||
ID: "foobar",
|
{
|
||||||
State: docker.State{Pid: 12345},
|
ID: "test_infra_container",
|
||||||
}
|
State: docker.State{Pid: 12345},
|
||||||
|
},
|
||||||
|
})
|
||||||
return dm
|
return dm
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTestDockerManager() (*dockertools.DockerManager, *dockertools.FakeDockerClient) {
|
func newTestDockerManager() (*dockertools.DockerManager, *dockertools.FakeDockerClient) {
|
||||||
fakeDocker := &dockertools.FakeDockerClient{VersionInfo: docker.Env{"Version=1.1.3", "ApiVersion=1.15"}, Errors: make(map[string]error), RemovedImages: sets.String{}}
|
fakeDocker := dockertools.NewFakeDockerClient()
|
||||||
fakeRecorder := &record.FakeRecorder{}
|
fakeRecorder := &record.FakeRecorder{}
|
||||||
containerRefManager := kubecontainer.NewRefManager()
|
containerRefManager := kubecontainer.NewRefManager()
|
||||||
networkPlugin, _ := network.InitNetworkPlugin([]network.NetworkPlugin{}, "", network.NewFakeHost(nil))
|
networkPlugin, _ := network.InitNetworkPlugin([]network.NetworkPlugin{}, "", network.NewFakeHost(nil))
|
||||||
@ -183,7 +184,7 @@ func TestCNIPlugin(t *testing.T) {
|
|||||||
t.Fatalf("Failed to select the desired plugin: %v", err)
|
t.Fatalf("Failed to select the desired plugin: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = plug.SetUpPod("podNamespace", "podName", "dockerid2345")
|
err = plug.SetUpPod("podNamespace", "podName", "test_infra_container")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected nil: %v", err)
|
t.Errorf("Expected nil: %v", err)
|
||||||
}
|
}
|
||||||
@ -194,16 +195,16 @@ func TestCNIPlugin(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to read output file %s: %v (env %s err %v)", outputFile, err, eo, eerr)
|
t.Errorf("Failed to read output file %s: %v (env %s err %v)", outputFile, err, eo, eerr)
|
||||||
}
|
}
|
||||||
expectedOutput := "ADD /proc/12345/ns/net podNamespace podName dockerid2345"
|
expectedOutput := "ADD /proc/12345/ns/net podNamespace podName test_infra_container"
|
||||||
if string(output) != expectedOutput {
|
if string(output) != expectedOutput {
|
||||||
t.Errorf("Mismatch in expected output for setup hook. Expected '%s', got '%s'", expectedOutput, string(output))
|
t.Errorf("Mismatch in expected output for setup hook. Expected '%s', got '%s'", expectedOutput, string(output))
|
||||||
}
|
}
|
||||||
err = plug.TearDownPod("podNamespace", "podName", "dockerid4545454")
|
err = plug.TearDownPod("podNamespace", "podName", "test_infra_container")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected nil: %v", err)
|
t.Errorf("Expected nil: %v", err)
|
||||||
}
|
}
|
||||||
output, err = ioutil.ReadFile(path.Join(testNetworkConfigPath, pluginName, pluginName+".out"))
|
output, err = ioutil.ReadFile(path.Join(testNetworkConfigPath, pluginName, pluginName+".out"))
|
||||||
expectedOutput = "DEL /proc/12345/ns/net podNamespace podName dockerid4545454"
|
expectedOutput = "DEL /proc/12345/ns/net podNamespace podName test_infra_container"
|
||||||
if string(output) != expectedOutput {
|
if string(output) != expectedOutput {
|
||||||
t.Errorf("Mismatch in expected output for setup hook. Expected '%s', got '%s'", expectedOutput, string(output))
|
t.Errorf("Mismatch in expected output for setup hook. Expected '%s', got '%s'", expectedOutput, string(output))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user