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

Added tests for pkg/version #5418

Closed
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
14 changes: 13 additions & 1 deletion pkg/version/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,24 @@ func TestReleaseVersion(t *testing.T) {
expectReleaseVersion: "v1.7.0-alpha.1",
expectError: false,
},
{
name: "nil version",
gitVersion: "",
expectReleaseVersion: "<nil>",
expectError: false,
},
}

for i := range tests {
tc := tests[i]
t.Run(tc.name, func(t *testing.T) {
rv, err := ParseGitVersion(tc.gitVersion)
var rv *ReleaseVersion
var err error
if tc.gitVersion != "" {
rv, err = ParseGitVersion(tc.gitVersion)
} else {
rv = &ReleaseVersion{Version: nil}
}
Comment on lines +58 to +62
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This use case may not be appropriate here because an empty string is not valid for the ParseGitVersion method. The expectError parameter is not considered in one dimension.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review, I added that check due to a new test case where r.Version is nil and I overlooked the expectError parameter in that case.

if err != nil {
if !tc.expectError {
t.Fatalf("No error is expected but got: %v", err)
Expand Down
142 changes: 142 additions & 0 deletions pkg/version/sharedcommand/sharedcommand_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
Copyright 2024 The Karmada Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package sharedcommand
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @anujagrawal699, some of the tests added to this file are pointless. The tested method itself creates a struct. More functionality is actually provided by the cobra package. The new tests seem to be testing these features. For karmada, this may be overkill. and is not easy to maintain.


import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewCmdVersion(t *testing.T) {
testCases := []struct {
name string
parentCommand string
expectedUse string
expectedShort string
expectedLong string
}{
{
name: "karmada-controller-manager version command",
parentCommand: "karmada-controller-manager",
expectedUse: "version",
expectedShort: versionShort,
expectedLong: versionLong,
},
{
name: "karmadactl version command",
parentCommand: "karmadactl",
expectedUse: "version",
expectedShort: versionShort,
expectedLong: versionLong,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cmd := NewCmdVersion(tc.parentCommand)

assert.Equal(t, tc.expectedUse, cmd.Use)
assert.Equal(t, tc.expectedShort, cmd.Short)
assert.Equal(t, tc.expectedLong, cmd.Long)
assert.Equal(t, fmt.Sprintf(versionExample, tc.parentCommand), cmd.Example)

oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

cmd.Run(cmd, []string{})
w.Close()
os.Stdout = oldStdout

var buf bytes.Buffer
_, err := io.Copy(&buf, r)
if err != nil {
t.Fatalf("Failed to copy: %v", err)
}
output := buf.String()

assert.Contains(t, output, tc.parentCommand)
assert.Contains(t, output, "version:")
assert.Contains(t, output, "GitVersion:")
assert.Contains(t, output, "GitCommit:")
assert.Contains(t, output, "GitTreeState:")
assert.Contains(t, output, "BuildDate:")
assert.Contains(t, output, "GoVersion:")
assert.Contains(t, output, "Compiler:")
assert.Contains(t, output, "Platform:")
})
}
}

func TestNewCmdVersionOutput(t *testing.T) {
oldStdout := os.Stdout

r, w, _ := os.Pipe()
os.Stdout = w

cmd := NewCmdVersion("test-command")
cmd.Run(cmd, []string{})

w.Close()
os.Stdout = oldStdout

var buf bytes.Buffer
_, err := io.Copy(&buf, r)
if err != nil {
t.Fatalf("Failed to copy: %v", err)
}
output := buf.String()

assert.Contains(t, output, "test-command")
assert.Contains(t, output, "version:")
assert.Contains(t, output, "GitVersion:")
assert.Contains(t, output, "GitCommit:")
assert.Contains(t, output, "GitTreeState:")
assert.Contains(t, output, "BuildDate:")
assert.Contains(t, output, "GoVersion:")
assert.Contains(t, output, "Compiler:")
assert.Contains(t, output, "Platform:")
}

func TestNewCmdVersionHelp(t *testing.T) {
cmd := NewCmdVersion("test-command")
buf := new(bytes.Buffer)
cmd.SetOut(buf)
err := cmd.Help()
if err != nil {
t.Fatalf("Failed to execute Help: %v", err)
}

output := buf.String()
assert.True(t, strings.Contains(output, "Usage:"))
assert.True(t, strings.Contains(output, "test-command version"))
assert.True(t, strings.Contains(output, versionShort))
assert.True(t, strings.Contains(output, versionLong))
assert.True(t, strings.Contains(output, "Examples:"))
assert.True(t, strings.Contains(output, fmt.Sprintf(versionExample, "test-command")))
}

func TestNewCmdVersionFlags(t *testing.T) {
cmd := NewCmdVersion("test-command")
assert.Equal(t, 0, len(cmd.Flags().Args()))
}
88 changes: 87 additions & 1 deletion pkg/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package version

import (
"fmt"
"runtime"
"strings"
"testing"
)

Expand All @@ -39,12 +42,95 @@ func TestInfo_String(t *testing.T) {
},
want: `version.Info{GitVersion:"1.3.0", GitCommit:"da070e68f3318410c8c70ed8186a2bc4736dacbd", GitTreeState:"clean", BuildDate:"2022-08-31T13:09:22Z", GoVersion:"go1.18.3", Compiler:"gc", Platform:"linux/amd64"}`,
},
{
name: "empty info",
info: Info{},
want: `version.Info{GitVersion:"", GitCommit:"", GitTreeState:"", BuildDate:"", GoVersion:"", Compiler:"", Platform:""}`,
},
}

fields := []string{"GitVersion", "GitCommit", "GitTreeState", "BuildDate", "GoVersion", "Compiler", "Platform"}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.info.String(); got != tt.want {
got := tt.info.String()
if got != tt.want {
t.Errorf("Info.String() = %v, want %v", got, tt.want)
}
for _, field := range fields {
if !strings.Contains(got, field) {
t.Errorf("Info.String() does not contain %s", field)
}
}
})
}
}

func TestGet(t *testing.T) {
info := Get()

tests := []struct {
name string
got string
expected string
}{
{"GitVersion", info.GitVersion, gitVersion},
{"GitCommit", info.GitCommit, gitCommit},
{"GitTreeState", info.GitTreeState, gitTreeState},
{"BuildDate", info.BuildDate, buildDate},
{"GoVersion", info.GoVersion, runtime.Version()},
{"Compiler", info.Compiler, runtime.Compiler},
{"Platform", info.Platform, fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.got != tt.expected {
t.Errorf("Get().%s = %v, want %v", tt.name, tt.got, tt.expected)
}
})
}
}

func TestInfoFields(t *testing.T) {
tests := []struct {
name string
info Info
field string
expected string
}{
{"GitVersion", Info{GitVersion: "v1.0.0"}, "GitVersion", "v1.0.0"},
{"GitCommit", Info{GitCommit: "abcdef123456"}, "GitCommit", "abcdef123456"},
{"GitTreeState", Info{GitTreeState: "clean"}, "GitTreeState", "clean"},
{"BuildDate", Info{BuildDate: "2023-04-01T00:00:00Z"}, "BuildDate", "2023-04-01T00:00:00Z"},
{"GoVersion", Info{GoVersion: "go1.16"}, "GoVersion", "go1.16"},
{"Compiler", Info{Compiler: "gc"}, "Compiler", "gc"},
{"Platform", Info{Platform: "linux/amd64"}, "Platform", "linux/amd64"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value := ""
switch tt.field {
case "GitVersion":
value = tt.info.GitVersion
case "GitCommit":
value = tt.info.GitCommit
case "GitTreeState":
value = tt.info.GitTreeState
case "BuildDate":
value = tt.info.BuildDate
case "GoVersion":
value = tt.info.GoVersion
case "Compiler":
value = tt.info.Compiler
case "Platform":
value = tt.info.Platform
}

if value != tt.expected {
t.Errorf("%s = %v, want %v", tt.field, value, tt.expected)
}
})
}
}