v0.0.3
v0.0.3
This release continues to make progress in making the framework usable in many real scenarios.
- Rename project default branch to
main
- Ability to use start, create, and destroy kind clusters during tests
- Clear way to inject klient.Client into environment config using a kubeconfig file
- Additional resource helper function to Patch resource objects
- Ability to test multiple features using
env.Test(feat1, feat2, ...)
- New pre-defined functions for launching kind clusters and automatically create Namespaces for tests
- Updated examples and documentations
Example
See how the pre-defined enviroment functions can be used to create kind clusters and generate a namespace to be used for all tests in the package.
See full example here
TestMain
func TestMain(m *testing.M) {
testenv = env.New()
kindClusterName := envconf.RandomName("my-cluster", 16)
namespace := envconf.RandomName("myns", 16)
testenv.Setup(
envfuncs.CreateKindCluster(kindClusterName),
envfuncs.CreateNamespace(namespace),
)
testenv.Finish(
envfuncs.DeleteNamespace(namespace),
envfuncs.DestroyKindCluster(kindClusterName),
)
os.Exit(testenv.Run(m))
}
TestKubernetes
Note accessing the current namespace using cfg.Namespace()
:
func TestKubernetes(t *testing.T) {
// feature uses pre-generated namespace (see TestMain)
depFeature := features.New("appsv1/deployment").
Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
// insert 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, podFeature, depFeature)
}
...