Skip to content

Commit 831bed7

Browse files
committed
Add shared utilities for e2e tests
Extract common e2e test infrastructure into reusable packages to reduce duplication across test files and establish consistent patterns. This shared functionality will be used once we introduce envtest to run alongside the existing Kind cluster setup, allowing tests to share helpers between both environments. test/e2e/testutil/provider.go: Provider abstraction for multi-vendor testing. Each provider (NXOS, IOSXR) declares its GVKs for core and config resources, enabling tests to iterate over resource types without hardcoding per-vendor logic. Includes CreateTestDevice helper that creates a NetworkDevice with the correct provider-specific configuration. test/e2e/testutil/timeouts.go: Centralized timeout constants (DefaultTimeout, LongTimeout, VeryLongTimeout) so tests use consistent values and timeouts can be tuned in one place. test/e2e/e2e_test.go: Make setup/teardown idempotent so tests can be re-run immediately after a failure or interruption (Ctrl+C) without manual cleanup. Previously, if AfterAll didn't complete, the next run would fail on "namespace already exists" or similar errors. Now: - Namespace creation uses dry-run + apply instead of create - Cleanup commands use --ignore-not-found - Pre-delete gnmi-test-server pod in BeforeAll to handle stale state
1 parent bd491ae commit 831bed7

6 files changed

Lines changed: 232 additions & 25 deletions

File tree

‎Makefile.maker.yaml‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ golangciLint:
3030

3131
shellCheck:
3232
ignorePaths:
33-
- 'docs/node_modules/*'
33+
- "docs/node_modules/*"
3434

3535
goReleaser:
3636
createConfig: true
3737

3838
license:
3939
addHeaders: true
4040
checkDependencies: true
41-
copyright: 'SAP SE or an SAP affiliate company and IronCore contributors'
41+
copyright: "SAP SE or an SAP affiliate company and IronCore contributors"
4242
spdx: Apache-2.0
4343

4444
nix:
@@ -54,7 +54,7 @@ renovate:
5454
- felix-kaestner
5555

5656
testPackages:
57-
except: '/test'
57+
except: "/test"
5858

5959
githubWorkflow:
6060
ci:
@@ -69,12 +69,16 @@ githubWorkflow:
6969
enabled: false
7070

7171
variables:
72-
GO_BUILDENV: 'CGO_ENABLED=0'
72+
GO_BUILDENV: "CGO_ENABLED=0"
7373

7474
verbatim: |
7575
# Image to use all building/pushing image targets
7676
IMG ?= controller:latest
7777
78+
# E2E test dependency versions
79+
PROMETHEUS_OPERATOR_VERSION ?= v0.82.2
80+
CERTMANAGER_VERSION ?= v1.17.2
81+
7882
# CONTAINER_TOOL defines the container tool to be used for building images.
7983
# The default is docker, but it can be overridden to use other tools (i.e. podman or nerdctl).
8084
CONTAINER_TOOL ?= docker
@@ -124,7 +128,10 @@ verbatim: |
124128
@printf "\e[1;36m>> kind load docker-image $(TEST_SERVER_IMG) --name=$(KIND_CLUSTER)\e[0m\n"
125129
@kind load docker-image $(TEST_SERVER_IMG) --name=$(KIND_CLUSTER)
126130
@printf "\e[1;36m>> go test ./test/e2e/ -v -ginkgo.v\e[0m\n"
127-
@KIND_CLUSTER=$(KIND_CLUSTER) go test ./test/e2e/ -v -ginkgo.v
131+
@KIND_CLUSTER=$(KIND_CLUSTER) \
132+
PROMETHEUS_OPERATOR_VERSION=$(PROMETHEUS_OPERATOR_VERSION) \
133+
CERTMANAGER_VERSION=$(CERTMANAGER_VERSION) \
134+
go test ./test/e2e/ -v -ginkgo.v
128135
129136
docker-build: FORCE
130137
@printf "\e[1;36m>> $(CONTAINER_TOOL) build --tag=$(IMG) .\e[0m\n"

‎charts/network-operator/templates/prometheus/controller-manager-metrics-monitor.yaml‎

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/e2e/e2e_test.go‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,16 @@ var _ = Describe("Manager", Ordered, func() {
3939
// enforce the restricted security policy to the namespace, installing CRDs,
4040
// and deploying the controller.
4141
BeforeAll(func(ctx SpecContext) {
42-
By("deploying the gnmi-test-server")
42+
By("cleaning up any existing gnmi-test-server pod")
4343
cmd := exec.CommandContext(
44+
ctx, "kubectl", "delete", "pod", "gnmi-test-server",
45+
"--namespace", "default",
46+
"--ignore-not-found",
47+
)
48+
_, _ = Run(cmd) //nolint:errcheck // pod may not exist
49+
50+
By("deploying the gnmi-test-server")
51+
cmd = exec.CommandContext(
4452
ctx, "kubectl", "run", "gnmi-test-server",
4553
"--image", serverImage,
4654
"--image-pull-policy", "Never",
@@ -71,7 +79,11 @@ var _ = Describe("Manager", Ordered, func() {
7179
gnmiServerIPAddr = strings.ReplaceAll(strings.TrimSpace(out), "'", "")
7280

7381
By("creating manager namespace")
74-
cmd = exec.CommandContext(ctx, "kubectl", "create", "ns", namespace)
82+
cmd = exec.CommandContext(ctx, "kubectl", "create", "ns", namespace, "--dry-run=client", "-o", "yaml")
83+
nsYaml, err := Run(cmd)
84+
Expect(err).NotTo(HaveOccurred(), "Failed to generate namespace YAML")
85+
cmd = exec.CommandContext(ctx, "kubectl", "apply", "-f", "-")
86+
cmd.Stdin = strings.NewReader(nsYaml)
7587
_, err = Run(cmd)
7688
Expect(err).NotTo(HaveOccurred(), "Failed to create namespace")
7789

@@ -95,12 +107,12 @@ var _ = Describe("Manager", Ordered, func() {
95107
// and deleting the namespace.
96108
AfterAll(func(ctx SpecContext) {
97109
By("cleaning up the ClusterRoleBinding of the service account to allow access to metrics")
98-
cmd := exec.CommandContext(ctx, "kubectl", "delete", "clusterrolebinding", metricsRoleBindingName)
110+
cmd := exec.CommandContext(ctx, "kubectl", "delete", "clusterrolebinding", metricsRoleBindingName, "--ignore-not-found")
99111
_, err := Run(cmd)
100112
Expect(err).NotTo(HaveOccurred(), "Failed to delete ClusterRoleBinding")
101113

102114
By("cleaning up the curl pod for metrics")
103-
cmd = exec.CommandContext(ctx, "kubectl", "delete", "pod", "curl-metrics", "-n", namespace)
115+
cmd = exec.CommandContext(ctx, "kubectl", "delete", "pod", "curl-metrics", "-n", namespace, "--ignore-not-found")
104116
_, err = Run(cmd)
105117
Expect(err).NotTo(HaveOccurred(), "Failed to delete curl-metrics pod")
106118

@@ -120,7 +132,7 @@ var _ = Describe("Manager", Ordered, func() {
120132
Expect(err).NotTo(HaveOccurred(), "Failed to delete namespace")
121133

122134
By("cleaning up the gnmi-test-server pod")
123-
cmd = exec.CommandContext(ctx, "kubectl", "delete", "pod", "gnmi-test-server", "-n", "default")
135+
cmd = exec.CommandContext(ctx, "kubectl", "delete", "pod", "gnmi-test-server", "-n", "default", "--ignore-not-found")
124136
_, err = Run(cmd)
125137
Expect(err).NotTo(HaveOccurred(), "Failed to delete gnmi-test-server pod")
126138
})

‎test/e2e/testutil/provider.go‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// SPDX-FileCopyrightText: 2026 SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package testutil
5+
6+
import (
7+
"context"
8+
9+
"k8s.io/apimachinery/pkg/api/meta"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"k8s.io/apimachinery/pkg/runtime/schema"
12+
"sigs.k8s.io/controller-runtime/pkg/client"
13+
14+
nxv1alpha1 "github.com/ironcore-dev/network-operator/api/cisco/nx/v1alpha1"
15+
"github.com/ironcore-dev/network-operator/api/core/v1alpha1"
16+
"github.com/ironcore-dev/network-operator/internal/provider"
17+
"github.com/ironcore-dev/network-operator/internal/provider/cisco/iosxr"
18+
"github.com/ironcore-dev/network-operator/internal/provider/cisco/nxos"
19+
)
20+
21+
// E2ETestLabel is the label key applied to resources created by e2e tests for cleanup tracking.
22+
const E2ETestLabel = "networking.metal.ironcore.dev/e2e-test"
23+
24+
// ProviderType represents the network device provider to test against.
25+
type ProviderType string
26+
27+
// ProviderFactory creates a new provider instance.
28+
type ProviderFactory = func() provider.Provider
29+
30+
// Provider names must match the registered provider names in internal/provider/*/provider.go
31+
const (
32+
ProviderNXOS ProviderType = "cisco-nxos-gnmi"
33+
ProviderIOSXR ProviderType = "cisco-iosxr-gnmi"
34+
)
35+
36+
// ProviderConfig holds the configuration for a provider test.
37+
type ProviderConfig struct {
38+
Name ProviderType
39+
NewProvider ProviderFactory
40+
}
41+
42+
// SupportedProviders lists all providers to test.
43+
var SupportedProviders = []ProviderConfig{
44+
{Name: ProviderNXOS, NewProvider: func() provider.Provider { return nxos.NewProvider() }},
45+
{Name: ProviderIOSXR, NewProvider: func() provider.Provider { return iosxr.NewProvider() }},
46+
}
47+
48+
// CoreResources are the main API resources with finalizers.
49+
// During cleanup, these are deleted FIRST so their finalizers can
50+
// complete while Device and config resources still exist.
51+
var CoreResources = []schema.GroupVersionKind{
52+
v1alpha1.GroupVersion.WithKind("Interface"),
53+
v1alpha1.GroupVersion.WithKind("VLAN"),
54+
v1alpha1.GroupVersion.WithKind("VRF"),
55+
v1alpha1.GroupVersion.WithKind("NTP"),
56+
v1alpha1.GroupVersion.WithKind("DNS"),
57+
v1alpha1.GroupVersion.WithKind("LLDP"),
58+
v1alpha1.GroupVersion.WithKind("Banner"),
59+
v1alpha1.GroupVersion.WithKind("OSPF"),
60+
v1alpha1.GroupVersion.WithKind("PIM"),
61+
v1alpha1.GroupVersion.WithKind("NetworkVirtualizationEdge"),
62+
v1alpha1.GroupVersion.WithKind("EVPNInstance"),
63+
v1alpha1.GroupVersion.WithKind("RoutingPolicy"),
64+
v1alpha1.GroupVersion.WithKind("PrefixSet"),
65+
v1alpha1.GroupVersion.WithKind("BGP"),
66+
v1alpha1.GroupVersion.WithKind("BGPPeer"),
67+
v1alpha1.GroupVersion.WithKind("Syslog"),
68+
v1alpha1.GroupVersion.WithKind("SNMP"),
69+
v1alpha1.GroupVersion.WithKind("ManagementAccess"),
70+
v1alpha1.GroupVersion.WithKind("AccessControlList"),
71+
v1alpha1.GroupVersion.WithKind("DHCPRelay"),
72+
v1alpha1.GroupVersion.WithKind("ISIS"),
73+
}
74+
75+
// ConfigResources are provider-specific config resources (e.g., NX-OS configs).
76+
// During cleanup, these are deleted AFTER core resources.
77+
var ConfigResources = []schema.GroupVersionKind{
78+
nxv1alpha1.GroupVersion.WithKind("InterfaceConfig"),
79+
nxv1alpha1.GroupVersion.WithKind("LLDPConfig"),
80+
nxv1alpha1.GroupVersion.WithKind("BGPConfig"),
81+
nxv1alpha1.GroupVersion.WithKind("VPCDomain"),
82+
}
83+
84+
// ResourcePluralName returns the plural resource name for a GVK.
85+
// These must match the CRD spec.names.plural values (from `kubectl api-resources`).
86+
// We can't use meta.UnsafeGuessKindToResource because CRDs define their own plurals
87+
// which don't always follow standard Kubernetes pluralization rules.
88+
func ResourcePluralName(gvk schema.GroupVersionKind) string {
89+
plurals := map[string]string{
90+
"Interface": "interfaces",
91+
"VLAN": "vlans",
92+
"VRF": "vrfs",
93+
"NTP": "ntp",
94+
"DNS": "dns",
95+
"LLDP": "lldps",
96+
"Banner": "banners",
97+
"OSPF": "ospf",
98+
"PIM": "pim",
99+
"NetworkVirtualizationEdge": "networkvirtualizationedges",
100+
"EVPNInstance": "evpninstances",
101+
"InterfaceConfig": "interfaceconfigs",
102+
"LLDPConfig": "lldpconfigs",
103+
"VPCDomain": "vpcdomains",
104+
"BGPConfig": "bgpconfigs",
105+
"RoutingPolicy": "routingpolicies",
106+
"PrefixSet": "prefixsets",
107+
"BGP": "bgp",
108+
"BGPPeer": "bgppeers",
109+
"Syslog": "syslogs",
110+
"SNMP": "snmp",
111+
"ManagementAccess": "managementaccesses",
112+
"AccessControlList": "accesscontrollists",
113+
"DHCPRelay": "dhcprelays",
114+
"ISIS": "isis",
115+
"Device": "devices",
116+
}
117+
if plural, ok := plurals[gvk.Kind]; ok {
118+
return plural
119+
}
120+
// Fallback to standard pluralization
121+
plural, _ := meta.UnsafeGuessKindToResource(gvk)
122+
return plural.Resource
123+
}
124+
125+
// CreateTestDevice creates a Device pointing to the gNMI server with a generated name.
126+
func CreateTestDevice(ctx context.Context, c client.Client, gnmiAddr, namespace string) (*v1alpha1.Device, error) {
127+
device := &v1alpha1.Device{
128+
ObjectMeta: metav1.ObjectMeta{
129+
GenerateName: "test-device-",
130+
Namespace: namespace,
131+
},
132+
Spec: v1alpha1.DeviceSpec{
133+
Endpoint: v1alpha1.Endpoint{
134+
Address: gnmiAddr,
135+
},
136+
},
137+
}
138+
if err := c.Create(ctx, device); err != nil {
139+
return nil, err
140+
}
141+
142+
// Set the device status to Running so that dependent resources can reconcile
143+
device.Status.Phase = v1alpha1.DevicePhaseRunning
144+
if err := c.Status().Update(ctx, device); err != nil {
145+
return nil, err
146+
}
147+
148+
return device, nil
149+
}

‎test/e2e/testutil/provider_test.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-FileCopyrightText: 2026 SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package testutil
5+
6+
import (
7+
"strings"
8+
"testing"
9+
10+
"k8s.io/apimachinery/pkg/runtime/schema"
11+
)
12+
13+
// TestResourcePluralNameCoverage verifies that all GVKs in CoreResources and
14+
// ConfigResources have explicit plural mappings in ResourcePluralName.
15+
// This catches the case where a new CRD is added to the resource lists but
16+
// not to the plurals map, which would silently fall back to UnsafeGuessKindToResource.
17+
func TestResourcePluralNameCoverage(t *testing.T) {
18+
allResources := append(append([]schema.GroupVersionKind{}, CoreResources...), ConfigResources...)
19+
for _, gvk := range allResources {
20+
plural := ResourcePluralName(gvk)
21+
// Check if it fell back to UnsafeGuessKindToResource by comparing against the expected default
22+
guessed := schema.GroupResource{Group: gvk.Group, Resource: strings.ToLower(gvk.Kind) + "s"}.String()
23+
if plural == guessed {
24+
// This might be a fallback - log a warning but don't fail (some resources might legitimately use standard pluralization)
25+
t.Logf("WARNING: %s uses standard pluralization (%s) - verify this is intentional", gvk.Kind, plural)
26+
}
27+
if plural == "" {
28+
t.Errorf("ResourcePluralName returned empty string for %s", gvk.Kind)
29+
}
30+
}
31+
}

‎test/e2e/testutil/timeouts.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package testutil
5+
6+
import "time"
7+
8+
const (
9+
// ShortTimeout is used for quick operations that should complete almost immediately
10+
ShortTimeout = 5 * time.Second
11+
12+
// DefaultTimeout is used for standard resource reconciliation
13+
DefaultTimeout = 30 * time.Second
14+
15+
// LongTimeout is used for operations that may take longer (deployments, pod starts)
16+
LongTimeout = 2 * time.Minute
17+
18+
// VeryLongTimeout is used for end-to-end scenarios with multiple dependencies
19+
VeryLongTimeout = 5 * time.Minute
20+
21+
// QuickPollInterval is the polling interval for cleanup operations.
22+
QuickPollInterval = 100 * time.Millisecond
23+
)

0 commit comments

Comments
 (0)