Skip to content

Commit

Permalink
test: add unit test for server version (argoproj#10720)
Browse files Browse the repository at this point in the history
* test: add unit test for server version

Signed-off-by: emirot <emirot.nolan@gmail.com>

* test: add unit test for server version

Signed-off-by: emirot <emirot.nolan@gmail.com>

* test: add unit test for server version

Signed-off-by: emirot <emirot.nolan@gmail.com>

* tests: update cmd dependencies

Signed-off-by: emirot <emirot.nolan@gmail.com>

Signed-off-by: emirot <emirot.nolan@gmail.com>
Signed-off-by: Niti Singhal <gupta.sweet.niti@gmail.com>
  • Loading branch information
emirot authored and nsinghal12 committed Oct 20, 2022
1 parent 8a71b8a commit 4adc118
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
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")
}

0 comments on commit 4adc118

Please sign in to comment.