Add UT and upgrade this test suite with go sub-test

This commit is contained in:
dhilipkumars 2017-07-21 10:43:18 +05:30
parent f17a572ece
commit 10af4fa9db

View File

@ -246,7 +246,7 @@ func TestGetNewRS(t *testing.T) {
newRC := generateRS(newDeployment)
tests := []struct {
test string
Name string
objs []runtime.Object
expected *extensions.ReplicaSet
}{
@ -282,18 +282,20 @@ func TestGetNewRS(t *testing.T) {
}
for _, test := range tests {
fakeClient := &fake.Clientset{}
fakeClient = addListPodsReactor(fakeClient, test.objs[0])
fakeClient = addListRSReactor(fakeClient, test.objs[1])
fakeClient = addUpdatePodsReactor(fakeClient)
fakeClient = addUpdateRSReactor(fakeClient)
rs, err := GetNewReplicaSet(&newDeployment, fakeClient)
if err != nil {
t.Errorf("In test case %s, got unexpected error %v", test.test, err)
}
if !apiequality.Semantic.DeepEqual(rs, test.expected) {
t.Errorf("In test case %s, expected %#v, got %#v", test.test, test.expected, rs)
}
t.Run(test.Name, func(t *testing.T) {
fakeClient := &fake.Clientset{}
fakeClient = addListPodsReactor(fakeClient, test.objs[0])
fakeClient = addListRSReactor(fakeClient, test.objs[1])
fakeClient = addUpdatePodsReactor(fakeClient)
fakeClient = addUpdateRSReactor(fakeClient)
rs, err := GetNewReplicaSet(&newDeployment, fakeClient)
if err != nil {
t.Errorf("In test case %s, got unexpected error %v", test.Name, err)
}
if !apiequality.Semantic.DeepEqual(rs, test.expected) {
t.Errorf("In test case %s, expected %#v, got %#v", test.Name, test.expected, rs)
}
})
}
}
@ -318,7 +320,7 @@ func TestGetOldRSs(t *testing.T) {
existedRS.Status.FullyLabeledReplicas = *(existedRS.Spec.Replicas)
tests := []struct {
test string
Name string
objs []runtime.Object
expected []*extensions.ReplicaSet
}{
@ -354,24 +356,26 @@ func TestGetOldRSs(t *testing.T) {
}
for _, test := range tests {
fakeClient := &fake.Clientset{}
fakeClient = addListRSReactor(fakeClient, test.objs[0])
fakeClient = addGetRSReactor(fakeClient, test.objs[0])
fakeClient = addUpdateRSReactor(fakeClient)
_, rss, err := GetOldReplicaSets(&newDeployment, fakeClient)
if err != nil {
t.Errorf("In test case %s, got unexpected error %v", test.test, err)
}
if !equal(rss, test.expected) {
t.Errorf("In test case %q, expected:", test.test)
for _, rs := range test.expected {
t.Errorf("rs = %#v", rs)
t.Run(test.Name, func(t *testing.T) {
fakeClient := &fake.Clientset{}
fakeClient = addListRSReactor(fakeClient, test.objs[0])
fakeClient = addGetRSReactor(fakeClient, test.objs[0])
fakeClient = addUpdateRSReactor(fakeClient)
_, rss, err := GetOldReplicaSets(&newDeployment, fakeClient)
if err != nil {
t.Errorf("In test case %s, got unexpected error %v", test.Name, err)
}
t.Errorf("In test case %q, got:", test.test)
for _, rs := range rss {
t.Errorf("rs = %#v", rs)
if !equal(rss, test.expected) {
t.Errorf("In test case %q, expected:", test.Name)
for _, rs := range test.expected {
t.Errorf("rs = %#v", rs)
}
t.Errorf("In test case %q, got:", test.Name)
for _, rs := range rss {
t.Errorf("rs = %#v", rs)
}
}
}
})
}
}
@ -390,7 +394,7 @@ func generatePodTemplateSpec(name, nodeName string, annotations, labels map[stri
func TestEqualIgnoreHash(t *testing.T) {
tests := []struct {
test string
Name string
former, latter v1.PodTemplateSpec
expected bool
}{
@ -445,28 +449,31 @@ func TestEqualIgnoreHash(t *testing.T) {
}
for _, test := range tests {
runTest := func(t1, t2 *v1.PodTemplateSpec, reversed bool) {
reverseString := ""
if reversed {
reverseString = " (reverse order)"
t.Run(test.Name, func(t *testing.T) {
runTest := func(t1, t2 *v1.PodTemplateSpec, reversed bool) {
reverseString := ""
if reversed {
reverseString = " (reverse order)"
}
// Run
equal, err := EqualIgnoreHash(t1, t2)
if err != nil {
t.Errorf("%s: unexpected error: %v", err, test.Name)
return
}
if equal != test.expected {
t.Errorf("%q%s: expected %v", test.Name, reverseString, test.expected)
return
}
if t1.Labels == nil || t2.Labels == nil {
t.Errorf("%q%s: unexpected labels becomes nil", test.Name, reverseString)
}
}
// Run
equal, err := EqualIgnoreHash(t1, t2)
if err != nil {
t.Errorf("%s: unexpected error: %v", err, test.test)
return
}
if equal != test.expected {
t.Errorf("%q%s: expected %v", test.test, reverseString, test.expected)
return
}
if t1.Labels == nil || t2.Labels == nil {
t.Errorf("%q%s: unexpected labels becomes nil", test.test, reverseString)
}
}
runTest(&test.former, &test.latter, false)
// Test the same case in reverse order
runTest(&test.latter, &test.former, true)
runTest(&test.former, &test.latter, false)
// Test the same case in reverse order
runTest(&test.latter, &test.former, true)
})
}
}
@ -489,25 +496,25 @@ func TestFindNewReplicaSet(t *testing.T) {
oldRS.Status.FullyLabeledReplicas = *(oldRS.Spec.Replicas)
tests := []struct {
test string
Name string
deployment extensions.Deployment
rsList []*extensions.ReplicaSet
expected *extensions.ReplicaSet
}{
{
test: "Get new ReplicaSet with the same template as Deployment spec but different pod-template-hash value",
Name: "Get new ReplicaSet with the same template as Deployment spec but different pod-template-hash value",
deployment: deployment,
rsList: []*extensions.ReplicaSet{&newRS, &oldRS},
expected: &newRS,
},
{
test: "Get the oldest new ReplicaSet when there are more than one ReplicaSet with the same template",
Name: "Get the oldest new ReplicaSet when there are more than one ReplicaSet with the same template",
deployment: deployment,
rsList: []*extensions.ReplicaSet{&newRS, &oldRS, &newRSDup},
expected: &newRSDup,
},
{
test: "Get nil new ReplicaSet",
Name: "Get nil new ReplicaSet",
deployment: deployment,
rsList: []*extensions.ReplicaSet{&oldRS},
expected: nil,
@ -515,9 +522,11 @@ func TestFindNewReplicaSet(t *testing.T) {
}
for _, test := range tests {
if rs, err := FindNewReplicaSet(&test.deployment, test.rsList); !reflect.DeepEqual(rs, test.expected) || err != nil {
t.Errorf("In test case %q, expected %#v, got %#v: %v", test.test, test.expected, rs, err)
}
t.Run(test.Name, func(t *testing.T) {
if rs, err := FindNewReplicaSet(&test.deployment, test.rsList); !reflect.DeepEqual(rs, test.expected) || err != nil {
t.Errorf("In test case %q, expected %#v, got %#v: %v", test.Name, test.expected, rs, err)
}
})
}
}
@ -543,7 +552,7 @@ func TestFindOldReplicaSets(t *testing.T) {
oldRS.CreationTimestamp = before
tests := []struct {
test string
Name string
deployment extensions.Deployment
rsList []*extensions.ReplicaSet
podList *v1.PodList
@ -551,28 +560,28 @@ func TestFindOldReplicaSets(t *testing.T) {
expectedRequire []*extensions.ReplicaSet
}{
{
test: "Get old ReplicaSets",
Name: "Get old ReplicaSets",
deployment: deployment,
rsList: []*extensions.ReplicaSet{&newRS, &oldRS},
expected: []*extensions.ReplicaSet{&oldRS},
expectedRequire: nil,
},
{
test: "Get old ReplicaSets with no new ReplicaSet",
Name: "Get old ReplicaSets with no new ReplicaSet",
deployment: deployment,
rsList: []*extensions.ReplicaSet{&oldRS},
expected: []*extensions.ReplicaSet{&oldRS},
expectedRequire: nil,
},
{
test: "Get old ReplicaSets with two new ReplicaSets, only the oldest new ReplicaSet is seen as new ReplicaSet",
Name: "Get old ReplicaSets with two new ReplicaSets, only the oldest new ReplicaSet is seen as new ReplicaSet",
deployment: deployment,
rsList: []*extensions.ReplicaSet{&oldRS, &newRS, &newRSDup},
expected: []*extensions.ReplicaSet{&oldRS, &newRS},
expectedRequire: []*extensions.ReplicaSet{&newRS},
},
{
test: "Get empty old ReplicaSets",
Name: "Get empty old ReplicaSets",
deployment: deployment,
rsList: []*extensions.ReplicaSet{&newRS},
expected: nil,
@ -581,16 +590,18 @@ func TestFindOldReplicaSets(t *testing.T) {
}
for _, test := range tests {
requireRS, allRS, err := FindOldReplicaSets(&test.deployment, test.rsList)
sort.Sort(controller.ReplicaSetsByCreationTimestamp(allRS))
sort.Sort(controller.ReplicaSetsByCreationTimestamp(test.expected))
if !reflect.DeepEqual(allRS, test.expected) || err != nil {
t.Errorf("In test case %q, expected %#v, got %#v: %v", test.test, test.expected, allRS, err)
}
// RSs are getting filtered correctly by rs.spec.replicas
if !reflect.DeepEqual(requireRS, test.expectedRequire) || err != nil {
t.Errorf("In test case %q, expected %#v, got %#v: %v", test.test, test.expectedRequire, requireRS, err)
}
t.Run(test.Name, func(t *testing.T) {
requireRS, allRS, err := FindOldReplicaSets(&test.deployment, test.rsList)
sort.Sort(controller.ReplicaSetsByCreationTimestamp(allRS))
sort.Sort(controller.ReplicaSetsByCreationTimestamp(test.expected))
if !reflect.DeepEqual(allRS, test.expected) || err != nil {
t.Errorf("In test case %q, expected %#v, got %#v: %v", test.Name, test.expected, allRS, err)
}
// RSs are getting filtered correctly by rs.spec.replicas
if !reflect.DeepEqual(requireRS, test.expectedRequire) || err != nil {
t.Errorf("In test case %q, expected %#v, got %#v: %v", test.Name, test.expectedRequire, requireRS, err)
}
})
}
}
@ -623,7 +634,7 @@ func TestGetReplicaCountForReplicaSets(t *testing.T) {
rs2.Status.Replicas = 3
tests := []struct {
test string
Name string
sets []*extensions.ReplicaSet
expectedCount int32
expectedActual int32
@ -643,14 +654,16 @@ func TestGetReplicaCountForReplicaSets(t *testing.T) {
}
for _, test := range tests {
rs := GetReplicaCountForReplicaSets(test.sets)
if rs != test.expectedCount {
t.Errorf("In test case %s, expectedCount %+v, got %+v", test.test, test.expectedCount, rs)
}
rs = GetActualReplicaCountForReplicaSets(test.sets)
if rs != test.expectedActual {
t.Errorf("In test case %s, expectedActual %+v, got %+v", test.test, test.expectedActual, rs)
}
t.Run(test.Name, func(t *testing.T) {
rs := GetReplicaCountForReplicaSets(test.sets)
if rs != test.expectedCount {
t.Errorf("In test case %s, expectedCount %+v, got %+v", test.Name, test.expectedCount, rs)
}
rs = GetActualReplicaCountForReplicaSets(test.sets)
if rs != test.expectedActual {
t.Errorf("In test case %s, expectedActual %+v, got %+v", test.Name, test.expectedActual, rs)
}
})
}
}
@ -698,24 +711,26 @@ func TestResolveFenceposts(t *testing.T) {
}
for num, test := range tests {
maxSurge := intstr.FromString(test.maxSurge)
maxUnavail := intstr.FromString(test.maxUnavailable)
surge, unavail, err := ResolveFenceposts(&maxSurge, &maxUnavail, test.desired)
if err != nil && !test.expectError {
t.Errorf("unexpected error %v", err)
}
if err == nil && test.expectError {
t.Error("expected error")
}
if surge != test.expectSurge || unavail != test.expectUnavailable {
t.Errorf("#%v got %v:%v, want %v:%v", num, surge, unavail, test.expectSurge, test.expectUnavailable)
}
t.Run("maxSurge="+test.maxSurge, func(t *testing.T) {
maxSurge := intstr.FromString(test.maxSurge)
maxUnavail := intstr.FromString(test.maxUnavailable)
surge, unavail, err := ResolveFenceposts(&maxSurge, &maxUnavail, test.desired)
if err != nil && !test.expectError {
t.Errorf("unexpected error %v", err)
}
if err == nil && test.expectError {
t.Error("expected error")
}
if surge != test.expectSurge || unavail != test.expectUnavailable {
t.Errorf("#%v got %v:%v, want %v:%v", num, surge, unavail, test.expectSurge, test.expectUnavailable)
}
})
}
}
func TestNewRSNewReplicas(t *testing.T) {
tests := []struct {
test string
Name string
strategyType extensions.DeploymentStrategyType
depReplicas int32
newRSReplicas int32
@ -744,20 +759,28 @@ func TestNewRSNewReplicas(t *testing.T) {
*(rs5.Spec.Replicas) = 5
for _, test := range tests {
*(newDeployment.Spec.Replicas) = test.depReplicas
newDeployment.Spec.Strategy = extensions.DeploymentStrategy{Type: test.strategyType}
newDeployment.Spec.Strategy.RollingUpdate = &extensions.RollingUpdateDeployment{
MaxUnavailable: func(i int) *intstr.IntOrString { x := intstr.FromInt(i); return &x }(1),
MaxSurge: func(i int) *intstr.IntOrString { x := intstr.FromInt(i); return &x }(test.maxSurge),
}
*(newRC.Spec.Replicas) = test.newRSReplicas
rs, err := NewRSNewReplicas(&newDeployment, []*extensions.ReplicaSet{&rs5}, &newRC)
if err != nil {
t.Errorf("In test case %s, got unexpected error %v", test.test, err)
}
if rs != test.expected {
t.Errorf("In test case %s, expected %+v, got %+v", test.test, test.expected, rs)
}
t.Run(test.Name, func(t *testing.T) {
*(newDeployment.Spec.Replicas) = test.depReplicas
newDeployment.Spec.Strategy = extensions.DeploymentStrategy{Type: test.strategyType}
newDeployment.Spec.Strategy.RollingUpdate = &extensions.RollingUpdateDeployment{
MaxUnavailable: func(i int) *intstr.IntOrString {
x := intstr.FromInt(i)
return &x
}(1),
MaxSurge: func(i int) *intstr.IntOrString {
x := intstr.FromInt(i)
return &x
}(test.maxSurge),
}
*(newRC.Spec.Replicas) = test.newRSReplicas
rs, err := NewRSNewReplicas(&newDeployment, []*extensions.ReplicaSet{&rs5}, &newRC)
if err != nil {
t.Errorf("In test case %s, got unexpected error %v", test.Name, err)
}
if rs != test.expected {
t.Errorf("In test case %s, expected %+v, got %+v", test.Name, test.expected, rs)
}
})
}
}
@ -825,11 +848,13 @@ func TestGetCondition(t *testing.T) {
}
for _, test := range tests {
cond := GetDeploymentCondition(test.status, test.condType)
exists := cond != nil
if exists != test.expected {
t.Errorf("%s: expected condition to exist: %t, got: %t", test.name, test.expected, exists)
}
t.Run(test.name, func(t *testing.T) {
cond := GetDeploymentCondition(test.status, test.condType)
exists := cond != nil
if exists != test.expected {
t.Errorf("%s: expected condition to exist: %t, got: %t", test.name, test.expected, exists)
}
})
}
}
@ -869,10 +894,12 @@ func TestSetCondition(t *testing.T) {
}
for _, test := range tests {
SetDeploymentCondition(test.status, test.cond)
if !reflect.DeepEqual(test.status, test.expectedStatus) {
t.Errorf("%s: expected status: %v, got: %v", test.name, test.expectedStatus, test.status)
}
t.Run(test.name, func(t *testing.T) {
SetDeploymentCondition(test.status, test.cond)
if !reflect.DeepEqual(test.status, test.expectedStatus) {
t.Errorf("%s: expected status: %v, got: %v", test.name, test.expectedStatus, test.status)
}
})
}
}
@ -912,10 +939,12 @@ func TestRemoveCondition(t *testing.T) {
}
for _, test := range tests {
RemoveDeploymentCondition(test.status, test.condType)
if !reflect.DeepEqual(test.status, test.expectedStatus) {
t.Errorf("%s: expected status: %v, got: %v", test.name, test.expectedStatus, test.status)
}
t.Run(test.name, func(t *testing.T) {
RemoveDeploymentCondition(test.status, test.condType)
if !reflect.DeepEqual(test.status, test.expectedStatus) {
t.Errorf("%s: expected status: %v, got: %v", test.name, test.expectedStatus, test.status)
}
})
}
}
@ -988,11 +1017,11 @@ func TestDeploymentComplete(t *testing.T) {
}
for _, test := range tests {
t.Log(test.name)
if got, exp := DeploymentComplete(test.d, &test.d.Status), test.expected; got != exp {
t.Errorf("expected complete: %t, got: %t", exp, got)
}
t.Run(test.name, func(t *testing.T) {
if got, exp := DeploymentComplete(test.d, &test.d.Status), test.expected; got != exp {
t.Errorf("expected complete: %t, got: %t", exp, got)
}
})
}
}
@ -1083,11 +1112,11 @@ func TestDeploymentProgressing(t *testing.T) {
}
for _, test := range tests {
t.Log(test.name)
if got, exp := DeploymentProgressing(test.d, &test.newStatus), test.expected; got != exp {
t.Errorf("expected progressing: %t, got: %t", exp, got)
}
t.Run(test.name, func(t *testing.T) {
if got, exp := DeploymentProgressing(test.d, &test.newStatus), test.expected; got != exp {
t.Errorf("expected progressing: %t, got: %t", exp, got)
}
})
}
}
@ -1149,12 +1178,12 @@ func TestDeploymentTimedOut(t *testing.T) {
}
for _, test := range tests {
t.Log(test.name)
nowFn = test.nowFn
if got, exp := DeploymentTimedOut(&test.d, &test.d.Status), test.expected; got != exp {
t.Errorf("expected timeout: %t, got: %t", exp, got)
}
t.Run(test.name, func(t *testing.T) {
nowFn = test.nowFn
if got, exp := DeploymentTimedOut(&test.d, &test.d.Status), test.expected; got != exp {
t.Errorf("expected timeout: %t, got: %t", exp, got)
}
})
}
}
@ -1228,9 +1257,70 @@ func TestMaxUnavailable(t *testing.T) {
for _, test := range tests {
t.Log(test.name)
maxUnavailable := MaxUnavailable(test.deployment)
if test.expected != maxUnavailable {
t.Fatalf("expected:%v, got:%v", test.expected, maxUnavailable)
}
t.Run(test.name, func(t *testing.T) {
maxUnavailable := MaxUnavailable(test.deployment)
if test.expected != maxUnavailable {
t.Fatalf("expected:%v, got:%v", test.expected, maxUnavailable)
}
})
}
}
//Set of simple tests for annotation related util functions
func TestAnnotationUtils(t *testing.T) {
//Setup
tDeployment := generateDeployment("nginx")
tRS := generateRS(tDeployment)
tDeployment.Annotations[RevisionAnnotation] = "1"
//Test Case 1: Check if anotations are copied properly from deployment to RS
t.Run("SetNewReplicaSetAnnotations", func(t *testing.T) {
//Try to set the increment revision from 1 through 20
for i := 0; i < 20; i++ {
nextRevision := fmt.Sprintf("%d", i+1)
SetNewReplicaSetAnnotations(&tDeployment, &tRS, nextRevision, true)
//Now the ReplicaSets Revision Annotation should be i+1
if tRS.Annotations[RevisionAnnotation] != nextRevision {
t.Errorf("Revision Expected=%s Obtained=%s", nextRevision, tRS.Annotations[RevisionAnnotation])
}
}
})
//Test Case 2: Check if annotations are set properly
t.Run("SetReplicasAnnotations", func(t *testing.T) {
updated := SetReplicasAnnotations(&tRS, 10, 11)
if !updated {
t.Errorf("SetReplicasAnnotations() failed")
}
value, ok := tRS.Annotations[DesiredReplicasAnnotation]
if !ok {
t.Errorf("SetReplicasAnnotations did not set DesiredReplicasAnnotation")
}
if value != "10" {
t.Errorf("SetReplicasAnnotations did not set DesiredReplicasAnnotation correctly value=%s", value)
}
if value, ok = tRS.Annotations[MaxReplicasAnnotation]; !ok {
t.Errorf("SetReplicasAnnotations did not set DesiredReplicasAnnotation")
}
if value != "11" {
t.Errorf("SetReplicasAnnotations did not set MaxReplicasAnnotation correctly value=%s", value)
}
})
//Test Case 3: Check if annotations reflect deployments state
tRS.Annotations[DesiredReplicasAnnotation] = "1"
tRS.Status.AvailableReplicas = 1
tRS.Spec.Replicas = new(int32)
*tRS.Spec.Replicas = 1
t.Run("IsSaturated", func(t *testing.T) {
saturated := IsSaturated(&tDeployment, &tRS)
if !saturated {
t.Errorf("SetReplicasAnnotations Expected=true Obtained=false")
}
})
//Tear Down
}