Skip to content

Commit

Permalink
Added test cases for HandleBuildPodDeletion and HandleBuildDeletion
Browse files Browse the repository at this point in the history
methods in build controller.
  • Loading branch information
soltysh committed Jul 10, 2015
1 parent 08f6608 commit c834fb1
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 7 deletions.
237 changes: 237 additions & 0 deletions pkg/build/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,3 +644,240 @@ func TestCancelBuild(t *testing.T) {
}
}
}

type customPodManager struct {
CreatePodFunc func(namespace string, pod *kapi.Pod) (*kapi.Pod, error)
DeletePodFunc func(namespace string, pod *kapi.Pod) error
GetPodFunc func(namespace, name string) (*kapi.Pod, error)
}

func (c *customPodManager) CreatePod(namespace string, pod *kapi.Pod) (*kapi.Pod, error) {
return c.CreatePodFunc(namespace, pod)
}

func (c *customPodManager) DeletePod(namespace string, pod *kapi.Pod) error {
return c.DeletePodFunc(namespace, pod)
}

func (c *customPodManager) GetPod(namespace, name string) (*kapi.Pod, error) {
return c.GetPodFunc(namespace, name)
}

func TestHandleHandleBuildDeletionOK(t *testing.T) {
deleteWasCalled := false
build := mockBuild(buildapi.BuildStatusComplete, buildapi.BuildOutput{})
ctrl := BuildDeleteController{&customPodManager{
GetPodFunc: func(namespace, names string) (*kapi.Pod, error) {
return &kapi.Pod{ObjectMeta: kapi.ObjectMeta{Labels: map[string]string{buildapi.BuildLabel: build.Name}}}, nil
},
DeletePodFunc: func(namespace string, pod *kapi.Pod) error {
deleteWasCalled = true
return nil
},
}}

err := ctrl.HandleBuildDeletion(build)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if !deleteWasCalled {
t.Error("DeletePod was not called when it should!")
}
}

func TestHandleHandleBuildDeletionFailGetPod(t *testing.T) {
build := mockBuild(buildapi.BuildStatusComplete, buildapi.BuildOutput{})
ctrl := BuildDeleteController{&customPodManager{
GetPodFunc: func(namespace, name string) (*kapi.Pod, error) {
return nil, errors.New("random")
},
}}

err := ctrl.HandleBuildDeletion(build)
if err == nil {
t.Error("Expected random error got none!")
}
}

func TestHandleHandleBuildDeletionGetPodNotFound(t *testing.T) {
deleteWasCalled := false
build := mockBuild(buildapi.BuildStatusComplete, buildapi.BuildOutput{})
ctrl := BuildDeleteController{&customPodManager{
GetPodFunc: func(namespace, name string) (*kapi.Pod, error) {
return nil, kerrors.NewNotFound("Pod", name)
},
DeletePodFunc: func(namespace string, pod *kapi.Pod) error {
deleteWasCalled = true
return nil
},
}}

err := ctrl.HandleBuildDeletion(build)
if err != nil {
t.Errorf("Unexpected error, %v", err)
}
if deleteWasCalled {
t.Error("DeletePod was called when it should not!")
}
}

func TestHandleHandleBuildDeletionMismatchedLabels(t *testing.T) {
deleteWasCalled := false
build := mockBuild(buildapi.BuildStatusComplete, buildapi.BuildOutput{})
ctrl := BuildDeleteController{&customPodManager{
GetPodFunc: func(namespace, names string) (*kapi.Pod, error) {
return &kapi.Pod{}, nil
},
DeletePodFunc: func(namespace string, pod *kapi.Pod) error {
deleteWasCalled = true
return nil
},
}}

err := ctrl.HandleBuildDeletion(build)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if deleteWasCalled {
t.Error("DeletePod was called when it should not!")
}
}

func TestHandleHandleBuildDeletionDeletePodError(t *testing.T) {
build := mockBuild(buildapi.BuildStatusComplete, buildapi.BuildOutput{})
ctrl := BuildDeleteController{&customPodManager{
GetPodFunc: func(namespace, names string) (*kapi.Pod, error) {
return &kapi.Pod{ObjectMeta: kapi.ObjectMeta{Labels: map[string]string{buildapi.BuildLabel: build.Name}}}, nil
},
DeletePodFunc: func(namespace string, pod *kapi.Pod) error {
return errors.New("random")
},
}}

err := ctrl.HandleBuildDeletion(build)
if err == nil {
t.Error("Expected random error got none!")
}
}

type customBuildUpdater struct {
UpdateFunc func(namespace string, build *buildapi.Build) error
}

func (c *customBuildUpdater) Update(namespace string, build *buildapi.Build) error {
return c.UpdateFunc(namespace, build)
}

func mockBuildPodDeleteController(build *buildapi.Build, buildUpdater *customBuildUpdater, err error) *BuildPodDeleteController {
return &BuildPodDeleteController{
BuildStore: buildtest.FakeBuildStore{Build: build, Err: err},
BuildUpdater: buildUpdater,
}
}

func TestHandleBuildPodDeletionOK(t *testing.T) {
updateWasCalled := false
// only not finished build (buildutil.IsBuildComplete) should be handled
build := mockBuild(buildapi.BuildStatusRunning, buildapi.BuildOutput{})
ctrl := mockBuildPodDeleteController(build, &customBuildUpdater{
UpdateFunc: func(namespace string, build *buildapi.Build) error {
updateWasCalled = true
return nil
},
}, nil)
pod := mockPod(kapi.PodSucceeded, 0)

err := ctrl.HandleBuildPodDeletion(pod)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if !updateWasCalled {
t.Error("UpdateBuild was not called when it should!")
}
}

func TestHandleBuildPodDeletionOKFinishedBuild(t *testing.T) {
updateWasCalled := false
// finished build buildutil.IsBuildComplete should not be handled
build := mockBuild(buildapi.BuildStatusComplete, buildapi.BuildOutput{})
ctrl := mockBuildPodDeleteController(build, &customBuildUpdater{
UpdateFunc: func(namespace string, build *buildapi.Build) error {
updateWasCalled = true
return nil
},
}, nil)
pod := mockPod(kapi.PodSucceeded, 0)

err := ctrl.HandleBuildPodDeletion(pod)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if updateWasCalled {
t.Error("UpdateBuild was called when it should not!")
}
}

func TestHandleBuildPodDeletionOKErroneousBuild(t *testing.T) {
updateWasCalled := false
// erroneous builds should not be handled
build := mockBuild(buildapi.BuildStatusError, buildapi.BuildOutput{})
ctrl := mockBuildPodDeleteController(build, &customBuildUpdater{
UpdateFunc: func(namespace string, build *buildapi.Build) error {
updateWasCalled = true
return nil
},
}, nil)
pod := mockPod(kapi.PodSucceeded, 0)

err := ctrl.HandleBuildPodDeletion(pod)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if updateWasCalled {
t.Error("UpdateBuild was called when it should not!")
}
}

func TestHandleBuildPodDeletionBuildGetError(t *testing.T) {
ctrl := mockBuildPodDeleteController(nil, &customBuildUpdater{}, errors.New("random"))
pod := mockPod(kapi.PodSucceeded, 0)

err := ctrl.HandleBuildPodDeletion(pod)
if err == nil {
t.Error("Expected random error, but got none!")
}
}

func TestHandleBuildPodDeletionBuildNotExists(t *testing.T) {
updateWasCalled := false
ctrl := mockBuildPodDeleteController(nil, &customBuildUpdater{
UpdateFunc: func(namespace string, build *buildapi.Build) error {
updateWasCalled = true
return nil
},
}, nil)
pod := mockPod(kapi.PodSucceeded, 0)

err := ctrl.HandleBuildPodDeletion(pod)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if updateWasCalled {
t.Error("UpdateBuild was called when it should not!")
}
}

func TestHandleBuildPodDeletionBuildUpdateError(t *testing.T) {
build := mockBuild(buildapi.BuildStatusRunning, buildapi.BuildOutput{})
ctrl := mockBuildPodDeleteController(build, &customBuildUpdater{
UpdateFunc: func(namespace string, build *buildapi.Build) error {
return errors.New("random")
},
}, nil)
pod := mockPod(kapi.PodSucceeded, 0)

err := ctrl.HandleBuildPodDeletion(pod)
if err == nil {
t.Error("Expected random error, but got none!")
}
}
6 changes: 3 additions & 3 deletions pkg/build/controller/test/fake_build_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ func (s FakeBuildStore) ContainedIDs() util.StringSet {
return util.NewStringSet()
}

func (s FakeBuildStore) Get(obj interface{}) (item interface{}, exists bool, err error) {
func (s FakeBuildStore) Get(obj interface{}) (interface{}, bool, error) {
return s.GetByKey("")
}

func (s FakeBuildStore) GetByKey(id string) (item interface{}, exists bool, err error) {
func (s FakeBuildStore) GetByKey(id string) (interface{}, bool, error) {
if s.Err != nil {
return nil, false, err
return nil, false, s.Err
}
if s.Build == nil {
return nil, false, nil
Expand Down
5 changes: 1 addition & 4 deletions pkg/build/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,5 @@ func NameFromImageStream(namespace string, ref *kapi.ObjectReference, tag string

// IsBuildComplete returns whether the provided build is complete or not
func IsBuildComplete(build *buildapi.Build) bool {
if build.Status.Phase != buildapi.BuildPhaseRunning && build.Status.Phase != buildapi.BuildPhasePending && build.Status.Phase != buildapi.BuildPhaseNew {
return true
}
return false
return build.Status.Phase != buildapi.BuildPhaseRunning && build.Status.Phase != buildapi.BuildPhasePending && build.Status.Phase != buildapi.BuildPhaseNew
}

0 comments on commit c834fb1

Please sign in to comment.