Skip to content

Commit 83fa3dd

Browse files
committed
add e2e-tests after rebased on the helm-init removal
Signed-off-by: Mario Constanti <mario.constanti@mercedes-benz.com>
1 parent 164e5ac commit 83fa3dd

File tree

8 files changed

+89
-41
lines changed

8 files changed

+89
-41
lines changed

pkg/cli/alpha/command.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,10 @@ If no output directory is provided, the current working directory will be cleane
7474
"If unset, re-scaffolding occurs in-place "+
7575
"and will delete existing files (except .git and PROJECT).")
7676

77+
scaffoldCmd.Flags().StringVar(&opts.HelmDirectory, "helm-output-dir", "",
78+
"Directory where the new project scaffold will be written. "+
79+
"If unset, re-scaffolding occurs in-place "+
80+
"and will delete existing files (except .git and PROJECT).")
81+
7782
return scaffoldCmd
7883
}

pkg/cli/alpha/internal/generate.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ import (
4040

4141
// Generate store the required info for the command
4242
type Generate struct {
43-
InputDir string
44-
OutputDir string
43+
InputDir string
44+
OutputDir string
45+
HelmDirectory string
4546
}
4647

4748
// Generate handles the migration and scaffolding process.
@@ -110,7 +111,7 @@ func (opts *Generate) Generate() error {
110111
}
111112

112113
if hasHelmPlugin(projectConfig) {
113-
if err = migrateHelmPlugin(projectConfig); err != nil {
114+
if err = migrateHelmPlugin(projectConfig, opts.HelmDirectory); err != nil {
114115
return fmt.Errorf("error migrating Helm plugin: %w", err)
115116
}
116117
}
@@ -489,7 +490,7 @@ func kubebuilderGrafanaEdit() error {
489490
}
490491

491492
// Migrates the Helm plugin.
492-
func migrateHelmPlugin(s store.Store) error {
493+
func migrateHelmPlugin(s store.Store, targetDir string) error {
493494
var helmPlugin helmv1alpha.PluginConfig
494495

495496
err := s.Config().DecodePluginConfig(plugin.KeyFor(helmv1alpha.Plugin{}), &helmPlugin)
@@ -500,13 +501,13 @@ func migrateHelmPlugin(s store.Store) error {
500501
return fmt.Errorf("failed to decode Helm plugin config: %w", err)
501502
}
502503

503-
return kubebuilderHelmEdit(helmPlugin)
504+
return kubebuilderHelmEdit(helmPlugin, targetDir)
504505
}
505506

506507
// Edits the project to include the Helm plugin.
507-
func kubebuilderHelmEdit(resourceData helmv1alpha.PluginConfig) error {
508+
func kubebuilderHelmEdit(resourceData helmv1alpha.PluginConfig, targetDir string) error {
508509
args := []string{"edit", "--plugins", plugin.KeyFor(helmv1alpha.Plugin{})}
509-
args = append(args, getHelmOptions(resourceData)...)
510+
args = append(args, getHelmOptions(resourceData, targetDir)...)
510511
if err := util.RunCmd("kubebuilder edit", "kubebuilder", args...); err != nil {
511512
return fmt.Errorf("failed to run edit subcommand for Helm plugin: %w", err)
512513
}
@@ -516,12 +517,15 @@ func kubebuilderHelmEdit(resourceData helmv1alpha.PluginConfig) error {
516517
// Gets the options for Helm resource.
517518
// If the directory is not the default, it sets the directory option.
518519
// otherwise, it returns an empty slice which then use the default value from the edit/init subcommand.
519-
func getHelmOptions(resourceData helmv1alpha.PluginConfig) []string {
520+
func getHelmOptions(resourceData helmv1alpha.PluginConfig, targetDir string) []string {
520521
var args []string
521522

522-
if resourceData.Options.Directory != helmv1alpha.HelmDefaultTargetDirectory {
523+
if targetDir != "" {
524+
log.Info("setting Helm chart directory")
525+
args = append(args, fmt.Sprintf("--output-dir=%s", targetDir))
526+
} else if resourceData.Options.Directory != helmv1alpha.HelmDefaultTargetDirectory {
523527
log.Info("setting directory for Helm chart")
524-
args = append(args, fmt.Sprintf("--directory=%s", resourceData.Options.Directory))
528+
args = append(args, fmt.Sprintf("--output-dir=%s", resourceData.Options.Directory))
525529
}
526530

527531
return args

pkg/plugins/optional/helm/v1alpha/edit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ manifests in the chart align with the latest changes.
6868

6969
func (p *editSubcommand) BindFlags(fs *pflag.FlagSet) {
7070
fs.BoolVar(&p.force, "force", false, "if true, regenerates all the files")
71-
fs.StringVar(&p.directory, "directory", HelmDefaultTargetDirectory, "domain for groups")
71+
fs.StringVar(&p.directory, "output-dir", HelmDefaultTargetDirectory, "the directory where the Helm chart will be generated. Defaults to 'dist' if not specified.")
7272
}
7373

7474
func (p *editSubcommand) InjectConfig(c config.Config) error {

test/e2e/alphagenerate/generate_test.go

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
2929
pluginutil "sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
3030
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/deploy-image/v1alpha1"
31+
helmv1alpha "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha"
3132
"sigs.k8s.io/kubebuilder/v4/test/e2e/utils"
3233

3334
. "github.com/onsi/ginkgo/v2"
@@ -62,10 +63,10 @@ var _ = Describe("kubebuilder", func() {
6263
Expect(err).NotTo(HaveOccurred(), "Failed to create project")
6364
})
6465

65-
AfterEach(func() {
66-
By("destroying directory")
67-
kbc.Destroy()
68-
})
66+
// AfterEach(func() {
67+
// By("destroying directory")
68+
// kbc.Destroy()
69+
// })
6970

7071
It("should regenerate the project in current directory with success", func() {
7172
generateProject(kbc)
@@ -76,20 +77,20 @@ var _ = Describe("kubebuilder", func() {
7677

7778
It("should regenerate project with grafana plugin with success", func() {
7879
generateProjectWithGrafanaPlugin(kbc)
79-
regenerateProjectWith(kbc, projectOutputDir)
80+
regenerateProjectWith(kbc, projectOutputDir, "")
8081
validateGrafanaPlugin(projectFilePath)
8182
})
8283

8384
It("should regenerate project with DeployImage plugin with success", func() {
8485
generateProjectWithDeployImagePlugin(kbc)
85-
regenerateProjectWith(kbc, projectOutputDir)
86+
regenerateProjectWith(kbc, projectOutputDir, "")
8687
validateDeployImagePlugin(projectFilePath)
8788
})
8889

8990
It("should regenerate project with helm plugin with success", func() {
90-
generateProjectWithHelmPlugin(kbc)
91-
regenerateProjectWith(kbc, projectOutputDir)
92-
validateHelmPlugin(projectFilePath)
91+
generateProjectWithHelmPlugin(kbc, "helm")
92+
regenerateProjectWith(kbc, projectOutputDir, "helm")
93+
validateHelmPlugin(projectFilePath, "helm")
9394
})
9495
})
9596
})
@@ -191,11 +192,20 @@ func regenerateProject(kbc *utils.TestContext) {
191192
Expect(err).NotTo(HaveOccurred(), "Failed to regenerate project")
192193
}
193194

194-
func regenerateProjectWith(kbc *utils.TestContext, projectOutputDir string) {
195+
func regenerateProjectWith(kbc *utils.TestContext, projectOutputDir, helmOutputDir string) {
195196
By("regenerating the project")
196-
err := kbc.Regenerate(
197+
198+
regenerateOptions := []string{
197199
fmt.Sprintf("--input-dir=%s", kbc.Dir),
198200
fmt.Sprintf("--output-dir=%s", projectOutputDir),
201+
}
202+
203+
if helmOutputDir != "" {
204+
regenerateOptions = append(regenerateOptions, "--helm-output-dir", helmOutputDir)
205+
}
206+
207+
err := kbc.Regenerate(
208+
regenerateOptions...,
199209
)
200210
Expect(err).NotTo(HaveOccurred(), "Failed to regenerate project")
201211
}
@@ -206,9 +216,21 @@ func generateProjectWithGrafanaPlugin(kbc *utils.TestContext) {
206216
Expect(err).NotTo(HaveOccurred(), "Failed to edit project to enable Grafana Plugin")
207217
}
208218

209-
func generateProjectWithHelmPlugin(kbc *utils.TestContext) {
219+
func generateProjectWithHelmPlugin(kbc *utils.TestContext, directory string) {
210220
By("editing project to enable Helm plugin")
211-
err := kbc.Edit("--plugins", "helm.kubebuilder.io/v1-alpha")
221+
editOptions := []string{
222+
"--plugins", "helm.kubebuilder.io/v1-alpha",
223+
}
224+
225+
if directory != "" {
226+
editOptions = append(editOptions, "--output-dir", directory)
227+
} else {
228+
directory = "dist"
229+
}
230+
231+
err := kbc.Edit(
232+
editOptions...,
233+
)
212234
Expect(err).NotTo(HaveOccurred(), "Failed to edit project to enable Helm Plugin")
213235
}
214236

@@ -355,12 +377,12 @@ func validateDeployImagePlugin(projectFile string) {
355377
Expect(options.RunAsUser).To(Equal("1001"), "Expected runAsUser to match")
356378
}
357379

358-
func validateHelmPlugin(projectFile string) {
380+
func validateHelmPlugin(projectFile string, directory string) {
359381
projectConfig := getConfigFromProjectFile(projectFile)
360382

361383
By("checking the Helm plugin in the PROJECT file")
362-
var helmPluginConfig map[string]interface{}
384+
var helmPluginConfig helmv1alpha.PluginConfig
363385
err := projectConfig.DecodePluginConfig("helm.kubebuilder.io/v1-alpha", &helmPluginConfig)
364386
Expect(err).NotTo(HaveOccurred(), "Failed to decode Helm plugin configuration")
365-
Expect(helmPluginConfig).NotTo(BeNil(), "Expected Helm plugin configuration to be present in the PROJECT file")
387+
Expect(helmPluginConfig.Options.Directory).To(Equal("helm"), "Expected Helm plugin configuration to be present in the PROJECT file")
366388
}

test/e2e/alphagenerate/generate_v4_multigroup_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var _ = Describe("kubebuilder", func() {
6262
})
6363

6464
It("should regenerate the project in project-v4-multigroup directory with success", func() {
65-
regenerateProjectWith(kbc, projectOutputDir)
65+
regenerateProjectWith(kbc, projectOutputDir, "")
6666
By("checking that the project file was generated in the current directory")
6767
validateV4MultigroupProjectFile(kbc, projectFilePath)
6868
})

test/e2e/alphagenerate/generate_v4_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ var _ = Describe("kubebuilder", func() {
6565
})
6666

6767
func regenerateAndValidate(kbc *utils.TestContext, projectOutputDir, projectFilePath string) {
68-
regenerateProjectWith(kbc, projectOutputDir)
68+
regenerateProjectWith(kbc, projectOutputDir, "")
6969
By("checking that the project file was generated in the current directory")
7070
validateV4ProjectFile(kbc, projectFilePath)
7171
}

test/e2e/alphagenerate/generate_v4_with_plugins_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ var _ = Describe("kubebuilder", func() {
6161
})
6262

6363
It("should regenerate the project in project-v4-with-plugins directory with success", func() {
64-
regenerateProjectWith(kbc, projectOutputDir)
64+
regenerateProjectWith(kbc, projectOutputDir, "helm")
6565
By("checking that the project file was generated in the current directory")
66-
validateV4WithPluginsProjectFile(kbc, projectFilePath)
66+
validateV4WithPluginsProjectFile(kbc, projectFilePath, "helm")
6767
})
6868
})
6969
})
7070

7171
// Validate the PROJECT file for basic content and additional resources
72-
func validateV4WithPluginsProjectFile(kbc *utils.TestContext, projectFile string) {
72+
func validateV4WithPluginsProjectFile(kbc *utils.TestContext, projectFile string, helmOutputDir string) {
7373
projectConfig := getConfigFromProjectFile(projectFile)
7474

7575
By("checking the layout in the PROJECT file")
@@ -194,5 +194,5 @@ func validateV4WithPluginsProjectFile(kbc *utils.TestContext, projectFile string
194194
Expect(err).NotTo(HaveOccurred(), "Failed to decode Helm plugin configuration")
195195

196196
// Validate the resource configuration
197-
Expect(helmConfig.Options.Directory).To(Equal("dist"), "Expected default directory to be 'dist'")
197+
Expect(helmConfig.Options.Directory).To(Equal(helmOutputDir), "Expected default directory to be 'dist'")
198198
}

test/e2e/helm/generate_test.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ var _ = Describe("kubebuilder", func() {
4545

4646
It("should extend a runnable project with helm plugin", func() {
4747
initTheProject(kbc)
48-
generateProject(kbc)
48+
generateProject(kbc, "")
4949
})
5050

5151
It("should extend a runnable project with helm plugin and webhooks", func() {
5252
initTheProject(kbc)
53-
generateProject(kbc)
53+
generateProject(kbc, "")
5454
extendProjectWithWebhooks(kbc)
5555

5656
By("re-edit the project after creating webhooks")
@@ -70,7 +70,7 @@ var _ = Describe("kubebuilder", func() {
7070
// Without --force, the webhooks should not be enabled in the values.yaml file.
7171
It("should extend a runnable project with helm plugin but not running with --force", func() {
7272
initTheProject(kbc)
73-
generateProject(kbc)
73+
generateProject(kbc, "")
7474
extendProjectWithWebhooks(kbc)
7575

7676
By("re-edit the project after creating webhooks without --force")
@@ -86,22 +86,39 @@ var _ = Describe("kubebuilder", func() {
8686
Expect(err).NotTo(HaveOccurred(), "Failed to read values.yaml file")
8787
Expect(fileContainsExpr).To(BeFalse(), "Failed to get enabled webhook value from values.yaml file")
8888
})
89+
90+
It("should extend a runnable project with helm plugin and a custom helm directory", func() {
91+
initTheProject(kbc)
92+
generateProject(kbc, "helm-charts")
93+
})
8994
})
9095
})
9196

9297
// generateProject implements a helm/v1(-alpha) plugin project defined by a TestContext.
93-
func generateProject(kbc *utils.TestContext) {
98+
func generateProject(kbc *utils.TestContext, directory string) {
9499
var err error
95100

101+
editOptions := []string{
102+
"--plugins", "helm.kubebuilder.io/v1-alpha",
103+
}
104+
105+
if directory != "" {
106+
editOptions = append(editOptions, "--output-dir", directory)
107+
} else {
108+
directory = "dist"
109+
}
110+
96111
By("editing a project")
97112
err = kbc.Edit(
98-
"--plugins", "helm.kubebuilder.io/v1-alpha",
113+
editOptions...,
99114
)
100115
Expect(err).NotTo(HaveOccurred(), "Failed to edit the project")
101116

102117
fileContainsExpr, err := pluginutil.HasFileContentWith(
103118
filepath.Join(kbc.Dir, "PROJECT"),
104-
`helm.kubebuilder.io/v1-alpha: {}`)
119+
`helm.kubebuilder.io/v1-alpha:
120+
options:
121+
directory: `+directory)
105122
Expect(err).NotTo(HaveOccurred(), "Failed to read PROJECT file")
106123
Expect(fileContainsExpr).To(BeTrue(), "Failed to find helm plugin in PROJECT file")
107124

@@ -112,13 +129,13 @@ func generateProject(kbc *utils.TestContext) {
112129
Expect(fileContainsExpr).To(BeTrue(), "Failed to find projectName in PROJECT file")
113130

114131
fileContainsExpr, err = pluginutil.HasFileContentWith(
115-
filepath.Join(kbc.Dir, "dist", "chart", "Chart.yaml"),
132+
filepath.Join(kbc.Dir, directory, "chart", "Chart.yaml"),
116133
"name: e2e-"+kbc.TestSuffix)
117134
Expect(err).NotTo(HaveOccurred(), "Failed to read Chart.yaml file")
118135
Expect(fileContainsExpr).To(BeTrue(), "Failed to find name in Chart.yaml file")
119136

120137
fileContainsExpr, err = pluginutil.HasFileContentWith(
121-
filepath.Join(kbc.Dir, "dist", "chart", "templates", "manager", "manager.yaml"),
138+
filepath.Join(kbc.Dir, directory, "chart", "templates", "manager", "manager.yaml"),
122139
`metadata:
123140
name: e2e-`+kbc.TestSuffix)
124141
Expect(err).NotTo(HaveOccurred(), "Failed to read manager.yaml file")

0 commit comments

Comments
 (0)