Skip to content

Commit 25b8054

Browse files
authored
Add unit tests for cmd/version (operator-framework#3454)
Description of the change: Add unit tests for cmd/version. Also includes a small refactor, putting the cmd logic into a run() method so it can be tested. Motivation for the change: Ongoing work as part of operator-framework#3246
1 parent 1248c1c commit 25b8054

File tree

3 files changed

+98
-6
lines changed

3 files changed

+98
-6
lines changed

cmd/operator-sdk/version/cmd.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ func NewCmd() *cobra.Command {
2727
Use: "version",
2828
Short: "Prints the version of operator-sdk",
2929
Run: func(cmd *cobra.Command, args []string) {
30-
version := ver.GitVersion
31-
if version == "unknown" {
32-
version = ver.Version
33-
}
34-
fmt.Printf("operator-sdk version: %q, commit: %q, kubernetes version: %q, go version: %q\n",
35-
version, ver.GitCommit, ver.KubernetesVersion, ver.GoVersion)
30+
run()
3631
},
3732
}
3833
return versionCmd
3934
}
35+
36+
func run() {
37+
version := ver.GitVersion
38+
if version == "unknown" {
39+
version = ver.Version
40+
}
41+
fmt.Printf("operator-sdk version: %q, commit: %q, kubernetes version: %q, go version: %q\n",
42+
version, ver.GitCommit, ver.KubernetesVersion, ver.GoVersion)
43+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2020 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package version
16+
17+
import (
18+
"fmt"
19+
"io/ioutil"
20+
"os"
21+
22+
. "github.com/onsi/ginkgo"
23+
. "github.com/onsi/gomega"
24+
ver "github.com/operator-framework/operator-sdk/version"
25+
)
26+
27+
var _ = Describe("Running a version command", func() {
28+
Describe("NewCmd", func() {
29+
It("builds a cobra command", func() {
30+
cmd := NewCmd()
31+
Expect(cmd).NotTo(BeNil())
32+
Expect(cmd.Use).NotTo(Equal(""))
33+
Expect(cmd.Short).NotTo(Equal(""))
34+
})
35+
})
36+
Describe("run", func() {
37+
It("prints the correct version info", func() {
38+
r, w, _ := os.Pipe()
39+
tmp := os.Stdout
40+
defer func() {
41+
os.Stdout = tmp
42+
}()
43+
os.Stdout = w
44+
go func() {
45+
run()
46+
w.Close()
47+
}()
48+
stdout, err := ioutil.ReadAll(r)
49+
Expect(err).To(BeNil())
50+
stdoutString := string(stdout)
51+
version := ver.GitVersion
52+
if version == "unknown" {
53+
version = ver.Version
54+
}
55+
Expect(stdoutString).To(ContainSubstring(fmt.Sprintf("version: %q", version)))
56+
Expect(stdoutString).To(ContainSubstring(fmt.Sprintf("commit: %q", ver.GitCommit)))
57+
Expect(stdoutString).To(ContainSubstring(fmt.Sprintf("kubernetes version: %q", ver.KubernetesVersion)))
58+
Expect(stdoutString).To(ContainSubstring(fmt.Sprintf("go version: %q", ver.GoVersion)))
59+
})
60+
})
61+
})
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2020 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package version
16+
17+
import (
18+
"testing"
19+
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
func TestVersion(t *testing.T) {
25+
RegisterFailHandler(Fail)
26+
RunSpecs(t, "Version Suite")
27+
}

0 commit comments

Comments
 (0)