kubernetes/test/e2e/framework
Patrick Ohly 4a3d822689 DRA e2e: make driver deployment possible in Go unit tests
This leverages ktesting as wrapper around Ginkgo and testing.T to make all
helper code that is needed to deploy a DRA driver available to Go unit
tests and thus integration tests.

How to proceed with unifying helper code for integration and E2E testing is
open. This is just a minimal first step in that direction. Ideally, such
code should be in separate packages where usage of Ginkgo, e2e/framework
and gomega.Expect/Eventually/Consistently are forbidden.

While at it, the builder gets extended to make cleanup optional.
This will be needed for upgrade/downgrade testing with sub-tests.

(cherry picked from commit 7c7b1e1018)
2026-01-16 07:53:00 +01:00
..
auth Merge pull request #131409 from bertinatto/fix-authz-test 2025-05-08 14:39:15 -07:00
autoscaling Add port name and custom metric name to autoscaling utils 2025-10-10 17:13:34 +08:00
config
conformance DRA: CRUD conformance tests 2025-10-02 17:43:33 +02:00
daemonset KEP-5471: Extend tolerations operators (#134665) 2025-11-10 12:42:54 -08:00
debug chore: remove residual uint64ptr usage with ptr package 2025-07-08 22:20:16 +08:00
deployment e2e: add TERM trap to pod sleep command 2024-06-11 13:52:47 +08:00
endpointslice Add e2eendpointslice.WaitForEndpointPorts, use in some tests. 2025-07-17 15:34:34 -04:00
events
gpu Remove remants of broken stuff - nvidia/autoscaling 2024-09-23 13:17:00 +00:00
ingress Fix Go vet errors for master golang 2024-09-20 12:36:38 +05:30
internal e2e framework: support creating TContext 2026-01-16 07:53:00 +01:00
job Add Complete reason verification to all e2e conformance tests 2025-03-11 06:14:55 +09:00
kubectl kubectl e2e: debug appended environment variables 2025-03-25 15:08:04 -07:00
kubelet fix check errRaw != nil but return a nil value error err 2024-12-17 20:45:17 +08:00
kubesystem
manifest
metrics test: add retry to getMetricsFromNode 2025-11-06 13:56:17 -06:00
network Add e2eendpointslice.WaitForEndpointCount, use it in some network tests 2025-07-16 11:42:00 -04:00
node KEP-5471: Extend tolerations operators (#134665) 2025-11-10 12:42:54 -08:00
perf
pod DRA e2e: make driver deployment possible in Go unit tests 2026-01-16 07:53:00 +01:00
providers Remove gcp in-tree cloud provider and credential provider 2024-05-01 09:03:53 -04:00
pv With new changes we will also have a VolumeModifying condition 2025-10-07 10:31:29 -04:00
rc chore: depr. pointer pkg replacement for test/e2e and utils 2025-07-06 22:18:03 +02:00
registry test: use localhost and HostNetwork for registry, mark test as disruptive 2025-11-07 11:13:28 -08:00
replicaset drop deprecated PollWithContext and adopt PollUntilContextTimeout instead 2024-06-21 19:23:31 +08:00
resource
security
service deflake e2e test: Services should implement NodePort and HealthCheckNodePort correctly when ExternalTrafficPolicy changes 2025-08-29 11:24:44 +08:00
skipper Merge pull request #123981 from lzhecheng/update-traffic-distribution-test 2024-10-01 15:53:49 +01:00
ssh remove gke from e2e 2025-01-20 21:50:46 +08:00
statefulset provision 10G disks for testing pvc instead of 1 byte or 1GB 2025-11-05 01:25:13 +03:00
testfiles fix: enable and fixes rules from testifylint on test package 2024-06-30 05:44:47 +00:00
timer
volume Add e2e test for VolumeAttachment cleanup when CSIDriver AttachRequired changes 2025-12-19 15:56:33 +08:00
websocket
.import-restrictions Lock down gofuzz in deps 2025-03-08 15:22:01 -08:00
bugs.go
expect.go e2e framework: better documentation of ExpectNoError 2024-09-23 10:58:06 +02:00
expect_test.go fix: enable and fixes rules from testifylint on test package 2024-06-30 05:44:47 +00:00
flake_reporting_util.go Fix Go vet errors for master golang 2024-09-20 12:36:38 +05:30
framework.go e2e framework: avoid memory overhead of ginkgo.GinkgoT 2026-01-16 07:53:00 +01:00
get.go E2E framework: retry also on EOF errors 2025-07-15 12:52:26 +02:00
ginkgologger.go e2e framework: fix inconsistency in log output 2026-01-05 13:45:03 +01:00
ginkgowrapper.go E2E framework: make usage of Ginkgo wrappers optional 2026-01-07 12:05:43 +01:00
ginkgowrapper_test.go
gomega.go DRA: CRUD conformance tests 2025-10-02 17:43:33 +02:00
log.go
log_test.go enhance boolean assertions 2024-07-31 15:58:15 +00:00
namespacedname.go
nodes_util.go
OWNERS update sig cl owners in k/k 2025-02-12 11:04:11 +02:00
ports.go
provider.go
README.md
size.go
test_context.go E2E: treat data races in e2e suite as failures 2025-09-16 19:34:36 +02:00
timeouts.go
util.go Remove unused WaitForServiceEndpointsNum function 2025-09-21 14:48:13 +02:00

Overview

The Kubernetes E2E framework simplifies writing Ginkgo tests suites. It's main usage is for these tests suites in the Kubernetes repository itself:

  • test/e2e: runs as client for a Kubernetes cluster. The e2e.test binary is used for conformance testing.
  • test/e2e_node: runs on the same node as a kubelet instance. Used for testing kubelet.
  • test/e2e_kubeadm: test suite for kubeadm.

Usage of the framework outside of Kubernetes is possible, but not encouraged. Downstream users have to be prepared to deal with API changes.

Code Organization

The core framework is the k8s.io/kubernetes/test/e2e/framework package. It contains functionality that all E2E suites are expected to need:

  • connecting to the apiserver
  • managing per-test namespaces
  • logging (Logf)
  • failure handling (Fail, Failf)
  • writing concise JUnit test results

It also contains a TestContext with settings that can be controlled via command line flags. For historic reasons, this also contains settings for individual tests or packages that are not part of the core framework.

Optional functionality is placed in sub packages like test/e2e/framework/pod. The core framework does not depend on those. Sub packages may depend on the core framework.

The advantages of splitting the code like this are:

  • leaner go doc packages by grouping related functions together
  • not forcing all E2E suites to import all functionality
  • avoiding import cycles

Execution Flow

When a test suite gets invoked, the top-level Describe calls register the callbacks that define individual tests, but does not invoke them yet. After that init phase, command line flags are parsed and the Describe callbacks are invoked. Those then define the actual tests for the test suite. Command line flags can be used to influence the test definitions.

Now Context/BeforeEach/AfterEach/It define code that will be called later when executing a specific test. During this setup phase, f := framework.NewDefaultFramework("some tests") creates a Framework instance for one or more tests. NewDefaultFramework initializes that instance anew for each test with a BeforeEach callback. Starting with Kubernetes 1.26, that instance gets cleaned up after all other code for a test has been invoked, so the following code is correct:

f := framework.NewDefaultFramework("some tests")

ginkgo.AfterEach(func() {
    # Do something with f.ClientSet.
}

ginkgo.It("test something", func(ctx context.Context) {
    # The actual test.
})

Optional functionality can be injected into each test by adding a callback to NewFrameworkExtensions in an init function. NewDefaultFramework will invoke those callbacks as if the corresponding code had been added to each test like this:

f := framework.NewDefaultFramework("some tests")

optional.SomeCallback(f)

SomeCallback then can register additional BeforeEach or AfterEach callbacks that use the test's Framework instance.

When a test runs, callbacks defined for it with BeforeEach and AfterEach are called in first-in-first-out order. Since the migration to ginkgo v2 in Kubernetes 1.25, the AfterEach callback is called also when there has been a test failure. This can be used to run cleanup code for a test reliably. However, ginkgo.DeferCleanup is often a better alternative. Its callbacks are executed in first-in-last-out order.

test/e2e/framework/internal/unittests/cleanup/cleanup.go shows how these different callbacks can be used and in which order they are going to run.