Skip to content

Commit

Permalink
feat: init e2e test
Browse files Browse the repository at this point in the history
feat: add test data

fix: add test in setup

fix: change the file to PROJECT

fix: test file update

fix: doc nit and test data

feat: Add By to check and project file

feat: separate fields

feat: use domain when scafflod

feat: add plugin arg when re-scaffold

fix: function name

feat: refactor plugins

fix: remove lint
  • Loading branch information
yyy1000 committed Jun 29, 2023
1 parent d9c14df commit 0ac8288
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pkg/rescaffold/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
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(
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

0 comments on commit 0ac8288

Please sign in to comment.