mirror of
				https://github.com/k3s-io/kubernetes.git
				synced 2025-10-31 13:50:01 +00:00 
			
		
		
		
	use subtest for table units (pkg/master)
This commit is contained in:
		| @@ -189,18 +189,18 @@ func TestWriteClientCAs(t *testing.T) { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	for _, test := range tests { | 	for _, test := range tests { | ||||||
|  | 		t.Run(test.name, func(t *testing.T) { | ||||||
| 			client := fake.NewSimpleClientset(test.preexistingObjs...) | 			client := fake.NewSimpleClientset(test.preexistingObjs...) | ||||||
| 			test.hook.tryToWriteClientCAs(client.Core()) | 			test.hook.tryToWriteClientCAs(client.Core()) | ||||||
|  |  | ||||||
| 			actualConfigMaps, updated := getFinalConfiMaps(client) | 			actualConfigMaps, updated := getFinalConfiMaps(client) | ||||||
| 			if !reflect.DeepEqual(test.expectedConfigMaps, actualConfigMaps) { | 			if !reflect.DeepEqual(test.expectedConfigMaps, actualConfigMaps) { | ||||||
| 			t.Errorf("%s: %v", test.name, diff.ObjectReflectDiff(test.expectedConfigMaps, actualConfigMaps)) | 				t.Fatalf("%s: %v", test.name, diff.ObjectReflectDiff(test.expectedConfigMaps, actualConfigMaps)) | ||||||
| 			continue |  | ||||||
| 			} | 			} | ||||||
| 			if test.expectUpdate != updated { | 			if test.expectUpdate != updated { | ||||||
| 			t.Errorf("%s: expected %v, got %v", test.name, test.expectUpdate, updated) | 				t.Fatalf("%s: expected %v, got %v", test.name, test.expectUpdate, updated) | ||||||
| 			continue |  | ||||||
| 			} | 			} | ||||||
|  | 		}) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -78,6 +78,7 @@ func TestHandleVersionUpdate(t *testing.T) { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	for _, test := range tests { | 	for _, test := range tests { | ||||||
|  | 		t.Run(test.name, func(t *testing.T) { | ||||||
| 			registration := &fakeAPIServiceRegistration{} | 			registration := &fakeAPIServiceRegistration{} | ||||||
| 			crdCache := cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}) | 			crdCache := cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}) | ||||||
| 			crdLister := crdlisters.NewCustomResourceDefinitionLister(crdCache) | 			crdLister := crdlisters.NewCustomResourceDefinitionLister(crdCache) | ||||||
| @@ -97,6 +98,7 @@ func TestHandleVersionUpdate(t *testing.T) { | |||||||
| 			if !reflect.DeepEqual(test.expectedRemoved, registration.removed) { | 			if !reflect.DeepEqual(test.expectedRemoved, registration.removed) { | ||||||
| 				t.Errorf("%s expected %v, got %v", test.name, test.expectedRemoved, registration.removed) | 				t.Errorf("%s expected %v, got %v", test.name, test.expectedRemoved, registration.removed) | ||||||
| 			} | 			} | ||||||
|  | 		}) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -509,6 +509,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { | |||||||
| 		}, | 		}, | ||||||
| 	} | 	} | ||||||
| 	for _, test := range nonReconcileTests { | 	for _, test := range nonReconcileTests { | ||||||
|  | 		t.Run(test.testName, func(t *testing.T) { | ||||||
| 			fakeLeases := newFakeLeases() | 			fakeLeases := newFakeLeases() | ||||||
| 			fakeLeases.SetKeys(test.endpointKeys) | 			fakeLeases.SetKeys(test.endpointKeys) | ||||||
| 			registry := ®istrytest.EndpointRegistry{ | 			registry := ®istrytest.EndpointRegistry{ | ||||||
| @@ -532,6 +533,7 @@ func TestLeaseEndpointReconciler(t *testing.T) { | |||||||
| 			if updatedKeys := fakeLeases.GetUpdatedKeys(); len(updatedKeys) != 1 || updatedKeys[0] != test.ip { | 			if updatedKeys := fakeLeases.GetUpdatedKeys(); len(updatedKeys) != 1 || updatedKeys[0] != test.ip { | ||||||
| 				t.Errorf("case %q: expected the master's IP to be refreshed, but the following IPs were refreshed instead: %v", test.testName, updatedKeys) | 				t.Errorf("case %q: expected the master's IP to be refreshed, but the following IPs were refreshed instead: %v", test.testName, updatedKeys) | ||||||
| 			} | 			} | ||||||
|  | 		}) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -604,6 +606,7 @@ func TestLeaseStopReconciling(t *testing.T) { | |||||||
| 		}, | 		}, | ||||||
| 	} | 	} | ||||||
| 	for _, test := range stopTests { | 	for _, test := range stopTests { | ||||||
|  | 		t.Run(test.testName, func(t *testing.T) { | ||||||
| 			fakeLeases := newFakeLeases() | 			fakeLeases := newFakeLeases() | ||||||
| 			fakeLeases.SetKeys(test.endpointKeys) | 			fakeLeases.SetKeys(test.endpointKeys) | ||||||
| 			registry := ®istrytest.EndpointRegistry{ | 			registry := ®istrytest.EndpointRegistry{ | ||||||
| @@ -629,5 +632,6 @@ func TestLeaseStopReconciling(t *testing.T) { | |||||||
| 					t.Errorf("case %q: Found ip %s in leases but shouldn't be there", test.testName, key) | 					t.Errorf("case %q: Found ip %s in leases but shouldn't be there", test.testName, key) | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
|  | 		}) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
| @@ -32,39 +32,66 @@ import ( | |||||||
| // TestSecondsSinceSync verifies that proper results are returned | // TestSecondsSinceSync verifies that proper results are returned | ||||||
| // when checking the time between syncs | // when checking the time between syncs | ||||||
| func TestSecondsSinceSync(t *testing.T) { | func TestSecondsSinceSync(t *testing.T) { | ||||||
|  | 	tests := []struct { | ||||||
|  | 		name     string | ||||||
|  | 		lastSync int64 | ||||||
|  | 		clock    *clock.FakeClock | ||||||
|  | 		want     int64 | ||||||
|  | 	}{ | ||||||
|  | 		{ | ||||||
|  | 			name:     "Nano Second. No difference", | ||||||
|  | 			lastSync: time.Date(2015, time.January, 1, 1, 1, 1, 1, time.UTC).Unix(), | ||||||
|  | 			clock:    clock.NewFakeClock(time.Date(2015, time.January, 1, 1, 1, 1, 2, time.UTC)), | ||||||
|  | 			want:     int64(0), | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name:     "Second", | ||||||
|  | 			lastSync: time.Date(2015, time.January, 1, 1, 1, 1, 1, time.UTC).Unix(), | ||||||
|  | 			clock:    clock.NewFakeClock(time.Date(2015, time.January, 1, 1, 1, 2, 1, time.UTC)), | ||||||
|  | 			want:     int64(1), | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name:     "Minute", | ||||||
|  | 			lastSync: time.Date(2015, time.January, 1, 1, 1, 1, 1, time.UTC).Unix(), | ||||||
|  | 			clock:    clock.NewFakeClock(time.Date(2015, time.January, 1, 1, 2, 1, 1, time.UTC)), | ||||||
|  | 			want:     int64(60), | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name:     "Hour", | ||||||
|  | 			lastSync: time.Date(2015, time.January, 1, 1, 1, 1, 1, time.UTC).Unix(), | ||||||
|  | 			clock:    clock.NewFakeClock(time.Date(2015, time.January, 1, 2, 1, 1, 1, time.UTC)), | ||||||
|  | 			want:     int64(3600), | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name:     "Day", | ||||||
|  | 			lastSync: time.Date(2015, time.January, 1, 1, 1, 1, 1, time.UTC).Unix(), | ||||||
|  | 			clock:    clock.NewFakeClock(time.Date(2015, time.January, 2, 1, 1, 1, 1, time.UTC)), | ||||||
|  | 			want:     int64(86400), | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name:     "Month", | ||||||
|  | 			lastSync: time.Date(2015, time.January, 1, 1, 1, 1, 1, time.UTC).Unix(), | ||||||
|  | 			clock:    clock.NewFakeClock(time.Date(2015, time.February, 1, 1, 1, 1, 1, time.UTC)), | ||||||
|  | 			want:     int64(2678400), | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name:     "Future Month. Should be -Month", | ||||||
|  | 			lastSync: time.Date(2015, time.February, 1, 1, 1, 1, 1, time.UTC).Unix(), | ||||||
|  | 			clock:    clock.NewFakeClock(time.Date(2015, time.January, 1, 1, 1, 1, 2, time.UTC)), | ||||||
|  | 			want:     int64(-2678400), | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	for _, tt := range tests { | ||||||
|  | 		t.Run(tt.name, func(t *testing.T) { | ||||||
| 			tunneler := &SSHTunneler{} | 			tunneler := &SSHTunneler{} | ||||||
| 			assert := assert.New(t) | 			assert := assert.New(t) | ||||||
|  | 			tunneler.lastSync = tt.lastSync | ||||||
|  | 			tunneler.clock = tt.clock | ||||||
|  | 			assert.Equal(int64(tt.want), tunneler.SecondsSinceSync()) | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	tunneler.lastSync = time.Date(2015, time.January, 1, 1, 1, 1, 1, time.UTC).Unix() |  | ||||||
|  |  | ||||||
| 	// Nano Second. No difference. |  | ||||||
| 	tunneler.clock = clock.NewFakeClock(time.Date(2015, time.January, 1, 1, 1, 1, 2, time.UTC)) |  | ||||||
| 	assert.Equal(int64(0), tunneler.SecondsSinceSync()) |  | ||||||
|  |  | ||||||
| 	// Second |  | ||||||
| 	tunneler.clock = clock.NewFakeClock(time.Date(2015, time.January, 1, 1, 1, 2, 1, time.UTC)) |  | ||||||
| 	assert.Equal(int64(1), tunneler.SecondsSinceSync()) |  | ||||||
|  |  | ||||||
| 	// Minute |  | ||||||
| 	tunneler.clock = clock.NewFakeClock(time.Date(2015, time.January, 1, 1, 2, 1, 1, time.UTC)) |  | ||||||
| 	assert.Equal(int64(60), tunneler.SecondsSinceSync()) |  | ||||||
|  |  | ||||||
| 	// Hour |  | ||||||
| 	tunneler.clock = clock.NewFakeClock(time.Date(2015, time.January, 1, 2, 1, 1, 1, time.UTC)) |  | ||||||
| 	assert.Equal(int64(3600), tunneler.SecondsSinceSync()) |  | ||||||
|  |  | ||||||
| 	// Day |  | ||||||
| 	tunneler.clock = clock.NewFakeClock(time.Date(2015, time.January, 2, 1, 1, 1, 1, time.UTC)) |  | ||||||
| 	assert.Equal(int64(86400), tunneler.SecondsSinceSync()) |  | ||||||
|  |  | ||||||
| 	// Month |  | ||||||
| 	tunneler.clock = clock.NewFakeClock(time.Date(2015, time.February, 1, 1, 1, 1, 1, time.UTC)) |  | ||||||
| 	assert.Equal(int64(2678400), tunneler.SecondsSinceSync()) |  | ||||||
|  |  | ||||||
| 	// Future Month. Should be -Month. |  | ||||||
| 	tunneler.lastSync = time.Date(2015, time.February, 1, 1, 1, 1, 1, time.UTC).Unix() |  | ||||||
| 	tunneler.clock = clock.NewFakeClock(time.Date(2015, time.January, 1, 1, 1, 1, 1, time.UTC)) |  | ||||||
| 	assert.Equal(int64(-2678400), tunneler.SecondsSinceSync()) |  | ||||||
| } | } | ||||||
|  |  | ||||||
| // generateTempFile creates a temporary file path | // generateTempFile creates a temporary file path | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user