Real-life example
When working with cluster components, or building custom resources, that are deployed in Kubernetes, it is often necessary to run full end-to-end tests to ensure all aspects of your solutions work as intended.
Project e2e-framework sigs.k8s.io/e2e-framework
Project e2e-framework, from Kubernetes-SIGs, makes it easy to create and run end-to-end tests using the standard Go test tool. Some high-level goals of the project includes:
Provide a sensible programmatic API using Go’s built-in test tools
Expose packages that are easy to programmatically consume
Provide helper packages that makes it easy to interact with the cluster and the Kubernetes API server
Hello e2e-framework
An e2e-framework test is written as a normal Go test function. First, you will need to get the e2e-framework module as a dependency for your project:
The first step in using e2e-framework is to (programmatically) setup and configure a test environment, tenv
, that will be used to run the defined test.
Prior to continue, it should be understood that e2e-framework tests are broken into units called features
. A feature can have a name, arbitrary labels (used for filtering), and lifecycle setup/teardown functions. Crucially, features can also include assessment functions which contain test logic for the feature.
Now that we know the parts of an e2e-framework test, let us define a simple Go test function with a simple test feature, feat
:
In the example above, feature feat
has title “Hello Feature” and defines label type=simple
. It also includes a Setup
function that is used to initialize variable name
. Method call Assess
defines an assessment function, that contains a simple test logic, with title “test message”. Lastly, test environment tenv
is used to trigger the test with method call tenv.Test(t, feat.Feature())
.
Running the e2e-framework test above is as simple as using the go test
command as shown below:
An end-to-end Kubernetes test
Now, let us explore how the e2e-framework can be used to write code to test resources deployed on a Kubernetes cluster.
Configure a test environment in TestMain
The snippet below uses Go test function TestMain
to programmatically configure a test environment, testenv
, in a test suite. Inside TestMain
, the code uses the test environment to define lifecycle function Setup
, triggered before any test feature is executed. The test environment also defines a teardown function, Finish
, which is triggered after all feature tests are executed in the test suite.
The source snippet above also highlights the fact that the e2e-framework comes bundled with several pre-defined environment functions (in package envfuncs
). This example uses environment function envfuncs.CreateKindCluster
to create a KinD cluster during the environment setup. Conversely, environment function envfuncs.DestroyKindCluster
is used to teardown the cluster after the test is finished.
Cluster component end-to-end test
Now let us create a simple test that uses the e2e-framework library to do the followings:
Create an appv1.Deployment on the Kubernetes cluster
Test to ensure the deployment is fully deployed within a specified time
Delete the deployment from the cluster
What is going on in the code above, you may be wondering? Go test function TestDeployment
defines feature variable feat
with a Setup
, an Assess
, and a Teardown
method which creates a Deployment object, waits for the deployment replicas to be fully deployed, and deletes the deployment respectively.
The test source code above also highlights the wait
package, which comes with the e2e-framework, to declare a condition and wait for that condition to become true within a time period. In the example above, the wait
package is used to wait for the Deployment to be marked available within one minute. If that condition fails, an error is returned and the test fails.
Testing the code
Because the e2e-framework integrates well with KinD, all that is required to run the test above is the following command:
The framework will automatically create a kind cluster, create the deployment object in the cluster, run the test specified in the assessment, delete the object, and finally delete the cluster once all tests have completed.
e2e-framework flags
The e2e-framework also exposes several flags to help you configure the execution of your tests at runtime. For instance, the following will only execute tests with features titled “deployment”.
Or, you can specify to only run features with a specific label.
The framework also supports flags that skips tests based on provided values. For instance, the following will skip assessments with title “pod-unstable” during test execution.
Read more about e2e-framework supported flags here.
Conclusion
This post provides a high-level introduction that shows how to get started with project Kubernetes-SIGs/e2e-framework to write and run end-to-end tests for your Kubernetes cluster components. The framework provides packages to compose and run tests that can automatically start a local cluster, deploy components on the cluster, and run assessments that test those components, and teardown all resources when done.
Last updated