Skip to content

Re-enable controller test suite and harden scheme registration#48

Merged
kodiakhq[bot] merged 3 commits into
masterfrom
fix-tests-and-review
Jun 26, 2026
Merged

Re-enable controller test suite and harden scheme registration#48
kodiakhq[bot] merged 3 commits into
masterfrom
fix-tests-and-review

Conversation

@tamalsaha

Copy link
Copy Markdown
Contributor

Summary

The ported upstream StatefulSet controller tests in pkg/controller/tests/ were entirely commented out (~6,500 lines across 5 files) and declared a package that could not reach the unexported identifiers they exercise, so go test reported [no tests to run] and 0 of 87 controller test functions ever ran. This PR re-enables them, fixes the bugs they surface, hardens a fragile production scheme registration, and adds first-time coverage for the AppsCode placement logic.

Re-enable the controller test suite

Moved the five files into pkg/controller/petset/ as white-box tests and adapted them to the fork's API drift:

  • Updated fakeObjectManager to the current StatefulPodControlObjectManager interface (PetSet params, ListPods, GetPlacementPolicy).
  • Added thin test adapters for newPetSetPod and NewStatefulPodControl, and wired the PlacementPolicy informer plus a fake OCM ManifestWork client/informer into newFakePetSetController.
  • Pointed feature-gate tests at the fork's features.DefaultFeatureGate and the new SetFeatureGateDuringTest signature.
  • Restored lost helpers (ascendingOrdinal, overlappingPetSets).

Bugs surfaced while re-enabling

  • setupController listed StatefulSets from the kube client (always empty) instead of PetSets from the api client, so the set never entered the indexer.
  • TestPetSetControl_getSetRevisions waited on a kube StatefulSets informer instantiated after Start() that never synced — hanging the whole suite to the 10‑minute timeout.
  • newFakePetSetController seeded CRD objects into the core kube fake client (which panics); CRD objects are now routed to the versioned fake.

Harden scheme registration

patchCodec (pkg/controller/petset/pet_set_utils.go) serializes PetSets into ControllerRevisions via the client-go global scheme, but the type was only registered as a side effect in pkg/cmds/root.go's PersistentPreRunE. Any path that builds the controller differently panics with "no kind is registered for the type v1.PetSet" the first time a revision is created — exactly what broke the tests. Registration now happens in an init() next to patchCodec (same package, so it always runs before use), and the redundant cmd-layer hook is removed.

New: placement_test.go

First coverage for the AppsCode-specific placement logic: the upsert helpers, spread/anti-affinity and node-affinity generation, domain selection in getAppropriateDomainIndex, and the CEL evaluator including its program cache.

Verification

go build ./..., go vet ./..., gofmt, and go test ./... all pass. The pkg/controller/petset package now runs all 87 previously-disabled tests plus the new placement tests.

kodiakhq[bot]
kodiakhq Bot previously approved these changes Jun 26, 2026
kodiakhq[bot]
kodiakhq Bot previously approved these changes Jun 26, 2026
kodiakhq[bot]
kodiakhq Bot previously approved these changes Jun 26, 2026
@tamalsaha tamalsaha force-pushed the fix-tests-and-review branch from db2a354 to 2a88d10 Compare June 26, 2026 08:07
The ported upstream StatefulSet controller tests in pkg/controller/tests/
were entirely commented out (~6500 lines) and lived in a package that could
not access the unexported identifiers they test, so none of them ran.

Move the five test files into pkg/controller/petset/ as white-box tests and
adapt them to the fork's API drift:

- Update the fakeObjectManager to the current StatefulPodControlObjectManager
  interface (PetSet params, ListPods, GetPlacementPolicy).
- Add thin test adapters for newPetSetPod and NewStatefulPodControl, and wire
  the PlacementPolicy informer plus a fake OCM ManifestWork client/informer
  into newFakePetSetController and NewPetSetController.
- Point feature-gate tests at the fork's features.DefaultFeatureGate and the
  new SetFeatureGateDuringTest signature.
- Restore lost helpers (ascendingOrdinal, overlappingPetSets).

Fix bugs surfaced while re-enabling the tests:

- setupController listed StatefulSets from the kube client instead of PetSets
  from the api client, so the set never entered the indexer.
- TestPetSetControl_getSetRevisions waited on a kube StatefulSets informer
  that was instantiated after Start() and never synced, hanging the suite.
- newFakePetSetController seeded CRD objects into the core kube fake client,
  which panics; route PetSet/PlacementPolicy to the versioned fake.

Harden patchCodec: register the PetSet/PlacementPolicy types with the
client-go scheme in an init() next to patchCodec instead of relying on a
PersistentPreRunE hook in cmd wiring. Otherwise constructing the controller
through any other path panics with "no kind is registered for the type
v1.PetSet" the first time a ControllerRevision is created.

Add placement_test.go covering the AppsCode placement logic: the upsert
helpers, spread/anti-affinity and node-affinity generation, domain selection
in getAppropriateDomainIndex, and the CEL evaluator with its program cache.

Signed-off-by: Tamal Saha <tamal@appscode.com>
Restore the upstream pkg/controller/controller_utils_test.go that was dropped
in c547220 ("Use golangci-lint 2.x"). All the helpers it exercises still
exist in the fork, so the coverage is worth keeping.

Adapt it to the fork and the vendored dependency set:

- Swap k8s.io/kubernetes/test/utils/ktesting for k8s.io/klog/v2/ktesting
  (identical NewTestContext API, and the former is not vendored).
- Point feature-gate tests at features.DefaultFeatureGate and the new
  SetFeatureGateDuringTest signature.
- ComputeHash now takes *api.PodTemplateSpec; update TestComputeHash.
- gofmt interface{} -> any to satisfy the repo formatter config.

Drop the tests that would require pulling additional heavy test-only deps
into vendor for little benefit:

- TestRemoveTaintOffNode / TestAddOrUpdateTaintOnNode need
  k8s.io/kubernetes/pkg/controller/testutil (FakeNodeHandler); the taint
  helpers they cover are unused in this fork.
- TestCreatePodsWithGenerateName needs k8s.io/client-go/util/testing
  (FakeHandler) plus a rewrite to the PodInfo-based CreatePods signature.

Vendor k8s.io/utils/clock/testing for the fake clock used by the
ControllerExpectations tests.

Relax errcheck/unparam on _test.go files in .golangci.yml: the revived
controller tests mirror the upstream Kubernetes StatefulSet controller tests,
which freely ignore errors from in-memory fake stores and use helper
signatures that trip unparam; excluding test files keeps the suite rebasable
against upstream while production code stays strictly linted.

Signed-off-by: Tamal Saha <tamal@appscode.com>
kodiakhq[bot]
kodiakhq Bot previously approved these changes Jun 26, 2026
make ci was check-license lint build with unit-tests/cover/verify commented
out, so CI ran no tests at all. Now that the controller suite is re-enabled
and race-clean under -race, add unit-tests to the ci target so the suite
actually guards changes. (cover/verify remain opt-in.)

Signed-off-by: Tamal Saha <tamal@appscode.com>
@tamalsaha tamalsaha added the automerge Kodiak will auto merge PRs that have this label label Jun 26, 2026
@kodiakhq kodiakhq Bot merged commit faf52bc into master Jun 26, 2026
4 checks passed
@kodiakhq kodiakhq Bot deleted the fix-tests-and-review branch June 26, 2026 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge Kodiak will auto merge PRs that have this label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant