Skip to content

Commit

Permalink
Add cmd line flags for test local --up-local
Browse files Browse the repository at this point in the history
This patch adds `--local-operator-flags` to
`operator-sdk test local --up-local` command

It is prvides the functionalisty provided by
`--operator-flags` flag in `operator-sdk up local --operator-flags` command.

At present there is no way to pass command line flags to an operator
while testing using `operator-sdk test local --up-local` command.

There is a flag `--go-test-flags` but it cannot be used to pass flags to
the operator code being tested using `--up-local` flag

This resolves operator-framework#1476 : **Custom flags for test local (no go test ones)**

Signed-off-by: Nikhil Thomas <[email protected] >
  • Loading branch information
nikhil-thomas committed Jun 3, 2019
1 parent e9e59c0 commit f97341c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
25 changes: 15 additions & 10 deletions cmd/operator-sdk/test/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ import (
var deployTestDir = filepath.Join(scaffold.DeployDir, "test")

type testLocalConfig struct {
kubeconfig string
globalManPath string
namespacedManPath string
goTestFlags string
moleculeTestFlags string
namespace string
upLocal bool
noSetup bool
debug bool
image string
kubeconfig string
globalManPath string
namespacedManPath string
goTestFlags string
moleculeTestFlags string
namespace string
upLocal bool
noSetup bool
debug bool
image string
localOperatorFlags string
}

var tlConfig testLocalConfig
Expand All @@ -70,6 +71,7 @@ func newTestLocalCmd() *cobra.Command {
testCmd.Flags().BoolVar(&tlConfig.noSetup, "no-setup", false, "Disable test resource creation")
testCmd.Flags().BoolVar(&tlConfig.debug, "debug", false, "Enable debug-level logging")
testCmd.Flags().StringVar(&tlConfig.image, "image", "", "Use a different operator image from the one specified in the namespaced manifest")
testCmd.Flags().StringVar(&tlConfig.localOperatorFlags, "local-operator-flags", "", "The flags that the operator needs (while using --up-local). Example: \"--flag1 value1 --flag2=value2\"")

return testCmd
}
Expand Down Expand Up @@ -207,6 +209,9 @@ func testLocalGoFunc(cmd *cobra.Command, args []string) error {
}
if tlConfig.upLocal {
testArgs = append(testArgs, "-"+test.LocalOperatorFlag)
if tlConfig.localOperatorFlags != "" {
testArgs = append(testArgs, "-"+test.LocalOperatorArgs, tlConfig.localOperatorFlags)
}
}
opts := projutil.GoTestOptions{
GoCmdOptions: projutil.GoCmdOptions{
Expand Down
1 change: 1 addition & 0 deletions doc/sdk-cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ Runs the tests locally
* `--go-test-flags` string - Additional flags to pass to go test
* `--molecule-test-flags` string - Additional flags to pass to molecule test
* `--up-local` - enable running operator locally with go run instead of as an image in the cluster
* `--local-operator-flags` string - flags that the operator needs, while using --up-local (e.g. \"--flag1 value1 --flag2=value2\")
* `--no-setup` - disable test resource creation
* `--image` string - use a different operator image from the one specified in the namespaced manifest
* `-h, --help` - help for local
Expand Down
9 changes: 8 additions & 1 deletion pkg/test/main_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os/exec"
"os/signal"
"path/filepath"
"strings"
"syscall"
"testing"

Expand All @@ -42,6 +43,7 @@ const (
SingleNamespaceFlag = "singleNamespace"
TestNamespaceEnv = "TEST_NAMESPACE"
LocalOperatorFlag = "localOperator"
LocalOperatorArgs = "localOperatorArgs"
)

func MainEntry(m *testing.M) {
Expand All @@ -51,6 +53,7 @@ func MainEntry(m *testing.M) {
namespacedManPath := flag.String(NamespacedManPathFlag, "", "path to rbac manifest")
singleNamespace = flag.Bool(SingleNamespaceFlag, false, "enable single namespace mode")
localOperator := flag.Bool(LocalOperatorFlag, false, "enable if operator is running locally (not in cluster)")
localOperatorArgs := flag.String(LocalOperatorArgs, "", "flags that the operator needs (while using --up-local). example: \"--flag1 value1 --flag2=value2\"")
flag.Parse()
// go test always runs from the test directory; change to project root
err := os.Chdir(*projRoot)
Expand All @@ -74,7 +77,11 @@ func MainEntry(m *testing.M) {
if err := projutil.GoBuild(opts); err != nil {
log.Fatalf("Failed to build local operator binary: %s", err)
}
localCmd = exec.Command(outputBinName)
args := []string{}
if *localOperatorArgs != "" {
args = append(args, strings.Split(*localOperatorArgs, " ")...)
}
localCmd = exec.Command(outputBinName, args...)
localCmd.Stdout = &localCmdOutBuf
localCmd.Stderr = &localCmdErrBuf
c := make(chan os.Signal)
Expand Down

0 comments on commit f97341c

Please sign in to comment.