diff --git a/cluster-autoscaler/clusterstate/utils/status_test.go b/cluster-autoscaler/clusterstate/utils/status_test.go index 60d1823e4e75..84f16fef464a 100644 --- a/cluster-autoscaler/clusterstate/utils/status_test.go +++ b/cluster-autoscaler/clusterstate/utils/status_test.go @@ -18,6 +18,7 @@ package utils import ( "errors" + "fmt" "io/ioutil" "testing" "time" @@ -88,51 +89,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: fmt.Errorf("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{