Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add unit test for server version #10720

Merged
merged 8 commits into from
Oct 19, 2022
33 changes: 17 additions & 16 deletions cmd/argocd/commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewVersionCmd(clientOpts *argocdclient.ClientOptions) *cobra.Command {
fmt.Fprint(cmd.OutOrStdout(), printClientVersion(&cv, short || (output == "short")))
if !client {
sv := getServerVersion(ctx, clientOpts, cmd)
printServerVersion(sv, short || (output == "short"))
fmt.Fprintf(cmd.OutOrStdout(), printServerVersion(sv, short || (output == "short")))
}
default:
log.Fatalf("unknown output format: %s", output)
Expand All @@ -82,7 +82,7 @@ func NewVersionCmd(clientOpts *argocdclient.ClientOptions) *cobra.Command {
return &versionCmd
}

func getServerVersion(ctx context.Context, options *argocdclient.ClientOptions, c *cobra.Command) *version.VersionMessage {
var getServerVersion = func(ctx context.Context, options *argocdclient.ClientOptions, c *cobra.Command) *version.VersionMessage {
conn, versionIf := headless.NewClientOrDie(options, c).NewVersionClientOrDie()
defer argoio.Close(conn)

Expand All @@ -109,44 +109,45 @@ func printClientVersion(version *common.Version, short bool) string {
return output
}

func printServerVersion(version *version.VersionMessage, short bool) {
fmt.Printf("%s: %s\n", "argocd-server", version.Version)
func printServerVersion(version *version.VersionMessage, short bool) string {
output := fmt.Sprintf("%s: %s\n", "argocd-server", version.Version)

if short {
return
return output
}

if version.BuildDate != "" {
fmt.Printf(" BuildDate: %s\n", version.BuildDate)
output += fmt.Sprintf(" BuildDate: %s\n", version.BuildDate)
}
if version.GitCommit != "" {
fmt.Printf(" GitCommit: %s\n", version.GitCommit)
output += fmt.Sprintf(" GitCommit: %s\n", version.GitCommit)
}
if version.GitTreeState != "" {
fmt.Printf(" GitTreeState: %s\n", version.GitTreeState)
output += fmt.Sprintf(" GitTreeState: %s\n", version.GitTreeState)
}
if version.GitTag != "" {
fmt.Printf(" GitTag: %s\n", version.GitTag)
output += fmt.Sprintf(" GitTag: %s\n", version.GitTag)
}
if version.GoVersion != "" {
fmt.Printf(" GoVersion: %s\n", version.GoVersion)
output += fmt.Sprintf(" GoVersion: %s\n", version.GoVersion)
}
if version.Compiler != "" {
fmt.Printf(" Compiler: %s\n", version.Compiler)
output += fmt.Sprintf(" Compiler: %s\n", version.Compiler)
}
if version.Platform != "" {
fmt.Printf(" Platform: %s\n", version.Platform)
output += fmt.Sprintf(" Platform: %s\n", version.Platform)
}
if version.KustomizeVersion != "" {
fmt.Printf(" Kustomize Version: %s\n", version.KustomizeVersion)
output += fmt.Sprintf(" Kustomize Version: %s\n", version.KustomizeVersion)
}
if version.HelmVersion != "" {
fmt.Printf(" Helm Version: %s\n", version.HelmVersion)
output += fmt.Sprintf(" Helm Version: %s\n", version.HelmVersion)
}
if version.KubectlVersion != "" {
fmt.Printf(" Kubectl Version: %s\n", version.KubectlVersion)
output += fmt.Sprintf(" Kubectl Version: %s\n", version.KubectlVersion)
}
if version.JsonnetVersion != "" {
fmt.Printf(" Jsonnet Version: %s\n", version.JsonnetVersion)
output += fmt.Sprintf(" Jsonnet Version: %s\n", version.JsonnetVersion)
}
return output
}
20 changes: 19 additions & 1 deletion cmd/argocd/commands/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package commands

import (
"bytes"
"context"
"testing"

argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient"
"github.com/argoproj/argo-cd/v2/pkg/apiclient/version"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestShortVersion(t *testing.T) {
func TestShortVersionClient(t *testing.T) {
buf := new(bytes.Buffer)
cmd := NewVersionCmd(&argocdclient.ClientOptions{})
cmd.SetOutput(buf)
Expand All @@ -20,3 +23,18 @@ func TestShortVersion(t *testing.T) {
output := buf.String()
assert.Equal(t, output, "argocd: v99.99.99+unknown\n")
}

func TestShortVersion(t *testing.T) {
oldVersionFun := getServerVersion
defer func() { getServerVersion = oldVersionFun }()
getServerVersion = func(ctx context.Context, options *argocdclient.ClientOptions, c *cobra.Command) *version.VersionMessage {
crenshaw-dev marked this conversation as resolved.
Show resolved Hide resolved
return &version.VersionMessage{Version: "argocd: v99.99.99+unknown"}
}
buf := new(bytes.Buffer)
cmd := NewVersionCmd(&argocdclient.ClientOptions{})
cmd.SetOutput(buf)
cmd.SetArgs([]string{"argocd", "version", "--short"})
cmd.Execute()
output := buf.String()
assert.Equal(t, output, "argocd: v99.99.99+unknown\nargocd-server: argocd: v99.99.99+unknown\n")
}