Skip to content

Commit 4e1ac26

Browse files
authored
test: Add cmd tests (#619)
1 parent 681a661 commit 4e1ac26

File tree

6 files changed

+134
-4
lines changed

6 files changed

+134
-4
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
go-version-file: "go.mod"
2323

2424
- name: Lint
25-
run: make lint
25+
run: make lint-ci
2626

2727
test:
2828
name: test

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ test-ci:
1818
@sed -i '/pkg\/mocks/d' coverage.out
1919
@sed -i '/pkg\/export\/archive.go/d' coverage.out
2020
@sed -i '/pkg\/export\/export.go/d' coverage.out
21-
@sed -i '/cmd/d' coverage.out
2221
@sed -i '/uor/d' coverage.out
2322
@sed -i '/log/d' coverage.out
2423
go tool cover -func coverage.out

cmd/cmd_suite_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cmd_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestTypes(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "cmd Suite")
13+
}

cmd/decrypt_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
var _ = Describe("Decrypt Command", func() {
12+
var testFile string
13+
var testContent string
14+
15+
BeforeEach(func() {
16+
// Create a temporary test file
17+
testFile = "temp-secret.yaml"
18+
19+
// Create test content with encrypted data
20+
testContent = `apiVersion: v1
21+
kind: Secret
22+
metadata:
23+
name: test-secret
24+
namespace: default
25+
type: Opaque
26+
data:
27+
username: KUBEXPORTER_AES@f6MMcnyTt4Tm4zGotyhAzPLjWSeV42ke8hu93AJG251W4Ew17RI6pA==
28+
password: KUBEXPORTER_AES@f6MMcnyTt4Tm4zGosDdI2vK9CDmU2W0eNvcVMbvee0S/vlFO6+Vf+w==
29+
stringData:
30+
api-key: KUBEXPORTER_AES@f6MMcnyTt4Tm4zGooBVt0vT6QS6H3RFI6AR8sM7D8/ElvOzygKsuM3ZT5nCfmg==
31+
token: KUBEXPORTER_AES@f6MMcnyTt4Tm4zGovgkj0/TtHiqDmUhM5hg/X5Nsgw07ZtUsFnYpG4OnGg==`
32+
33+
err := os.WriteFile(testFile, []byte(testContent), 0o644)
34+
Expect(err).NotTo(HaveOccurred())
35+
})
36+
37+
AfterEach(func() {
38+
os.Remove(testFile)
39+
})
40+
41+
It("should decrypt the file successfully", func() {
42+
cmd := &cobra.Command{}
43+
cmd.AddCommand(decrypt)
44+
cmd.SetArgs([]string{"decrypt", testFile, "--aes-key", "1234567890123456"})
45+
46+
err := cmd.Execute()
47+
Expect(err).NotTo(HaveOccurred())
48+
49+
// Verify the file was decrypted
50+
content, err := os.ReadFile(testFile)
51+
Expect(err).NotTo(HaveOccurred())
52+
53+
// Check that encrypted fields were decrypted
54+
Expect(string(content)).To(ContainSubstring("dXNlcm5hbWU=")) // base64 encoded "username"
55+
Expect(string(content)).To(ContainSubstring("cGFzc3dvcmQ=")) // base64 encoded "password"
56+
Expect(string(content)).To(ContainSubstring("secret-api-key-123"))
57+
Expect(string(content)).To(ContainSubstring("my-secret-token"))
58+
Expect(string(content)).NotTo(ContainSubstring("KUBEXPORTER_AES@"))
59+
})
60+
})

cmd/encrypt_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
var _ = Describe("Encrypt Command", func() {
12+
var testFile string
13+
14+
BeforeEach(func() {
15+
testFile = "temp-secret.yaml"
16+
testContent := `apiVersion: v1
17+
kind: Secret
18+
metadata:
19+
name: test-secret
20+
namespace: default
21+
type: Opaque
22+
data:
23+
username: dXNlcm5hbWU=
24+
password: cGFzc3dvcmQ=
25+
stringData:
26+
api-key: "secret-api-key-123"
27+
token: "my-secret-token"`
28+
29+
err := os.WriteFile(testFile, []byte(testContent), 0o644)
30+
Expect(err).NotTo(HaveOccurred())
31+
})
32+
33+
AfterEach(func() {
34+
os.Remove(testFile)
35+
})
36+
37+
It("should encrypt and decrypt a file", func() {
38+
cmd := &cobra.Command{}
39+
cmd.AddCommand(encrypt)
40+
cmd.AddCommand(decrypt)
41+
cmd.SetArgs([]string{"encrypt", testFile, "--aes-key", "1234567890123456"})
42+
43+
err := cmd.Execute()
44+
Expect(err).NotTo(HaveOccurred())
45+
46+
content, err := os.ReadFile(testFile)
47+
Expect(err).NotTo(HaveOccurred())
48+
Expect(string(content)).To(ContainSubstring("KUBEXPORTER_AES@"))
49+
50+
cmd.SetArgs([]string{"decrypt", testFile, "--aes-key", "1234567890123456"})
51+
err = cmd.Execute()
52+
Expect(err).NotTo(HaveOccurred())
53+
54+
content, err = os.ReadFile(testFile)
55+
Expect(err).NotTo(HaveOccurred())
56+
Expect(string(content)).To(ContainSubstring("apiVersion: v1"))
57+
Expect(string(content)).To(ContainSubstring("kind: Secret"))
58+
Expect(string(content)).To(ContainSubstring("secret-api-key-123"))
59+
})
60+
})

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ require (
1616
github.com/onsi/gomega v1.38.0
1717
github.com/spf13/cobra v1.9.1
1818
github.com/spf13/pflag v1.0.7
19-
github.com/stretchr/testify v1.10.0
2019
github.com/vardius/worker-pool/v2 v2.1.0
2120
github.com/vbauerster/mpb/v8 v8.10.2
2221
go.uber.org/mock v0.5.2
@@ -101,7 +100,6 @@ require (
101100
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
102101
github.com/philhofer/fwd v1.2.0 // indirect
103102
github.com/pkg/errors v0.9.1 // indirect
104-
github.com/pmezard/go-difflib v1.0.0 // indirect
105103
github.com/rivo/uniseg v0.4.7 // indirect
106104
github.com/rs/xid v1.6.0 // indirect
107105
github.com/russross/blackfriday/v2 v2.1.0 // indirect

0 commit comments

Comments
 (0)