diff --git a/internal/common/common_test.go b/internal/common/common_test.go index 2c61f6b..0973a08 100644 --- a/internal/common/common_test.go +++ b/internal/common/common_test.go @@ -43,6 +43,15 @@ 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 + }, + "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 + }, } for name, test := range tests { @@ -56,16 +65,29 @@ func Test_Run(t *testing.T) { arrays, mapper, defaultArray, err := common.GetPowerStoreArrays(filePath, logger) - 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/empty-array.yaml b/internal/common/testdata/empty-array.yaml new file mode 100644 index 0000000..5501ca5 --- /dev/null +++ b/internal/common/testdata/empty-array.yaml @@ -0,0 +1 @@ +arrays: [] 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..6e6b7d6 --- /dev/null +++ b/internal/common/testdata/nil-array.yaml @@ -0,0 +1,2 @@ +arrays: + - 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) + } + }) + } +} 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") + }) }