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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/argocd/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewCommand() *cobra.Command {
}

command.AddCommand(NewCompletionCommand())
command.AddCommand(initialize.InitCommand(NewVersionCmd(&clientOpts)))
command.AddCommand(initialize.InitCommand(NewVersionCmd(&clientOpts, nil)))
command.AddCommand(initialize.InitCommand(NewClusterCommand(&clientOpts, pathOpts)))
command.AddCommand(initialize.InitCommand(NewApplicationCommand(&clientOpts)))
command.AddCommand(initialize.InitCommand(NewAppSetCommand(&clientOpts)))
Expand Down
47 changes: 29 additions & 18 deletions cmd/argocd/commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// NewVersionCmd returns a new `version` command to be used as a sub-command to root
func NewVersionCmd(clientOpts *argocdclient.ClientOptions) *cobra.Command {
func NewVersionCmd(clientOpts *argocdclient.ClientOptions, serverVersion *version.VersionMessage) *cobra.Command {
var (
short bool
client bool
Expand Down Expand Up @@ -54,7 +54,12 @@ func NewVersionCmd(clientOpts *argocdclient.ClientOptions) *cobra.Command {
}

if !client {
sv := getServerVersion(ctx, clientOpts, cmd)
var sv *version.VersionMessage
if serverVersion == nil {
sv = getServerVersion(ctx, clientOpts, cmd)
} else {
sv = serverVersion
}

if short {
v["server"] = map[string]string{"argocd-server": sv.Version}
Expand All @@ -68,8 +73,13 @@ func NewVersionCmd(clientOpts *argocdclient.ClientOptions) *cobra.Command {
case "wide", "short", "":
fmt.Fprint(cmd.OutOrStdout(), printClientVersion(&cv, short || (output == "short")))
if !client {
sv := getServerVersion(ctx, clientOpts, cmd)
printServerVersion(sv, short || (output == "short"))
var sv *version.VersionMessage
if serverVersion == nil {
sv = getServerVersion(ctx, clientOpts, cmd)
} else {
sv = serverVersion
}
fmt.Fprint(cmd.OutOrStdout(), printServerVersion(sv, short || (output == "short")))
}
default:
log.Fatalf("unknown output format: %s", output)
Expand Down Expand Up @@ -109,44 +119,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
}
19 changes: 17 additions & 2 deletions cmd/argocd/commands/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"testing"

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

func TestShortVersion(t *testing.T) {
func TestShortVersionClient(t *testing.T) {
buf := new(bytes.Buffer)
cmd := NewVersionCmd(&argocdclient.ClientOptions{})
cmd := NewVersionCmd(&argocdclient.ClientOptions{}, nil)
cmd.SetOutput(buf)
cmd.SetArgs([]string{"version", "--short", "--client"})
err := cmd.Execute()
Expand All @@ -20,3 +21,17 @@ func TestShortVersion(t *testing.T) {
output := buf.String()
assert.Equal(t, output, "argocd: v99.99.99+unknown\n")
}

func TestShortVersion(t *testing.T) {
serverVersion := &version.VersionMessage{Version: "v99.99.99+unknown"}
buf := new(bytes.Buffer)
cmd := NewVersionCmd(&argocdclient.ClientOptions{}, serverVersion)
cmd.SetOutput(buf)
cmd.SetArgs([]string{"argocd", "version", "--short"})
err := cmd.Execute()
if err != nil {
t.Fatal("Failed to execute short version command")
}
output := buf.String()
assert.Equal(t, output, "argocd: v99.99.99+unknown\nargocd-server: v99.99.99+unknown\n")
}