🐛 crd: add integration test for indirect type alias resolution (#1063)#1344
🐛 crd: add integration test for indirect type alias resolution (#1063)#1344meliezer wants to merge 2 commits intokubernetes-sigs:mainfrom
Conversation
Signed-off-by: Menashe Eliezer <menashe.eliezer@gmail.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: meliezer The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
Welcome @meliezer! |
|
Hi @meliezer. Thanks for your PR. I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions 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. |
| @@ -0,0 +1,91 @@ | |||
| package crd_test | |||
There was a problem hiding this comment.
Sorry not sure I follow why this test cannot be added like all our other tests
There was a problem hiding this comment.
Thanks for taking a look! The only reason this test runs controller-gen from pkg/crd/testdata is that pkg/crd/testdata is its own Go module (module testdata.kubebuilder.io/cronjob in pkg/crd/testdata/go.mod).
The repro packages live under that module (testdata.kubebuilder.io/cronjob/issue1063/...). If the test runs controller-gen with cmd.Dir set to the repo root (main module), go/packages ends up trying to resolve those imports from the main module and fails with “no required module provides package …/issue1063/repro2”. Running from the testdata module root makes the imports resolve exactly as intended and matches how this repository already structures other fixtures under pkg/crd/testdata.
I can add a short comment in the test explaining this (pkg/crd/testdata is a separate module; run controller-gen from there so package resolution uses that go.mod) if that helps readability.
If you prefer a different pattern that keeps cmd.Dir at repo root (e.g. setting GOMOD/-C), I’m happy to adjust, but I wasn’t able to make that work reliably with go/packages without executing inside the testdata module.
There was a problem hiding this comment.
I was just naively wondering what makes this test different from the ones in parser_integration_test.go
There was a problem hiding this comment.
Thanks for the question, and for your patience.
The existing tests in parser_integration_test.go are close, but they exercise the parser through the internal API (loader.LoadRoots + parser.NeedPackage/NeedCRDFor) after chdir-ing into pkg/crd/testdata.
This test is intentionally a bit different: it runs controller-gen end-to-end as a subprocess, from the existing pkg/crd/testdata module (testdata.kubebuilder.io/cronjob). That matters for the #1063 scenario because the historical failure was tied to package/module resolution for the alias chain repro1 -> repro2 -> repro3, where the target type lived in a package not directly imported by the root package.
So the overlap is real, but this adds regression coverage for the CLI/module-resolution path rather than only the parser API path.
If you’d prefer, I can also rework it into parser_integration_test.go, but I wanted to keep the test exercising the exact module-resolution setup that originally made this scenario fail.
There was a problem hiding this comment.
I think, I understand the concern, but this is already solved by the existing test pattern. Look at parser_integration_test.go lines:
By("switching into testdata to appease go modules")
prevCwd, err = os.Getwd()
Expect(err).NotTo(HaveOccurred())
Expect(prevCwd).To(BeADirectory())
Expect(os.Chdir("./testdata")).To(Succeed()) // ← Here we have the magic
All existing tests:
- Change directory to testdata (separate module)
- Call loader.LoadRoots()
- The loader uses the testdata/go.mod for resolution
This is exactly the same as running controller-gen from the testdata directory, but using internal APIs instead of subprocess.
| @@ -0,0 +1,4 @@ | |||
| // Package repro1 contains API Schema definitions. | |||
There was a problem hiding this comment.
Instead of issue number and repro1 and etc
Could we add meaningful names?
There was a problem hiding this comment.
Something like
pkg/crd/testdata/
└── typealiasindirect/
├── rootpkg/
│ ├── doc.go // Package doc with kubebuilder markers
│ └── types.go // CRD with field using alias from aliaspkg
├── aliaspkg/
│ └── alias.go // type MyAlias = targetpkg.TargetStruct
└── targetpkg/
└── types.go // The actual struct definition
Also, it seems that would fit better to pkg/crd/parser_integration_test.go or create pkg/crd/type_alias_integration_test.go
| ) | ||
|
|
||
| // repoRoot walks up from this test file until it finds the repository go.mod. | ||
| func repoRoot(t *testing.T) string { |
There was a problem hiding this comment.
The tests should follow the project standards that means BBD style and use;
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Could you please check the other integration and tests in the project?
There was a problem hiding this comment.
Thanks for adding regression coverage for this. The scenario is valid and worth testing. 🎉
I do think the test should be refactored before merge so it matches the existing patterns in this package.
The main changes I’d ask for are:
- Please rewrite it in the existing Ginkgo/Gomega style instead of
testing.T - Please avoid the subprocess approach here and use the internal parser APIs instead
- Please rename the testdata packages to something descriptive, so the scenario is easier to understand when reading the fixture layout
I think the internal API path should work for this case too. The existing integration tests already change into testdata to use that module’s go.mod, then call loader.LoadRoots(...). That seems like the right fit here as well, and it keeps this test consistent with the rest of the suite.
I’d also suggest renaming the fixture packages from repro1/repro2/repro3 to names that describe their role in the alias chain. That will make the test much easier to follow later.
So overall: good regression test, but It think it need to be rewritten to follow the established integration-test structure before merging.
| @@ -0,0 +1,91 @@ | |||
| package crd_test | |||
There was a problem hiding this comment.
I think, I understand the concern, but this is already solved by the existing test pattern. Look at parser_integration_test.go lines:
By("switching into testdata to appease go modules")
prevCwd, err = os.Getwd()
Expect(err).NotTo(HaveOccurred())
Expect(prevCwd).To(BeADirectory())
Expect(os.Chdir("./testdata")).To(Succeed()) // ← Here we have the magic
All existing tests:
- Change directory to testdata (separate module)
- Call loader.LoadRoots()
- The loader uses the testdata/go.mod for resolution
This is exactly the same as running controller-gen from the testdata directory, but using internal APIs instead of subprocess.
| @@ -0,0 +1,4 @@ | |||
| // Package repro1 contains API Schema definitions. | |||
There was a problem hiding this comment.
Something like
pkg/crd/testdata/
└── typealiasindirect/
├── rootpkg/
│ ├── doc.go // Package doc with kubebuilder markers
│ └── types.go // CRD with field using alias from aliaspkg
├── aliaspkg/
│ └── alias.go // type MyAlias = targetpkg.TargetStruct
└── targetpkg/
└── types.go // The actual struct definition
Also, it seems that would fit better to pkg/crd/parser_integration_test.go or create pkg/crd/type_alias_integration_test.go
|
Thanks for the detailed feedback — that makes sense. You’re right that the existing integration-test pattern already handles the separate testdata module correctly by switching into pkg/crd/testdata before calling loader.LoadRoots(...), so there’s no real need for the subprocess approach here. I’ll rework this to follow the existing Ginkgo/Gomega integration style, use the internal parser APIs, and rename the fixture packages to something more descriptive for the alias chain scenario. I also agree it fits better alongside the existing parser integration coverage. Thanks again — I’ll update the PR accordingly. |
Add regression coverage for the scenario reported in kubernetes-sigs#1063, where CRD generation could panic when a root type used a type alias whose target type lived in a package not directly imported by the root package. The test follows the existing Ginkgo/Gomega integration pattern in parser_integration_test.go and uses the existing testdata module for package resolution. Also remove the earlier subprocess-based test and rename fixtures to reflect the alias chain scenario more clearly. Likely addressed by kubernetes-sigs#1122; this test prevents regressions.
|
Thanks for the guidance — I’ve updated the PR to follow the existing integration-test pattern. The test now:
I’ve also updated the PR description to reflect the current approach. |
Adds regression coverage for the scenario reported in #1063.
In controller-tools v0.16.x, CRD generation could panic when a root API type
contained a field defined via a type alias whose target type lived in a package
not directly imported by the root package.
This PR adds integration-level coverage using the existing Ginkgo/Gomega test
pattern in parser_integration_test.go:
The test uses a descriptive fixture layout to model the alias chain:
rootpkg → aliaspkg → targetpkg
Also removes the previous subprocess-based test approach and replaces it with
the standard integration test structure used in this package.
The issue no longer reproduces on main (and v0.20.1), likely fixed as a side
effect of #1122. This test ensures the behavior remains covered and prevents
future regressions.