Skip to content
Merged
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
36 changes: 36 additions & 0 deletions cmd/operator-sdk/run/cmd_test.go
Original file line number Diff line number Diff line change
@@ -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"))
})
})
})
35 changes: 26 additions & 9 deletions cmd/operator-sdk/run/packagemanifests/packagemanifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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. '<project-root>/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 {
Expand All @@ -59,3 +58,21 @@ must be set to a valid package manifests root directory, ex. '<project-root>/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"
}
}
Original file line number Diff line number Diff line change
@@ -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")
}
70 changes: 70 additions & 0 deletions cmd/operator-sdk/run/packagemanifests/packagemanifests_test.go
Original file line number Diff line number Diff line change
@@ -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"))
})
})
})
27 changes: 27 additions & 0 deletions cmd/operator-sdk/run/run_suite_test.go
Original file line number Diff line number Diff line change
@@ -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")
}