Skip to content

Commit f5d9660

Browse files
🌱 e2e tests improve assertions by simplify style of checks (#4456)
e2e tests improve assertions by simplify style of checks
1 parent 0159ba3 commit f5d9660

File tree

5 files changed

+21
-21
lines changed

5 files changed

+21
-21
lines changed

docs/book/src/plugins/extending/testing-plugins.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Here’s a general workflow to create a sample project using the `go/v4` plugin
5555
"--domain", kbc.Domain,
5656
"--fetch-deps=false",
5757
)
58-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
58+
Expect(err).NotTo(HaveOccurred(), "Failed to initialize a project")
5959
```
6060

6161
- **To define API:**
@@ -70,7 +70,7 @@ Here’s a general workflow to create a sample project using the `go/v4` plugin
7070
"--controller",
7171
"--make=false",
7272
)
73-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
73+
Expect(err).NotTo(HaveOccurred(), "Failed to create an API")
7474
```
7575

7676
- **To scaffold webhook configurations:**
@@ -83,7 +83,7 @@ Here’s a general workflow to create a sample project using the `go/v4` plugin
8383
"--defaulting",
8484
"--programmatic-validation",
8585
)
86-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
86+
Expect(err).NotTo(HaveOccurred(), "Failed to create an webhook")
8787
```
8888

8989
[cert-manager-install]: https://pkg.go.dev/sigs.k8s.io/kubebuilder/v4/test/e2e/utils#TestContext.InstallCertManager

test/e2e/alphagenerate/generate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func generateProject(kbc *utils.TestContext) {
182182
"--external-api-path=github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1",
183183
"--external-api-domain=cert-manager.io",
184184
)
185-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
185+
Expect(err).NotTo(HaveOccurred(), "Failed to scaffold API with external API")
186186
}
187187

188188
func regenerateProject(kbc *utils.TestContext, projectOutputDir string) {

test/e2e/deployimage/generate_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func creatingAPI(kbc *utils.TestContext) {
5151
"--make=false",
5252
"--manifests=false",
5353
)
54-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
54+
Expect(err).NotTo(HaveOccurred(), "Failed to create API definition")
5555
}
5656

5757
func creatingAPIWithOptions(kbc *utils.TestContext) {
@@ -69,7 +69,7 @@ func creatingAPIWithOptions(kbc *utils.TestContext) {
6969
"--make=false",
7070
"--manifests=false",
7171
)
72-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
72+
Expect(err).NotTo(HaveOccurred(), "Failed to create API definition with deploy-image/v1-alpha")
7373
}
7474

7575
func initTheProject(kbc *utils.TestContext) {
@@ -79,5 +79,5 @@ func initTheProject(kbc *utils.TestContext) {
7979
"--project-version", "3",
8080
"--domain", kbc.Domain,
8181
)
82-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
82+
Expect(err).NotTo(HaveOccurred(), "Failed to initialize project")
8383
}

test/e2e/grafana/generate_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func GenerateProject(kbc *utils.TestContext) {
6060
err = kbc.Init(
6161
"--plugins", "grafana.kubebuilder.io/v1-alpha",
6262
)
63-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
63+
Expect(err).NotTo(HaveOccurred(), "Failed to initialize project")
6464

6565
By("verifying the initial template content and updating for real custom metrics")
6666
ExpectWithOffset(1, pluginutil.ReplaceInFile(
@@ -83,17 +83,17 @@ customMetrics:
8383
err = kbc.Edit(
8484
"--plugins", "grafana.kubebuilder.io/v1-alpha",
8585
)
86-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
86+
Expect(err).NotTo(HaveOccurred(), "Failed to edit base of the project")
8787

8888
fileContainsExpr, err := pluginutil.HasFileContentWith(
8989
filepath.Join(kbc.Dir, "grafana", "custom-metrics", "custom-metrics-dashboard.json"),
9090
`sum(rate(foo_bar{job=\"$job\", namespace=\"$namespace\"}[5m])) by (instance, pod)`)
91-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
92-
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
91+
Expect(err).NotTo(HaveOccurred(), "Failed to edit sum rate for custom metrics")
92+
Expect(fileContainsExpr).To(BeTrue())
9393

9494
fileContainsExpr, err = pluginutil.HasFileContentWith(
9595
filepath.Join(kbc.Dir, "grafana", "custom-metrics", "custom-metrics-dashboard.json"),
9696
`histogram_quantile(0.90, sum by(instance, le) (rate(foo_bar{job=\"$job\", namespace=\"$namespace\"}[5m])))`)
97-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
98-
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
97+
Expect(err).NotTo(HaveOccurred(), "Failed to edit histogram_quantile for custom metrics")
98+
Expect(fileContainsExpr).To(BeTrue())
9999
}

test/e2e/v4/generate_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ func GenerateV4(kbc *utils.TestContext) {
4747
"--programmatic-validation",
4848
"--make=false",
4949
)
50-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
50+
Expect(err).NotTo(HaveOccurred(), "Failed to scaffolding mutating webhook")
5151

5252
By("implementing the mutating and validating webhooks")
5353
webhookFilePath := filepath.Join(
5454
kbc.Dir, "internal/webhook", kbc.Version,
5555
fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind)))
5656
err = utils.ImplementWebhooks(webhookFilePath, strings.ToLower(kbc.Kind))
57-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
57+
Expect(err).NotTo(HaveOccurred(), "Failed to implement webhooks")
5858

5959
scaffoldConversionWebhook(kbc)
6060

@@ -95,14 +95,14 @@ func GenerateV4WithoutMetrics(kbc *utils.TestContext) {
9595
"--programmatic-validation",
9696
"--make=false",
9797
)
98-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
98+
Expect(err).NotTo(HaveOccurred(), "Failed to scaffolding mutating webhook")
9999

100100
By("implementing the mutating and validating webhooks")
101101
webhookFilePath := filepath.Join(
102102
kbc.Dir, "internal/webhook", kbc.Version,
103103
fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind)))
104104
err = utils.ImplementWebhooks(webhookFilePath, strings.ToLower(kbc.Kind))
105-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
105+
Expect(err).NotTo(HaveOccurred(), "Failed to implement webhooks")
106106

107107
scaffoldConversionWebhook(kbc)
108108

@@ -158,14 +158,14 @@ func GenerateV4WithNetworkPolicies(kbc *utils.TestContext) {
158158
"--programmatic-validation",
159159
"--make=false",
160160
)
161-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
161+
Expect(err).NotTo(HaveOccurred(), "Failed to scaffolding mutating webhook")
162162

163163
By("implementing the mutating and validating webhooks")
164164
webhookFilePath := filepath.Join(
165165
kbc.Dir, "internal/webhook", kbc.Version,
166166
fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind)))
167167
err = utils.ImplementWebhooks(webhookFilePath, strings.ToLower(kbc.Kind))
168-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
168+
Expect(err).NotTo(HaveOccurred(), "Failed to implement webhooks")
169169

170170
scaffoldConversionWebhook(kbc)
171171

@@ -222,7 +222,7 @@ func creatingAPI(kbc *utils.TestContext) {
222222
"--controller",
223223
"--make=false",
224224
)
225-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
225+
Expect(err).NotTo(HaveOccurred(), "Failed to create API")
226226

227227
By("implementing the API")
228228
ExpectWithOffset(1, pluginutil.InsertCode(
@@ -241,7 +241,7 @@ func initingTheProject(kbc *utils.TestContext) {
241241
"--project-version", "3",
242242
"--domain", kbc.Domain,
243243
)
244-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
244+
Expect(err).NotTo(HaveOccurred(), "Failed to initialize project")
245245
}
246246

247247
const metricsTarget = `- path: manager_metrics_patch.yaml

0 commit comments

Comments
 (0)