Skip to content

refactor(tests): replace uuid.NewString() with GenerateName in envtest tests#3707

Open
superyyrrzz wants to merge 7 commits intoKong:mainfrom
superyyrrzz:worktree-issue102
Open

refactor(tests): replace uuid.NewString() with GenerateName in envtest tests#3707
superyyrrzz wants to merge 7 commits intoKong:mainfrom
superyyrrzz:worktree-issue102

Conversation

@superyyrrzz
Copy link
Copy Markdown

@superyyrrzz superyyrrzz commented Mar 26, 2026

Summary

Resolves #102 (partial — safe usages only)

  • Replace uuid.NewString() with Kubernetes native GenerateName field for test resource names across envtest helpers and test files
  • Applies only to safe usages where .Name is not referenced before Create() — the API server assigns the name on creation
  • Makes test resource names more descriptive and easier to trace (e.g. gw-abc12 instead of a full UUID)

Files changed (10)

File Change
test/envtest/k8s.go CreateNamespace / CreatePodGenerateName: "ns-" / "pod-"
test/envtest/setup.go deployGateway / deployGatewayUsingGatewayClassGenerateName: "gw-" / "gwc-"
test/envtest/gateway_envtest_test.go HTTPRoute → GenerateName: "httproute-"
test/envtest/httproute_controller_test.go GatewayClass, Gateway, HTTPRoute, ReferenceGrant → GenerateName
test/envtest/ingress_test.go EndpointSlice → GenerateName: "endpointslice-"
test/envtest/kongadminapi_controller_envtest_test.go 6 EndpointSlice names → GenerateName (keeps uuid for UID)
test/envtest/adminapi_discoverer_envtest_test.go EndpointSlice → GenerateName: "endpointslice-"
test/envtest/kongupstreampolicy_test.go GatewayClass, Gateway, HTTPRoute → GenerateName
test/envtest/k8s_objects_status_envtest_test.go First HTTPRoute → GenerateName (second kept as-is, see below)
test/helpers/deploy/deploy_resources.go 2 safe helpers → GenerateName

Intentionally left unchanged

These usages require further discussion (#102):

  • test/envtest/k8s_objects_status_envtest_test.go:170.Name read via DeepCopy + ObjectKeyFromObject before Create()
  • test/envtest/gatewayclass_envtest_test.go:384.Name logged before Create()
  • test/helpers/deploy/deploy_resources.go — 6 functions with "dual-name" pattern (ObjectMeta.Name reused for Spec.Name / Spec.CreateControlPlaneRequest.Name)
  • test/helpers/generators.go — returned objects used by callers before Create()
  • Konnect IDs, UIDs, non-k8s namesGenerateName doesn't apply

Test plan

  • make test.envtest passes — all modified tests exercise the GenerateName path
  • make test.unit passes — no compilation errors
  • make lint passes

…t helpers and tests

Use Kubernetes native GenerateName field for resource names in test code
where .Name is not referenced before Create(). The API server assigns
unique names with descriptive prefixes (e.g. "gw-abc12", "httproute-xyz34"),
making test output easier to trace.

Files updated:
- test/envtest/k8s.go (CreateNamespace, CreatePod)
- test/envtest/setup.go (deployGateway, deployGatewayUsingGatewayClass)
- test/envtest/gateway_envtest_test.go (HTTPRoute in createHTTPRoutes)
- test/envtest/httproute_controller_test.go (GatewayClass, Gateway, HTTPRoute, ReferenceGrant)
- test/envtest/ingress_test.go (EndpointSlice)
- test/envtest/k8s_objects_status_envtest_test.go (first HTTPRoute only)
- test/envtest/kongadminapi_controller_envtest_test.go (6 EndpointSlices)
- test/envtest/kongupstreampolicy_test.go (GatewayClass, Gateway, HTTPRoute)
- test/envtest/adminapi_discoverer_envtest_test.go (EndpointSlice)
- test/helpers/deploy/deploy_resources.go (2 safe functions)

uuid.NewString() is intentionally kept where:
- .Name is referenced before Create() (e.g. cross-resource references)
- Used for non-name fields (UIDs, Konnect entity IDs)
- Name is shared between ObjectMeta and Spec (dual-name pattern)

Ref: Kong#102
@superyyrrzz superyyrrzz requested a review from a team as a code owner March 26, 2026 03:47
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 26, 2026

CLA assistant check
All committers have signed the CLA.

superyyrrzz and others added 2 commits March 26, 2026 13:49
Fix gci lint: align Namespace field with longer GenerateName field
in struct literals across 6 envtest test files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use generateName for resources created during tests, instead of uuid.NewString()

2 participants