diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7f79983..1426427 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -42,12 +42,13 @@ jobs: run: | go get . + - name: Test with the Go CLI + run: go test ./... -v + - name: Build run: | ${{ matrix.build_params }} go build --ldflags="-X 'keyvalDetector/cmd.buildTimeVersion=${{ github.ref_name }}'" - - name: Test with the Go CLI - run: go test - uses: svenstaro/upload-release-action@v2 with: diff --git a/cmd/cmd_test.go b/cmd/cmd_test.go new file mode 100644 index 0000000..40868a0 --- /dev/null +++ b/cmd/cmd_test.go @@ -0,0 +1,78 @@ +/* +Copyright © 2023 Dimitris Dalianis +This file is part of CLI application keyvalDetector +*/ +package cmd + +import "testing" + +func TestIsSystemConfigMap(t *testing.T) { + testCases := []struct { + name string + element string + expected bool + }{ + { + name: "Empty string", + element: "", + expected: false, + }, + { + name: "Non system configmap", + element: "my-configmap", + expected: false, + }, + { + name: "System configmap", + element: "cluster-info", + expected: true, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := isSystemConfigMap(tc.element) + if result != tc.expected { + t.Errorf("Expected %v, but got %v", tc.expected, result) + } + }) + } + +} + +func TestContains(t *testing.T) { + testCases := []struct { + name string + input []string + element string + expected bool + }{ + { + name: "Empty slice", + input: []string{}, + element: "abc", + expected: false, + }, + { + name: "Element present in slice", + input: []string{"abc", "def", "ghi"}, + element: "def", + expected: true, + }, + { + name: "Element not present in slice", + input: []string{"abc", "def", "ghi"}, + element: "xyz", + expected: false, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := contains(tc.input, tc.element) + if result != tc.expected { + t.Errorf("Expected %v, but got %v", tc.expected, result) + } + }) + } +} diff --git a/cmd/root.go b/cmd/root.go index 4527107..206bd64 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -229,25 +229,13 @@ func keyvalDetector() error { // } func isSystemConfigMap(configmap string) bool { - defaultConfigMaps := []string{"kube-root-ca.crt", "cluster-info", "kubelet-config", "kubeadm-config"} - - for _, value := range defaultConfigMaps { - if configmap == value { - return true - } - } - return false + systemConfigMaps := []string{"kube-root-ca.crt", "cluster-info", "kubelet-config", "kubeadm-config"} + return contains(systemConfigMaps, configmap) } func isSystemSecret(secret string) bool { - defaultSecrets := []string{"foobar"} - - for _, value := range defaultSecrets { - if secret == value { - return true - } - } - return false + systemSecrets := []string{"foobar"} + return contains(systemSecrets, secret) } func getCurrentK8sContext(kubeConfigPath string) string {