-
Notifications
You must be signed in to change notification settings - Fork 93
Add envtest integration tests for kube controller #2487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AryanP123
merged 3 commits into
skupperproject:main
from
AryanP123:add-kube-controller-envtest
Jun 30, 2026
+461
−1
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # Integration tests | ||
|
|
||
| Go integration tests that run Skupper components against a real Kubernetes API server | ||
| using [envtest](https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/envtest) (kube-apiserver + | ||
| etcd), without needing a full cluster. | ||
|
|
||
| These sit between unit tests (fake clients, synchronous event processing) and Ansible E2E | ||
| tests under `tests/e2e/` (real clusters, cross-site networking). | ||
|
|
||
| ## Layout | ||
|
|
||
| Mirrors `internal/` so kube and nonkube integration tests can live alongside their | ||
| production packages. | ||
|
|
||
| | Directory | Tests | | ||
|
AryanP123 marked this conversation as resolved.
|
||
| |-----------|-------| | ||
| | `kube/controller/` | Skupper kube controller (`internal/kube/controller`, `cmd/controller`) | | ||
| | `nonkube/controller/` | (future) nonkube controller (`internal/nonkube/controller`) | | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| The `setup-envtest` version is pinned in the root `go.mod` `tool` directive (matching | ||
| controller-runtime release-0.21 / k8s 1.33). Run from the repository root: | ||
|
|
||
| ```bash | ||
| go tool setup-envtest use 1.33.0 | ||
| ``` | ||
|
|
||
| Or let `make test-integration` download binaries on first run. | ||
|
|
||
| To pre-download Kubernetes test binaries without running tests: | ||
|
|
||
| ```bash | ||
| go tool setup-envtest use 1.33.0 | ||
| ``` | ||
|
|
||
|
|
||
| ## Run | ||
|
|
||
| From the repository root: | ||
|
|
||
| ```bash | ||
| make -C tests test-integration | ||
| ``` | ||
|
|
||
| Or from `tests/`: | ||
|
|
||
| ```bash | ||
| make test-integration | ||
| ``` | ||
|
|
||
| Or directly: | ||
|
|
||
| ```bash | ||
| export KUBEBUILDER_ASSETS=$(go tool setup-envtest use 1.33.0 -p path) | ||
| go test -tags=integration -v ./tests/integration/kube/controller/... | ||
| ``` | ||
|
|
||
| Default `make test` does **not** run these (they use the `integration` build tag and take | ||
| ~1 minute). | ||
|
|
||
| ## Running against an existing cluster | ||
|
|
||
| By default, tests start a local envtest apiserver (kube-apiserver + etcd). To run against a | ||
| full Kubernetes cluster instead, set `USE_EXISTING_CLUSTER=true`. envtest will use your | ||
| current kubeconfig (`KUBECONFIG` or `~/.kube/config`) and install Skupper CRDs from | ||
| `config/crd/bases` before the tests run. | ||
|
|
||
| ```bash | ||
| # Ensure kubectl context points at the target cluster | ||
| kubectl config current-context | ||
|
|
||
| USE_EXISTING_CLUSTER=true make -C tests test-integration | ||
| ``` | ||
|
|
||
| Or directly: | ||
|
|
||
| ```bash | ||
| export USE_EXISTING_CLUSTER=true | ||
| go test -tags=integration -v ./tests/integration/kube/controller/... | ||
| ``` | ||
|
|
||
| When using an existing cluster, `setup-envtest` / `KUBEBUILDER_ASSETS` are not required. | ||
| The in-process controller still runs locally; tests create namespaces and Skupper resources | ||
| on the cluster — use a development or disposable cluster, not production. | ||
|
|
||
| ## Notes | ||
|
|
||
| - Tests start a shared controller instance and a fresh envtest apiserver per package run. | ||
| - Gateway, Contour, and OpenShift Route CRDs are not installed; related watcher errors in | ||
| logs are expected and harmless for current scenarios. | ||
| - A teardown warning about kube-apiserver shutdown may appear after tests pass; this is a | ||
| known envtest quirk and does not indicate test failure. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| //go:build integration | ||
|
|
||
| package kubecontrollertest | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/skupperproject/skupper/internal/fixtures" | ||
| "github.com/skupperproject/skupper/internal/utils" | ||
| "gotest.tools/v3/assert" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/api/meta" | ||
|
|
||
| skupperv2alpha1 "github.com/skupperproject/skupper/pkg/apis/skupper/v2alpha1" | ||
| ) | ||
|
|
||
| func waitFor(t *testing.T, timeout, interval time.Duration, fn func() (bool, error)) { | ||
| t.Helper() | ||
| err := utils.Retry(interval, int(timeout/interval), fn) | ||
| assert.NilError(t, err) | ||
| } | ||
|
|
||
| func retryOnNotFound(err error) (bool, error) { | ||
| if err == nil { | ||
| return true, nil | ||
| } | ||
| if errors.IsNotFound(err) { | ||
| return false, nil | ||
| } | ||
| return false, err | ||
| } | ||
|
|
||
| func listenerWithHostPort(name, namespace, host string, port int) *skupperv2alpha1.Listener { | ||
| l := fixtures.Listener(name, namespace) | ||
| l.Spec.Host = host | ||
| l.Spec.Port = port | ||
| return l | ||
| } | ||
|
|
||
| func routerSelector() map[string]string { | ||
| return map[string]string{ | ||
| "skupper.io/component": "router", | ||
| "application": "skupper-router", | ||
| } | ||
| } | ||
|
|
||
| func verifyStatus(t *testing.T, expected, actual skupperv2alpha1.Status) { | ||
| t.Helper() | ||
| assert.Equal(t, expected.StatusType, actual.StatusType, actual.Message) | ||
| assert.Equal(t, expected.Message, actual.Message) | ||
| for _, c := range expected.Conditions { | ||
| existing := meta.FindStatusCondition(actual.Conditions, c.Type) | ||
| assert.Assert(t, existing != nil) | ||
| assert.Equal(t, c.Status, existing.Status) | ||
| assert.Equal(t, c.Reason, existing.Reason) | ||
| if c.Message != "" { | ||
| assert.Equal(t, c.Message, existing.Message) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| //go:build integration | ||
|
|
||
| package kubecontrollertest | ||
|
|
||
| import ( | ||
| "context" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/skupperproject/skupper/api/types" | ||
| "github.com/skupperproject/skupper/internal/fixtures" | ||
| "gotest.tools/v3/assert" | ||
| "k8s.io/apimachinery/pkg/api/meta" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| skupperv2alpha1 "github.com/skupperproject/skupper/pkg/apis/skupper/v2alpha1" | ||
| ) | ||
|
|
||
| func TestSiteWithListener(t *testing.T) { | ||
| tc := setup(t) | ||
| namespace := "site-with-listener" | ||
| tc.createNamespace(namespace) | ||
|
|
||
| ctx := context.Background() | ||
| _, err := tc.clients.GetSkupperClient().SkupperV2alpha1().Sites(namespace).Create(ctx, fixtures.Site("mysvc", namespace), metav1.CreateOptions{}) | ||
| assert.NilError(t, err) | ||
| _, err = tc.clients.GetSkupperClient().SkupperV2alpha1().Listeners(namespace).Create(ctx, listenerWithHostPort("mylistener", namespace, "mysvc", 8080), metav1.CreateOptions{}) | ||
| assert.NilError(t, err) | ||
|
|
||
| waitFor(t, 30*time.Second, 250*time.Millisecond, func() (bool, error) { | ||
| l, err := tc.clients.GetSkupperClient().SkupperV2alpha1().Listeners(namespace).Get(ctx, "mylistener", metav1.GetOptions{}) | ||
| if done, err := retryOnNotFound(err); !done { | ||
| return false, err | ||
| } | ||
| configured := meta.FindStatusCondition(l.Status.Conditions, skupperv2alpha1.CONDITION_TYPE_CONFIGURED) | ||
| if configured == nil || configured.Status != metav1.ConditionTrue { | ||
| return false, nil | ||
| } | ||
| _, err = tc.clients.GetKubeClient().CoreV1().Services(namespace).Get(ctx, "mysvc", metav1.GetOptions{}) | ||
| if done, err := retryOnNotFound(err); !done { | ||
| return false, err | ||
| } | ||
| return true, nil | ||
| }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| actualSite, err := tc.clients.GetSkupperClient().SkupperV2alpha1().Sites(namespace).Get(ctx, "mysvc", metav1.GetOptions{}) | ||
| assert.NilError(t, err) | ||
| verifyStatus(t, fixtures.Status(skupperv2alpha1.StatusPending, "Not Running", | ||
| fixtures.Condition(skupperv2alpha1.CONDITION_TYPE_CONFIGURED, metav1.ConditionTrue, "Ready", "OK")), | ||
| actualSite.Status.Status) | ||
|
|
||
| deployment, err := tc.clients.GetKubeClient().AppsV1().Deployments(namespace).Get(ctx, "skupper-router", metav1.GetOptions{}) | ||
| assert.NilError(t, err) | ||
| assert.Equal(t, deployment.Labels["skupper.io/component"], "router") | ||
|
|
||
| svc, err := tc.clients.GetKubeClient().CoreV1().Services(namespace).Get(ctx, "mysvc", metav1.GetOptions{}) | ||
| assert.NilError(t, err) | ||
| assert.DeepEqual(t, svc.Spec.Selector, routerSelector()) | ||
| assert.Equal(t, len(svc.Spec.Ports), 1) | ||
| assert.Equal(t, svc.Spec.Ports[0].Port, int32(8080)) | ||
| assert.Equal(t, svc.Labels["internal.skupper.io/listener"], "mylistener") | ||
|
|
||
| routerConfig, err := tc.clients.GetKubeClient().CoreV1().ConfigMaps(namespace).Get(ctx, "skupper-router", metav1.GetOptions{}) | ||
| assert.NilError(t, err) | ||
| assert.Assert(t, strings.Contains(routerConfig.Data[types.TransportConfigFile], "listener/mylistener")) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest we add
-count=1to disable test caching. If I run this todaymake test-integration; make test-integration ENVTEST_K8S_VERSION=1.30.0the go tests only runs once (assuming it passes a first time).Also
-shortdoes nothing right now, although it could be interesting to have short and long timeouts for local vs CI or something. I could go either way on enabling-racedetection.