Skip to content

Commit

Permalink
Add basic unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
DalianisDim committed Jun 5, 2023
1 parent 6ed2566 commit ed38958
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 18 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
78 changes: 78 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright © 2023 Dimitris Dalianis <[email protected]>
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)
}
})
}
}
20 changes: 4 additions & 16 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ed38958

Please sign in to comment.