diff --git a/pkg/rescaffold/migrate.go b/pkg/rescaffold/migrate.go index 91854c24783..999ea78f929 100644 --- a/pkg/rescaffold/migrate.go +++ b/pkg/rescaffold/migrate.go @@ -100,8 +100,24 @@ func getOutputPath(currentWorkingDirectory, outputPath string) (string, error) { return "", err } -func kubebuilderInit(_ store.Store) error { +func kubebuilderInit(store store.Store) error { var args []string args = append(args, "init") + args = append(args, getInitArgs(store)...) return util.RunCmd("kubebuilder init", "kubebuilder", args...) } + +func getInitArgs(store store.Store) []string { + var args []string + plugins := store.Config().GetPluginChain() + if len(plugins) > 0 { + args = append(args, "--plugins") + args = append(args, plugins...) + } + domain := store.Config().GetDomain() + if domain != "" { + args = append(args, "--domain") + args = append(args, domain) + } + return args +} diff --git a/test/e2e/alphagenerate/e2e_suite_test.go b/test/e2e/alphagenerate/e2e_suite_test.go new file mode 100644 index 00000000000..eab37e6dac4 --- /dev/null +++ b/test/e2e/alphagenerate/e2e_suite_test.go @@ -0,0 +1,32 @@ +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package alphagenerate + +import ( + "fmt" + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +// Run e2e tests using the Ginkgo runner. +func TestE2E(t *testing.T) { + RegisterFailHandler(Fail) + fmt.Fprintf(GinkgoWriter, "Starting kubebuilder suite test for the alpha command generate\n") + RunSpecs(t, "Kubebuilder alpha generate suite") +} diff --git a/test/e2e/alphagenerate/generate_test.go b/test/e2e/alphagenerate/generate_test.go new file mode 100644 index 00000000000..b2d455a5131 --- /dev/null +++ b/test/e2e/alphagenerate/generate_test.go @@ -0,0 +1,96 @@ +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package alphagenerate + +import ( + "fmt" + "path/filepath" + + pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "sigs.k8s.io/kubebuilder/v3/test/e2e/utils" +) + +var _ = Describe("kubebuilder", func() { + Context("alpha generate ", func() { + var ( + kbc *utils.TestContext + ) + + BeforeEach(func() { + var err error + kbc, err = utils.NewTestContext(pluginutil.KubebuilderBinName, "GO111MODULE=on") + Expect(err).NotTo(HaveOccurred()) + Expect(kbc.Prepare()).To(Succeed()) + }) + + AfterEach(func() { + kbc.Destroy() + }) + + It("should regenerate the project with success", func() { + ReGenerateProject(kbc) + }) + + }) +}) + +// ReGenerateProject implements a project that is regenerated by kubebuilder. +func ReGenerateProject(kbc *utils.TestContext) { + var err error + + By("initializing a project") + err = kbc.Init( + "--plugins", "go/v4", + "--project-version", "3", + "--domain", kbc.Domain, + ) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + + By("regenerating the project") + err = kbc.Regenerate( + "--input-dir", kbc.Dir, + "--output-dir", filepath.Join(kbc.Dir, "testdir"), + ) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + + By("checking if the project file was generated with the expected layout") + var layout = `layout: +- go.kubebuilder.io/v4 +` + fileContainsExpr, err := pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "testdir", "PROJECT"), layout) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) + + By("checking if the project file was generated with the expected domain") + var domain = fmt.Sprintf("domain: %s", kbc.Domain) + fileContainsExpr, err = pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "testdir", "PROJECT"), domain) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) + + By("checking if the project file was generated with the expected version") + var version = `version: "3"` + fileContainsExpr, err = pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "testdir", "PROJECT"), version) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) +} diff --git a/test/e2e/setup.sh b/test/e2e/setup.sh index 20e7a9852fa..15210adafc3 100755 --- a/test/e2e/setup.sh +++ b/test/e2e/setup.sh @@ -66,6 +66,7 @@ function test_cluster { go test $(dirname "$0")/deployimage $flags -timeout 30m go test $(dirname "$0")/v4 $flags -timeout 30m go test $(dirname "$0")/externalplugin $flags -timeout 30m + go test $(dirname "$0")/alphagenerate $flags -timeout 30m } function build_sample_external_plugin { diff --git a/test/e2e/utils/test_context.go b/test/e2e/utils/test_context.go index d9630d6c7d8..6a0f61deedb 100644 --- a/test/e2e/utils/test_context.go +++ b/test/e2e/utils/test_context.go @@ -214,6 +214,15 @@ func (t *TestContext) CreateWebhook(resourceOptions ...string) error { return err } +// Regenerate is for running `kubebuilder alpha generate` +func (t *TestContext) Regenerate(resourceOptions ...string) error { + resourceOptions = append([]string{"alpha", "generate"}, resourceOptions...) + //nolint:gosec + cmd := exec.Command(t.BinaryName, resourceOptions...) + _, err := t.Run(cmd) + return err +} + // Make is for running `make` with various targets func (t *TestContext) Make(makeOptions ...string) error { cmd := exec.Command("make", makeOptions...)