Skip to content

Commit

Permalink
commands/.../test/local,pkg/test: add no-setup flag
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNPavel committed Nov 21, 2018
1 parent c793226 commit c18d616
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
30 changes: 27 additions & 3 deletions commands/operator-sdk/cmd/test/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type testLocalConfig struct {
namespacedManPath string
goTestFlags string
namespace string
noSetup bool
}

var tlConfig testLocalConfig
Expand All @@ -54,6 +55,7 @@ func NewTestLocalCmd() *cobra.Command {
testCmd.Flags().StringVar(&tlConfig.namespacedManPath, "namespaced-manifest", "", "Path to manifest for per-test, namespaced resources (e.g. RBAC and Operator manifest)")
testCmd.Flags().StringVar(&tlConfig.goTestFlags, "go-test-flags", "", "Additional flags to pass to go test")
testCmd.Flags().StringVar(&tlConfig.namespace, "namespace", "", "If non-empty, single namespace to run tests in")
testCmd.Flags().BoolVar(&tlConfig.noSetup, "no-setup", false, "Disable test resource creation")

return testCmd
}
Expand All @@ -62,11 +64,14 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
if len(args) != 1 {
log.Fatal("operator-sdk test local requires exactly 1 argument")
}
if (tlConfig.noSetup && tlConfig.globalManPath != "") || (tlConfig.noSetup && tlConfig.namespacedManPath != "") {
log.Fatal("the global-manifest and namespaced-manifest flags cannot be enabled at the same time as the no-setup flag")
}

log.Info("Testing operator locally.")

// if no namespaced manifest path is given, combine deploy/service_account.yaml, deploy/role.yaml, deploy/role_binding.yaml and deploy/operator.yaml
if tlConfig.namespacedManPath == "" {
if tlConfig.namespacedManPath == "" && !tlConfig.noSetup {
err := os.MkdirAll(deployTestDir, os.FileMode(fileutil.DefaultDirFileMode))
if err != nil {
log.Fatalf("could not create %s: (%v)", deployTestDir, err)
Expand Down Expand Up @@ -105,7 +110,7 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
}
}()
}
if tlConfig.globalManPath == "" {
if tlConfig.globalManPath == "" && !tlConfig.noSetup {
err := os.MkdirAll(deployTestDir, os.FileMode(fileutil.DefaultDirFileMode))
if err != nil {
log.Fatalf("could not create %s: (%v)", deployTestDir, err)
Expand Down Expand Up @@ -141,6 +146,25 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
}
}()
}
if tlConfig.noSetup {
err := os.MkdirAll(deployTestDir, os.FileMode(fileutil.DefaultDirFileMode))
if err != nil {
log.Fatalf("could not create %s: (%v)", deployTestDir, err)
}
tlConfig.namespacedManPath = filepath.Join(deployTestDir, "empty.yaml")
tlConfig.globalManPath = filepath.Join(deployTestDir, "empty.yaml")
emptyBytes := []byte{}
err = ioutil.WriteFile(tlConfig.globalManPath, emptyBytes, os.FileMode(fileutil.DefaultFileMode))
if err != nil {
log.Fatalf("could not create empty manifest file: (%v)", err)
}
defer func() {
err := os.Remove(tlConfig.globalManPath)
if err != nil {
log.Fatalf("could not delete empty manifest file: (%v)", err)
}
}()
}
testArgs := []string{"test", args[0] + "/..."}
testArgs = append(testArgs, "-"+test.KubeConfigFlag, tlConfig.kubeconfig)
testArgs = append(testArgs, "-"+test.NamespacedManPathFlag, tlConfig.namespacedManPath)
Expand All @@ -151,7 +175,7 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
if tlConfig.goTestFlags != "" {
testArgs = append(testArgs, strings.Split(tlConfig.goTestFlags, " ")...)
}
if tlConfig.namespace != "" {
if tlConfig.namespace != "" || tlConfig.noSetup {
testArgs = append(testArgs, "-"+test.SingleNamespaceFlag, "-parallel=1")
}
dc := exec.Command("go", testArgs...)
Expand Down
17 changes: 9 additions & 8 deletions pkg/test/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ type Framework struct {
}

func setup(kubeconfigPath, namespacedManPath *string) error {
namespace := ""
if *singleNamespace {
namespace = os.Getenv(TestNamespaceEnv)
}
var err error
var kubeconfig *rest.Config
if *kubeconfigPath == "incluster" {
Expand All @@ -74,7 +78,11 @@ func setup(kubeconfigPath, namespacedManPath *string) error {
kubeconfig, err = rest.InClusterConfig()
*singleNamespace = true
} else {
kubeconfig, _, err = k8sInternal.GetKubeconfigAndNamespace(*kubeconfigPath)
var kcNamespace string
kubeconfig, kcNamespace, err = k8sInternal.GetKubeconfigAndNamespace(*kubeconfigPath)
if *singleNamespace && namespace == "" {
namespace = kcNamespace
}
}
if err != nil {
return fmt.Errorf("failed to build the kubeconfig: %v", err)
Expand All @@ -91,13 +99,6 @@ func setup(kubeconfigPath, namespacedManPath *string) error {
return fmt.Errorf("failed to build the dynamic client: %v", err)
}
dynamicDecoder = serializer.NewCodecFactory(scheme).UniversalDeserializer()
namespace := ""
if *singleNamespace {
namespace = os.Getenv(TestNamespaceEnv)
if len(namespace) == 0 {
return fmt.Errorf("namespace set in %s cannot be empty", TestNamespaceEnv)
}
}
Global = &Framework{
Client: &frameworkClient{Client: dynClient},
KubeConfig: kubeconfig,
Expand Down
4 changes: 4 additions & 0 deletions pkg/test/resource_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func (ctx *TestCtx) createFromYAML(yamlFile []byte, skipIfExists bool, cleanupOp
}
yamlSplit := bytes.Split(yamlFile, []byte("\n---\n"))
for _, yamlSpec := range yamlSplit {
// some autogenerated files may include an extra `---` at the end of the file
if string(yamlSpec) == "" {
continue
}
yamlSpec, err = setNamespaceYAML(yamlSpec, namespace)
if err != nil {
return err
Expand Down

0 comments on commit c18d616

Please sign in to comment.