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