Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Fix issue found to re-generate the scaffolds with the new alpha command and adding e2e tests #3461

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion pkg/rescaffold/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,23 @@ 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...)
Comment on lines +114 to +115
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we do all the appends together?

Suggested change
args = append(args, "--plugins")
args = append(args, plugins...)
args = append(args, "--plugins", plugins...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I tried this and it will not compile:

too many arguments in call to append
        have ([]string, string, []string)
        want ([]string, ...string)
make: *** [Makefile:57: build] Error 1

}
domain := store.Config().GetDomain()
if domain != "" {
args = append(args, "--domain", domain)
}
return args
}
32 changes: 32 additions & 0 deletions test/e2e/alphagenerate/e2e_suite_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
96 changes: 96 additions & 0 deletions test/e2e/alphagenerate/generate_test.go
Original file line number Diff line number Diff line change
@@ -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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fileContainsExpr, err := pluginutil.HasFileContentWith(
By("checking if the project file was generated with the expected content")
fileContainsExpr, err := pluginutil.HasFileContentWith(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a description so we have further info when fails to know where it failed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea.

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())
}
1 change: 1 addition & 0 deletions test/e2e/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/utils/test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
Loading