generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more info for before/after hooks
- Adds two different types of functions for actions to store/call which take features and *testing.T objects - This allows before/after hooks that can actually reference the object in question - This enables an extremely useful and common test case of creating a namespace for each test - The namespace-per-test use case is shown in a new example Signed-off-by: John Schnake <[email protected]>
- Loading branch information
1 parent
96bd618
commit 1ada6a6
Showing
10 changed files
with
323 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Custom Namespaces For Every Test | ||
|
||
This example shows you how to use the env hooks in order to set up | ||
a custom namespace for every test. This could easily be done for every | ||
feature as well if that is your preference. | ||
|
||
First, you'll have to set up the env. In this example we assume an in-cluster configuration. | ||
```go | ||
var testenv env.Environment | ||
|
||
func TestMain(m *testing.M) { | ||
testenv = env.New() | ||
... | ||
} | ||
``` | ||
Second, set the BeforeEachTest hook to create the namespace. We store it in the | ||
context so that it can be looked up on a per-test basis for deletion. | ||
```go | ||
testenv.BeforeEachTest(func(ctx context.Context, cfg *envconf.Config, t *testing.T) (context.Context, error) { | ||
return createNSForTest(ctx,cfg,t,runID) | ||
}) | ||
... | ||
// The creation uses the typical c.Resources() object. | ||
cfg.Client().Resources().Create(ctx,&nsObj) | ||
``` | ||
Third, set the AfterEachTest hook to lookup and delete the namespace. | ||
```go | ||
testenv.AfterEachTest(func(ctx context.Context, cfg *envconf.Config, t *testing.T) (context.Context, error) { | ||
return deleteNSForTest(ctx,cfg,t,runID) | ||
}) | ||
... | ||
// The deletion uses the typical c.Resources() object. | ||
cfg.Client().Resources().Delete(ctx,&nsObj) | ||
``` | ||
|
||
So, tying it all together, the `TestMain` looks like this: | ||
```go | ||
func TestMain(m *testing.M) { | ||
testenv = env.New() | ||
|
||
// Specifying a run ID so that multiple runs wouldn't collide. | ||
runID := envconf.RandomName("", 4) | ||
|
||
/* Skipping cluster creation for brevity */ | ||
|
||
testenv.BeforeEachTest(func(ctx context.Context, cfg *envconf.Config, t *testing.T) (context.Context, error) { | ||
return createNSForTest(ctx, cfg, t, runID) | ||
}) | ||
testenv.AfterEachTest(func(ctx context.Context, cfg *envconf.Config, t *testing.T) (context.Context, error) { | ||
return deleteNSForTest(ctx, cfg, t, runID) | ||
}) | ||
|
||
os.Exit(testenv.Run(m)) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package every_test_custom_ns | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/e2e-framework/pkg/envconf" | ||
"sigs.k8s.io/e2e-framework/pkg/features" | ||
) | ||
|
||
func TestListPods(t *testing.T) { | ||
f := features.New("pod list"). | ||
Assess("pods from kube-system", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { | ||
var pods corev1.PodList | ||
err := cfg.Client().Resources("kube-system").List(context.TODO(), &pods) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Logf("found %d pods", len(pods.Items)) | ||
if len(pods.Items) == 0 { | ||
t.Fatal("no pods in namespace kube-system") | ||
} | ||
return ctx | ||
}) | ||
|
||
testenv.Test(t, f.Feature()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package every_test_custom_ns | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/e2e-framework/pkg/env" | ||
"sigs.k8s.io/e2e-framework/pkg/envconf" | ||
"sigs.k8s.io/e2e-framework/support/kind" | ||
) | ||
|
||
var testenv env.Environment | ||
|
||
func TestMain(m *testing.M) { | ||
testenv = env.New() | ||
|
||
// Specifying a run ID so that multiple runs wouldn't collide. | ||
runID := envconf.RandomName("ns", 4) | ||
|
||
testenv.Setup( | ||
// Step: creates kind cluster, propagate kind cluster object | ||
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) { | ||
name := envconf.RandomName("my-cluster", 16) | ||
cluster := kind.NewCluster(name) | ||
kubeconfig, err := cluster.Create() | ||
if err != nil { | ||
return ctx, err | ||
} | ||
// stall a bit to allow most pods to come up | ||
time.Sleep(time.Second * 10) | ||
|
||
// update environment with kubecofig file | ||
cfg.WithKubeconfigFile(kubeconfig) | ||
|
||
// propagate cluster value | ||
return context.WithValue(ctx, "cluster", cluster), nil | ||
}).Finish( | ||
// Teardown func: delete kind cluster | ||
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) { | ||
cluster := ctx.Value("cluster").(*kind.Cluster) // nil should be tested | ||
if err := cluster.Destroy(); err != nil { | ||
return ctx, err | ||
} | ||
return ctx, nil | ||
}, | ||
) | ||
|
||
testenv.BeforeEachTest(func(ctx context.Context, cfg *envconf.Config, t *testing.T) (context.Context, error) { | ||
return createNSForTest(ctx, cfg, t, runID) | ||
}) | ||
testenv.AfterEachTest(func(ctx context.Context, cfg *envconf.Config, t *testing.T) (context.Context, error) { | ||
return deleteNSForTest(ctx, cfg, t, runID) | ||
}) | ||
|
||
os.Exit(testenv.Run(m)) | ||
} | ||
|
||
// CreateNSForTest creates a random namespace with the runID as a prefix. It is stored in the context | ||
// so that the deleteNSForTest routine can look it up and delete it. | ||
func createNSForTest(ctx context.Context, cfg *envconf.Config, t *testing.T, runID string) (context.Context, error) { | ||
ns := envconf.RandomName(runID, 10) | ||
ctx = context.WithValue(ctx, nsKey(t), ns) | ||
|
||
t.Logf("Creating NS %v for test %v", ns, t.Name()) | ||
nsObj := v1.Namespace{} | ||
nsObj.Name = ns | ||
return ctx, cfg.Client().Resources().Create(ctx, &nsObj) | ||
} | ||
|
||
// DeleteNSForTest looks up the namespace corresponding to the given test and deletes it. | ||
func deleteNSForTest(ctx context.Context, cfg *envconf.Config, t *testing.T, runID string) (context.Context, error) { | ||
ns := fmt.Sprint(ctx.Value(nsKey(t))) | ||
t.Logf("Deleting NS %v for test %v", ns, t.Name()) | ||
|
||
nsObj := v1.Namespace{} | ||
nsObj.Name = ns | ||
return ctx, cfg.Client().Resources().Delete(ctx, &nsObj) | ||
} | ||
|
||
func nsKey(t *testing.T) string { | ||
return "NS-for-%v" + t.Name() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.