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 `--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 #1476 : **Custom flags for test local (no go test ones)**

Signed-off-by: Nikhil Thomas <[email protected] >
  • Loading branch information
nikhil-thomas committed Jun 2, 2019
1 parent e9e59c0 commit 989c646
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions cmd/operator-sdk/test/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type testLocalConfig struct {
noSetup bool
debug bool
image string
operatorFlags 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.operatorFlags, "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.operatorFlags != "" {
testArgs = append(testArgs, "-"+test.OperatorFlags, tlConfig.operatorFlags)
}
}
opts := projutil.GoTestOptions{
GoCmdOptions: projutil.GoCmdOptions{
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"
OperatorFlags = "operatorFlags"
)

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)")
operatorFlags := flag.String(OperatorFlags, "", "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 *operatorFlags != "" {
args = append(args, strings.Split(*operatorFlags, " ")...)
}
localCmd = exec.Command(outputBinName, args...)
localCmd.Stdout = &localCmdOutBuf
localCmd.Stderr = &localCmdErrBuf
c := make(chan os.Signal)
Expand Down

0 comments on commit 989c646

Please sign in to comment.