v0.0.2
This release introduces many necessary changes in the way that the works including:
- Support for
klieint.Client
to provide centralized access to the klient types used to interact with Kubernetes - Introduction of package
envconf
to encapsulates types for environment configurations - Refactor of the
env.Environment
type to capture acontext.Context
value early during value construction - Refactor of signatures for
EnvFunc
andStepFunc
operation types to properly propagate context values - Step functions and environment functions now receive
envconf.Config
to access environment runtime configuration - Updated and new examples showing how to use these changes
- Updated documentation
Tests with value propagation using context.Context
The changes above allow test writers to construct tests using simpler abstractions. For example, the following snippet starts a kind cluster and uses context to propagate the name of the kubeconfig
file and the cluster name:
var testenv = env.New()
func TestMain(m *testing.M) {
testenv.Setup(
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
cluster := envconf.RandomName("my-cluster", 16)
kubecfg, err := createKindCluster(cluster)
if err != nil {
return ctx, err
}
return context.WithValue(context.WithValue(ctx, 1, kubecfg), 2, cluster), nil
},
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
kubecfg := ctx.Value(1).(string)
client, err := klient.NewWithKubeConfigFile(kubecfg)
if err != nil {
return ctx, fmt.Errorf("create klient.Client: %w", err)
}
cfg.WithClient(client) // set client in envconfig
return ctx, nil
},
)
...
os.Exit(testenv.Run(m))
}
In the test function, test writers can access the environment configuration to use the klient.Client
to interact with the API server as shown:
func TestListPods(t *testing.T) {
f := features.New("example with klient package").
Assess("get 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())
}
See the full example here.