Skip to content

Commit

Permalink
Fix/expand kubecfg unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeda committed Sep 4, 2014
1 parent f8e2f92 commit 3c0a736
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
5 changes: 3 additions & 2 deletions pkg/client/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package client
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/version"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)
Expand All @@ -43,7 +44,7 @@ type Fake struct {

func (c *Fake) ListPods(selector labels.Selector) (api.PodList, error) {
c.Actions = append(c.Actions, FakeAction{Action: "list-pods"})
return c.Pods, nil
return *runtime.CopyOrDie(c.Pods).(*api.PodList), nil
}

func (c *Fake) GetPod(name string) (api.Pod, error) {
Expand Down Expand Up @@ -73,7 +74,7 @@ func (c *Fake) ListReplicationControllers(selector labels.Selector) (api.Replica

func (c *Fake) GetReplicationController(name string) (api.ReplicationController, error) {
c.Actions = append(c.Actions, FakeAction{Action: "get-controller", Value: name})
return c.Ctrl, nil
return *runtime.CopyOrDie(c.Ctrl).(*api.ReplicationController), nil
}

func (c *Fake) CreateReplicationController(controller api.ReplicationController) (api.ReplicationController, error) {
Expand Down
46 changes: 43 additions & 3 deletions pkg/kubecfg/kubecfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import (

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
)

func validateAction(expectedAction, actualAction client.FakeAction, t *testing.T) {
if expectedAction != actualAction {
if !reflect.DeepEqual(expectedAction, actualAction) {
t.Errorf("Unexpected Action: %#v, expected: %#v", actualAction, expectedAction)
}
}
Expand All @@ -43,7 +44,7 @@ func TestUpdateWithPods(t *testing.T) {
},
},
}
Update("foo", &fakeClient, 0)
Update("foo", &fakeClient, 0, "")
if len(fakeClient.Actions) != 5 {
t.Errorf("Unexpected action list %#v", fakeClient.Actions)
}
Expand All @@ -57,14 +58,53 @@ func TestUpdateWithPods(t *testing.T) {

func TestUpdateNoPods(t *testing.T) {
fakeClient := client.Fake{}
Update("foo", &fakeClient, 0)
Update("foo", &fakeClient, 0, "")
if len(fakeClient.Actions) != 2 {
t.Errorf("Unexpected action list %#v", fakeClient.Actions)
}
validateAction(client.FakeAction{Action: "get-controller", Value: "foo"}, fakeClient.Actions[0], t)
validateAction(client.FakeAction{Action: "list-pods"}, fakeClient.Actions[1], t)
}

func TestUpdateWithNewImage(t *testing.T) {
fakeClient := client.Fake{
Pods: api.PodList{
Items: []api.Pod{
{JSONBase: api.JSONBase{ID: "pod-1"}},
{JSONBase: api.JSONBase{ID: "pod-2"}},
},
},
Ctrl: api.ReplicationController{
DesiredState: api.ReplicationControllerState{
PodTemplate: api.PodTemplate{
DesiredState: api.PodState{
Manifest: api.ContainerManifest{
Containers: []api.Container{
{Image: "fooImage:1"},
},
},
},
},
},
},
}
Update("foo", &fakeClient, 0, "fooImage:2")
if len(fakeClient.Actions) != 6 {
t.Errorf("Unexpected action list %#v", fakeClient.Actions)
}
validateAction(client.FakeAction{Action: "get-controller", Value: "foo"}, fakeClient.Actions[0], t)

newCtrl := *runtime.CopyOrDie(fakeClient.Ctrl).(*api.ReplicationController)
newCtrl.DesiredState.PodTemplate.DesiredState.Manifest.Containers[0].Image = "fooImage:2"
validateAction(client.FakeAction{Action: "update-controller", Value: newCtrl}, fakeClient.Actions[1], t)

validateAction(client.FakeAction{Action: "list-pods"}, fakeClient.Actions[2], t)
// Update deletes the pods, it relies on the replication controller to replace them.
validateAction(client.FakeAction{Action: "delete-pod", Value: "pod-1"}, fakeClient.Actions[3], t)
validateAction(client.FakeAction{Action: "delete-pod", Value: "pod-2"}, fakeClient.Actions[4], t)
validateAction(client.FakeAction{Action: "list-pods"}, fakeClient.Actions[5], t)
}

func TestRunController(t *testing.T) {
fakeClient := client.Fake{}
name := "name"
Expand Down
18 changes: 18 additions & 0 deletions pkg/runtime/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ func DecodeInto(data []byte, obj interface{}) error {
return conversionScheme.DecodeInto(data, obj)
}

// Does a deep copy of an API object. Useful mostly for tests.
// TODO(dbsmith): implement directly instead of via Encode/Decode
func Copy(obj interface{}) (interface{}, error) {
data, err := Encode(obj)
if err != nil {
return nil, err
}
return Decode(data)
}

func CopyOrDie(obj interface{}) interface{} {
newObj, err := Copy(obj)
if err != nil {
panic(err)
}
return newObj
}

// metaInsertion implements conversion.MetaInsertionFactory, which lets the conversion
// package figure out how to encode our object's types and versions. These fields are
// located in our JSONBase.
Expand Down

0 comments on commit 3c0a736

Please sign in to comment.