From 98b5fa0000d652d1628a55a7ef8fbda654961785 Mon Sep 17 00:00:00 2001 From: Jonathan Berkhahn Date: Mon, 13 Jul 2020 16:25:37 -0700 Subject: [PATCH] add unit tests for cmd/run --- cmd/operator-sdk/run/cmd_test.go | 36 ++++++++++ .../run/packagemanifests/packagemanifests.go | 35 +++++++--- .../packagemanifests_suite_test.go | 27 +++++++ .../packagemanifests/packagemanifests_test.go | 70 +++++++++++++++++++ cmd/operator-sdk/run/run_suite_test.go | 27 +++++++ 5 files changed, 186 insertions(+), 9 deletions(-) create mode 100644 cmd/operator-sdk/run/cmd_test.go create mode 100644 cmd/operator-sdk/run/packagemanifests/packagemanifests_suite_test.go create mode 100644 cmd/operator-sdk/run/packagemanifests/packagemanifests_test.go create mode 100644 cmd/operator-sdk/run/run_suite_test.go diff --git a/cmd/operator-sdk/run/cmd_test.go b/cmd/operator-sdk/run/cmd_test.go new file mode 100644 index 0000000000..49f6917345 --- /dev/null +++ b/cmd/operator-sdk/run/cmd_test.go @@ -0,0 +1,36 @@ +// Copyright 2020 The Operator-SDK 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 run + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Running a run command", func() { + Describe("NewCmd", func() { + It("builds a cobra command with the correct subcommands", func() { + cmd := NewCmd() + Expect(cmd).NotTo(BeNil()) + Expect(cmd.Use).NotTo(BeNil()) + Expect(cmd.Short).NotTo(BeNil()) + Expect(cmd.Long).NotTo(BeNil()) + + subcommands := cmd.Commands() + Expect(len(subcommands)).To(Equal(1)) + Expect(subcommands[0].Use).To(Equal("packagemanifests")) + }) + }) +}) diff --git a/cmd/operator-sdk/run/packagemanifests/packagemanifests.go b/cmd/operator-sdk/run/packagemanifests/packagemanifests.go index 736e4931ab..e7d4532318 100644 --- a/cmd/operator-sdk/run/packagemanifests/packagemanifests.go +++ b/cmd/operator-sdk/run/packagemanifests/packagemanifests.go @@ -36,16 +36,15 @@ func NewCmd() *cobra.Command { Long: `'run packagemanifests' deploys an Operator's package manifests with OLM. The command's argument must be set to a valid package manifests root directory, ex. '/packagemanifests'.`, Aliases: []string{"pm"}, - RunE: func(cmd *cobra.Command, args []string) error { - if len(args) > 0 { - if len(args) > 1 { - return fmt.Errorf("exactly one argument is required") - } - c.ManifestsDir = args[0] - } else { - c.ManifestsDir = "packagemanifests" + PreRunE: func(cmd *cobra.Command, args []string) error { + err := c.validate(args) + if err != nil { + log.Fatalf("Failed to validate input: %v", err) } - + c.setDefaults(args) + return nil + }, + RunE: func(cmd *cobra.Command, args []string) error { log.Infof("Running operator from directory %s", c.ManifestsDir) if err := c.Run(); err != nil { @@ -59,3 +58,21 @@ must be set to a valid package manifests root directory, ex. '/pac return cmd } + +func (c *packagemanifestsCmd) validate(args []string) error { + if len(args) > 0 { + if len(args) > 1 { + return fmt.Errorf("exactly one argument is required") + } + } + + return nil +} + +func (c *packagemanifestsCmd) setDefaults(args []string) { + if len(args) != 0 { + c.ManifestsDir = args[0] + } else { + c.ManifestsDir = "packagemanifests" + } +} diff --git a/cmd/operator-sdk/run/packagemanifests/packagemanifests_suite_test.go b/cmd/operator-sdk/run/packagemanifests/packagemanifests_suite_test.go new file mode 100644 index 0000000000..248b5fb6ed --- /dev/null +++ b/cmd/operator-sdk/run/packagemanifests/packagemanifests_suite_test.go @@ -0,0 +1,27 @@ +// Copyright 2020 The Operator-SDK 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 packagemanifests + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestRun(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Packagemanifests Suite") +} diff --git a/cmd/operator-sdk/run/packagemanifests/packagemanifests_test.go b/cmd/operator-sdk/run/packagemanifests/packagemanifests_test.go new file mode 100644 index 0000000000..f963baaacc --- /dev/null +++ b/cmd/operator-sdk/run/packagemanifests/packagemanifests_test.go @@ -0,0 +1,70 @@ +// Copyright 2020 The Operator-SDK 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 packagemanifests + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("Running a run packagemanifests command", func() { + Describe("NewCmd", func() { + It("builds a cobra command", func() { + cmd := NewCmd() + Expect(cmd).NotTo(BeNil()) + Expect(cmd.Use).NotTo(BeNil()) + Expect(cmd.Short).NotTo(BeNil()) + Expect(cmd.Long).NotTo(BeNil()) + aliases := cmd.Aliases + Expect(len(aliases)).To(Equal(1)) + Expect(aliases[0]).To(Equal("pm")) + }) + }) + Describe("validate", func() { + var ( + c packagemanifestsCmd + err error + ) + BeforeEach(func() { + c = packagemanifestsCmd{} + }) + It("fails if provided more than 1 arg", func() { + err = c.validate([]string{"foo", "bar"}) + Expect(err).NotTo(BeNil()) + Expect(err.Error()).To(ContainSubstring("exactly one argument is required")) + }) + It("succeeds and if exactly 1 arg is provided", func() { + arg := "baz" + err = c.validate([]string{arg}) + Expect(err).To(BeNil()) + }) + }) + Describe("setDefaults", func() { + var ( + c packagemanifestsCmd + ) + BeforeEach(func() { + c = packagemanifestsCmd{} + }) + It("defaults to 'packagemanifests' if no args are provided", func() { + c.setDefaults([]string{}) + Expect(c.ManifestsDir).To(Equal("packagemanifests")) + }) + It("sets ManifestDir to the first arg if provided more than 0", func() { + c.setDefaults([]string{"config/potato"}) + Expect(c.ManifestsDir).To(Equal("config/potato")) + }) + }) +}) diff --git a/cmd/operator-sdk/run/run_suite_test.go b/cmd/operator-sdk/run/run_suite_test.go new file mode 100644 index 0000000000..834df52ff0 --- /dev/null +++ b/cmd/operator-sdk/run/run_suite_test.go @@ -0,0 +1,27 @@ +// Copyright 2020 The Operator-SDK 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 run + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestRun(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Run Suite") +}