From fbb3bafe6fda1490530826c8730b8f297916f5f5 Mon Sep 17 00:00:00 2001 From: H Date: Thu, 21 Nov 2024 21:25:12 +0530 Subject: [PATCH 1/7] increased code coverage of common.go to 93.8 --- internal/common/common_test.go | 9 +++++++++ internal/common/testdata/empty-array.yaml | 1 + 2 files changed, 10 insertions(+) create mode 100644 internal/common/testdata/empty-array.yaml diff --git a/internal/common/common_test.go b/internal/common/common_test.go index 2c61f6b..ea914ee 100644 --- a/internal/common/common_test.go +++ b/internal/common/common_test.go @@ -43,6 +43,9 @@ func Test_Run(t *testing.T) { "no global id": func(*testing.T) (string, map[string]string, bool) { return "testdata/no-global-id.yaml", nil, true }, + "empty arrays": func(*testing.T) (string, map[string]string, bool) { + return "testdata/empty-array.yaml",map[string]string{common.EnvThrottlingRateLimit: "abc"}, false + }, } for name, test := range tests { @@ -56,6 +59,12 @@ func Test_Run(t *testing.T) { arrays, mapper, defaultArray, err := common.GetPowerStoreArrays(filePath, logger) + if name == "empty arrays" { + assert.Equal(t, 0, len(arrays)) // Expect empty arrays + assert.Nil(t, defaultArray) // No default array + return + } + if expectError { assert.Nil(t, arrays) assert.Nil(t, mapper) diff --git a/internal/common/testdata/empty-array.yaml b/internal/common/testdata/empty-array.yaml new file mode 100644 index 0000000..b1b0f79 --- /dev/null +++ b/internal/common/testdata/empty-array.yaml @@ -0,0 +1 @@ +arrays: [] \ No newline at end of file From 50f1adb6a9da21bfb588e3b790a9470802e46816 Mon Sep 17 00:00:00 2001 From: H Date: Fri, 22 Nov 2024 17:17:12 +0530 Subject: [PATCH 2/7] increased code coverage of run.go to 92.4 --- internal/entrypoint/run_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/internal/entrypoint/run_test.go b/internal/entrypoint/run_test.go index 2393d5c..854d6a7 100644 --- a/internal/entrypoint/run_test.go +++ b/internal/entrypoint/run_test.go @@ -275,3 +275,27 @@ func Test_Run(t *testing.T) { func noCheckConfig(_ *entrypoint.Config) error { return nil } + +func Test_ValidateConfig(t *testing.T) { + tests := []struct { + name string + config *entrypoint.Config + wantErr bool + }{ + {"valid config", &entrypoint.Config{VolumeTickInterval: 10 * time.Second, SpaceTickInterval: 10 * time.Second, ArrayTickInterval: 10 * time.Second, FileSystemTickInterval: 10 * time.Second}, false}, + {"nil config", nil, true}, + {"invalid volume tick interval", &entrypoint.Config{VolumeTickInterval: 1 * time.Second}, true}, + {"invalid space tick interval", &entrypoint.Config{SpaceTickInterval: 1 * time.Second}, true}, + {"invalid array tick interval", &entrypoint.Config{ArrayTickInterval: 1 * time.Second}, true}, + {"invalid filesystem tick interval", &entrypoint.Config{FileSystemTickInterval: 1 * time.Second}, true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := entrypoint.ValidateConfig(tt.config) + if (err != nil) != tt.wantErr { + t.Errorf("ValidateConfig() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} From 469053f285856ca9f53e8aff084b60c860ffc96a Mon Sep 17 00:00:00 2001 From: H Date: Sat, 23 Nov 2024 10:34:23 +0530 Subject: [PATCH 3/7] Increased test coverage for k8sapi.go to 100% --- internal/k8s/k8sapi_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/internal/k8s/k8sapi_test.go b/internal/k8s/k8sapi_test.go index f9e1b7b..808c03c 100644 --- a/internal/k8s/k8sapi_test.go +++ b/internal/k8s/k8sapi_test.go @@ -136,4 +136,21 @@ func Test_NewForConfigError(t *testing.T) { if err != nil { assert.Equal(t, expected, err.Error()) } + mockClientset := &kubernetes.Clientset{} + k8s.NewConfigFn = func(_ *rest.Config) (*kubernetes.Clientset, error) { + return mockClientset, nil + } + + err = k8s.ConnectFn(k8sapi) + assert.NoError(t, err, "ConnectFn should succeed with no error") + assert.Equal(t, mockClientset, k8sapi.Client, "Client should be initialized with the mock clientset") +} + +func Test_NewConfigFn(t *testing.T) { + t.Run("success", func(t *testing.T) { + config := &rest.Config{} + clientset, err := k8s.NewConfigFn(config) + assert.NoError(t, err, "NewConfigFn should succeed") + assert.NotNil(t, clientset, "Clientset should not be nil") + }) } From 5cccb8c8b6d73ac2f8fd99e13245f4e2837fb245 Mon Sep 17 00:00:00 2001 From: H Date: Tue, 26 Nov 2024 18:06:47 +0530 Subject: [PATCH 4/7] Increased common.go code coverage --- internal/common/common_test.go | 43 +++++++++++++------- internal/common/testdata/invalid-client.yaml | 6 +++ internal/common/testdata/nil-array.yaml | 2 + 3 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 internal/common/testdata/invalid-client.yaml create mode 100644 internal/common/testdata/nil-array.yaml diff --git a/internal/common/common_test.go b/internal/common/common_test.go index ea914ee..0973a08 100644 --- a/internal/common/common_test.go +++ b/internal/common/common_test.go @@ -44,7 +44,13 @@ func Test_Run(t *testing.T) { return "testdata/no-global-id.yaml", nil, true }, "empty arrays": func(*testing.T) (string, map[string]string, bool) { - return "testdata/empty-array.yaml",map[string]string{common.EnvThrottlingRateLimit: "abc"}, false + return "testdata/empty-array.yaml", map[string]string{common.EnvThrottlingRateLimit: "abc"}, false + }, + "nil array entry": func(*testing.T) (string, map[string]string, bool) { + return "testdata/nil-array.yaml", nil, true + }, + "invalid client creation": func(*testing.T) (string, map[string]string, bool) { + return "testdata/invalid-client.yaml", nil, true }, } @@ -59,22 +65,29 @@ func Test_Run(t *testing.T) { arrays, mapper, defaultArray, err := common.GetPowerStoreArrays(filePath, logger) - if name == "empty arrays" { - assert.Equal(t, 0, len(arrays)) // Expect empty arrays - assert.Nil(t, defaultArray) // No default array - return - } - - if expectError { - assert.Nil(t, arrays) - assert.Nil(t, mapper) + switch name { + case "empty arrays": + assert.Equal(t, 0, len(arrays)) + assert.Nil(t, defaultArray) + case "nil array entry": + assert.Empty(t, arrays) + assert.Empty(t, mapper) assert.Nil(t, defaultArray) - assert.NotNil(t, err) - } else { - assert.NotNil(t, arrays) - assert.NotNil(t, mapper) - assert.NotNil(t, defaultArray) assert.Nil(t, err) + return + // Other cases... + default: + if expectError { + assert.Nil(t, arrays) + assert.Nil(t, mapper) + assert.Nil(t, defaultArray) + assert.NotNil(t, err) + } else { + assert.NotNil(t, arrays) + assert.NotNil(t, mapper) + assert.NotNil(t, defaultArray) + assert.Nil(t, err) + } } }) } diff --git a/internal/common/testdata/invalid-client.yaml b/internal/common/testdata/invalid-client.yaml new file mode 100644 index 0000000..4b82dd3 --- /dev/null +++ b/internal/common/testdata/invalid-client.yaml @@ -0,0 +1,6 @@ +arrays: + - endpoint: "https://invalid_endpoint" + globalID: "array1" + username: "user" + password: "password" + skipCertificateValidation: true diff --git a/internal/common/testdata/nil-array.yaml b/internal/common/testdata/nil-array.yaml new file mode 100644 index 0000000..e0c73e2 --- /dev/null +++ b/internal/common/testdata/nil-array.yaml @@ -0,0 +1,2 @@ +arrays: + - # Nil entry From e8e952492e28f61970f9ae9ea89418da0392c48a Mon Sep 17 00:00:00 2001 From: H Date: Wed, 27 Nov 2024 16:30:59 +0530 Subject: [PATCH 5/7] Lint issue --- internal/common/testdata/nil-array.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/common/testdata/nil-array.yaml b/internal/common/testdata/nil-array.yaml index e0c73e2..55424c9 100644 --- a/internal/common/testdata/nil-array.yaml +++ b/internal/common/testdata/nil-array.yaml @@ -1,2 +1,2 @@ arrays: - - # Nil entry + - # Nil entry \ No newline at end of file From 310cc6e80a706bf63580821dcc810b1f55cfd37f Mon Sep 17 00:00:00 2001 From: H Date: Wed, 27 Nov 2024 16:52:02 +0530 Subject: [PATCH 6/7] Lint issue --- internal/common/testdata/empty-array.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/common/testdata/empty-array.yaml b/internal/common/testdata/empty-array.yaml index b1b0f79..5501ca5 100644 --- a/internal/common/testdata/empty-array.yaml +++ b/internal/common/testdata/empty-array.yaml @@ -1 +1 @@ -arrays: [] \ No newline at end of file +arrays: [] From e790d5779297cd8cea2148d94fdd4aa99bbb0dd4 Mon Sep 17 00:00:00 2001 From: H Date: Wed, 27 Nov 2024 16:55:57 +0530 Subject: [PATCH 7/7] Lint issue --- internal/common/testdata/nil-array.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/common/testdata/nil-array.yaml b/internal/common/testdata/nil-array.yaml index 55424c9..6e6b7d6 100644 --- a/internal/common/testdata/nil-array.yaml +++ b/internal/common/testdata/nil-array.yaml @@ -1,2 +1,2 @@ arrays: - - # Nil entry \ No newline at end of file + -