mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-10 21:50:05 +00:00
Clean unittests
This commit is contained in:
@@ -27,14 +27,11 @@ import (
|
|||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||||
"github.com/fsouza/go-dockerclient"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHTTPPodInfoGetter(t *testing.T) {
|
func TestHTTPPodInfoGetter(t *testing.T) {
|
||||||
expectObj := api.PodInfo{
|
expectObj := api.PodInfo{
|
||||||
"myID": api.ContainerStatus{
|
"myID": api.ContainerStatus{},
|
||||||
DetailInfo: docker.Container{ID: "myID"},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
body, err := json.Marshal(expectObj)
|
body, err := json.Marshal(expectObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -69,17 +66,14 @@ func TestHTTPPodInfoGetter(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// reflect.DeepEqual(expectObj, gotObj) doesn't handle blank times well
|
// reflect.DeepEqual(expectObj, gotObj) doesn't handle blank times well
|
||||||
if len(gotObj) != len(expectObj) ||
|
if len(gotObj) != len(expectObj) {
|
||||||
expectObj["myID"].DetailInfo.ID != gotObj["myID"].DetailInfo.ID {
|
|
||||||
t.Errorf("Unexpected response. Expected: %#v, received %#v", expectObj, gotObj)
|
t.Errorf("Unexpected response. Expected: %#v, received %#v", expectObj, gotObj)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHTTPPodInfoGetterNotFound(t *testing.T) {
|
func TestHTTPPodInfoGetterNotFound(t *testing.T) {
|
||||||
expectObj := api.PodInfo{
|
expectObj := api.PodInfo{
|
||||||
"myID": api.ContainerStatus{
|
"myID": api.ContainerStatus{},
|
||||||
DetailInfo: docker.Container{ID: "myID"},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
_, err := json.Marshal(expectObj)
|
_, err := json.Marshal(expectObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -29,6 +29,7 @@ type FakeDockerClient struct {
|
|||||||
sync.Mutex
|
sync.Mutex
|
||||||
ContainerList []docker.APIContainers
|
ContainerList []docker.APIContainers
|
||||||
Container *docker.Container
|
Container *docker.Container
|
||||||
|
Image *docker.Image
|
||||||
Err error
|
Err error
|
||||||
called []string
|
called []string
|
||||||
Stopped []string
|
Stopped []string
|
||||||
@@ -67,10 +68,19 @@ func (f *FakeDockerClient) ListContainers(options docker.ListContainersOptions)
|
|||||||
func (f *FakeDockerClient) InspectContainer(id string) (*docker.Container, error) {
|
func (f *FakeDockerClient) InspectContainer(id string) (*docker.Container, error) {
|
||||||
f.Lock()
|
f.Lock()
|
||||||
defer f.Unlock()
|
defer f.Unlock()
|
||||||
f.called = append(f.called, "inspect")
|
f.called = append(f.called, "inspect_container")
|
||||||
return f.Container, f.Err
|
return f.Container, f.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InspectImage is a test-spy implementation of DockerInterface.InspectImage.
|
||||||
|
// It adds an entry "inspect" to the internal method call record.
|
||||||
|
func (f *FakeDockerClient) InspectImage(name string) (*docker.Image, error) {
|
||||||
|
f.Lock()
|
||||||
|
defer f.Unlock()
|
||||||
|
f.called = append(f.called, "inspect_image")
|
||||||
|
return f.Image, f.Err
|
||||||
|
}
|
||||||
|
|
||||||
// CreateContainer is a test-spy implementation of DockerInterface.CreateContainer.
|
// CreateContainer is a test-spy implementation of DockerInterface.CreateContainer.
|
||||||
// It adds an entry "create" to the internal method call record.
|
// It adds an entry "create" to the internal method call record.
|
||||||
func (f *FakeDockerClient) CreateContainer(c docker.CreateContainerOptions) (*docker.Container, error) {
|
func (f *FakeDockerClient) CreateContainer(c docker.CreateContainerOptions) (*docker.Container, error) {
|
||||||
|
@@ -227,7 +227,7 @@ func TestSyncPodsCreatesNetAndContainer(t *testing.T) {
|
|||||||
kubelet.drainWorkers()
|
kubelet.drainWorkers()
|
||||||
|
|
||||||
verifyCalls(t, fakeDocker, []string{
|
verifyCalls(t, fakeDocker, []string{
|
||||||
"list", "list", "create", "start", "list", "inspect", "list", "create", "start"})
|
"list", "list", "create", "start", "list", "inspect_container", "list", "create", "start"})
|
||||||
|
|
||||||
fakeDocker.Lock()
|
fakeDocker.Lock()
|
||||||
|
|
||||||
@@ -316,7 +316,7 @@ func TestSyncPodsWithNetCreatesContainer(t *testing.T) {
|
|||||||
kubelet.drainWorkers()
|
kubelet.drainWorkers()
|
||||||
|
|
||||||
verifyCalls(t, fakeDocker, []string{
|
verifyCalls(t, fakeDocker, []string{
|
||||||
"list", "list", "list", "inspect", "list", "create", "start"})
|
"list", "list", "list", "inspect_container", "list", "create", "start"})
|
||||||
|
|
||||||
fakeDocker.Lock()
|
fakeDocker.Lock()
|
||||||
if len(fakeDocker.Created) != 1 ||
|
if len(fakeDocker.Created) != 1 ||
|
||||||
@@ -366,7 +366,7 @@ func TestSyncPodsWithNetCreatesContainerCallsHandler(t *testing.T) {
|
|||||||
kubelet.drainWorkers()
|
kubelet.drainWorkers()
|
||||||
|
|
||||||
verifyCalls(t, fakeDocker, []string{
|
verifyCalls(t, fakeDocker, []string{
|
||||||
"list", "list", "list", "inspect", "list", "create", "start"})
|
"list", "list", "list", "inspect_container", "list", "create", "start"})
|
||||||
|
|
||||||
fakeDocker.Lock()
|
fakeDocker.Lock()
|
||||||
if len(fakeDocker.Created) != 1 ||
|
if len(fakeDocker.Created) != 1 ||
|
||||||
@@ -406,7 +406,7 @@ func TestSyncPodsDeletesWithNoNetContainer(t *testing.T) {
|
|||||||
kubelet.drainWorkers()
|
kubelet.drainWorkers()
|
||||||
|
|
||||||
verifyCalls(t, fakeDocker, []string{
|
verifyCalls(t, fakeDocker, []string{
|
||||||
"list", "list", "stop", "create", "start", "list", "list", "inspect", "list", "create", "start"})
|
"list", "list", "stop", "create", "start", "list", "list", "inspect_container", "list", "create", "start"})
|
||||||
|
|
||||||
// A map iteration is used to delete containers, so must not depend on
|
// A map iteration is used to delete containers, so must not depend on
|
||||||
// order here.
|
// order here.
|
||||||
|
@@ -31,7 +31,6 @@ import (
|
|||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||||
"github.com/fsouza/go-dockerclient"
|
|
||||||
"github.com/google/cadvisor/info"
|
"github.com/google/cadvisor/info"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -147,9 +146,7 @@ func TestContainers(t *testing.T) {
|
|||||||
func TestPodInfo(t *testing.T) {
|
func TestPodInfo(t *testing.T) {
|
||||||
fw := newServerTest()
|
fw := newServerTest()
|
||||||
expected := api.PodInfo{
|
expected := api.PodInfo{
|
||||||
"goodpod": api.ContainerStatus{
|
"goodpod": api.ContainerStatus{},
|
||||||
DetailInfo: docker.Container{ID: "myContainerID"},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
fw.fakeKubelet.infoFunc = func(name string) (api.PodInfo, error) {
|
fw.fakeKubelet.infoFunc = func(name string) (api.PodInfo, error) {
|
||||||
if name == "goodpod.etcd" {
|
if name == "goodpod.etcd" {
|
||||||
|
@@ -22,7 +22,6 @@ import (
|
|||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
|
||||||
"github.com/fsouza/go-dockerclient"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type FakePodInfoGetter struct {
|
type FakePodInfoGetter struct {
|
||||||
@@ -42,9 +41,7 @@ func TestPodCacheGet(t *testing.T) {
|
|||||||
cache := NewPodCache(nil, nil)
|
cache := NewPodCache(nil, nil)
|
||||||
|
|
||||||
expected := api.PodInfo{
|
expected := api.PodInfo{
|
||||||
"foo": api.ContainerStatus{
|
"foo": api.ContainerStatus{},
|
||||||
DetailInfo: docker.Container{ID: "foo"},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
cache.podInfo["foo"] = expected
|
cache.podInfo["foo"] = expected
|
||||||
|
|
||||||
@@ -71,9 +68,7 @@ func TestPodCacheGetMissing(t *testing.T) {
|
|||||||
|
|
||||||
func TestPodGetPodInfoGetter(t *testing.T) {
|
func TestPodGetPodInfoGetter(t *testing.T) {
|
||||||
expected := api.PodInfo{
|
expected := api.PodInfo{
|
||||||
"foo": api.ContainerStatus{
|
"foo": api.ContainerStatus{},
|
||||||
DetailInfo: docker.Container{ID: "foo"},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
fake := FakePodInfoGetter{
|
fake := FakePodInfoGetter{
|
||||||
data: expected,
|
data: expected,
|
||||||
@@ -107,9 +102,7 @@ func TestPodUpdateAllContainers(t *testing.T) {
|
|||||||
mockRegistry := registrytest.NewPodRegistry(&api.PodList{Items: pods})
|
mockRegistry := registrytest.NewPodRegistry(&api.PodList{Items: pods})
|
||||||
|
|
||||||
expected := api.PodInfo{
|
expected := api.PodInfo{
|
||||||
"foo": api.ContainerStatus{
|
"foo": api.ContainerStatus{},
|
||||||
DetailInfo: docker.Container{ID: "foo"},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
fake := FakePodInfoGetter{
|
fake := FakePodInfoGetter{
|
||||||
data: expected,
|
data: expected,
|
||||||
|
@@ -31,8 +31,6 @@ import (
|
|||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||||
|
|
||||||
"github.com/fsouza/go-dockerclient"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func expectApiStatusError(t *testing.T, ch <-chan runtime.Object, msg string) {
|
func expectApiStatusError(t *testing.T, ch <-chan runtime.Object, msg string) {
|
||||||
@@ -605,16 +603,17 @@ func (f *FakePodInfoGetter) GetPodInfo(host, podID string) (api.PodInfo, error)
|
|||||||
|
|
||||||
func TestFillPodInfo(t *testing.T) {
|
func TestFillPodInfo(t *testing.T) {
|
||||||
expectedIP := "1.2.3.4"
|
expectedIP := "1.2.3.4"
|
||||||
|
expectedTime, _ := time.Parse("2013-Feb-03", "2013-Feb-03")
|
||||||
fakeGetter := FakePodInfoGetter{
|
fakeGetter := FakePodInfoGetter{
|
||||||
info: map[string]api.ContainerStatus{
|
info: map[string]api.ContainerStatus{
|
||||||
"net": {
|
"net": {
|
||||||
DetailInfo: docker.Container{
|
State: api.ContainerState{
|
||||||
ID: "foobar",
|
Running: &api.ContainerStateRunning{
|
||||||
Path: "bin/run.sh",
|
StartedAt: expectedTime,
|
||||||
NetworkSettings: &docker.NetworkSettings{
|
|
||||||
IPAddress: expectedIP,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
RestartCount: 1,
|
||||||
|
PodIP: expectedIP,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -636,10 +635,7 @@ func TestFillPodInfoNoData(t *testing.T) {
|
|||||||
fakeGetter := FakePodInfoGetter{
|
fakeGetter := FakePodInfoGetter{
|
||||||
info: map[string]api.ContainerStatus{
|
info: map[string]api.ContainerStatus{
|
||||||
"net": {
|
"net": {
|
||||||
DetailInfo: docker.Container{
|
State: api.ContainerState{},
|
||||||
ID: "foobar",
|
|
||||||
Path: "bin/run.sh",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user