Files
kubernetes/pkg/registry/pod/storage_test.go
Kelsey Hightower c21a0ca39f Breakup the registry package into separate packages.
Currently all registry implementations live in a single package,
which makes it bit harder to maintain. The different registry
implementations do not follow the same coding style and naming
conventions, which makes the code harder to read.

Breakup the registry package into smaller packages based on
the registry implementation. Refactor the registry packages
to follow a similar coding style and naming convention.

This patch does not introduce any changes in behavior.
2014-08-11 20:58:09 -07:00

498 lines
12 KiB
Go

/*
Copyright 2014 Google Inc. 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 pod
import (
"fmt"
"reflect"
"testing"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/minion"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/registrytest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/scheduler"
"github.com/fsouza/go-dockerclient"
)
func expectApiStatusError(t *testing.T, ch <-chan interface{}, msg string) {
out := <-ch
status, ok := out.(*api.Status)
if !ok {
t.Errorf("Expected an api.Status object, was %#v", out)
return
}
if msg != status.Message {
t.Errorf("Expected %#v, was %s", msg, status.Message)
}
}
func expectPod(t *testing.T, ch <-chan interface{}) (*api.Pod, bool) {
out := <-ch
pod, ok := out.(*api.Pod)
if !ok || pod == nil {
t.Errorf("Expected an api.Pod object, was %#v", out)
return nil, false
}
return pod, true
}
func TestCreatePodRegistryError(t *testing.T) {
podRegistry := &registrytest.PodRegistry{
Err: fmt.Errorf("test error"),
}
storage := RegistryStorage{
scheduler: &registrytest.Scheduler{},
registry: podRegistry,
}
desiredState := api.PodState{
Manifest: api.ContainerManifest{
Version: "v1beta1",
},
}
pod := &api.Pod{DesiredState: desiredState}
ch, err := storage.Create(pod)
if err != nil {
t.Errorf("Expected %#v, Got %#v", nil, err)
}
expectApiStatusError(t, ch, podRegistry.Err.Error())
}
func TestCreatePodSchedulerError(t *testing.T) {
mockScheduler := registrytest.Scheduler{
Err: fmt.Errorf("test error"),
}
storage := RegistryStorage{
scheduler: &mockScheduler,
}
desiredState := api.PodState{
Manifest: api.ContainerManifest{
Version: "v1beta1",
},
}
pod := &api.Pod{DesiredState: desiredState}
ch, err := storage.Create(pod)
if err != nil {
t.Errorf("Expected %#v, Got %#v", nil, err)
}
expectApiStatusError(t, ch, mockScheduler.Err.Error())
}
func TestCreatePodSetsIds(t *testing.T) {
mockRegistry := &registrytest.PodRegistryStorage{
PodRegistry: registrytest.PodRegistry{Err: fmt.Errorf("test error")},
}
storage := RegistryStorage{
scheduler: &registrytest.Scheduler{Machine: "test"},
registry: mockRegistry,
}
desiredState := api.PodState{
Manifest: api.ContainerManifest{
Version: "v1beta1",
},
}
pod := &api.Pod{DesiredState: desiredState}
ch, err := storage.Create(pod)
if err != nil {
t.Errorf("Expected %#v, Got %#v", nil, err)
}
expectApiStatusError(t, ch, mockRegistry.Err.Error())
if len(mockRegistry.PodRegistry.Pod.ID) == 0 {
t.Errorf("Expected pod ID to be set, Got %#v", pod)
}
if mockRegistry.PodRegistry.Pod.DesiredState.Manifest.ID != mockRegistry.PodRegistry.Pod.ID {
t.Errorf("Expected manifest ID to be equal to pod ID, Got %#v", pod)
}
}
func TestListPodsError(t *testing.T) {
mockRegistry := registrytest.PodRegistry{
Err: fmt.Errorf("test error"),
}
storage := RegistryStorage{
registry: &mockRegistry,
}
pods, err := storage.List(labels.Everything())
if err != mockRegistry.Err {
t.Errorf("Expected %#v, Got %#v", mockRegistry.Err, err)
}
if len(pods.(api.PodList).Items) != 0 {
t.Errorf("Unexpected non-zero pod list: %#v", pods)
}
}
func TestListEmptyPodList(t *testing.T) {
mockRegistry := registrytest.PodRegistry{}
storage := RegistryStorage{
registry: &mockRegistry,
}
pods, err := storage.List(labels.Everything())
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(pods.(api.PodList).Items) != 0 {
t.Errorf("Unexpected non-zero pod list: %#v", pods)
}
}
func TestListPodList(t *testing.T) {
mockRegistry := registrytest.PodRegistry{
Pods: []api.Pod{
{
JSONBase: api.JSONBase{
ID: "foo",
},
},
{
JSONBase: api.JSONBase{
ID: "bar",
},
},
},
}
storage := RegistryStorage{
registry: &mockRegistry,
}
podsObj, err := storage.List(labels.Everything())
pods := podsObj.(api.PodList)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(pods.Items) != 2 {
t.Errorf("Unexpected pod list: %#v", pods)
}
if pods.Items[0].ID != "foo" {
t.Errorf("Unexpected pod: %#v", pods.Items[0])
}
if pods.Items[1].ID != "bar" {
t.Errorf("Unexpected pod: %#v", pods.Items[1])
}
}
func TestPodDecode(t *testing.T) {
mockRegistry := registrytest.PodRegistry{}
storage := RegistryStorage{
registry: &mockRegistry,
}
expected := &api.Pod{
JSONBase: api.JSONBase{
ID: "foo",
},
}
body, err := api.Encode(expected)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
actual := storage.New()
if err := api.DecodeInto(body, actual); err != nil {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Expected %#v, Got %#v", expected, actual)
}
}
func TestGetPod(t *testing.T) {
mockRegistry := registrytest.PodRegistry{
Pod: &api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
},
}
storage := RegistryStorage{
registry: &mockRegistry,
}
obj, err := storage.Get("foo")
pod := obj.(*api.Pod)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(*mockRegistry.Pod, *pod) {
t.Errorf("Unexpected pod. Expected %#v, Got %#v", *mockRegistry.Pod, *pod)
}
}
func TestGetPodCloud(t *testing.T) {
fakeCloud := &cloudprovider.FakeCloud{}
mockRegistry := registrytest.PodRegistry{
Pod: &api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
},
}
storage := RegistryStorage{
registry: &mockRegistry,
cloudProvider: fakeCloud,
}
obj, err := storage.Get("foo")
pod := obj.(*api.Pod)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(*mockRegistry.Pod, *pod) {
t.Errorf("Unexpected pod. Expected %#v, Got %#v", *mockRegistry.Pod, *pod)
}
if len(fakeCloud.Calls) != 1 || fakeCloud.Calls[0] != "ip-address" {
t.Errorf("Unexpected calls: %#v", fakeCloud.Calls)
}
}
func TestMakePodStatus(t *testing.T) {
desiredState := api.PodState{
Manifest: api.ContainerManifest{
Version: "v1beta1",
Containers: []api.Container{
{Name: "containerA"},
{Name: "containerB"},
},
},
}
currentState := api.PodState{
Host: "machine",
}
pod := &api.Pod{DesiredState: desiredState, CurrentState: currentState}
status := makePodStatus(pod)
if status != api.PodWaiting {
t.Errorf("Expected 'Waiting', got '%s'", status)
}
runningState := docker.Container{
State: docker.State{
Running: true,
},
}
stoppedState := docker.Container{
State: docker.State{
Running: false,
},
}
// All running.
pod = &api.Pod{
DesiredState: desiredState,
CurrentState: api.PodState{
Info: map[string]docker.Container{
"containerA": runningState,
"containerB": runningState,
},
Host: "machine",
},
}
status = makePodStatus(pod)
if status != api.PodRunning {
t.Errorf("Expected 'Running', got '%s'", status)
}
// All stopped.
pod = &api.Pod{
DesiredState: desiredState,
CurrentState: api.PodState{
Info: map[string]docker.Container{
"containerA": stoppedState,
"containerB": stoppedState,
},
Host: "machine",
},
}
status = makePodStatus(pod)
if status != api.PodTerminated {
t.Errorf("Expected 'Terminated', got '%s'", status)
}
// Mixed state.
pod = &api.Pod{
DesiredState: desiredState,
CurrentState: api.PodState{
Info: map[string]docker.Container{
"containerA": runningState,
"containerB": stoppedState,
},
Host: "machine",
},
}
status = makePodStatus(pod)
if status != api.PodWaiting {
t.Errorf("Expected 'Waiting', got '%s'", status)
}
// Mixed state.
pod = &api.Pod{
DesiredState: desiredState,
CurrentState: api.PodState{
Info: map[string]docker.Container{
"containerA": runningState,
},
Host: "machine",
},
}
status = makePodStatus(pod)
if status != api.PodWaiting {
t.Errorf("Expected 'Waiting', got '%s'", status)
}
}
func TestPodStorageValidatesCreate(t *testing.T) {
mockRegistry := &registrytest.PodRegistryStorage{
PodRegistry: registrytest.PodRegistry{Err: fmt.Errorf("test error")},
}
storage := RegistryStorage{
scheduler: &registrytest.Scheduler{Machine: "test"},
registry: mockRegistry,
}
pod := &api.Pod{}
c, err := storage.Create(pod)
if c != nil {
t.Errorf("Expected nil channel")
}
if err == nil {
t.Errorf("Expected to get an error")
}
}
func TestPodStorageValidatesUpdate(t *testing.T) {
mockRegistry := &registrytest.PodRegistryStorage{
PodRegistry: registrytest.PodRegistry{Err: fmt.Errorf("test error")},
}
storage := RegistryStorage{
scheduler: &registrytest.Scheduler{Machine: "test"},
registry: mockRegistry,
}
pod := &api.Pod{}
c, err := storage.Update(pod)
if c != nil {
t.Errorf("Expected nil channel")
}
if err == nil {
t.Errorf("Expected to get an error")
}
}
func TestCreatePod(t *testing.T) {
mockRegistry := registrytest.PodRegistry{
Pod: &api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
CurrentState: api.PodState{
Host: "machine",
},
},
}
storage := RegistryStorage{
registry: &mockRegistry,
podPollPeriod: time.Millisecond * 100,
scheduler: scheduler.MakeRoundRobinScheduler(),
minionLister: minion.NewRegistry([]string{"machine"}),
}
desiredState := api.PodState{
Manifest: api.ContainerManifest{
Version: "v1beta1",
},
}
pod := &api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
DesiredState: desiredState,
}
channel, err := storage.Create(pod)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
select {
case <-time.After(time.Millisecond * 100):
// Do nothing, this is expected.
case <-channel:
t.Error("Unexpected read from async channel")
}
mockRegistry.UpdatePod(api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
CurrentState: api.PodState{
Status: api.PodRunning,
},
})
select {
case <-time.After(time.Second * 1):
t.Error("Unexpected timeout")
case <-channel:
// Do nothing, this is expected.
}
}
type FakePodInfoGetter struct {
info api.PodInfo
err error
}
func (f *FakePodInfoGetter) GetPodInfo(host, podID string) (api.PodInfo, error) {
return f.info, f.err
}
func TestFillPodInfo(t *testing.T) {
expectedIP := "1.2.3.4"
fakeGetter := FakePodInfoGetter{
info: map[string]docker.Container{
"net": {
ID: "foobar",
Path: "bin/run.sh",
NetworkSettings: &docker.NetworkSettings{
IPAddress: expectedIP,
},
},
},
}
storage := RegistryStorage{
podCache: &fakeGetter,
}
pod := api.Pod{}
storage.fillPodInfo(&pod)
if !reflect.DeepEqual(fakeGetter.info, pod.CurrentState.Info) {
t.Errorf("Expected: %#v, Got %#v", fakeGetter.info, pod.CurrentState.Info)
}
if pod.CurrentState.PodIP != expectedIP {
t.Errorf("Expected %s, Got %s", expectedIP, pod.CurrentState.PodIP)
}
}
func TestFillPodInfoNoData(t *testing.T) {
expectedIP := ""
fakeGetter := FakePodInfoGetter{
info: map[string]docker.Container{
"net": {
ID: "foobar",
Path: "bin/run.sh",
},
},
}
storage := RegistryStorage{
podCache: &fakeGetter,
}
pod := api.Pod{}
storage.fillPodInfo(&pod)
if !reflect.DeepEqual(fakeGetter.info, pod.CurrentState.Info) {
t.Errorf("Expected %#v, Got %#v", fakeGetter.info, pod.CurrentState.Info)
}
if pod.CurrentState.PodIP != expectedIP {
t.Errorf("Expected %s, Got %s", expectedIP, pod.CurrentState.PodIP)
}
}