Skip to content

Commit 9684b34

Browse files
authored
chore(tests): add unit tests for cmd/template.go, cmd/util.go and cmd/version.go (argoproj-labs#765)
Signed-off-by: Mangaal <[email protected]>
1 parent d8d3b01 commit 9684b34

File tree

5 files changed

+275
-13
lines changed

5 files changed

+275
-13
lines changed

cmd/template.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/argoproj-labs/argocd-image-updater/pkg/argocd"
1010
"github.com/argoproj-labs/argocd-image-updater/pkg/common"
1111
"github.com/argoproj-labs/argocd-image-updater/pkg/image"
12-
"github.com/argoproj-labs/argocd-image-updater/pkg/log"
1312
"github.com/argoproj-labs/argocd-image-updater/pkg/tag"
1413

1514
"github.com/spf13/cobra"
@@ -39,12 +38,14 @@ If PATH is not given, will show you the default message that is used.
3938
commitMessageTemplatePath = args[0]
4039
tplData, err := os.ReadFile(commitMessageTemplatePath)
4140
if err != nil {
42-
log.Fatalf("%v", err)
41+
fmt.Fprintf(cmd.ErrOrStderr(), "%v", err)
42+
return
4343
}
4444
tplStr = string(tplData)
4545
}
4646
if tpl, err = template.New("commitMessage").Parse(tplStr); err != nil {
47-
log.Fatalf("could not parse commit message template: %v", err)
47+
fmt.Fprintf(cmd.ErrOrStderr(), "could not parse commit message template: %v", err)
48+
return
4849
}
4950
chL := []argocd.ChangeEntry{
5051
{
@@ -58,7 +59,7 @@ If PATH is not given, will show you the default message that is used.
5859
NewTag: tag.NewImageTag("", time.Now(), "sha256:7aa7a5359173d05b63cfd682e3c38487f3cb4f7f1d60659fe59fab1505977d4c"),
5960
},
6061
}
61-
fmt.Printf("%s\n", argocd.TemplateCommitMessage(tpl, "example-app", chL))
62+
fmt.Fprintf(cmd.OutOrStdout(), "%s\n", argocd.TemplateCommitMessage(tpl, "example-app", chL))
6263
},
6364
}
6465
return runCmd

cmd/template_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestNewTemplateCommandWithEmptyArgs(t *testing.T) {
12+
13+
defaultExpectedOutput := `build: automatic update of example-app
14+
15+
updates image example/example tag '1.0.0' to '1.0.1'
16+
updates image example/updater tag 'sha256:01d09d19c2139a46aebfb577780d123d7396e97201bc7ead210a2ebff8239dee' to 'sha256:7aa7a5359173d05b63cfd682e3c38487f3cb4f7f1d60659fe59fab1505977d4c'
17+
18+
`
19+
cmd := newTemplateCommand()
20+
buf := new(bytes.Buffer)
21+
args := []string{}
22+
cmd.SetOut(buf)
23+
cmd.SetArgs(args)
24+
err := cmd.Execute()
25+
if err != nil {
26+
t.Fatalf("could not execute command: %v", err)
27+
}
28+
assert.NoError(t, err)
29+
assert.Equal(t, defaultExpectedOutput, buf.String())
30+
31+
}
32+
33+
func TestNewTemplateCommandWithCustomTemplate(t *testing.T) {
34+
35+
testTemplate := `Custom Commit Message:
36+
App: {{.AppName}}
37+
{{- range .AppChanges }}
38+
- {{.Image}}: {{.OldTag}} -> {{.NewTag}}
39+
{{- end }}`
40+
41+
expectedOutput := `Custom Commit Message:
42+
App: example-app
43+
- example/example: 1.0.0 -> 1.0.1
44+
- example/updater: sha256:01d09d19c2139a46aebfb577780d123d7396e97201bc7ead210a2ebff8239dee -> sha256:7aa7a5359173d05b63cfd682e3c38487f3cb4f7f1d60659fe59fab1505977d4c
45+
`
46+
47+
// Create a temporary file to hold the test template
48+
tempFile, err := os.CreateTemp("", "test-template.tmpl")
49+
if err != nil {
50+
t.Fatalf("could not create temp file: %v", err)
51+
}
52+
defer os.Remove(tempFile.Name())
53+
_, err = tempFile.WriteString(testTemplate)
54+
if err != nil {
55+
t.Fatalf("could not write to temp file: %v", err)
56+
}
57+
tempFile.Close()
58+
59+
cmd := newTemplateCommand()
60+
buf := new(bytes.Buffer)
61+
args := []string{tempFile.Name()}
62+
cmd.SetOut(buf)
63+
cmd.SetArgs(args)
64+
err = cmd.Execute()
65+
if err != nil {
66+
t.Fatalf("could not execute command: %v", err)
67+
}
68+
assert.Equal(t, expectedOutput, buf.String())
69+
}
70+
71+
func TestNewTemplateCommandWithInvalidTemplate(t *testing.T) {
72+
73+
testTemplate := `Custom Commit Message:
74+
App: {{.AppName}}
75+
{{- range .AppChanges }}
76+
- {{.Image}}: {{.OldTag}} -> {{.NewTag}}
77+
{{- end`
78+
79+
// Create a temporary file to hold the test template
80+
tempFile, err := os.CreateTemp("", "test-template.tmpl")
81+
if err != nil {
82+
t.Fatalf("could not create temp file: %v", err)
83+
}
84+
defer os.Remove(tempFile.Name())
85+
_, err = tempFile.WriteString(testTemplate)
86+
if err != nil {
87+
t.Fatalf("could not write to temp file: %v", err)
88+
}
89+
tempFile.Close()
90+
91+
cmd := newTemplateCommand()
92+
buf := new(bytes.Buffer)
93+
args := []string{tempFile.Name()}
94+
cmd.SetErr(buf)
95+
cmd.SetArgs(args)
96+
err = cmd.Execute()
97+
if err != nil {
98+
t.Fatalf("could not execute command: %v", err)
99+
}
100+
assert.Contains(t, buf.String(), "could not parse commit message template")
101+
}
102+
103+
func TestNewTemplateCommandWithInvalidPath(t *testing.T) {
104+
105+
cmd := newTemplateCommand()
106+
buf := new(bytes.Buffer)
107+
args := []string{"test-template.tmpl"}
108+
cmd.SetErr(buf)
109+
cmd.SetArgs(args)
110+
err := cmd.Execute()
111+
if err != nil {
112+
t.Fatalf("could not execute command: %v", err)
113+
}
114+
assert.Contains(t, buf.String(), "no such file or directory")
115+
}

cmd/util_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestGetKubeConfig(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
namespace string
16+
configPath string
17+
expectError bool
18+
expectedNS string
19+
}{
20+
{
21+
name: "Valid KubeConfig",
22+
namespace: "",
23+
configPath: "../test/testdata/kubernetes/config",
24+
expectError: false,
25+
expectedNS: "default",
26+
},
27+
{
28+
name: "Invalid KubeConfig Path",
29+
namespace: "",
30+
configPath: "invalid/kubernetes/config",
31+
expectError: true,
32+
},
33+
{
34+
name: "Valid KubeConfig with Namespace",
35+
namespace: "argocd",
36+
configPath: "../test/testdata/kubernetes/config",
37+
expectError: false,
38+
expectedNS: "argocd",
39+
},
40+
}
41+
42+
for _, tt := range tests {
43+
t.Run(tt.name, func(t *testing.T) {
44+
client, err := getKubeConfig(context.TODO(), tt.namespace, tt.configPath)
45+
if tt.expectError {
46+
require.Error(t, err)
47+
} else {
48+
require.NoError(t, err)
49+
assert.NotNil(t, client)
50+
assert.Equal(t, tt.expectedNS, client.Namespace)
51+
}
52+
})
53+
}
54+
}
55+
func TestGetPrintableInterval(t *testing.T) {
56+
tests := []struct {
57+
input time.Duration
58+
expected string
59+
}{
60+
{0, "once"},
61+
{time.Second, "1s"},
62+
{time.Minute, "1m0s"},
63+
{time.Hour, "1h0m0s"},
64+
{time.Hour + 30*time.Minute, "1h30m0s"},
65+
{24 * time.Hour, "24h0m0s"},
66+
}
67+
68+
for _, test := range tests {
69+
result := getPrintableInterval(test.input)
70+
if result != test.expected {
71+
t.Errorf("For input %v, expected %v, but got %v", test.input, test.expected, result)
72+
}
73+
}
74+
}
75+
76+
func TestGetPrintableHealthPort(t *testing.T) {
77+
testPorts := []struct {
78+
input int
79+
expected string
80+
}{
81+
{0, "off"},
82+
{8080, "8080"},
83+
{9090, "9090"},
84+
}
85+
86+
for _, testPort := range testPorts {
87+
result := getPrintableHealthPort(testPort.input)
88+
89+
if result != testPort.expected {
90+
t.Errorf("For input %v, expected %v, but got %v", testPort.input, testPort.expected, result)
91+
}
92+
}
93+
94+
}

cmd/version.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/argoproj-labs/argocd-image-updater/pkg/version"
7-
86
"github.com/spf13/cobra"
7+
8+
"github.com/argoproj-labs/argocd-image-updater/pkg/version"
99
)
1010

1111
// newVersionCommand implements "version" command
@@ -15,15 +15,16 @@ func newVersionCommand() *cobra.Command {
1515
Use: "version",
1616
Short: "Display version information",
1717
RunE: func(cmd *cobra.Command, args []string) error {
18+
out := cmd.OutOrStdout()
1819
if !short {
19-
fmt.Printf("%s\n", version.Useragent())
20-
fmt.Printf(" BuildDate: %s\n", version.BuildDate())
21-
fmt.Printf(" GitCommit: %s\n", version.GitCommit())
22-
fmt.Printf(" GoVersion: %s\n", version.GoVersion())
23-
fmt.Printf(" GoCompiler: %s\n", version.GoCompiler())
24-
fmt.Printf(" Platform: %s\n", version.GoPlatform())
20+
fmt.Fprintf(out, "%s\n", version.Useragent())
21+
fmt.Fprintf(out, " BuildDate: %s\n", version.BuildDate())
22+
fmt.Fprintf(out, " GitCommit: %s\n", version.GitCommit())
23+
fmt.Fprintf(out, " GoVersion: %s\n", version.GoVersion())
24+
fmt.Fprintf(out, " GoCompiler: %s\n", version.GoCompiler())
25+
fmt.Fprintf(out, " Platform: %s\n", version.GoPlatform())
2526
} else {
26-
fmt.Printf("%s\n", version.Version())
27+
fmt.Fprintf(out, "%s\n", version.Version())
2728
}
2829
return nil
2930
},

cmd/version_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
9+
"github.com/argoproj-labs/argocd-image-updater/pkg/version"
10+
)
11+
12+
// Helper function to create the expected full version output
13+
func fullVersionOutput() string {
14+
return version.Useragent() + "\n" +
15+
" BuildDate: " + version.BuildDate() + "\n" +
16+
" GitCommit: " + version.GitCommit() + "\n" +
17+
" GoVersion: " + version.GoVersion() + "\n" +
18+
" GoCompiler: " + version.GoCompiler() + "\n" +
19+
" Platform: " + version.GoPlatform() + "\n"
20+
}
21+
22+
func TestNewVersionCommand(t *testing.T) {
23+
tests := []struct {
24+
name string
25+
args []string
26+
expected string
27+
}{
28+
{
29+
name: "default output",
30+
args: []string{},
31+
expected: fullVersionOutput(),
32+
},
33+
{
34+
name: "short flag output",
35+
args: []string{"--short"},
36+
expected: version.Version() + "\n",
37+
},
38+
}
39+
40+
for _, tt := range tests {
41+
t.Run(tt.name, func(t *testing.T) {
42+
cmd := newVersionCommand()
43+
buf := new(bytes.Buffer)
44+
cmd.SetOut(buf)
45+
cmd.SetArgs(tt.args)
46+
err := cmd.Execute()
47+
assert.NoError(t, err)
48+
assert.Equal(t, tt.expected, buf.String())
49+
})
50+
}
51+
}

0 commit comments

Comments
 (0)