Skip to content

Trial hegel-go property-based tests - #1064

Draft
wallrj wants to merge 3 commits into
cert-manager:mainfrom
wallrj:hegel-go-trial
Draft

wallrj wants to merge 3 commits into
cert-manager:mainfrom
wallrj:hegel-go-trial

Conversation

@wallrj

@wallrj wallrj commented Aug 15, 2026

Copy link
Copy Markdown
Member

Not for merge as-is — this is the trust-manager instalment of the property-based-testing trial previously run on cert-manager (cert-manager/cert-manager#9149, which describes how candidates are chosen) and approver-policy (cert-manager/approver-policy#989). If maintainers want any of it, it should be split per package, and the new test-only dependency (hegel-go, a Hypothesis-style property-testing library with a native shrinking engine, pulled in via hegel.dev/go/hegel + purego) needs discussing first.

What's here

Three commits, each replacing example/table tests with properties where the rows merely instantiate a recomputable rule, and keeping the tests that pin external bytes or interop:

  1. pkg/util CertPool — the trust boundary that keeps private keys and stray material out of distributed bundles.
    • Bundles of generated certificates (CA/non-CA, valid/expired, duplicate subjects) interleaved with junk text: the pool must hold exactly the distinct certificates passing the configured filters, sorted by SHA256, with PEM() a byte-identical fixed point and PEMSplit() consistent with it.
    • Strictness: any non-CERTIFICATE block, any PEM headers, or unparseable DER anywhere in the bundle rejects the whole bundle.
    • Arbitrary bytes never panic; nil is always an error; anything accepted round-trips.
    • Replaces the tables in pem_test.go and cert_pool_test.go; the generated adversarial blocks cover the old fixtures (headered cert, corrupt base64, private key) and more.
  2. pkg/bundle/internal/truststore — encode→decode oracle: any non-empty subset of certificates, any password/profile; JKS decoded with keystore-go must contain exactly one entry per certificate with the certAlias alias, the DER content and the NotBefore creation time; PKCS#12 decoded with pkcs12.DecodeTrustStore must return the pool in order. Determinism asserted byte-for-byte for JKS and passwordless PKCS#12 (password-protected PKCS#12 uses random salts). Replaces Test_Encoder_Deterministic and Test_encodeJKSAliases; keeps Test_certAlias (stability pin) and the Java interop tests.
  3. pkg/bundle condition helpers — setBundleCondition preserves LastTransitionTime exactly when type+status are unchanged, replaces in place or appends, and leaves other entries untouched; bundleHasCondition matches on all fields except LastTransitionTime; setBundleStatusDefaultCAVersion always leaves the status equal to the required ID and returns whether that changed. Replaces the three tables in util_test.go.

Not attempted: the v1alpha1↔v1alpha2 conversion round trip — TestFuzzyConversion already covers that law with apimachinery's FuzzTestFunc and careful custom fuzzers, which is exactly the property-shaped prior art this trial looks for. Webhook validation was skipped for the same reason as in approver-policy (the error list is the spec; a property would restate it).

What it found

No behavioural bugs — but one stale doc comment with a history:

The strictness property hammered the private-key/header rejection path with ~2,000 adversarial bundles per run and it held throughout — worth having as a permanent regression net given what leaks through that boundary would mean.

Coverage (main → branch, go test -coverprofile)

package main branch
pkg/util 81.1% 90.5%
pkg/bundle/internal/truststore 88.5% 92.3%
pkg/bundle 49.3% 51.1%

Zero previously covered blocks lost; the gains are the nil-input branch, all of PEMSplit, setBundleCondition's replace-in-place branch and a PKCS#12 profile case.

Note: make test-unit currently fails on my machine on clean main with a toolchain mismatch (compile: version "go1.26.0" does not match go tool version "go1.25.5") — unrelated to this branch; all suites pass with go test ./cmd/... ./pkg/..., the testnegativeserialon variant and the trust-packages module directly.

with claude fable-5

wallrj and others added 3 commits August 15, 2026 07:22
Add property-based tests for CertPool using hegel-go: a filter/dedup/
ordering oracle for bundles of generated certificates interleaved with
junk text, a strictness property rejecting non-certificate, headered and
unparseable PEM blocks, and a never-panic/fixed-point property over
arbitrary bytes.

The replaced table rows all instantiated these rules; the generated
inputs cover the same fixtures (headered certificate, corrupt DER,
private key, expired and non-CA certificates) and many more combinations.

The properties also document that AddCertsFromPEM does not return an
error for a bundle containing no certificates, despite its doc comment
claiming otherwise.

Co-Authored-By: Claude Fable 5 <[email protected]>
Signed-off-by: Richard Wall <[email protected]>
Add a property test that encodes any non-empty subset of generated
certificates with any password/profile configuration and decodes the
result with the same library: JKS entries must match the pool exactly
(alias, DER content, NotBefore creation time) and PKCS#12 trust stores
must decode to the pool's certificates in order. The property also
asserts byte-for-byte determinism for JKS and passwordless PKCS#12;
password-protected PKCS#12 uses random salts, so no assertion there.

This replaces Test_Encoder_Deterministic (six example rows) and
Test_encodeJKSAliases (subsumed by the alias round trip). Test_certAlias
stays as an alias-stability pin, and the Java interop tests stay as
external-tool checks.

Co-Authored-By: Claude Fable 5 <[email protected]>
Signed-off-by: Richard Wall <[email protected]>
Add properties for the bundle status helpers: setBundleCondition must
preserve LastTransitionTime exactly when an existing condition has the
same type and status, replace in place or append in the patch list while
leaving other entries untouched, and leave a state bundleHasCondition can
find; bundleHasCondition must match on type, status, reason, message and
observed generation, ignoring LastTransitionTime; and
setBundleStatusDefaultCAVersion must leave the status equal to the
required ID and report whether that changed anything.

The replaced tables in util_test.go instantiated exactly these rules.
Condition types are drawn unique per list, matching the API's
listType=map / listMapKey=type markers on the conditions field.

Co-Authored-By: Claude Fable 5 <[email protected]>
Signed-off-by: Richard Wall <[email protected]>
@cert-manager-prow

Copy link
Copy Markdown
Contributor

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@cert-manager-prow cert-manager-prow Bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. labels Aug 15, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign munnerz for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@cert-manager-prow

Copy link
Copy Markdown
Contributor

PR needs rebase.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant