|
| 1 | +--- |
| 2 | +title: SCS Conformance Test KaaS Sonobuoy |
| 3 | +type: |
| 4 | +status: Draft |
| 5 | +track: Global |
| 6 | +--- |
| 7 | + |
| 8 | +SovereignCloudStack (SCS) makes use of [Sonobuoy][sonobuoy]] as a test framework to run its tests on Kubernetes clusters, which are to be audited for compliance. |
| 9 | +The aim of using this framework is to make the execution of tests on a KaaS infrastructure as simple as possible. |
| 10 | +Hence this reduces the effort required to establish SCS conformity to a minimum. |
| 11 | + |
| 12 | +In short this is achieved by storing all tests in a container image, which can then be called and launched on the clusters managed by sonoobuoy. |
| 13 | + |
| 14 | +> A more detailed description of why the SCS has decided to use sonobuoys can be found on the corresponding [Decsision Record][sonbouy-decision-record] |
| 15 | +> In addition, sonobuoy is also used as the toolset for executing Kubernetes very own [conformance tests][k8s-conformance]. |
| 16 | +
|
| 17 | + |
| 18 | +This document is intended to assist conformance test authors to integrate their tests into the sonobuoy framework. |
| 19 | +This requires a user to write the conformance tests in Golang, as this is the language provided by the framework itself. |
| 20 | + |
| 21 | + |
| 22 | +# Step-by-step instructions for the development of sonobuoy tests using `docker` and `kind`: |
| 23 | + |
| 24 | +> This guide refers to the brief instructions provided in the [standards repository][scs-sonobuoy-example-guide] |
| 25 | +
|
| 26 | + |
| 27 | +## Prerequisite |
| 28 | + |
| 29 | +* [docker][docker-installation] |
| 30 | +* [kind][kind-installation] |
| 31 | +* [sonobuoy][sonobuoy-installation] |
| 32 | + |
| 33 | +``` |
| 34 | +go install github.com/vmware-tanzu/sonobuoy@latest |
| 35 | +``` |
| 36 | + |
| 37 | +## Set up development environment: |
| 38 | + |
| 39 | + |
| 40 | +1. Clone the standard repository containing the conformance tests and navigate to the Sonobuoy example located at `standards/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework` |
| 41 | + |
| 42 | + |
| 43 | +``` |
| 44 | +git clone https://github.com/SovereignCloudStack/standards |
| 45 | +cd standards/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/ |
| 46 | +``` |
| 47 | + |
| 48 | + |
| 49 | +2. Check the prerequisites |
| 50 | + |
| 51 | +Within this directory you will find a `Makefile`, which is also used to create the test images. |
| 52 | +To begin with, this can be used to check whether all the requirements for the development environment are met. |
| 53 | + |
| 54 | +``` |
| 55 | +make dev-prerequests |
| 56 | +``` |
| 57 | + |
| 58 | + |
| 59 | +3. Create a kind cluster |
| 60 | + |
| 61 | +Once all the prerequisite software is installed, you can proceed by starting an kind cluster using the `Makefile` as follows: |
| 62 | + |
| 63 | +``` |
| 64 | +make dev-setup |
| 65 | +``` |
| 66 | + |
| 67 | + |
| 68 | +4. Setting environment variables for the image build process |
| 69 | + |
| 70 | +``` |
| 71 | +kubectl config view |
| 72 | +``` |
| 73 | + |
| 74 | +``` |
| 75 | +export IMAGE_VERSION_TAG="dev" |
| 76 | +export K8S_HOST=<kind-cluster-ip> |
| 77 | +export K8S_PORT=<kind-cluster-port> |
| 78 | +``` |
| 79 | + |
| 80 | +## Create a test: |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +In general, SCS tests for KaaS are derived from standards that define certain expected behaviors and functionalities of Kubernetes. |
| 86 | +As an example for this step-by-step guide, let's assume a scenario in which there is a fictional standard called "scs-0299-v1-example-standard.md". |
| 87 | +Pretend that the fictitious standard here stipulates that at least one pod MUST run in the namespace "namespace-test-a". |
| 88 | + |
| 89 | +> The functions and behaviors to be tested MUST be precisely defined in a standard. |
| 90 | +> If you as a developer want to test something that you think is best tested but is not yet part of any standard, you MUST update the standard accordingly. |
| 91 | +
|
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +1. Create example test |
| 97 | + |
| 98 | +The `scs_k8s_tests` directory contains the Golang files that define the tests. |
| 99 | + |
| 100 | +``` |
| 101 | +cd standards/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework/scs_k8s_tests |
| 102 | +``` |
| 103 | + |
| 104 | +First create a test file according to your standard and adhere to the naming convention accordingly. |
| 105 | + |
| 106 | +* The prefix MUST contain the name of the standard in lower case letters. |
| 107 | +* As a suffix, the file must end with "_test.go". |
| 108 | + |
| 109 | +> The suffix requirement comes from the go test framework itself. All test files must end with `_test.go`. |
| 110 | +> Otherwise they will not be selected by the test environment. |
| 111 | +
|
| 112 | +As an example, we will create a file for the fictitious standard "scs-0299-v1-example-standard.md" as follows: |
| 113 | + |
| 114 | +``` |
| 115 | +touch scs_0299_v1_example_standard_test.go |
| 116 | +``` |
| 117 | + |
| 118 | +In this file, we can define the behavior we want to test for. |
| 119 | +As an example, we test here whether there are more than zero pods in the namespace "namespace-test-a". |
| 120 | +The execution of this test should fail by default as there should be no pods in the namespace and the namespace itself should not exist. |
| 121 | +The aim is to display the results of a failed test so that we can show their interpretation in a later step. |
| 122 | + |
| 123 | +> Attention!!!: in order for the framework to select the functions for testing, their names must begin with "TEST_" in accordance with the naming convention of the golang test framework. |
| 124 | +> TODO:!!! link to golang test framework docs |
| 125 | +
|
| 126 | +Copy the following text into the file `scs_0299_v1_example_standard_test.go`: |
| 127 | + |
| 128 | +``` |
| 129 | +package scs_k8s_tests |
| 130 | +
|
| 131 | +import ( |
| 132 | + "context" |
| 133 | + "testing" |
| 134 | + "time" |
| 135 | + "fmt" |
| 136 | + plugin_helper "github.com/vmware-tanzu/sonobuoy-plugins/plugin-helper" |
| 137 | + corev1 "k8s.io/api/core/v1" |
| 138 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 139 | + "sigs.k8s.io/e2e-framework/pkg/features" |
| 140 | +) |
| 141 | +
|
| 142 | +
|
| 143 | +// This function checks if there are any pods in 'namespace-test-a'. |
| 144 | +// The check should fail as there should be no pods present in this namespace. |
| 145 | +// The purpose of this function is to display the handling of failed tests. |
| 146 | +func Test_scs_0299_TestListPodsFailing(t *testing.T) { |
| 147 | + f := features.New("pod list").Assess( |
| 148 | + "pods from namespace 'namespace-test-a'", |
| 149 | + func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 150 | + var pods corev1.PodList |
| 151 | + err := cfg.Client().Resources("namespace-test-a").List(context.TODO(), &pods) |
| 152 | + if err != nil { |
| 153 | + t.Fatal(err) |
| 154 | + } |
| 155 | + t.Logf("found %d pods", len(pods.Items)) |
| 156 | + if len(pods.Items) == 0 { |
| 157 | + t.Fatal("no pods in namespace 'namespace-test-a'") |
| 158 | + } |
| 159 | + return ctx |
| 160 | + }) |
| 161 | +
|
| 162 | + testenv.Test(t, f.Feature()) |
| 163 | +} |
| 164 | +
|
| 165 | +``` |
| 166 | + |
| 167 | +3. Build the test image, upload it to the art cluster and run it |
| 168 | + |
| 169 | + |
| 170 | +To create the image, execute the following: |
| 171 | +``` |
| 172 | +make dev-build |
| 173 | +``` |
| 174 | +This creates the Sonobuoy image and automatically uploads it to the image register of the kind cluster. |
| 175 | + |
| 176 | + |
| 177 | +You can then run the tests by: |
| 178 | +``` |
| 179 | +make dev-run |
| 180 | +``` |
| 181 | + |
| 182 | +Depending on how extensive the tests are, this may take some time. |
| 183 | +To check the current test status manually, you can use the following command: |
| 184 | + |
| 185 | +``` |
| 186 | +sonobuoy status |
| 187 | +``` |
| 188 | + |
| 189 | + |
| 190 | +4. Retrieving the results |
| 191 | + |
| 192 | +Once all tests have been executed successfully, you can read the results and receive feedback. |
| 193 | +You can call up the results as follows: |
| 194 | + |
| 195 | +``` |
| 196 | +TODO:!!! be described in more detail |
| 197 | +``` |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | +## Clean up after: |
| 202 | + |
| 203 | +``` |
| 204 | +make dev-clean |
| 205 | +make dev-purge |
| 206 | +``` |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | +[sonobuoy]: https://sonobuoy.io/ |
| 211 | +[sonbouy-decision-record]: https://github.com/SovereignCloudStack/standards/blob/main/Standards/scs-0200-v1-using-sonobuoy-for-kaas-conformance-tests.md |
| 212 | +[k8s-conformance]: https://github.com/cncf/k8s-conformance/blob/master/instructions.md |
| 213 | + |
| 214 | +[kaas-sonobuoy-example] https://github.com/SovereignCloudStack/standards/tree/main/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework#sonobuoy-usage-for-development-of-tests |
| 215 | + |
| 216 | + |
| 217 | +[docker-installation] https://docs.docker.com/engine/install/ |
| 218 | +[sonobuoy-installation] https://sonobuoy.io/docs/v0.57.1/#installation |
| 219 | +[kind-installation]https://kind.sigs.k8s.io/docs/user/quick-start/#installation |
| 220 | + |
| 221 | +[scs-sonobuoy-example-guide] https://github.com/SovereignCloudStack/standards/tree/main/Tests/kaas/kaas-sonobuoy-go-example-e2e-framework#sonobuoy-usage-for-development-of-tests |
0 commit comments