mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	Add UT and upgrade this test suite with go sub-test
This commit is contained in:
		@@ -246,7 +246,7 @@ func TestGetNewRS(t *testing.T) {
 | 
				
			|||||||
	newRC := generateRS(newDeployment)
 | 
						newRC := generateRS(newDeployment)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tests := []struct {
 | 
						tests := []struct {
 | 
				
			||||||
		test     string
 | 
							Name     string
 | 
				
			||||||
		objs     []runtime.Object
 | 
							objs     []runtime.Object
 | 
				
			||||||
		expected *extensions.ReplicaSet
 | 
							expected *extensions.ReplicaSet
 | 
				
			||||||
	}{
 | 
						}{
 | 
				
			||||||
@@ -282,6 +282,7 @@ func TestGetNewRS(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, test := range tests {
 | 
						for _, test := range tests {
 | 
				
			||||||
 | 
							t.Run(test.Name, func(t *testing.T) {
 | 
				
			||||||
			fakeClient := &fake.Clientset{}
 | 
								fakeClient := &fake.Clientset{}
 | 
				
			||||||
			fakeClient = addListPodsReactor(fakeClient, test.objs[0])
 | 
								fakeClient = addListPodsReactor(fakeClient, test.objs[0])
 | 
				
			||||||
			fakeClient = addListRSReactor(fakeClient, test.objs[1])
 | 
								fakeClient = addListRSReactor(fakeClient, test.objs[1])
 | 
				
			||||||
@@ -289,11 +290,12 @@ func TestGetNewRS(t *testing.T) {
 | 
				
			|||||||
			fakeClient = addUpdateRSReactor(fakeClient)
 | 
								fakeClient = addUpdateRSReactor(fakeClient)
 | 
				
			||||||
			rs, err := GetNewReplicaSet(&newDeployment, fakeClient)
 | 
								rs, err := GetNewReplicaSet(&newDeployment, fakeClient)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
			t.Errorf("In test case %s, got unexpected error %v", test.test, err)
 | 
									t.Errorf("In test case %s, got unexpected error %v", test.Name, err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if !apiequality.Semantic.DeepEqual(rs, test.expected) {
 | 
								if !apiequality.Semantic.DeepEqual(rs, test.expected) {
 | 
				
			||||||
			t.Errorf("In test case %s, expected %#v, got %#v", test.test, test.expected, rs)
 | 
									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)
 | 
						existedRS.Status.FullyLabeledReplicas = *(existedRS.Spec.Replicas)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tests := []struct {
 | 
						tests := []struct {
 | 
				
			||||||
		test     string
 | 
							Name     string
 | 
				
			||||||
		objs     []runtime.Object
 | 
							objs     []runtime.Object
 | 
				
			||||||
		expected []*extensions.ReplicaSet
 | 
							expected []*extensions.ReplicaSet
 | 
				
			||||||
	}{
 | 
						}{
 | 
				
			||||||
@@ -354,24 +356,26 @@ func TestGetOldRSs(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, test := range tests {
 | 
						for _, test := range tests {
 | 
				
			||||||
 | 
							t.Run(test.Name, func(t *testing.T) {
 | 
				
			||||||
			fakeClient := &fake.Clientset{}
 | 
								fakeClient := &fake.Clientset{}
 | 
				
			||||||
			fakeClient = addListRSReactor(fakeClient, test.objs[0])
 | 
								fakeClient = addListRSReactor(fakeClient, test.objs[0])
 | 
				
			||||||
			fakeClient = addGetRSReactor(fakeClient, test.objs[0])
 | 
								fakeClient = addGetRSReactor(fakeClient, test.objs[0])
 | 
				
			||||||
			fakeClient = addUpdateRSReactor(fakeClient)
 | 
								fakeClient = addUpdateRSReactor(fakeClient)
 | 
				
			||||||
			_, rss, err := GetOldReplicaSets(&newDeployment, fakeClient)
 | 
								_, rss, err := GetOldReplicaSets(&newDeployment, fakeClient)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
			t.Errorf("In test case %s, got unexpected error %v", test.test, err)
 | 
									t.Errorf("In test case %s, got unexpected error %v", test.Name, err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if !equal(rss, test.expected) {
 | 
								if !equal(rss, test.expected) {
 | 
				
			||||||
			t.Errorf("In test case %q, expected:", test.test)
 | 
									t.Errorf("In test case %q, expected:", test.Name)
 | 
				
			||||||
				for _, rs := range test.expected {
 | 
									for _, rs := range test.expected {
 | 
				
			||||||
					t.Errorf("rs = %#v", rs)
 | 
										t.Errorf("rs = %#v", rs)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			t.Errorf("In test case %q, got:", test.test)
 | 
									t.Errorf("In test case %q, got:", test.Name)
 | 
				
			||||||
				for _, rs := range rss {
 | 
									for _, rs := range rss {
 | 
				
			||||||
					t.Errorf("rs = %#v", rs)
 | 
										t.Errorf("rs = %#v", rs)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -390,7 +394,7 @@ func generatePodTemplateSpec(name, nodeName string, annotations, labels map[stri
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func TestEqualIgnoreHash(t *testing.T) {
 | 
					func TestEqualIgnoreHash(t *testing.T) {
 | 
				
			||||||
	tests := []struct {
 | 
						tests := []struct {
 | 
				
			||||||
		test           string
 | 
							Name           string
 | 
				
			||||||
		former, latter v1.PodTemplateSpec
 | 
							former, latter v1.PodTemplateSpec
 | 
				
			||||||
		expected       bool
 | 
							expected       bool
 | 
				
			||||||
	}{
 | 
						}{
 | 
				
			||||||
@@ -445,6 +449,7 @@ func TestEqualIgnoreHash(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, test := range tests {
 | 
						for _, test := range tests {
 | 
				
			||||||
 | 
							t.Run(test.Name, func(t *testing.T) {
 | 
				
			||||||
			runTest := func(t1, t2 *v1.PodTemplateSpec, reversed bool) {
 | 
								runTest := func(t1, t2 *v1.PodTemplateSpec, reversed bool) {
 | 
				
			||||||
				reverseString := ""
 | 
									reverseString := ""
 | 
				
			||||||
				if reversed {
 | 
									if reversed {
 | 
				
			||||||
@@ -453,20 +458,22 @@ func TestEqualIgnoreHash(t *testing.T) {
 | 
				
			|||||||
				// Run
 | 
									// Run
 | 
				
			||||||
				equal, err := EqualIgnoreHash(t1, t2)
 | 
									equal, err := EqualIgnoreHash(t1, t2)
 | 
				
			||||||
				if err != nil {
 | 
									if err != nil {
 | 
				
			||||||
				t.Errorf("%s: unexpected error: %v", err, test.test)
 | 
										t.Errorf("%s: unexpected error: %v", err, test.Name)
 | 
				
			||||||
					return
 | 
										return
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				if equal != test.expected {
 | 
									if equal != test.expected {
 | 
				
			||||||
				t.Errorf("%q%s: expected %v", test.test, reverseString, test.expected)
 | 
										t.Errorf("%q%s: expected %v", test.Name, reverseString, test.expected)
 | 
				
			||||||
					return
 | 
										return
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				if t1.Labels == nil || t2.Labels == nil {
 | 
									if t1.Labels == nil || t2.Labels == nil {
 | 
				
			||||||
				t.Errorf("%q%s: unexpected labels becomes nil", test.test, reverseString)
 | 
										t.Errorf("%q%s: unexpected labels becomes nil", test.Name, reverseString)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			runTest(&test.former, &test.latter, false)
 | 
								runTest(&test.former, &test.latter, false)
 | 
				
			||||||
			// Test the same case in reverse order
 | 
								// Test the same case in reverse order
 | 
				
			||||||
			runTest(&test.latter, &test.former, true)
 | 
								runTest(&test.latter, &test.former, true)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -489,25 +496,25 @@ func TestFindNewReplicaSet(t *testing.T) {
 | 
				
			|||||||
	oldRS.Status.FullyLabeledReplicas = *(oldRS.Spec.Replicas)
 | 
						oldRS.Status.FullyLabeledReplicas = *(oldRS.Spec.Replicas)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tests := []struct {
 | 
						tests := []struct {
 | 
				
			||||||
		test       string
 | 
							Name       string
 | 
				
			||||||
		deployment extensions.Deployment
 | 
							deployment extensions.Deployment
 | 
				
			||||||
		rsList     []*extensions.ReplicaSet
 | 
							rsList     []*extensions.ReplicaSet
 | 
				
			||||||
		expected   *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,
 | 
								deployment: deployment,
 | 
				
			||||||
			rsList:     []*extensions.ReplicaSet{&newRS, &oldRS},
 | 
								rsList:     []*extensions.ReplicaSet{&newRS, &oldRS},
 | 
				
			||||||
			expected:   &newRS,
 | 
								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,
 | 
								deployment: deployment,
 | 
				
			||||||
			rsList:     []*extensions.ReplicaSet{&newRS, &oldRS, &newRSDup},
 | 
								rsList:     []*extensions.ReplicaSet{&newRS, &oldRS, &newRSDup},
 | 
				
			||||||
			expected:   &newRSDup,
 | 
								expected:   &newRSDup,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			test:       "Get nil new ReplicaSet",
 | 
								Name:       "Get nil new ReplicaSet",
 | 
				
			||||||
			deployment: deployment,
 | 
								deployment: deployment,
 | 
				
			||||||
			rsList:     []*extensions.ReplicaSet{&oldRS},
 | 
								rsList:     []*extensions.ReplicaSet{&oldRS},
 | 
				
			||||||
			expected:   nil,
 | 
								expected:   nil,
 | 
				
			||||||
@@ -515,9 +522,11 @@ func TestFindNewReplicaSet(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, test := range tests {
 | 
						for _, test := range tests {
 | 
				
			||||||
 | 
							t.Run(test.Name, func(t *testing.T) {
 | 
				
			||||||
			if rs, err := FindNewReplicaSet(&test.deployment, test.rsList); !reflect.DeepEqual(rs, test.expected) || err != nil {
 | 
								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.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
 | 
						oldRS.CreationTimestamp = before
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tests := []struct {
 | 
						tests := []struct {
 | 
				
			||||||
		test            string
 | 
							Name            string
 | 
				
			||||||
		deployment      extensions.Deployment
 | 
							deployment      extensions.Deployment
 | 
				
			||||||
		rsList          []*extensions.ReplicaSet
 | 
							rsList          []*extensions.ReplicaSet
 | 
				
			||||||
		podList         *v1.PodList
 | 
							podList         *v1.PodList
 | 
				
			||||||
@@ -551,28 +560,28 @@ func TestFindOldReplicaSets(t *testing.T) {
 | 
				
			|||||||
		expectedRequire []*extensions.ReplicaSet
 | 
							expectedRequire []*extensions.ReplicaSet
 | 
				
			||||||
	}{
 | 
						}{
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			test:            "Get old ReplicaSets",
 | 
								Name:            "Get old ReplicaSets",
 | 
				
			||||||
			deployment:      deployment,
 | 
								deployment:      deployment,
 | 
				
			||||||
			rsList:          []*extensions.ReplicaSet{&newRS, &oldRS},
 | 
								rsList:          []*extensions.ReplicaSet{&newRS, &oldRS},
 | 
				
			||||||
			expected:        []*extensions.ReplicaSet{&oldRS},
 | 
								expected:        []*extensions.ReplicaSet{&oldRS},
 | 
				
			||||||
			expectedRequire: nil,
 | 
								expectedRequire: nil,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			test:            "Get old ReplicaSets with no new ReplicaSet",
 | 
								Name:            "Get old ReplicaSets with no new ReplicaSet",
 | 
				
			||||||
			deployment:      deployment,
 | 
								deployment:      deployment,
 | 
				
			||||||
			rsList:          []*extensions.ReplicaSet{&oldRS},
 | 
								rsList:          []*extensions.ReplicaSet{&oldRS},
 | 
				
			||||||
			expected:        []*extensions.ReplicaSet{&oldRS},
 | 
								expected:        []*extensions.ReplicaSet{&oldRS},
 | 
				
			||||||
			expectedRequire: nil,
 | 
								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,
 | 
								deployment:      deployment,
 | 
				
			||||||
			rsList:          []*extensions.ReplicaSet{&oldRS, &newRS, &newRSDup},
 | 
								rsList:          []*extensions.ReplicaSet{&oldRS, &newRS, &newRSDup},
 | 
				
			||||||
			expected:        []*extensions.ReplicaSet{&oldRS, &newRS},
 | 
								expected:        []*extensions.ReplicaSet{&oldRS, &newRS},
 | 
				
			||||||
			expectedRequire: []*extensions.ReplicaSet{&newRS},
 | 
								expectedRequire: []*extensions.ReplicaSet{&newRS},
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			test:            "Get empty old ReplicaSets",
 | 
								Name:            "Get empty old ReplicaSets",
 | 
				
			||||||
			deployment:      deployment,
 | 
								deployment:      deployment,
 | 
				
			||||||
			rsList:          []*extensions.ReplicaSet{&newRS},
 | 
								rsList:          []*extensions.ReplicaSet{&newRS},
 | 
				
			||||||
			expected:        nil,
 | 
								expected:        nil,
 | 
				
			||||||
@@ -581,16 +590,18 @@ func TestFindOldReplicaSets(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, test := range tests {
 | 
						for _, test := range tests {
 | 
				
			||||||
 | 
							t.Run(test.Name, func(t *testing.T) {
 | 
				
			||||||
			requireRS, allRS, err := FindOldReplicaSets(&test.deployment, test.rsList)
 | 
								requireRS, allRS, err := FindOldReplicaSets(&test.deployment, test.rsList)
 | 
				
			||||||
			sort.Sort(controller.ReplicaSetsByCreationTimestamp(allRS))
 | 
								sort.Sort(controller.ReplicaSetsByCreationTimestamp(allRS))
 | 
				
			||||||
			sort.Sort(controller.ReplicaSetsByCreationTimestamp(test.expected))
 | 
								sort.Sort(controller.ReplicaSetsByCreationTimestamp(test.expected))
 | 
				
			||||||
			if !reflect.DeepEqual(allRS, test.expected) || err != nil {
 | 
								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)
 | 
									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
 | 
								// RSs are getting filtered correctly by rs.spec.replicas
 | 
				
			||||||
			if !reflect.DeepEqual(requireRS, test.expectedRequire) || err != nil {
 | 
								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.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
 | 
						rs2.Status.Replicas = 3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tests := []struct {
 | 
						tests := []struct {
 | 
				
			||||||
		test           string
 | 
							Name           string
 | 
				
			||||||
		sets           []*extensions.ReplicaSet
 | 
							sets           []*extensions.ReplicaSet
 | 
				
			||||||
		expectedCount  int32
 | 
							expectedCount  int32
 | 
				
			||||||
		expectedActual int32
 | 
							expectedActual int32
 | 
				
			||||||
@@ -643,14 +654,16 @@ func TestGetReplicaCountForReplicaSets(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, test := range tests {
 | 
						for _, test := range tests {
 | 
				
			||||||
 | 
							t.Run(test.Name, func(t *testing.T) {
 | 
				
			||||||
			rs := GetReplicaCountForReplicaSets(test.sets)
 | 
								rs := GetReplicaCountForReplicaSets(test.sets)
 | 
				
			||||||
			if rs != test.expectedCount {
 | 
								if rs != test.expectedCount {
 | 
				
			||||||
			t.Errorf("In test case %s, expectedCount %+v, got %+v", test.test, test.expectedCount, rs)
 | 
									t.Errorf("In test case %s, expectedCount %+v, got %+v", test.Name, test.expectedCount, rs)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			rs = GetActualReplicaCountForReplicaSets(test.sets)
 | 
								rs = GetActualReplicaCountForReplicaSets(test.sets)
 | 
				
			||||||
			if rs != test.expectedActual {
 | 
								if rs != test.expectedActual {
 | 
				
			||||||
			t.Errorf("In test case %s, expectedActual %+v, got %+v", test.test, test.expectedActual, rs)
 | 
									t.Errorf("In test case %s, expectedActual %+v, got %+v", test.Name, test.expectedActual, rs)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -698,6 +711,7 @@ func TestResolveFenceposts(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for num, test := range tests {
 | 
						for num, test := range tests {
 | 
				
			||||||
 | 
							t.Run("maxSurge="+test.maxSurge, func(t *testing.T) {
 | 
				
			||||||
			maxSurge := intstr.FromString(test.maxSurge)
 | 
								maxSurge := intstr.FromString(test.maxSurge)
 | 
				
			||||||
			maxUnavail := intstr.FromString(test.maxUnavailable)
 | 
								maxUnavail := intstr.FromString(test.maxUnavailable)
 | 
				
			||||||
			surge, unavail, err := ResolveFenceposts(&maxSurge, &maxUnavail, test.desired)
 | 
								surge, unavail, err := ResolveFenceposts(&maxSurge, &maxUnavail, test.desired)
 | 
				
			||||||
@@ -710,12 +724,13 @@ func TestResolveFenceposts(t *testing.T) {
 | 
				
			|||||||
			if surge != test.expectSurge || unavail != test.expectUnavailable {
 | 
								if surge != test.expectSurge || unavail != test.expectUnavailable {
 | 
				
			||||||
				t.Errorf("#%v got %v:%v, want %v:%v", num, surge, unavail, test.expectSurge, test.expectUnavailable)
 | 
									t.Errorf("#%v got %v:%v, want %v:%v", num, surge, unavail, test.expectSurge, test.expectUnavailable)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestNewRSNewReplicas(t *testing.T) {
 | 
					func TestNewRSNewReplicas(t *testing.T) {
 | 
				
			||||||
	tests := []struct {
 | 
						tests := []struct {
 | 
				
			||||||
		test          string
 | 
							Name          string
 | 
				
			||||||
		strategyType  extensions.DeploymentStrategyType
 | 
							strategyType  extensions.DeploymentStrategyType
 | 
				
			||||||
		depReplicas   int32
 | 
							depReplicas   int32
 | 
				
			||||||
		newRSReplicas int32
 | 
							newRSReplicas int32
 | 
				
			||||||
@@ -744,20 +759,28 @@ func TestNewRSNewReplicas(t *testing.T) {
 | 
				
			|||||||
	*(rs5.Spec.Replicas) = 5
 | 
						*(rs5.Spec.Replicas) = 5
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, test := range tests {
 | 
						for _, test := range tests {
 | 
				
			||||||
 | 
							t.Run(test.Name, func(t *testing.T) {
 | 
				
			||||||
			*(newDeployment.Spec.Replicas) = test.depReplicas
 | 
								*(newDeployment.Spec.Replicas) = test.depReplicas
 | 
				
			||||||
			newDeployment.Spec.Strategy = extensions.DeploymentStrategy{Type: test.strategyType}
 | 
								newDeployment.Spec.Strategy = extensions.DeploymentStrategy{Type: test.strategyType}
 | 
				
			||||||
			newDeployment.Spec.Strategy.RollingUpdate = &extensions.RollingUpdateDeployment{
 | 
								newDeployment.Spec.Strategy.RollingUpdate = &extensions.RollingUpdateDeployment{
 | 
				
			||||||
			MaxUnavailable: func(i int) *intstr.IntOrString { x := intstr.FromInt(i); return &x }(1),
 | 
									MaxUnavailable: func(i int) *intstr.IntOrString {
 | 
				
			||||||
			MaxSurge:       func(i int) *intstr.IntOrString { x := intstr.FromInt(i); return &x }(test.maxSurge),
 | 
										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
 | 
								*(newRC.Spec.Replicas) = test.newRSReplicas
 | 
				
			||||||
			rs, err := NewRSNewReplicas(&newDeployment, []*extensions.ReplicaSet{&rs5}, &newRC)
 | 
								rs, err := NewRSNewReplicas(&newDeployment, []*extensions.ReplicaSet{&rs5}, &newRC)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
			t.Errorf("In test case %s, got unexpected error %v", test.test, err)
 | 
									t.Errorf("In test case %s, got unexpected error %v", test.Name, err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if rs != test.expected {
 | 
								if rs != test.expected {
 | 
				
			||||||
			t.Errorf("In test case %s, expected %+v, got %+v", test.test, test.expected, rs)
 | 
									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 {
 | 
						for _, test := range tests {
 | 
				
			||||||
 | 
							t.Run(test.name, func(t *testing.T) {
 | 
				
			||||||
			cond := GetDeploymentCondition(test.status, test.condType)
 | 
								cond := GetDeploymentCondition(test.status, test.condType)
 | 
				
			||||||
			exists := cond != nil
 | 
								exists := cond != nil
 | 
				
			||||||
			if exists != test.expected {
 | 
								if exists != test.expected {
 | 
				
			||||||
				t.Errorf("%s: expected condition to exist: %t, got: %t", test.name, test.expected, exists)
 | 
									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 {
 | 
						for _, test := range tests {
 | 
				
			||||||
 | 
							t.Run(test.name, func(t *testing.T) {
 | 
				
			||||||
			SetDeploymentCondition(test.status, test.cond)
 | 
								SetDeploymentCondition(test.status, test.cond)
 | 
				
			||||||
			if !reflect.DeepEqual(test.status, test.expectedStatus) {
 | 
								if !reflect.DeepEqual(test.status, test.expectedStatus) {
 | 
				
			||||||
				t.Errorf("%s: expected status: %v, got: %v", test.name, test.expectedStatus, test.status)
 | 
									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 {
 | 
						for _, test := range tests {
 | 
				
			||||||
 | 
							t.Run(test.name, func(t *testing.T) {
 | 
				
			||||||
			RemoveDeploymentCondition(test.status, test.condType)
 | 
								RemoveDeploymentCondition(test.status, test.condType)
 | 
				
			||||||
			if !reflect.DeepEqual(test.status, test.expectedStatus) {
 | 
								if !reflect.DeepEqual(test.status, test.expectedStatus) {
 | 
				
			||||||
				t.Errorf("%s: expected status: %v, got: %v", test.name, test.expectedStatus, test.status)
 | 
									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 {
 | 
						for _, test := range tests {
 | 
				
			||||||
		t.Log(test.name)
 | 
							t.Run(test.name, func(t *testing.T) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
			if got, exp := DeploymentComplete(test.d, &test.d.Status), test.expected; got != exp {
 | 
								if got, exp := DeploymentComplete(test.d, &test.d.Status), test.expected; got != exp {
 | 
				
			||||||
				t.Errorf("expected complete: %t, got: %t", exp, got)
 | 
									t.Errorf("expected complete: %t, got: %t", exp, got)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1083,11 +1112,11 @@ func TestDeploymentProgressing(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, test := range tests {
 | 
						for _, test := range tests {
 | 
				
			||||||
		t.Log(test.name)
 | 
							t.Run(test.name, func(t *testing.T) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
			if got, exp := DeploymentProgressing(test.d, &test.newStatus), test.expected; got != exp {
 | 
								if got, exp := DeploymentProgressing(test.d, &test.newStatus), test.expected; got != exp {
 | 
				
			||||||
				t.Errorf("expected progressing: %t, got: %t", exp, got)
 | 
									t.Errorf("expected progressing: %t, got: %t", exp, got)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1149,12 +1178,12 @@ func TestDeploymentTimedOut(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, test := range tests {
 | 
						for _, test := range tests {
 | 
				
			||||||
		t.Log(test.name)
 | 
							t.Run(test.name, func(t *testing.T) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
			nowFn = test.nowFn
 | 
								nowFn = test.nowFn
 | 
				
			||||||
			if got, exp := DeploymentTimedOut(&test.d, &test.d.Status), test.expected; got != exp {
 | 
								if got, exp := DeploymentTimedOut(&test.d, &test.d.Status), test.expected; got != exp {
 | 
				
			||||||
				t.Errorf("expected timeout: %t, got: %t", exp, got)
 | 
									t.Errorf("expected timeout: %t, got: %t", exp, got)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1228,9 +1257,70 @@ func TestMaxUnavailable(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	for _, test := range tests {
 | 
						for _, test := range tests {
 | 
				
			||||||
		t.Log(test.name)
 | 
							t.Log(test.name)
 | 
				
			||||||
 | 
							t.Run(test.name, func(t *testing.T) {
 | 
				
			||||||
			maxUnavailable := MaxUnavailable(test.deployment)
 | 
								maxUnavailable := MaxUnavailable(test.deployment)
 | 
				
			||||||
			if test.expected != maxUnavailable {
 | 
								if test.expected != maxUnavailable {
 | 
				
			||||||
				t.Fatalf("expected:%v, got:%v", 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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user