Skip to content

Commit

Permalink
Fix every_test_custom_ns test
Browse files Browse the repository at this point in the history
  • Loading branch information
maruina committed May 2, 2023
1 parent 64d85de commit 2d12faa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
10 changes: 4 additions & 6 deletions examples/every_test_custom_ns/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ import (

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 {
Assess("pods from namespace", 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)
namespace := ctx.Value(GetNamespaceKey(t)).(string)
err := cfg.Client().Resources(namespace).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")
}
t.Logf("found %d pods in namespace %s", len(pods.Items), namespace)
return ctx
})

Expand Down
22 changes: 16 additions & 6 deletions examples/every_test_custom_ns/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"os"
"strings"
"testing"
"time"

Expand All @@ -29,6 +30,9 @@ import (
"sigs.k8s.io/e2e-framework/support/kind"
)

type NamespaceCtxKey string
type ClusterCtxKey string

var testenv env.Environment

func TestMain(m *testing.M) {
Expand All @@ -53,11 +57,14 @@ func TestMain(m *testing.M) {
cfg.WithKubeconfigFile(kubeconfig)

// propagate cluster value
return context.WithValue(ctx, "cluster", cluster), nil
return context.WithValue(ctx, ClusterCtxKey("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
cluster := ctx.Value(ClusterCtxKey("cluster")).(*kind.Cluster) // nil should be tested
if cluster == nil {
return ctx, fmt.Errorf("error getting kind cluster from context")
}
if err := cluster.Destroy(); err != nil {
return ctx, err
}
Expand All @@ -79,7 +86,7 @@ func TestMain(m *testing.M) {
// 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)
ctx = context.WithValue(ctx, GetNamespaceKey(t), ns)

t.Logf("Creating NS %v for test %v", ns, t.Name())
nsObj := v1.Namespace{}
Expand All @@ -89,14 +96,17 @@ func createNSForTest(ctx context.Context, cfg *envconf.Config, t *testing.T, run

// 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)))
ns := fmt.Sprint(ctx.Value(GetNamespaceKey(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()
func GetNamespaceKey(t *testing.T) NamespaceCtxKey {
if strings.Contains(t.Name(), "/") {
return NamespaceCtxKey(strings.Split(t.Name(), "/")[0])
}
return NamespaceCtxKey(t.Name())
}

0 comments on commit 2d12faa

Please sign in to comment.