Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add TestRemoveVolumeFromDeploymentConfig #563

Merged
merged 1 commit into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/occlient/fakeclient.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package occlient

import (
fakeAppClientset "github.com/openshift/client-go/apps/clientset/versioned/fake"
fakeRouteClientset "github.com/openshift/client-go/route/clientset/versioned/fake"
fakeKubeClientset "k8s.io/client-go/kubernetes/fake"
)
Expand All @@ -10,6 +11,7 @@ import (
type FakeClientset struct {
Kubernetes *fakeKubeClientset.Clientset
RouteClientset *fakeRouteClientset.Clientset
AppClientset *fakeAppClientset.Clientset
}

// FakeNew creates new fake client for testing
Expand All @@ -25,5 +27,8 @@ func FakeNew() (*Client, *FakeClientset) {
fkclientset.Kubernetes = fakeKubeClientset.NewSimpleClientset()
client.kubeClient = fkclientset.Kubernetes

fkclientset.AppClientset = fakeAppClientset.NewSimpleClientset()
client.appsClient = fkclientset.AppClientset.Apps()

return &client, &fkclientset
}
84 changes: 84 additions & 0 deletions pkg/occlient/occlient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,97 @@ import (
"reflect"
"testing"

appsv1 "github.com/openshift/api/apps/v1"
routev1 "github.com/openshift/api/route/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime"
ktesting "k8s.io/client-go/testing"
)

func TestRemoveVolumeFromDeploymentConfig(t *testing.T) {
type args struct {
pvc string
dcName string
}
tests := []struct {
name string
dcBefore *appsv1.DeploymentConfig
args args
wantErr bool
}{
{
name: "Test case : 1",
dcBefore: &appsv1.DeploymentConfig{
Spec: appsv1.DeploymentConfigSpec{
Selector: map[string]string{
"deploymentconfig": "test",
},
Template: &corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test",
VolumeMounts: []corev1.VolumeMount{
{
MountPath: "/tmp",
Name: "test-pvc",
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "test-pvc",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: "test-pvc",
},
},
},
},
},
},
},
},
args: args{
pvc: "test-pvc",
dcName: "test",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fakeClient, fakeClientSet := FakeNew()

fakeClientSet.AppClientset.PrependReactor("get", "deploymentconfigs", func(action ktesting.Action) (bool, runtime.Object, error) {
return true, tt.dcBefore, nil
})
fakeClientSet.AppClientset.PrependReactor("update", "deploymentconfigs", func(action ktesting.Action) (bool, runtime.Object, error) {
return true, nil, nil
})
err := fakeClient.RemoveVolumeFromDeploymentConfig(tt.args.pvc, tt.args.dcName)

// Checks for error in positive cases
if !tt.wantErr == (err != nil) {
t.Errorf(" client.RemoveVolumeFromDeploymentConfig(pvc, dcName) unexpected error %v, wantErr %v", err, tt.wantErr)
}
// Check for validating number of actions performed
if (len(fakeClientSet.AppClientset.Actions()) != 2) && (tt.wantErr != true) {
t.Errorf("expected 2 actions in GetPVCFromName got: %v", fakeClientSet.Kubernetes.Actions())
}
updatedDc := fakeClientSet.AppClientset.Actions()[1].(ktesting.UpdateAction).GetObject().(*appsv1.DeploymentConfig)
// validating volume got removed from dc
for _, volume := range updatedDc.Spec.Template.Spec.Volumes {
if volume.PersistentVolumeClaim.ClaimName == tt.args.pvc {
t.Errorf("expected volume with name : %v to be removed from dc", tt.args.pvc)
}
}
})
}
}

func TestGetPVCFromName(t *testing.T) {
tests := []struct {
name string
Expand Down