Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commands/.../test.go: allow passing of flags to go test #392

Merged
merged 2 commits into from
Aug 10, 2018
Merged
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
10 changes: 4 additions & 6 deletions commands/operator-sdk/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd

import (
"os"
"strings"

"github.com/operator-framework/operator-sdk/pkg/test"

Expand All @@ -28,10 +29,9 @@ var (
crdManifestPath string
opManifestPath string
rbacManifestPath string
verbose bool
goTestFlags string
)

// TODO: allow users to pass flags through to `go test`
func NewTestCmd() *cobra.Command {
testCmd := &cobra.Command{
Use: "test --test-location <path to tests directory> [flags]",
Expand All @@ -49,20 +49,18 @@ func NewTestCmd() *cobra.Command {
testCmd.Flags().StringVarP(&crdManifestPath, "crd", "c", "deploy/crd.yaml", "Path to CRD manifest")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go test also takes in -c and -o flags. Will that conflicts to the operator-sdk test flags -c and -o?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That won't cause conflict. The way that Flags().SetInterspersed(false) works is that any arguments/flags given after the first argument it sees after flags (argument being anything that doesn't start with a -) is not processed and instead is given to us in args []string. For instance, if I run operator-sdk test -t ./test/e2e -k ~/.kube/config args -v -parallel 2, I get this as my args []string: ["args" "-v" "-parallel" "2"].

testCmd.Flags().StringVarP(&opManifestPath, "operator", "o", "deploy/operator.yaml", "Path to operator manifest")
testCmd.Flags().StringVarP(&rbacManifestPath, "rbac", "r", "deploy/rbac.yaml", "Path to RBAC manifest")
testCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose go test")
testCmd.Flags().StringVarP(&goTestFlags, "go-test-flags", "g", "", "Additional flags to pass to go test")

return testCmd
}

func testFunc(cmd *cobra.Command, args []string) {
testArgs := []string{"test", testLocation + "/..."}
if verbose {
testArgs = append(testArgs, "-v")
}
testArgs = append(testArgs, "-"+test.KubeConfigFlag, kubeconfig)
testArgs = append(testArgs, "-"+test.CrdManPathFlag, crdManifestPath)
testArgs = append(testArgs, "-"+test.OpManPathFlag, opManifestPath)
testArgs = append(testArgs, "-"+test.RbacManPathFlag, rbacManifestPath)
testArgs = append(testArgs, "-"+test.ProjRootFlag, mustGetwd())
testArgs = append(testArgs, strings.Split(goTestFlags, " ")...)
execCmd(os.Stdout, "go", testArgs...)
}