|
| 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 | +``` |
0 commit comments