diff options
| author | Mangaal <44372157+Mangaal@users.noreply.github.com> | 2024-07-02 18:34:26 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-02 09:04:26 -0400 |
| commit | 9684b34c64da7e43886cf2ec2586c14f5da6e3b1 (patch) | |
| tree | 2059f2cf720e3d31b2122d9f45e3f46fc39c8448 | |
| parent | d8d3b018247be19cb4df90230327b0c4bb235c96 (diff) | |
chore(tests): add unit tests for cmd/template.go, cmd/util.go and cmd/version.go (#765)
Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>
| -rw-r--r-- | cmd/template.go | 9 | ||||
| -rw-r--r-- | cmd/template_test.go | 115 | ||||
| -rw-r--r-- | cmd/util_test.go | 94 | ||||
| -rw-r--r-- | cmd/version.go | 19 | ||||
| -rw-r--r-- | cmd/version_test.go | 51 |
5 files changed, 275 insertions, 13 deletions
diff --git a/cmd/template.go b/cmd/template.go index 4c6c925..b135071 100644 --- a/cmd/template.go +++ b/cmd/template.go @@ -9,7 +9,6 @@ import ( "github.com/argoproj-labs/argocd-image-updater/pkg/argocd" "github.com/argoproj-labs/argocd-image-updater/pkg/common" "github.com/argoproj-labs/argocd-image-updater/pkg/image" - "github.com/argoproj-labs/argocd-image-updater/pkg/log" "github.com/argoproj-labs/argocd-image-updater/pkg/tag" "github.com/spf13/cobra" @@ -39,12 +38,14 @@ If PATH is not given, will show you the default message that is used. commitMessageTemplatePath = args[0] tplData, err := os.ReadFile(commitMessageTemplatePath) if err != nil { - log.Fatalf("%v", err) + fmt.Fprintf(cmd.ErrOrStderr(), "%v", err) + return } tplStr = string(tplData) } if tpl, err = template.New("commitMessage").Parse(tplStr); err != nil { - log.Fatalf("could not parse commit message template: %v", err) + fmt.Fprintf(cmd.ErrOrStderr(), "could not parse commit message template: %v", err) + return } chL := []argocd.ChangeEntry{ { @@ -58,7 +59,7 @@ If PATH is not given, will show you the default message that is used. NewTag: tag.NewImageTag("", time.Now(), "sha256:7aa7a5359173d05b63cfd682e3c38487f3cb4f7f1d60659fe59fab1505977d4c"), }, } - fmt.Printf("%s\n", argocd.TemplateCommitMessage(tpl, "example-app", chL)) + fmt.Fprintf(cmd.OutOrStdout(), "%s\n", argocd.TemplateCommitMessage(tpl, "example-app", chL)) }, } return runCmd diff --git a/cmd/template_test.go b/cmd/template_test.go new file mode 100644 index 0000000..3d9e127 --- /dev/null +++ b/cmd/template_test.go @@ -0,0 +1,115 @@ +package main + +import ( + "bytes" + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewTemplateCommandWithEmptyArgs(t *testing.T) { + + defaultExpectedOutput := `build: automatic update of example-app + +updates image example/example tag '1.0.0' to '1.0.1' +updates image example/updater tag 'sha256:01d09d19c2139a46aebfb577780d123d7396e97201bc7ead210a2ebff8239dee' to 'sha256:7aa7a5359173d05b63cfd682e3c38487f3cb4f7f1d60659fe59fab1505977d4c' + +` + cmd := newTemplateCommand() + buf := new(bytes.Buffer) + args := []string{} + cmd.SetOut(buf) + cmd.SetArgs(args) + err := cmd.Execute() + if err != nil { + t.Fatalf("could not execute command: %v", err) + } + assert.NoError(t, err) + assert.Equal(t, defaultExpectedOutput, buf.String()) + +} + +func TestNewTemplateCommandWithCustomTemplate(t *testing.T) { + + testTemplate := `Custom Commit Message: +App: {{.AppName}} +{{- range .AppChanges }} +- {{.Image}}: {{.OldTag}} -> {{.NewTag}} +{{- end }}` + + expectedOutput := `Custom Commit Message: +App: example-app +- example/example: 1.0.0 -> 1.0.1 +- example/updater: sha256:01d09d19c2139a46aebfb577780d123d7396e97201bc7ead210a2ebff8239dee -> sha256:7aa7a5359173d05b63cfd682e3c38487f3cb4f7f1d60659fe59fab1505977d4c +` + + // Create a temporary file to hold the test template + tempFile, err := os.CreateTemp("", "test-template.tmpl") + if err != nil { + t.Fatalf("could not create temp file: %v", err) + } + defer os.Remove(tempFile.Name()) + _, err = tempFile.WriteString(testTemplate) + if err != nil { + t.Fatalf("could not write to temp file: %v", err) + } + tempFile.Close() + + cmd := newTemplateCommand() + buf := new(bytes.Buffer) + args := []string{tempFile.Name()} + cmd.SetOut(buf) + cmd.SetArgs(args) + err = cmd.Execute() + if err != nil { + t.Fatalf("could not execute command: %v", err) + } + assert.Equal(t, expectedOutput, buf.String()) +} + +func TestNewTemplateCommandWithInvalidTemplate(t *testing.T) { + + testTemplate := `Custom Commit Message: +App: {{.AppName}} +{{- range .AppChanges }} +- {{.Image}}: {{.OldTag}} -> {{.NewTag}} +{{- end` + + // Create a temporary file to hold the test template + tempFile, err := os.CreateTemp("", "test-template.tmpl") + if err != nil { + t.Fatalf("could not create temp file: %v", err) + } + defer os.Remove(tempFile.Name()) + _, err = tempFile.WriteString(testTemplate) + if err != nil { + t.Fatalf("could not write to temp file: %v", err) + } + tempFile.Close() + + cmd := newTemplateCommand() + buf := new(bytes.Buffer) + args := []string{tempFile.Name()} + cmd.SetErr(buf) + cmd.SetArgs(args) + err = cmd.Execute() + if err != nil { + t.Fatalf("could not execute command: %v", err) + } + assert.Contains(t, buf.String(), "could not parse commit message template") +} + +func TestNewTemplateCommandWithInvalidPath(t *testing.T) { + + cmd := newTemplateCommand() + buf := new(bytes.Buffer) + args := []string{"test-template.tmpl"} + cmd.SetErr(buf) + cmd.SetArgs(args) + err := cmd.Execute() + if err != nil { + t.Fatalf("could not execute command: %v", err) + } + assert.Contains(t, buf.String(), "no such file or directory") +} diff --git a/cmd/util_test.go b/cmd/util_test.go new file mode 100644 index 0000000..6875307 --- /dev/null +++ b/cmd/util_test.go @@ -0,0 +1,94 @@ +package main + +import ( + "context" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetKubeConfig(t *testing.T) { + tests := []struct { + name string + namespace string + configPath string + expectError bool + expectedNS string + }{ + { + name: "Valid KubeConfig", + namespace: "", + configPath: "../test/testdata/kubernetes/config", + expectError: false, + expectedNS: "default", + }, + { + name: "Invalid KubeConfig Path", + namespace: "", + configPath: "invalid/kubernetes/config", + expectError: true, + }, + { + name: "Valid KubeConfig with Namespace", + namespace: "argocd", + configPath: "../test/testdata/kubernetes/config", + expectError: false, + expectedNS: "argocd", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + client, err := getKubeConfig(context.TODO(), tt.namespace, tt.configPath) + if tt.expectError { + require.Error(t, err) + } else { + require.NoError(t, err) + assert.NotNil(t, client) + assert.Equal(t, tt.expectedNS, client.Namespace) + } + }) + } +} +func TestGetPrintableInterval(t *testing.T) { + tests := []struct { + input time.Duration + expected string + }{ + {0, "once"}, + {time.Second, "1s"}, + {time.Minute, "1m0s"}, + {time.Hour, "1h0m0s"}, + {time.Hour + 30*time.Minute, "1h30m0s"}, + {24 * time.Hour, "24h0m0s"}, + } + + for _, test := range tests { + result := getPrintableInterval(test.input) + if result != test.expected { + t.Errorf("For input %v, expected %v, but got %v", test.input, test.expected, result) + } + } +} + +func TestGetPrintableHealthPort(t *testing.T) { + testPorts := []struct { + input int + expected string + }{ + {0, "off"}, + {8080, "8080"}, + {9090, "9090"}, + } + + for _, testPort := range testPorts { + result := getPrintableHealthPort(testPort.input) + + if result != testPort.expected { + t.Errorf("For input %v, expected %v, but got %v", testPort.input, testPort.expected, result) + } + } + +} diff --git a/cmd/version.go b/cmd/version.go index 289cd90..b14ff30 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -3,9 +3,9 @@ package main import ( "fmt" - "github.com/argoproj-labs/argocd-image-updater/pkg/version" - "github.com/spf13/cobra" + + "github.com/argoproj-labs/argocd-image-updater/pkg/version" ) // newVersionCommand implements "version" command @@ -15,15 +15,16 @@ func newVersionCommand() *cobra.Command { Use: "version", Short: "Display version information", RunE: func(cmd *cobra.Command, args []string) error { + out := cmd.OutOrStdout() if !short { - fmt.Printf("%s\n", version.Useragent()) - fmt.Printf(" BuildDate: %s\n", version.BuildDate()) - fmt.Printf(" GitCommit: %s\n", version.GitCommit()) - fmt.Printf(" GoVersion: %s\n", version.GoVersion()) - fmt.Printf(" GoCompiler: %s\n", version.GoCompiler()) - fmt.Printf(" Platform: %s\n", version.GoPlatform()) + fmt.Fprintf(out, "%s\n", version.Useragent()) + fmt.Fprintf(out, " BuildDate: %s\n", version.BuildDate()) + fmt.Fprintf(out, " GitCommit: %s\n", version.GitCommit()) + fmt.Fprintf(out, " GoVersion: %s\n", version.GoVersion()) + fmt.Fprintf(out, " GoCompiler: %s\n", version.GoCompiler()) + fmt.Fprintf(out, " Platform: %s\n", version.GoPlatform()) } else { - fmt.Printf("%s\n", version.Version()) + fmt.Fprintf(out, "%s\n", version.Version()) } return nil }, diff --git a/cmd/version_test.go b/cmd/version_test.go new file mode 100644 index 0000000..76c6532 --- /dev/null +++ b/cmd/version_test.go @@ -0,0 +1,51 @@ +package main + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/argoproj-labs/argocd-image-updater/pkg/version" +) + +// Helper function to create the expected full version output +func fullVersionOutput() string { + return version.Useragent() + "\n" + + " BuildDate: " + version.BuildDate() + "\n" + + " GitCommit: " + version.GitCommit() + "\n" + + " GoVersion: " + version.GoVersion() + "\n" + + " GoCompiler: " + version.GoCompiler() + "\n" + + " Platform: " + version.GoPlatform() + "\n" +} + +func TestNewVersionCommand(t *testing.T) { + tests := []struct { + name string + args []string + expected string + }{ + { + name: "default output", + args: []string{}, + expected: fullVersionOutput(), + }, + { + name: "short flag output", + args: []string{"--short"}, + expected: version.Version() + "\n", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cmd := newVersionCommand() + buf := new(bytes.Buffer) + cmd.SetOut(buf) + cmd.SetArgs(tt.args) + err := cmd.Execute() + assert.NoError(t, err) + assert.Equal(t, tt.expected, buf.String()) + }) + } +} |
