Skip to content

Commit cc79a70

Browse files
authored
test local: fix reconcile 'cannot find RESTMapping' error (#826)
*/memcached_test.go: bump WaitFor(Operator)?Deployment timeouts to 60s * use fileutil file modes in memcached_test.go * cleanupTimeout to 10s * check if tlConfig.kubeconfig is empty in test local * check dynclient.New error
1 parent b0dce88 commit cc79a70

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
186186
}
187187
}
188188
testArgs := []string{"test", args[0] + "/..."}
189-
testArgs = append(testArgs, "-"+test.KubeConfigFlag, tlConfig.kubeconfig)
189+
if tlConfig.kubeconfig != "" {
190+
testArgs = append(testArgs, "-"+test.KubeConfigFlag, tlConfig.kubeconfig)
191+
}
190192
testArgs = append(testArgs, "-"+test.NamespacedManPathFlag, tlConfig.namespacedManPath)
191193
testArgs = append(testArgs, "-"+test.GlobalManPathFlag, tlConfig.globalManPath)
192194
testArgs = append(testArgs, "-"+test.ProjRootFlag, projutil.MustGetwd())

pkg/test/framework.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,11 @@ func AddToFrameworkScheme(addToScheme addToSchemeFunc, obj runtime.Object) error
144144
if err != nil {
145145
return err
146146
}
147-
dynClient, err := dynclient.New(Global.KubeConfig, dynclient.Options{Scheme: Global.Scheme, Mapper: restMapper})
148147
restMapper.Reset()
148+
dynClient, err := dynclient.New(Global.KubeConfig, dynclient.Options{Scheme: Global.Scheme, Mapper: restMapper})
149+
if err != nil {
150+
return fmt.Errorf("failed to initialize new dynamic client: (%v)", err)
151+
}
149152
err = wait.PollImmediate(time.Second, time.Second*10, func() (done bool, err error) {
150153
if *singleNamespace {
151154
err = dynClient.List(goctx.TODO(), &dynclient.ListOptions{Namespace: Global.Namespace}, obj)

test/e2e/memcached_test.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"time"
3030

3131
"github.com/ghodss/yaml"
32+
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
3233
"github.com/operator-framework/operator-sdk/internal/util/projutil"
3334
framework "github.com/operator-framework/operator-sdk/pkg/test"
3435
"github.com/operator-framework/operator-sdk/pkg/test/e2eutil"
@@ -41,9 +42,11 @@ import (
4142
)
4243

4344
const (
44-
filemode os.FileMode = 0664
45-
dirmode os.FileMode = 0750
46-
crYAML string = "apiVersion: \"cache.example.com/v1alpha1\"\nkind: \"Memcached\"\nmetadata:\n name: \"example-memcached\"\nspec:\n size: 3"
45+
crYAML string = "apiVersion: \"cache.example.com/v1alpha1\"\nkind: \"Memcached\"\nmetadata:\n name: \"example-memcached\"\nspec:\n size: 3"
46+
retryInterval = time.Second * 5
47+
timeout = time.Second * 60
48+
cleanupRetryInterval = time.Second * 1
49+
cleanupTimeout = time.Second * 10
4750
)
4851

4952
func TestMemcached(t *testing.T) {
@@ -64,7 +67,7 @@ func TestMemcached(t *testing.T) {
6467

6568
// Setup
6669
absProjectPath := filepath.Join(gopath, "src/github.com/example-inc")
67-
if err := os.MkdirAll(absProjectPath, dirmode); err != nil {
70+
if err := os.MkdirAll(absProjectPath, fileutil.DefaultDirFileMode); err != nil {
6871
t.Fatal(err)
6972
}
7073
if err := os.Chdir(absProjectPath); err != nil {
@@ -108,7 +111,7 @@ func TestMemcached(t *testing.T) {
108111
gopkgString := string(gopkg)
109112
gopkgLoc := strings.LastIndex(gopkgString, "\n name = \"github.com/operator-framework/operator-sdk\"\n")
110113
gopkgString = gopkgString[:gopkgLoc] + "\n source = \"https://github.com/" + prSlug + "\"\n revision = \"" + prSha + "\"\n" + gopkgString[gopkgLoc+1:]
111-
err = ioutil.WriteFile("Gopkg.toml", []byte(gopkgString), filemode)
114+
err = ioutil.WriteFile("Gopkg.toml", []byte(gopkgString), fileutil.DefaultFileMode)
112115
if err != nil {
113116
t.Fatalf("failed to write updated Gopkg.toml: %v", err)
114117
}
@@ -178,7 +181,7 @@ func TestMemcached(t *testing.T) {
178181
}
179182
}
180183
os.Remove("pkg/apis/cache/v1alpha1/memcached_types.go")
181-
err = ioutil.WriteFile("pkg/apis/cache/v1alpha1/memcached_types.go", bytes.Join(memcachedTypesFileLines, []byte("\n")), filemode)
184+
err = ioutil.WriteFile("pkg/apis/cache/v1alpha1/memcached_types.go", bytes.Join(memcachedTypesFileLines, []byte("\n")), fileutil.DefaultFileMode)
182185
if err != nil {
183186
t.Fatal(err)
184187
}
@@ -190,7 +193,7 @@ func TestMemcached(t *testing.T) {
190193
}
191194

192195
t.Log("Copying test files to ./test")
193-
if err = os.MkdirAll("./test", dirmode); err != nil {
196+
if err = os.MkdirAll("./test", fileutil.DefaultDirFileMode); err != nil {
194197
t.Fatalf("could not create test/e2e dir: %v", err)
195198
}
196199
cmdOut, err = exec.Command("cp", "-a", filepath.Join(gopath, "src/github.com/operator-framework/operator-sdk/test/e2e/incluster-test-code"), "./test/e2e").CombinedOutput()
@@ -227,7 +230,7 @@ func TestMemcached(t *testing.T) {
227230
// create crd
228231
filename := file.Name()
229232
framework.Global.NamespacedManPath = &filename
230-
err = ctx.InitializeClusterResources(&framework.CleanupOptions{TestContext: ctx, Timeout: time.Second * 10, RetryInterval: time.Second * 1})
233+
err = ctx.InitializeClusterResources(&framework.CleanupOptions{TestContext: ctx, Timeout: cleanupTimeout, RetryInterval: cleanupRetryInterval})
231234
if err != nil {
232235
t.Fatal(err)
233236
}
@@ -247,7 +250,7 @@ func memcachedLeaderTest(t *testing.T, f *framework.Framework, ctx *framework.Te
247250
return err
248251
}
249252

250-
err = e2eutil.WaitForOperatorDeployment(t, f.KubeClient, namespace, "memcached-operator", 1, time.Second*5, time.Second*30)
253+
err = e2eutil.WaitForOperatorDeployment(t, f.KubeClient, namespace, "memcached-operator", 1, retryInterval, timeout)
251254
if err != nil {
252255
return err
253256
}
@@ -263,7 +266,7 @@ func memcachedLeaderTest(t *testing.T, f *framework.Framework, ctx *framework.Te
263266
return err
264267
}
265268

266-
err = e2eutil.WaitForOperatorDeployment(t, f.KubeClient, namespace, "memcached-operator", 1, time.Second*5, time.Second*30)
269+
err = e2eutil.WaitForOperatorDeployment(t, f.KubeClient, namespace, "memcached-operator", 1, retryInterval, timeout)
267270
if err != nil {
268271
return err
269272
}
@@ -320,14 +323,14 @@ func memcachedScaleTest(t *testing.T, f *framework.Framework, ctx *framework.Tes
320323
filename := "deploy/cr.yaml"
321324
err := ioutil.WriteFile(filename,
322325
[]byte(crYAML),
323-
filemode)
326+
fileutil.DefaultFileMode)
324327
if err != nil {
325328
return err
326329
}
327330

328331
// create memcached custom resource
329332
framework.Global.NamespacedManPath = &filename
330-
err = ctx.InitializeClusterResources(&framework.CleanupOptions{TestContext: ctx, Timeout: time.Second * 10, RetryInterval: time.Second * 1})
333+
err = ctx.InitializeClusterResources(&framework.CleanupOptions{TestContext: ctx, Timeout: cleanupTimeout, RetryInterval: cleanupRetryInterval})
331334
if err != nil {
332335
return err
333336
}
@@ -338,7 +341,7 @@ func memcachedScaleTest(t *testing.T, f *framework.Framework, ctx *framework.Tes
338341
return err
339342
}
340343
// wait for example-memcached to reach 3 replicas
341-
err = e2eutil.WaitForDeployment(t, f.KubeClient, namespace, "example-memcached", 3, time.Second*5, time.Second*30)
344+
err = e2eutil.WaitForDeployment(t, f.KubeClient, namespace, "example-memcached", 3, retryInterval, timeout)
342345
if err != nil {
343346
return err
344347
}
@@ -367,7 +370,7 @@ func memcachedScaleTest(t *testing.T, f *framework.Framework, ctx *framework.Tes
367370
}
368371

369372
// wait for example-memcached to reach 4 replicas
370-
return e2eutil.WaitForDeployment(t, f.KubeClient, namespace, "example-memcached", 4, time.Second*5, time.Second*30)
373+
return e2eutil.WaitForDeployment(t, f.KubeClient, namespace, "example-memcached", 4, retryInterval, timeout)
371374
}
372375

373376
func MemcachedLocal(t *testing.T) {
@@ -427,7 +430,7 @@ func MemcachedCluster(t *testing.T) {
427430
t.Fatal(err)
428431
}
429432
operatorYAML = bytes.Replace(operatorYAML, []byte("imagePullPolicy: Always"), []byte("imagePullPolicy: Never"), 1)
430-
err = ioutil.WriteFile("deploy/operator.yaml", operatorYAML, filemode)
433+
err = ioutil.WriteFile("deploy/operator.yaml", operatorYAML, fileutil.DefaultFileMode)
431434
if err != nil {
432435
t.Fatal(err)
433436
}
@@ -461,7 +464,7 @@ func MemcachedCluster(t *testing.T) {
461464
// create namespaced resources
462465
filename := file.Name()
463466
framework.Global.NamespacedManPath = &filename
464-
err = ctx.InitializeClusterResources(&framework.CleanupOptions{TestContext: ctx, Timeout: time.Second * 10, RetryInterval: time.Second * 1})
467+
err = ctx.InitializeClusterResources(&framework.CleanupOptions{TestContext: ctx, Timeout: cleanupTimeout, RetryInterval: cleanupRetryInterval})
465468
if err != nil {
466469
t.Fatal(err)
467470
}
@@ -472,7 +475,7 @@ func MemcachedCluster(t *testing.T) {
472475
t.Fatal(err)
473476
}
474477
// wait for memcached-operator to be ready
475-
err = e2eutil.WaitForOperatorDeployment(t, framework.Global.KubeClient, namespace, "memcached-operator", 1, time.Second*5, time.Second*30)
478+
err = e2eutil.WaitForOperatorDeployment(t, framework.Global.KubeClient, namespace, "memcached-operator", 1, retryInterval, timeout)
476479
if err != nil {
477480
t.Fatal(err)
478481
}
@@ -494,7 +497,7 @@ func MemcachedClusterTest(t *testing.T) {
494497
// create sa
495498
filename := "deploy/service_account.yaml"
496499
framework.Global.NamespacedManPath = &filename
497-
err := ctx.InitializeClusterResources(&framework.CleanupOptions{TestContext: ctx, Timeout: time.Second * 10, RetryInterval: time.Second * 1})
500+
err := ctx.InitializeClusterResources(&framework.CleanupOptions{TestContext: ctx, Timeout: cleanupTimeout, RetryInterval: cleanupRetryInterval})
498501
if err != nil {
499502
t.Fatal(err)
500503
}
@@ -503,15 +506,15 @@ func MemcachedClusterTest(t *testing.T) {
503506
// create rbac
504507
filename = "deploy/role.yaml"
505508
framework.Global.NamespacedManPath = &filename
506-
err = ctx.InitializeClusterResources(&framework.CleanupOptions{TestContext: ctx, Timeout: time.Second * 10, RetryInterval: time.Second * 1})
509+
err = ctx.InitializeClusterResources(&framework.CleanupOptions{TestContext: ctx, Timeout: cleanupTimeout, RetryInterval: cleanupRetryInterval})
507510
if err != nil {
508511
t.Fatal(err)
509512
}
510513
t.Log("Created role")
511514

512515
filename = "deploy/role_binding.yaml"
513516
framework.Global.NamespacedManPath = &filename
514-
err = ctx.InitializeClusterResources(&framework.CleanupOptions{TestContext: ctx, Timeout: time.Second * 10, RetryInterval: time.Second * 1})
517+
err = ctx.InitializeClusterResources(&framework.CleanupOptions{TestContext: ctx, Timeout: cleanupTimeout, RetryInterval: cleanupRetryInterval})
515518
if err != nil {
516519
t.Fatal(err)
517520
}

test/test-framework/test/e2e/memcached_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"k8s.io/apimachinery/pkg/types"
3030
)
3131

32-
var (
32+
const (
3333
retryInterval = time.Second * 5
3434
timeout = time.Second * 60
3535
cleanupRetryInterval = time.Second * 1

0 commit comments

Comments
 (0)