|
| 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 | +}) |
0 commit comments