diff --git a/pkg/argoutil/api_test.go b/pkg/argoutil/api_test.go new file mode 100644 index 000000000..5ddf09d9e --- /dev/null +++ b/pkg/argoutil/api_test.go @@ -0,0 +1,57 @@ +package argoutil + +/*import ( + argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1" + "github.com/argoproj-labs/argocd-operator/common" + "k8s.io/client-go/rest" + "reflect" + "testing" + + "k8s.io/client-go/kubernetes/fake" + "sigs.k8s.io/controller-runtime/pkg/client/config" + "sigs.k8s.io/controller-runtime/pkg/client/fake" +) + +func TestVerifyAPI(t *testing.T) { + k8s := fake.NewSimpleClientset() + + config.GetConfig = func() (*rest.Config, error) { + return &rest.Config{}, nil + } + + defer func() { + config.GetConfig = config.Rea + }() + type args struct { + cr *argoproj.ArgoCD + } + tests := []struct { + name string + args args + want map[string]string + }{ + { + name: "simple annotations", + args: args{ + &argoproj.ArgoCD{ + ObjectMeta: v1.ObjectMeta{ + Name: "foo", + Namespace: "bar", + }, + }, + }, + want: map[string]string{ + "argocds.argoproj.io/name": "foo", + "argocds.argoproj.io/namespace": "bar", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := common.DefaultAnnotations(tt.args.cr.Name, tt.args.cr.Namespace); !reflect.DeepEqual(got, tt.want) { + t.Errorf("DefaultAnnotations() = %v, want %v", got, tt.want) + } + }) + } +} +*/ diff --git a/pkg/argoutil/auth_test.go b/pkg/argoutil/auth_test.go new file mode 100644 index 000000000..8279c3765 --- /dev/null +++ b/pkg/argoutil/auth_test.go @@ -0,0 +1,73 @@ +package argoutil + +import ( + "github.com/stretchr/testify/assert" + corev1 "k8s.io/api/core/v1" + "testing" +) + +func TestGenerateArgoAdminPassword(t *testing.T) { + password, err := GenerateArgoAdminPassword() + assert.NoError(t, err) + assert.NotNil(t, password) +} + +func TestGenerateArgoServerSessionKey(t *testing.T) { + password, err := GenerateArgoServerSessionKey() + assert.NoError(t, err) + assert.NotNil(t, password) +} + +func TestHasArgoAdminPasswordChanged(t *testing.T) { + t.Run("Admin Password Changed", func(t *testing.T) { + old_admin_password, err := GenerateArgoAdminPassword() + if err != nil { + t.Errorf("Error when generating admin password") + + } + old_password := &corev1.Secret{ + Data: map[string][]byte{ + "admin-password": old_admin_password, + }, + } + + new_admin_password, err := GenerateArgoAdminPassword() + if err != nil { + t.Errorf("Error when generating admin password") + + } + new_password := &corev1.Secret{ + Data: map[string][]byte{ + "admin-password": new_admin_password, + }, + } + + got := HasArgoAdminPasswordChanged(old_password, new_password) + if got != true { + t.Errorf("HasAdminPasswordChanged() = %v, want true", got) + } + }) + t.Run("Admin Password Not Changed", func(t *testing.T) { + old_admin_password, err := GenerateArgoAdminPassword() + if err != nil { + t.Errorf("Error when generating admin password") + + } + old_password := &corev1.Secret{ + Data: map[string][]byte{ + "admin-password": old_admin_password, + }, + } + + /*new_password := &corev1.Secret{ + Data: map[string][]byte{ + "admin-password": old_admin_password, + }, + }*/ + + got := HasArgoAdminPasswordChanged(old_password, old_password) + if got != false { + t.Errorf("HasAdminPasswordChanged() = %v, want false", got) + } + }) +} diff --git a/pkg/util/bool_test.go b/pkg/util/bool_test.go new file mode 100644 index 000000000..9bdf5c1d3 --- /dev/null +++ b/pkg/util/bool_test.go @@ -0,0 +1,24 @@ +package util + +import ( + "testing" +) + +func TestBoolPtr(t *testing.T) { + tests := []struct { + name string + value bool + }{ + {"True", true}, + {"False", false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := BoolPtr(tt.value) + if *got != tt.value { + t.Errorf("BoolPtr() = %v", got) + } + }) + } +} diff --git a/pkg/util/env_test.go b/pkg/util/env_test.go index ce65a292a..9e9389f6d 100644 --- a/pkg/util/env_test.go +++ b/pkg/util/env_test.go @@ -1,6 +1,7 @@ package util import ( + "os" "reflect" "testing" @@ -150,3 +151,86 @@ func Test_EnvMerge_testSorted(t *testing.T) { } }) } + +func TestGetEnv(t *testing.T) { + t.Run("Setting Test env", func(t *testing.T) { + key := "TEST_ENV_VAR" + value := "test_value" + err := os.Setenv(key, value) + if err != nil { + t.Errorf("error setting env %v", err) + } + defer func(key string) { + err := os.Unsetenv(key) + if err != nil { + t.Errorf("Error unsetting env %v", err) + } + }(key) + + result := GetEnv(key) + + if result != value { + t.Errorf("GetEnv() result = %v, want %v", result, value) + } + }) +} + +/*func TestCaseInsensitiveGetenv(t *testing.T) { + t.Run("Merge non-existing env", func(t *testing.T) { + key := "TEST_env_VAR" + value := "tEsT_vAlUE1" + key_lowercase := "test_env_var" + err := os.Setenv(key, value) + if err != nil { + t.Errorf("error setting env %v", err) + } + defer func(key string) { + err := os.Unsetenv(key) + if err != nil { + t.Errorf("Error unsetting env %v", err) + } + }(key) + + result_caseinsensitive, result := CaseInsensitiveGetenv(key) + + if result != value || result_caseinsensitive != key_lowercase { + t.Errorf("GetEnv() result = %v, %v, want %v", result_caseinsensitive, result, key_lowercase) + } + }) +}*/ + +func TestProxyEnvVars(t *testing.T) { + t.Run("Merge non-existing env", func(t *testing.T) { + proxyKeys := []string{HttpProxy, HttpsProxy, NoProxy} + values := []string{"http://proxy.example.com", "https://proxy.example.com", "localhost,127.0.0.1"} + for i := 0; i < 3; i++ { + err := os.Setenv(proxyKeys[i], values[i]) + if err != nil { + t.Errorf("error setting env %v", err) + } + defer func(key string) { + err := os.Unsetenv(key) + if err != nil { + t.Errorf("Error unsetting env %v", err) + } + }(proxyKeys[i]) + } + + envVars := ProxyEnvVars() + + expectedEnvVars := []corev1.EnvVar{ + {Name: proxyKeys[0], Value: values[0]}, + {Name: proxyKeys[1], Value: values[1]}, + {Name: proxyKeys[2], Value: values[2]}, + } + + if len(envVars) != len(expectedEnvVars) { + t.Errorf("ProxyEnvVars() result length = %v, want %v", len(envVars), len(expectedEnvVars)) + } + + if !reflect.DeepEqual(envVars, expectedEnvVars) { + t.Errorf("ProxyEnvVars() result = %v, want %v", envVars, expectedEnvVars) + + } + }) +} diff --git a/pkg/util/file_test.go b/pkg/util/file_test.go new file mode 100644 index 000000000..e7de9e6d9 --- /dev/null +++ b/pkg/util/file_test.go @@ -0,0 +1,67 @@ +package util + +import ( + "os" + "testing" +) + +func TestLoadTemplateFile(t *testing.T) { + t.Run("Simple Template", func(t *testing.T) { + testfile, err := os.CreateTemp("", "testing") + if err != nil { + t.Errorf("Error creating temporary file: %v", err) + } + defer func(name string) { + err := os.Remove(name) + if err != nil { + t.Errorf("Error removing temporary file: %v", err) + } + }(testfile.Name()) + + _, err = testfile.Write([]byte("Day and time entered: {{.Day}}, {{.Time}}.")) + + if err != nil { + t.Errorf("Error wriing to temporary file: %v", err) + } + + err = testfile.Close() + if err != nil { + t.Errorf("Error closing temporary file: %v", err) + } + + params := map[string]string{ + "Day": "Monday", + "Time": "12.00", + } + + result, err := LoadTemplateFile(testfile.Name(), params) + + if err != nil { + t.Errorf("LoadTemplateFile() error = %v", err) + } + + expected := "Day and time entered: Monday, 12.00." + + if result != expected { + t.Errorf("LoadTemplateFile() result = %v, want %v", err, expected) + } + }) + t.Run("Non-existent File", func(t *testing.T) { + params := map[string]string{ + "Day": "Monday", + "Time": "12.00", + } + + result, err := LoadTemplateFile("some_path", params) + + if err == nil { + t.Errorf("LoadTemplateFile() should throw error because of non-existent template file") + } + + expected := "" + + if result != expected { + t.Errorf("LoadTemplateFile() result = %v, want %v", err, expected) + } + }) +} diff --git a/pkg/util/log_test.go b/pkg/util/log_test.go index 47fded774..2cf6266bb 100644 --- a/pkg/util/log_test.go +++ b/pkg/util/log_test.go @@ -32,6 +32,16 @@ func TestGetLogLevel(t *testing.T) { "debug", zapcore.DebugLevel, }, + { + "GetLogLevel Panic", + "panic", + zapcore.PanicLevel, + }, + { + "GetLogLevel Fatal", + "fatal", + zapcore.FatalLevel, + }, { "GetLogLevel Default", "unknown", diff --git a/pkg/util/map_test.go b/pkg/util/map_test.go index 23953d8ad..f1e767f1b 100644 --- a/pkg/util/map_test.go +++ b/pkg/util/map_test.go @@ -91,3 +91,73 @@ func TestStringMapKeys(t *testing.T) { }) } } + +func TestByteMapKeys(t *testing.T) { + tests := []struct { + name string + inputMap map[string][]byte + expected []string + }{ + { + "Basic String Keys", + map[string][]byte{"key3": []byte("value3"), "key1": []byte("value1"), "key2": []byte("value2")}, + []string{"key1", "key2", "key3"}, + }, + { + "Empty Map", + map[string][]byte{}, + []string{}, + }, + { + "Map with Single Key", + map[string][]byte{"single_key": []byte("value")}, + []string{"single_key"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := ByteMapKeys(tt.inputMap) + + sort.Strings(got) // Sorting for consistent comparison + + if !reflect.DeepEqual(got, tt.expected) { + t.Errorf("ByteMapKeys() = %v, want %v", got, tt.expected) + } + }) + } +} + +func TestStringSliceToMap(t *testing.T) { + tests := []struct { + name string + inputSlice []string + expected map[string]string + }{ + { + "Basic String Keys", + []string{"key1", "key2", "key3"}, + map[string]string{"key1": "", "key2": "", "key3": ""}, + }, + { + "Empty Slice", + []string{}, + map[string]string{}, + }, + { + "Slice with Single Key", + []string{"single_key"}, + map[string]string{"single_key": ""}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := StringSliceToMap(tt.inputSlice) + + if !reflect.DeepEqual(got, tt.expected) { + t.Errorf("StringSliceToMap() = %v, want %v", got, tt.expected) + } + }) + } +} diff --git a/pkg/util/set_test.go b/pkg/util/set_test.go new file mode 100644 index 000000000..59ccd096b --- /dev/null +++ b/pkg/util/set_test.go @@ -0,0 +1,106 @@ +package util + +import ( + "reflect" + "testing" +) + +func TestSetDiff(t *testing.T) { + tests := []struct { + name string + mapA map[string]string + mapB map[string]string + expected map[string]string + }{ + { + "Basic Diff", + map[string]string{"key1": "value1", "key2": "value2"}, + map[string]string{"key2": "value2", "key3": "value3"}, + map[string]string{"key1": ""}, + }, + { + "Empty Maps", + map[string]string{}, + map[string]string{}, + map[string]string{}, + }, + { + "Extracting All Elements (B Contains A)", + map[string]string{"key1": "value1", "key2": "value2"}, + map[string]string{"key1": "value3", "key2": "value4", "key3": "value4"}, + map[string]string{}, + }, + { + "Maps with Different Types", + map[string]string{"key1": "value1", "key2": "value2"}, + map[string]string{"key3": "value3", "key4": "value4"}, + map[string]string{"key1": "", "key2": ""}, + }, + { + "Substracting from Empty Map A", + map[string]string{}, + map[string]string{"key1": "value1", "key2": "value2"}, + map[string]string{}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := SetDiff(tt.mapA, tt.mapB) + + if !reflect.DeepEqual(got, tt.expected) { + t.Errorf("SetDiff() = %v, want %v", got, tt.expected) + } + }) + } +} + +func TestSetIntersection(t *testing.T) { + tests := []struct { + name string + mapA map[string]string + mapB map[string]string + expected map[string]string + }{ + { + "Basic Intersection", + map[string]string{"key1": "value1", "key2": "value2"}, + map[string]string{"key2": "value2", "key3": "value3"}, + map[string]string{"key2": ""}, + }, + { + "Empty Maps", + map[string]string{}, + map[string]string{}, + map[string]string{}, + }, + { + "Extracting All Elements (B Contains A)", + map[string]string{"key1": "value1", "key2": "value2"}, + map[string]string{"key1": "value3", "key2": "value4", "key3": "value4"}, + map[string]string{"key1": "", "key2": ""}, + }, + { + "No Match", + map[string]string{"key1": "value1", "key2": "value2"}, + map[string]string{"key3": "value3", "key4": "value4"}, + map[string]string{}, + }, + { + "Intersection with Empty Map A", + map[string]string{}, + map[string]string{"key1": "value1", "key2": "value2"}, + map[string]string{}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := SetIntersection(tt.mapA, tt.mapB) + + if !reflect.DeepEqual(got, tt.expected) { + t.Errorf("SetIntersection() = %v, want %v", got, tt.expected) + } + }) + } +} diff --git a/pkg/util/string_test.go b/pkg/util/string_test.go index 5ecbc9cb9..8046418a8 100644 --- a/pkg/util/string_test.go +++ b/pkg/util/string_test.go @@ -121,7 +121,6 @@ func TestGenerateRandomString(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := GenerateRandomString(tt.length) - if err != nil { t.Errorf("GenerateRandomString() error = %v", err) return @@ -138,4 +137,53 @@ func TestGenerateRandomString(t *testing.T) { } }) } + + /*t.Run("Error", func(t *testing.T) { + _, err := GenerateRandomString(-1) + if err == nil { + t.Errorf("Error expected") + } + })*/ +} + +func TestConstructString(t *testing.T) { + tests := []struct { + name string + separator string + parts []string + want string + }{ + {"DotSep", DotSep, []string{"a", "b", "c"}, "a.b.c"}, + {"UnderscoreSep", UnderscoreSep, []string{"a", "b", "c"}, "a_b_c"}, + {"Single char", UnderscoreSep, []string{"a"}, "a"}, + {"Empty parts", DotSep, []string{}, ""}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := ConstructString(tt.separator, tt.parts...) + if got != tt.want { + t.Errorf("ConstructStrings() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestStringPtr(t *testing.T) { + tests := []struct { + name string + value string + }{ + {"Simple string", "apple"}, + {"Emty string", ""}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := StringPtr(tt.value) + if *got != tt.value { + t.Errorf("StringPtr() = %v", got) + } + }) + } }