Skip to content

Commit 63fa8b0

Browse files
authored
Merge pull request #46 from vladimirvivien/support-functions
Kind and Namespace `Environment.Func` functions + Examples
2 parents c834d8f + 1197268 commit 63fa8b0

File tree

15 files changed

+628
-325
lines changed

15 files changed

+628
-325
lines changed

examples/custom_env_funcs/README.md

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Using Custom Environment Funcs
2+
3+
This package demonstrates how to use all part of the framework to setup and run an
4+
end-to-end test that retrieves API objects from Kubernetes for assessments. Specifically,
5+
it shows how to use custom environment functions when setting up testing environment.
6+
7+
## TestMain (test suite)
8+
File `main_tests.go` sets up the test suite for the package. In this test, the environment functions
9+
are used to
10+
11+
### Declare the test environment
12+
The first step is to declare the global environment that will be used to run the test.
13+
14+
```go
15+
var (
16+
testenv env.Environment
17+
)
18+
19+
func TestMain(m *testing.M) {
20+
testenv = env.New()
21+
}
22+
```
23+
24+
### Define an environment `Setup` function
25+
The next step is to define an environment function in the `Setup` method as shown below. The example
26+
shows a function that is used to create a kind cluster (using the `support/kind` package), then
27+
stores the cluster (of type `*kind.Cluster`) in the context for future operations.
28+
29+
```go
30+
func TestMain(m *testing.M) {
31+
testenv = env.New()
32+
testenv.Setup(
33+
// Step: creates kind cluster, propagate kind cluster object
34+
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
35+
name := envconf.RandomName("my-cluster", 16)
36+
cluster := kind.NewCluster(name)
37+
kubeconfig, err := cluster.Create()
38+
if err != nil {
39+
return ctx, err
40+
}
41+
// stall a bit to allow most pods to come up
42+
time.Sleep(time.Second * 10)
43+
44+
// update environment with kubecofig file
45+
if _, err := cfg.WithKubeconfigFile(kubeconfig); err != nil {
46+
return ctx, err
47+
}
48+
49+
// propagate cluster value
50+
return context.WithValue(ctx, 1, cluster), nil
51+
},
52+
)
53+
...
54+
}
55+
```
56+
57+
### Define a teardown operation in `Finish`
58+
The next custom operation is defined as parameter for the `Finish` method which is executed at the end of all
59+
package tests. The operation retrieves the cluster value, from the context, and calls its destroy method to delete
60+
the kind cluster.
61+
62+
```go
63+
func TestMain(m *testing.M) {
64+
testenv = env.New()
65+
...
66+
testenv.Finish(
67+
// Teardown func: delete kind cluster
68+
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
69+
cluster := ctx.Value(1).(*kind.Cluster) // nil should be tested
70+
if err := cluster.Destroy(); err != nil{
71+
return ctx, err
72+
}
73+
return ctx, nil
74+
},
75+
)
76+
77+
os.Exit(testenv.Run(m))
78+
}
79+
```
80+
81+
### Start the test suite
82+
The last step in defining the test suite is to launch it:
83+
```go
84+
func TestMain(m *testing.M) {
85+
...
86+
os.Exit(testenv.Run(m))
87+
}
88+
```
89+
## Test functions
90+
The framework uses a regular Go test function to define the end-to-end test. Once a suite is launched, after its setup
91+
is successful, Go test will run the Go test functions in the package.
92+
93+
### Testing Kubernetes
94+
The test functions in this example, found in `k8s_test.go`, use the framework to define a test feature. The following
95+
example defines one test feature with a single assessment. The assessment retrieves pods in the `kube-system`
96+
namespace and inspect the quantity returned.
97+
98+
```go
99+
func TestKubernetes(t *testing.T) {
100+
f := features.New("example with klient package").
101+
Assess("get pods from kube-system", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
102+
var pods corev1.PodList
103+
err := cfg.Client().Resources("kube-system").List(context.TODO(), &pods)
104+
if err != nil {
105+
t.Fatal(err)
106+
}
107+
t.Logf("found %d pods", len(pods.Items))
108+
if len(pods.Items) == 0 {
109+
t.Fatal("no pods in namespace kube-system")
110+
}
111+
return ctx
112+
})
113+
114+
testenv.Test(t, f.Feature())
115+
}
116+
```
117+
118+
## Run the test
119+
Use the Go test tool to run the test.
120+
121+
```bash
122+
$ go test . -v
123+
```

examples/k8s/k8s_test.go examples/custom_env_funcs/k8s_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package k8s
17+
package custom_env_funcs
1818

1919
import (
2020
"context"
@@ -26,8 +26,8 @@ import (
2626
)
2727

2828
func TestListPods(t *testing.T) {
29-
f := features.New("example with klient package").
30-
Assess("get pods from kube-system", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
29+
f := features.New("pod list").
30+
Assess("pods from kube-system", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
3131
var pods corev1.PodList
3232
err := cfg.Client().Resources("kube-system").List(context.TODO(), &pods)
3333
if err != nil {
@@ -41,4 +41,4 @@ func TestListPods(t *testing.T) {
4141
})
4242

4343
testenv.Test(t, f.Feature())
44-
}
44+
}

examples/k8s/main_test.go examples/custom_env_funcs/main_test.go

+16-20
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package k8s
17+
package custom_env_funcs
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"os"
2322
"testing"
2423
"time"
2524

2625
"sigs.k8s.io/e2e-framework/pkg/env"
2726
"sigs.k8s.io/e2e-framework/pkg/envconf"
27+
"sigs.k8s.io/e2e-framework/support/kind"
2828
)
2929

3030
var (
@@ -34,39 +34,35 @@ var (
3434
func TestMain(m *testing.M) {
3535
testenv = env.New()
3636
testenv.Setup(
37-
// env func: creates kind cluster, propagate kubeconfig file name
37+
// Step: creates kind cluster, propagate kind cluster object
3838
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
39-
cluster := envconf.RandomName("my-cluster", 16)
40-
kubecfg, err := createKindCluster(cluster)
39+
name := envconf.RandomName("my-cluster", 16)
40+
cluster := kind.NewCluster(name)
41+
kubeconfig, err := cluster.Create()
4142
if err != nil {
4243
return ctx, err
4344
}
4445
// stall a bit to allow most pods to come up
4546
time.Sleep(time.Second * 10)
4647

47-
// propagate cluster name and kubeconfig file name
48-
return context.WithValue(context.WithValue(ctx, 1, kubecfg), 2, cluster), nil
49-
},
50-
// env func: creates a klient.Client for the envconfig.Config
51-
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
52-
kubecfg := ctx.Value(1).(string)
53-
_, err := cfg.WithKubeconfigFile(kubecfg) // set client in envconfig
54-
if err != nil {
55-
return ctx, fmt.Errorf("create klient.Client: %w", err)
48+
// update environment with kubecofig file
49+
if _, err := cfg.WithKubeconfigFile(kubeconfig); err != nil {
50+
return ctx, err
5651
}
57-
return ctx, nil
52+
53+
// propagate cluster value
54+
return context.WithValue(ctx, 1, cluster), nil
5855
},
5956
).Finish(
60-
// Teardown func: delete kind cluster and delete kubecfg file
57+
// Teardown func: delete kind cluster
6158
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
62-
kubecfg := ctx.Value(1).(string)
63-
cluster := ctx.Value(2).(string)
64-
if err := deleteKindCluster(cluster, kubecfg); err != nil {
59+
cluster := ctx.Value(1).(*kind.Cluster) // nil should be tested
60+
if err := cluster.Destroy(); err != nil{
6561
return ctx, err
6662
}
6763
return ctx, nil
6864
},
6965
)
7066

7167
os.Exit(testenv.Run(m))
72-
}
68+
}

examples/k8s/README.md

-131
This file was deleted.

0 commit comments

Comments
 (0)