You can define complex test logic using features.New(). This allows you to encapsulate the lifecycle of a Kubernetes resource:
- Setup: Create the resource (e.g., a Deployment) using
cfg.Client().Resources().Create(). - Assess: Verify the resource exists and is in the expected state using
cfg.Client().Resources().Get(). You can pass data from Assess to Teardown using context.WithValue. - Teardown: Clean up the resource using
cfg.Client().Resources().Delete().
deploymentFeature := features.New("appsv1/deployment").
Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
// start a deployment
deployment := newDeployment(cfg.Namespace(), "test-deployment", 1)
if err := cfg.Client().Resources().Create(ctx, deployment); err != nil {
t.Fatal(err)
}
time.Sleep(2 * time.Second)
return ctx
}).
Assess("deployment creation", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
var dep appsv1.Deployment
if err := cfg.Client().Resources().Get(ctx, "test-deployment", cfg.Namespace(), &dep); err != nil {
t.Fatal(err)
}
if &dep != nil {
t.Logf("deployment found: %s", dep.Name)
}
return context.WithValue(ctx, "test-deployment", &dep)
}).
Teardown(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
dep := ctx.Value("test-deployment").(*appsv1.Deployment)
if err := cfg.Client().Resources().Delete(ctx, dep); err != nil {
t.Fatal(err)
}
return ctx
}).Feature()
testenv.Test(t, deploymentFeature)