Skip to content

Commit

Permalink
pkg/test,commands/.../test.go: use flags instead of env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNPavel committed Aug 3, 2018
1 parent 75a4641 commit 52202fd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 45 deletions.
21 changes: 10 additions & 11 deletions commands/operator-sdk/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,23 @@ func NewTestCmd() *cobra.Command {
}

func testFunc(cmd *cobra.Command, args []string) {
wd, err := os.Getwd()
if err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("could not determine working directory: %v", err))
}
testArgs := []string{"test", testLocation + "/..."}
if verbose {
testArgs = append(testArgs, "-v")
}
testArgs = append(testArgs, "-kubeconfig", kubeconfig)
testArgs = append(testArgs, "-crd", crdManifestPath)
testArgs = append(testArgs, "-op", opManifestPath)
testArgs = append(testArgs, "-rbac", rbacManifestPath)
testArgs = append(testArgs, "-namespace", namespace)
testArgs = append(testArgs, "-root", wd)
dc := exec.Command("go", testArgs...)
dc.Stdout = os.Stdout
dc.Stderr = os.Stderr
wd, err := os.Getwd()
if err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("could not determine working directory: %v", err))
}
dc.Env = append(os.Environ(),
fmt.Sprintf("%v=%v", "TEST_KUBECONFIG", kubeconfig),
fmt.Sprintf("%v=%v", "TEST_CRDMAN", crdManifestPath),
fmt.Sprintf("%v=%v", "TEST_OPMAN", opManifestPath),
fmt.Sprintf("%v=%v", "TEST_RBACMAN", rbacManifestPath),
fmt.Sprintf("%v=%v", "TEST_NAMESPACE", namespace),
fmt.Sprintf("%v=%v", "TEST_PROJROOT", wd))
err = dc.Run()
if err != nil {
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("test failed: %v", err))
Expand Down
34 changes: 6 additions & 28 deletions pkg/test/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
package test

import (
"errors"
"fmt"
"os"

extensions "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/client-go/kubernetes"
Expand All @@ -37,12 +35,8 @@ type Framework struct {
RbacManPath *string
}

func setup() error {
kubeconfigEnv, ok := os.LookupEnv("TEST_KUBECONFIG")
if !ok {
return errors.New("Missing test environment variable; please run with `operator-sdk` test command")
}
kubeconfig, err := clientcmd.BuildConfigFromFlags("", kubeconfigEnv)
func setup(kubeconfigPath, namespace, crdManPath, opManPath, rbacManPath *string) error {
kubeconfig, err := clientcmd.BuildConfigFromFlags("", *kubeconfigPath)
if err != nil {
return fmt.Errorf("failed to build the kubeconfig: %v", err)
}
Expand All @@ -54,30 +48,14 @@ func setup() error {
if err != nil {
return fmt.Errorf("failed to build the extensionsClient: %v", err)
}
namespace, ok := os.LookupEnv("TEST_NAMESPACE")
if !ok {
return errors.New("Missing test environment variable; please run with `operator-sdk` test command")
}
crdManPath, ok := os.LookupEnv("TEST_CRDMAN")
if !ok {
return errors.New("Missing test environment variable; please run with `operator-sdk` test command")
}
opManPath, ok := os.LookupEnv("TEST_OPMAN")
if !ok {
return errors.New("Missing test environment variable; please run with `operator-sdk` test command")
}
rbacManPath, ok := os.LookupEnv("TEST_RBACMAN")
if !ok {
return errors.New("Missing test environment variable; please run with `operator-sdk` test command")
}
Global = &Framework{
KubeConfig: kubeconfig,
KubeClient: kubeclient,
ExtensionsClient: extensionsClient,
Namespace: &namespace,
CrdManPath: &crdManPath,
OpManPath: &opManPath,
RbacManPath: &rbacManPath,
Namespace: namespace,
CrdManPath: crdManPath,
OpManPath: opManPath,
RbacManPath: rbacManPath,
}
return nil
}
16 changes: 10 additions & 6 deletions pkg/test/main_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@
package test

import (
"flag"
"log"
"os"
"testing"
)

func MainEntry(m *testing.M) {
projRoot := flag.String("root", "", "path to project root")
kubeconfigPath := flag.String("kubeconfig", "", "path to kubeconfig")
namespace := flag.String("namespace", "", "namespace to tests to run in")
crdManPath := flag.String("crd", "", "path to crd manifest")
opManPath := flag.String("op", "", "path to operator manifest")
rbacManPath := flag.String("rbac", "", "path to rbac manifest")
flag.Parse()
// go test always runs from the test directory; change to project root
root, ok := os.LookupEnv("TEST_PROJROOT")
if !ok {
log.Fatal("failed to get project root dir environment variable")
}
os.Chdir(root)
if err := setup(); err != nil {
os.Chdir(*projRoot)
if err := setup(kubeconfigPath, namespace, crdManPath, opManPath, rbacManPath); err != nil {
log.Fatalf("failed to set up framework: %v", err)
}

Expand Down

0 comments on commit 52202fd

Please sign in to comment.