Skip to content

Commit c18d616

Browse files
committed
commands/.../test/local,pkg/test: add no-setup flag
1 parent c793226 commit c18d616

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

commands/operator-sdk/cmd/test/local.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type testLocalConfig struct {
3939
namespacedManPath string
4040
goTestFlags string
4141
namespace string
42+
noSetup bool
4243
}
4344

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

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

6671
log.Info("Testing operator locally.")
6772

6873
// if no namespaced manifest path is given, combine deploy/service_account.yaml, deploy/role.yaml, deploy/role_binding.yaml and deploy/operator.yaml
69-
if tlConfig.namespacedManPath == "" {
74+
if tlConfig.namespacedManPath == "" && !tlConfig.noSetup {
7075
err := os.MkdirAll(deployTestDir, os.FileMode(fileutil.DefaultDirFileMode))
7176
if err != nil {
7277
log.Fatalf("could not create %s: (%v)", deployTestDir, err)
@@ -105,7 +110,7 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
105110
}
106111
}()
107112
}
108-
if tlConfig.globalManPath == "" {
113+
if tlConfig.globalManPath == "" && !tlConfig.noSetup {
109114
err := os.MkdirAll(deployTestDir, os.FileMode(fileutil.DefaultDirFileMode))
110115
if err != nil {
111116
log.Fatalf("could not create %s: (%v)", deployTestDir, err)
@@ -141,6 +146,25 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
141146
}
142147
}()
143148
}
149+
if tlConfig.noSetup {
150+
err := os.MkdirAll(deployTestDir, os.FileMode(fileutil.DefaultDirFileMode))
151+
if err != nil {
152+
log.Fatalf("could not create %s: (%v)", deployTestDir, err)
153+
}
154+
tlConfig.namespacedManPath = filepath.Join(deployTestDir, "empty.yaml")
155+
tlConfig.globalManPath = filepath.Join(deployTestDir, "empty.yaml")
156+
emptyBytes := []byte{}
157+
err = ioutil.WriteFile(tlConfig.globalManPath, emptyBytes, os.FileMode(fileutil.DefaultFileMode))
158+
if err != nil {
159+
log.Fatalf("could not create empty manifest file: (%v)", err)
160+
}
161+
defer func() {
162+
err := os.Remove(tlConfig.globalManPath)
163+
if err != nil {
164+
log.Fatalf("could not delete empty manifest file: (%v)", err)
165+
}
166+
}()
167+
}
144168
testArgs := []string{"test", args[0] + "/..."}
145169
testArgs = append(testArgs, "-"+test.KubeConfigFlag, tlConfig.kubeconfig)
146170
testArgs = append(testArgs, "-"+test.NamespacedManPathFlag, tlConfig.namespacedManPath)
@@ -151,7 +175,7 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
151175
if tlConfig.goTestFlags != "" {
152176
testArgs = append(testArgs, strings.Split(tlConfig.goTestFlags, " ")...)
153177
}
154-
if tlConfig.namespace != "" {
178+
if tlConfig.namespace != "" || tlConfig.noSetup {
155179
testArgs = append(testArgs, "-"+test.SingleNamespaceFlag, "-parallel=1")
156180
}
157181
dc := exec.Command("go", testArgs...)

pkg/test/framework.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ type Framework struct {
5757
}
5858

5959
func setup(kubeconfigPath, namespacedManPath *string) error {
60+
namespace := ""
61+
if *singleNamespace {
62+
namespace = os.Getenv(TestNamespaceEnv)
63+
}
6064
var err error
6165
var kubeconfig *rest.Config
6266
if *kubeconfigPath == "incluster" {
@@ -74,7 +78,11 @@ func setup(kubeconfigPath, namespacedManPath *string) error {
7478
kubeconfig, err = rest.InClusterConfig()
7579
*singleNamespace = true
7680
} else {
77-
kubeconfig, _, err = k8sInternal.GetKubeconfigAndNamespace(*kubeconfigPath)
81+
var kcNamespace string
82+
kubeconfig, kcNamespace, err = k8sInternal.GetKubeconfigAndNamespace(*kubeconfigPath)
83+
if *singleNamespace && namespace == "" {
84+
namespace = kcNamespace
85+
}
7886
}
7987
if err != nil {
8088
return fmt.Errorf("failed to build the kubeconfig: %v", err)
@@ -91,13 +99,6 @@ func setup(kubeconfigPath, namespacedManPath *string) error {
9199
return fmt.Errorf("failed to build the dynamic client: %v", err)
92100
}
93101
dynamicDecoder = serializer.NewCodecFactory(scheme).UniversalDeserializer()
94-
namespace := ""
95-
if *singleNamespace {
96-
namespace = os.Getenv(TestNamespaceEnv)
97-
if len(namespace) == 0 {
98-
return fmt.Errorf("namespace set in %s cannot be empty", TestNamespaceEnv)
99-
}
100-
}
101102
Global = &Framework{
102103
Client: &frameworkClient{Client: dynClient},
103104
KubeConfig: kubeconfig,

pkg/test/resource_creator.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ func (ctx *TestCtx) createFromYAML(yamlFile []byte, skipIfExists bool, cleanupOp
6666
}
6767
yamlSplit := bytes.Split(yamlFile, []byte("\n---\n"))
6868
for _, yamlSpec := range yamlSplit {
69+
// some autogenerated files may include an extra `---` at the end of the file
70+
if string(yamlSpec) == "" {
71+
continue
72+
}
6973
yamlSpec, err = setNamespaceYAML(yamlSpec, namespace)
7074
if err != nil {
7175
return err

0 commit comments

Comments
 (0)