Skip to content

Commit a632400

Browse files
authored
Merge pull request #3461 from yyy1000/e2e
🐛 Fix issue found to re-generate the scaffolds with the new alpha command and adding e2e tests
2 parents 66f3fbc + 141f762 commit a632400

File tree

5 files changed

+154
-1
lines changed

5 files changed

+154
-1
lines changed

pkg/rescaffold/migrate.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,23 @@ func getOutputPath(currentWorkingDirectory, outputPath string) (string, error) {
100100
return "", err
101101
}
102102

103-
func kubebuilderInit(_ store.Store) error {
103+
func kubebuilderInit(store store.Store) error {
104104
var args []string
105105
args = append(args, "init")
106+
args = append(args, getInitArgs(store)...)
106107
return util.RunCmd("kubebuilder init", "kubebuilder", args...)
107108
}
109+
110+
func getInitArgs(store store.Store) []string {
111+
var args []string
112+
plugins := store.Config().GetPluginChain()
113+
if len(plugins) > 0 {
114+
args = append(args, "--plugins")
115+
args = append(args, plugins...)
116+
}
117+
domain := store.Config().GetDomain()
118+
if domain != "" {
119+
args = append(args, "--domain", domain)
120+
}
121+
return args
122+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package alphagenerate
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
23+
. "github.com/onsi/ginkgo/v2"
24+
. "github.com/onsi/gomega"
25+
)
26+
27+
// Run e2e tests using the Ginkgo runner.
28+
func TestE2E(t *testing.T) {
29+
RegisterFailHandler(Fail)
30+
fmt.Fprintf(GinkgoWriter, "Starting kubebuilder suite test for the alpha command generate\n")
31+
RunSpecs(t, "Kubebuilder alpha generate suite")
32+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package alphagenerate
18+
19+
import (
20+
"fmt"
21+
"path/filepath"
22+
23+
pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
24+
25+
. "github.com/onsi/ginkgo/v2"
26+
. "github.com/onsi/gomega"
27+
28+
"sigs.k8s.io/kubebuilder/v3/test/e2e/utils"
29+
)
30+
31+
var _ = Describe("kubebuilder", func() {
32+
Context("alpha generate ", func() {
33+
var (
34+
kbc *utils.TestContext
35+
)
36+
37+
BeforeEach(func() {
38+
var err error
39+
kbc, err = utils.NewTestContext(pluginutil.KubebuilderBinName, "GO111MODULE=on")
40+
Expect(err).NotTo(HaveOccurred())
41+
Expect(kbc.Prepare()).To(Succeed())
42+
})
43+
44+
AfterEach(func() {
45+
kbc.Destroy()
46+
})
47+
48+
It("should regenerate the project with success", func() {
49+
ReGenerateProject(kbc)
50+
})
51+
52+
})
53+
})
54+
55+
// ReGenerateProject implements a project that is regenerated by kubebuilder.
56+
func ReGenerateProject(kbc *utils.TestContext) {
57+
var err error
58+
59+
By("initializing a project")
60+
err = kbc.Init(
61+
"--plugins", "go/v4",
62+
"--project-version", "3",
63+
"--domain", kbc.Domain,
64+
)
65+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
66+
67+
By("regenerating the project")
68+
err = kbc.Regenerate(
69+
"--input-dir", kbc.Dir,
70+
"--output-dir", filepath.Join(kbc.Dir, "testdir"),
71+
)
72+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
73+
74+
By("checking if the project file was generated with the expected layout")
75+
var layout = `layout:
76+
- go.kubebuilder.io/v4
77+
`
78+
fileContainsExpr, err := pluginutil.HasFileContentWith(
79+
filepath.Join(kbc.Dir, "testdir", "PROJECT"), layout)
80+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
81+
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
82+
83+
By("checking if the project file was generated with the expected domain")
84+
var domain = fmt.Sprintf("domain: %s", kbc.Domain)
85+
fileContainsExpr, err = pluginutil.HasFileContentWith(
86+
filepath.Join(kbc.Dir, "testdir", "PROJECT"), domain)
87+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
88+
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
89+
90+
By("checking if the project file was generated with the expected version")
91+
var version = `version: "3"`
92+
fileContainsExpr, err = pluginutil.HasFileContentWith(
93+
filepath.Join(kbc.Dir, "testdir", "PROJECT"), version)
94+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
95+
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
96+
}

test/e2e/setup.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ function test_cluster {
6666
go test $(dirname "$0")/deployimage $flags -timeout 30m
6767
go test $(dirname "$0")/v4 $flags -timeout 30m
6868
go test $(dirname "$0")/externalplugin $flags -timeout 30m
69+
go test $(dirname "$0")/alphagenerate $flags -timeout 30m
6970
}
7071

7172
function build_sample_external_plugin {

test/e2e/utils/test_context.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@ func (t *TestContext) CreateWebhook(resourceOptions ...string) error {
214214
return err
215215
}
216216

217+
// Regenerate is for running `kubebuilder alpha generate`
218+
func (t *TestContext) Regenerate(resourceOptions ...string) error {
219+
resourceOptions = append([]string{"alpha", "generate"}, resourceOptions...)
220+
//nolint:gosec
221+
cmd := exec.Command(t.BinaryName, resourceOptions...)
222+
_, err := t.Run(cmd)
223+
return err
224+
}
225+
217226
// Make is for running `make` with various targets
218227
func (t *TestContext) Make(makeOptions ...string) error {
219228
cmd := exec.Command("make", makeOptions...)

0 commit comments

Comments
 (0)