|
| 1 | +# Testing Kubernetes Resources |
| 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. The test |
| 5 | +is split into two parts, the test suite definition and the test functions. |
| 6 | + |
| 7 | +## Test suite definition |
| 8 | +File `main_tests.go` sets up the test suite for the package. |
| 9 | + |
| 10 | +### Declare the global |
| 11 | +The first step is to declare the global environment that will used to run the test. |
| 12 | + |
| 13 | +```go |
| 14 | +var ( |
| 15 | + testenv env.Environment |
| 16 | +) |
| 17 | + |
| 18 | +func TestMain(m *testing.M) { |
| 19 | + testenv = env.New() |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +### Define environment `Setup` functions |
| 24 | +The next step is to define some environment functions. Here we have two `Setup` EnvFuncs |
| 25 | +to do the following setups: |
| 26 | + * The first `Setup` func, uses functions from `kindsupport.go`, to create a kind cluster and gets the kubeconfig file as a result |
| 27 | + * The second setup func, creates a `klient.Client` type used to interact with the API server |
| 28 | +```go |
| 29 | +func TestMain(m *testing.M) { |
| 30 | + testenv = env.New() |
| 31 | + testenv.Setup( |
| 32 | + // env func: creates kind cluster, propagate kubeconfig file name |
| 33 | + func(ctx context.Context, cfg *envconf.Config) (context.Context, error) { |
| 34 | + cluster := envconf.RandomName("my-cluster", 16) |
| 35 | + kubecfg, err := createKindCluster(cluster) |
| 36 | + if err != nil { |
| 37 | + return ctx, err |
| 38 | + } |
| 39 | + // stall a bit to allow most pods to come up |
| 40 | + time.Sleep(time.Second*10) |
| 41 | + |
| 42 | + // propagate cluster name and kubeconfig file name |
| 43 | + return context.WithValue(context.WithValue(ctx, 1, kubecfg), 2, cluster), nil |
| 44 | + }, |
| 45 | + // env func: creates a klient.Client for the envconfig.Config |
| 46 | + func(ctx context.Context, cfg *envconf.Config) (context.Context, error) { |
| 47 | + kubecfg := ctx.Value(1).(string) |
| 48 | + // create a klient.Client and set it for the env config |
| 49 | + client, err := klient.NewWithKubeConfigFile(kubecfg) |
| 50 | + if err != nil { |
| 51 | + return ctx, fmt.Errorf("create klient.Client: %w", err) |
| 52 | + } |
| 53 | + cfg.WithClient(client) // set client in envconfig |
| 54 | + return ctx, nil |
| 55 | + }, |
| 56 | + ) |
| 57 | +... |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +### Define environment `Finish` for cleanup |
| 62 | +The last step in creating the suite is to define cleanup code for the environment. |
| 63 | + |
| 64 | +```go |
| 65 | +func TestMain(m *testing.M) { |
| 66 | + testenv = env.New() |
| 67 | + ... |
| 68 | + testenv.Finish( |
| 69 | + // Teardown func: delete kind cluster and delete kubecfg file |
| 70 | + func(ctx context.Context, cfg *envconf.Config) (context.Context, error) { |
| 71 | + kubecfg := ctx.Value(1).(string) |
| 72 | + cluster := ctx.Value(2).(string) |
| 73 | + if err := deleteKindCluster(cluster, kubecfg); err != nil { |
| 74 | + return ctx, err |
| 75 | + } |
| 76 | + return ctx, nil |
| 77 | + }, |
| 78 | + ) |
| 79 | +} |
| 80 | +``` |
| 81 | +### Start the suite |
| 82 | +The last step in defining the test suite is to launch it: |
| 83 | +```go |
| 84 | +func TestMain(m *testing.M) { |
| 85 | + testenv = env.New() |
| 86 | + ... |
| 87 | + os.Exit(testenv.Run(m)) |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +* Uses support functions, found in `kindsupport.go` to stand up a kind cluster |
| 92 | + |
| 93 | +The test suite uses context propagation to propagate the `kubeconfig` file name and the name of the cluster, that created, |
| 94 | +so that they can be cleaned in the `Finish` stage. |
| 95 | + |
| 96 | +## Test functions |
| 97 | +The framework uses a regular Go test function to define the end-to-end test. Once a suite is launched, after its setup |
| 98 | +is successful, Go test will run the Go test functions in the package. |
| 99 | + |
| 100 | +### Testing Kubernetes |
| 101 | +The test functions in this example, found in `k8s_test.go`, use the E2E test harness framework to define a test feature. The framework |
| 102 | +allows test authors to inject custom function definitions as hooks that get executed during the test. The following |
| 103 | +example defines one test feature with a single assessment. The assessment retrieves pods in the kube-system |
| 104 | +namespace and inspect the quantity returned. |
| 105 | + |
| 106 | +```go |
| 107 | +func TestListPods(t *testing.T) { |
| 108 | + f := features.New("example with klient package"). |
| 109 | + Assess("get pods from kube-system", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 110 | + var pods corev1.PodList |
| 111 | + err := cfg.Client().Resources("kube-system").List(context.TODO(), &pods) |
| 112 | + if err != nil { |
| 113 | + t.Fatal(err) |
| 114 | + } |
| 115 | + t.Logf("found %d pods", len(pods.Items)) |
| 116 | + if len(pods.Items) == 0 { |
| 117 | + t.Fatal("no pods in namespace kube-system") |
| 118 | + } |
| 119 | + return ctx |
| 120 | + }) |
| 121 | + |
| 122 | + testenv.Test(t, f.Feature()) |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +## Run the test |
| 127 | +Use the Go test tool to run the test. |
| 128 | + |
| 129 | +```bash |
| 130 | +$ go test . -v |
| 131 | +``` |
0 commit comments