-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BeforeSuite and AfterSuite modules not running in the ginkgo v2 #3646
Comments
Hi @dashanji, Thank you for highlighting this issue. It's crucial to clarify that within By convention, tests should be implemented under the specific controller type. Here's an illustrative example: With this structure, it's understandable that ginkgo might ignore the Here's a potential path forward:
Furthermore, regarding the new templates for tests:
Make sure to differentiate between APIs which have webhooks ONLY and the ones for each controller. Use Looking forward to a collaborative resolution. |
Hey, How it is a good-first-issue? REading the full issue overwhelmed me. I dont think this is a good-first-issue. |
/assign
Hi @Sajiyah-Salat. I think it's a good first issue for people like me who have experience using kubebuilder but no actual development experience. Also, the code modification is small and we can refer to the existing template to get some insights.
Hi @camilamacedo86, it looks like we need to add a new command like |
Okay, would you like to take this up. It would be great as you have gone through the issue. If not let me know the detailed step I will make it happen. |
Yeah, I would take it done. |
No. We just need to create the templates and call them in the subcommands to create controllers and webhooks So, we need to add the templates and call them out in the commands. |
Following the some clarification about what was requested in this task and how to achieve the goal. GoalEnsure that every scaffolded controller and webhook have a corresponding test file. The test file should contain a significant test to ensure that the suite isn't merely ignored. For instance, a test assertion like Examplevar _ = BeforeSuite(func(done Done) {
Expect(1).To(Equal(2))
}, 60)
var _ = AfterSuite(func() {
Expect(1).To(Equal(2))
}) Implementation Steps
func (f *WebhookSuite) SetTemplateDefaults() error {
// ... existing logic ...
switch f.WebhookType {
case "defaulting":
f.TemplateBody = defaultingWebhookTestSuiteTemplate
case "validating":
f.TemplateBody = validatingWebhookTestSuiteTemplate
case "conversion":
f.TemplateBody = conversionWebhookTestSuiteTemplate
default:
return fmt.Errorf("unknown webhook type: %s", f.WebhookType)
}
// ... remaining logic ...
} Scaffold examplesFor controllers package controller
import (
"context"
"time"
//nolint:golint
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
// HERE will be the API see how your template will be implemented for it works: https://github.com/kubernetes-sigs/kubebuilder/blob/master/pkg/plugins/golang/v4/scaffolds/internal/templates/controllers/controller.go#L77-L79
apiversion "path/to/your/api/version"
)
var _ = Describe("Controller<Kind>Reconciler", func() {
Context("When reconciling a resource", func() {
const resourceName = "test-resource"
ctx := context.Background()
typeResourceName := types.NamespacedName{
Name: resourceName,
Namespace: "default", // TODO(user):Modify as needed
}
resource := &yourapiversion.YourResourceKind{} //HERE SHOULD BE THE KIND
BeforeEach(func() {
testResource := &yourapiversion.YourResourceKind{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: "default",
},
// TODO (user): Specify other spec details if needed.
}
Expect(k8sClient.Create(ctx, testResource)).To(Succeed())
})
AfterEach(func() {
// TODO(user): Cleanup logic after each test, like removing the resource instance.
By("Cleanup the specific resource instance <KIND>")
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
})
It("should successfully reconcile the resource", func() {
By("Reconciling the created resource")
controllerReconciler := &YourControllerReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
}
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeResourceName,
})
Expect(err).To(Not(HaveOccurred()))
// TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
// Example: If you expect a certain status condition after reconciliation, verify it here.
})
})
}) For Webhooks var _ = Describe("MyResource Webhook", func() {
Context("When creating MyResource", func() {
It("Should deny if a required field is empty", func() {
// Assuming MyResource has a field "Name" that shouldn't be empty.
myResource := &MyResource{
Spec: MyResourceSpec{
Name: "",
// other fields...
},
}
err := k8sClient.Create(context.Background(), myResource)
Expect(err).To(HaveOccurred()) // Expect an error because the Name field is empty.
// Optionally, you can check for specific error messages or types.
})
It("Should admit if all required fields are provided", func() {
myResource := &MyResource{
Spec: MyResourceSpec{
Name: "ValidName",
// other fields...
},
}
err := k8sClient.Create(context.Background(), myResource)
Expect(err).NotTo(HaveOccurred()) // Expect no error as it's a valid resource.
})
})
}) Acceptance Criteria
|
I removed /good-first-issue |
Will work on that soon! Thank you for detailed steps |
Hi @Sajiyah-Salat, I just updated the PR to address this issue. If you have any suggestions, please let me know. Your assistance has been invaluable. Thank you. |
What broke? What's expected?
What broke?
The BeforeSuite and AfterSuite modules not running in the ginkgo v2. In other words, the current
make test
do nothing in all project with the ginkgo v2.Refer to the ginkgo v2.0 note, the BeforeSuite and AfterSuite will not run if all tests in a suite are skipped.
What's expected?
In the project with the ginkgo v2, the
make test
command can run successfully as expected and do the operations such as installing CRD actually.Reproducing this issue
$ cd project
Add a wrong assertion as follows.
Run
make test
, and you will get some errors because the BeforeSuite module executed actually.$ cd project/testdata/project-v4
Add a wrong assertion as follows.
Run
make test
, and you will run successfully because the BeforeSuite module not executed actually.KubeBuilder (CLI) Version
3.12.0
PROJECT version
3
Plugin versions
Other versions
No response
Extra Labels
No response
The text was updated successfully, but these errors were encountered: