Skip to content

Commit

Permalink
Merge pull request kubernetes#6410 from walidghallab/refactor
Browse files Browse the repository at this point in the history
Refactors status_test.go to use table tests
  • Loading branch information
k8s-ci-robot authored Jan 8, 2024
2 parents 0024f25 + 138c448 commit 5737488
Showing 1 changed file with 75 additions and 43 deletions.
118 changes: 75 additions & 43 deletions cluster-autoscaler/clusterstate/utils/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,51 +88,83 @@ func setUpTest(t *testing.T) *testInfo {
return &result
}

func TestWriteStatusConfigMapExisting(t *testing.T) {
ti := setUpTest(t)
result, err := WriteStatusConfigMap(ti.client, ti.namespace, api.ClusterAutoscalerStatus{Message: "TEST_MSG"}, nil, "my-cool-configmap", time.Now())
assert.Equal(t, ti.configMap, result)
assert.Contains(t, result.Data["status"], "TEST_MSG")
assert.Contains(t, result.ObjectMeta.Annotations, ConfigMapLastUpdatedKey)
assert.Nil(t, err)
assert.True(t, ti.getCalled)
assert.True(t, ti.updateCalled)
assert.False(t, ti.createCalled)

// to test the case where configmap is empty
ti.configMap.Data = nil
result, err = WriteStatusConfigMap(ti.client, ti.namespace, api.ClusterAutoscalerStatus{Message: "TEST_MSG"}, nil, "my-cool-configmap", time.Now())
assert.Equal(t, ti.configMap, result)
assert.Contains(t, result.Data["status"], "TEST_MSG")
assert.Contains(t, result.ObjectMeta.Annotations, ConfigMapLastUpdatedKey)
assert.Nil(t, err)
assert.True(t, ti.getCalled)
assert.True(t, ti.updateCalled)
assert.False(t, ti.createCalled)
}
func TestWriteStatusConfigMap(t *testing.T) {
currentTime := time.Date(2023, 12, 21, 0, 0, 0, 0, time.UTC)
clusterAutoscalerStatus := api.ClusterAutoscalerStatus{Message: "TEST_MSG"}
defaultConfigMapForTest := &apiv1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "my-cool-configmap",
Namespace: "kube-system",
Annotations: map[string]string{ConfigMapLastUpdatedKey: "2023-12-21 00:00:00 +0000 UTC"},
},
Data: map[string]string{"status": "time: 2023-12-21 00:00:00 +0000 UTC\nmessage: TEST_MSG\n"},
}
testCases := []struct {
name string
preprocessor func(*testInfo)
wantConfigMap *apiv1.ConfigMap
wantError error
wantGetCalled bool
wantUpdateCalled bool
wantCreateCalled bool
}{
{
name: "Existing config map",
preprocessor: nil,
wantConfigMap: defaultConfigMapForTest,
wantError: nil,
wantGetCalled: true,
wantUpdateCalled: true,
wantCreateCalled: false,
},
{
name: "Existing config map when configmap data is empty",
preprocessor: func(ti *testInfo) { ti.configMap.Data = nil },
wantConfigMap: defaultConfigMapForTest,
wantError: nil,
wantGetCalled: true,
wantUpdateCalled: true,
wantCreateCalled: false,
},
{
name: "Create config map",
preprocessor: func(ti *testInfo) {
ti.getError = kube_errors.NewNotFound(apiv1.Resource("configmap"), "nope, not found")
},
wantConfigMap: defaultConfigMapForTest,
wantError: nil,
wantGetCalled: true,
wantUpdateCalled: false,
wantCreateCalled: true,
},
{
name: "Config map with error",
preprocessor: func(ti *testInfo) {
ti.getError = errors.New("stuff bad")
},
wantConfigMap: nil,
wantError: errors.New("Failed to retrieve status configmap for update: stuff bad"),
wantGetCalled: true,
wantUpdateCalled: false,
wantCreateCalled: false,
},
}

func TestWriteStatusConfigMapCreate(t *testing.T) {
ti := setUpTest(t)
ti.getError = kube_errors.NewNotFound(apiv1.Resource("configmap"), "nope, not found")
result, err := WriteStatusConfigMap(ti.client, ti.namespace, api.ClusterAutoscalerStatus{Message: "TEST_MSG"}, nil, "my-cool-configmap", time.Now())
assert.Contains(t, result.Data["status"], "TEST_MSG")
assert.Contains(t, result.ObjectMeta.Annotations, ConfigMapLastUpdatedKey)
assert.Nil(t, err)
assert.True(t, ti.getCalled)
assert.False(t, ti.updateCalled)
assert.True(t, ti.createCalled)
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ti := setUpTest(t)
if tc.preprocessor != nil {
tc.preprocessor(ti)
}
result, err := WriteStatusConfigMap(ti.client, ti.namespace, clusterAutoscalerStatus, nil, "my-cool-configmap", currentTime)
assert.Equal(t, tc.wantError, err)
assert.Equal(t, tc.wantConfigMap, result)
assert.Equal(t, tc.wantGetCalled, ti.getCalled)
assert.Equal(t, tc.wantUpdateCalled, ti.updateCalled)
assert.Equal(t, tc.wantCreateCalled, ti.createCalled)
})
}

func TestWriteStatusConfigMapError(t *testing.T) {
ti := setUpTest(t)
ti.getError = errors.New("stuff bad")
result, err := WriteStatusConfigMap(ti.client, ti.namespace, api.ClusterAutoscalerStatus{Message: "TEST_MSG"}, nil, "my-cool-configmap", time.Now())
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "stuff bad")
assert.Nil(t, result)
assert.True(t, ti.getCalled)
assert.False(t, ti.updateCalled)
assert.False(t, ti.createCalled)
}

var status api.ClusterAutoscalerStatus = api.ClusterAutoscalerStatus{
Expand Down

0 comments on commit 5737488

Please sign in to comment.