diff --git a/test/e2e/suite_test.go b/test/e2e/suite_test.go new file mode 100644 index 00000000000..94f60da3316 --- /dev/null +++ b/test/e2e/suite_test.go @@ -0,0 +1,39 @@ +package e2e_test + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "sigs.k8s.io/kubebuilder/test/e2e/utils" +) + +var kbc *utils.TestContext + +func TestE2e(t *testing.T) { + if testing.Short() { + t.Skip("skipping E2E Suite testing in short mode") + } + RegisterFailHandler(Fail) + RunSpecs(t, "E2E Suite") +} + +var _ = BeforeSuite(func(done Done) { + var err error + kbc, err = utils.NewTestContext(utils.KubebuilderBinName, "GO111MODULE=on") + Expect(err).NotTo(HaveOccurred()) + + By("installing cert manager bundle") + Expect(kbc.InstallCertManager()).To(Succeed()) + + By("installing prometheus operator") + Expect(kbc.InstallPrometheusOperManager()).To(Succeed()) + + close(done) +}, 60) + +var _ = AfterSuite(func() { + By("uninstalling prometheus manager bundle") + kbc.UninstallPrometheusOperManager() +}) diff --git a/test/e2e/utils/util.go b/test/e2e/utils/util.go index 155d56cb5e2..1d50e8d3ebd 100644 --- a/test/e2e/utils/util.go +++ b/test/e2e/utils/util.go @@ -19,6 +19,7 @@ package utils import ( "bytes" "crypto/rand" + "fmt" "io/ioutil" "math/big" "strings" @@ -81,7 +82,7 @@ func UncommentCode(filename, target, prefix string) error { idx := strings.Index(strContent, target) if idx < 0 { - return nil + return fmt.Errorf("unable to find the code %s to be uncomment", target) } out := new(bytes.Buffer) @@ -106,3 +107,63 @@ func UncommentCode(filename, target, prefix string) error { // nolint:gosec return ioutil.WriteFile(filename, out.Bytes(), 0644) } + +// ImplementWebhooks will mock a valid webhook data +func ImplementWebhooks(filename string) error { + bs, err := ioutil.ReadFile(filename) + if err != nil { + return err + } + str := string(bs) + + str, err = EnsureExistAndReplace( + str, + "import (", + `import ( + "errors"`) + if err != nil { + return err + } + + // implement defaulting webhook logic + str, err = EnsureExistAndReplace( + str, + "// TODO(user): fill in your defaulting logic.", + `if r.Spec.Count == 0 { + r.Spec.Count = 5 + }`) + if err != nil { + return err + } + + // implement validation webhook logic + str, err = EnsureExistAndReplace( + str, + "// TODO(user): fill in your validation logic upon object creation.", + `if r.Spec.Count < 0 { + return errors.New(".spec.count must >= 0") + }`) + if err != nil { + return err + } + str, err = EnsureExistAndReplace( + str, + "// TODO(user): fill in your validation logic upon object update.", + `if r.Spec.Count < 0 { + return errors.New(".spec.count must >= 0") + }`) + if err != nil { + return err + } + // false positive + // nolint:gosec + return ioutil.WriteFile(filename, []byte(str), 0644) +} + +// EnsureExistAndReplace will check that exists to perform the replace +func EnsureExistAndReplace(input, match, replace string) (string, error) { + if !strings.Contains(input, match) { + return "", fmt.Errorf("can't find %q", match) + } + return strings.Replace(input, match, replace, -1), nil +} diff --git a/test/e2e/v2/e2e_test.go b/test/e2e/v2/e2e_test.go deleted file mode 100644 index fbddd1cb86f..00000000000 --- a/test/e2e/v2/e2e_test.go +++ /dev/null @@ -1,32 +0,0 @@ -/* -Copyright 2018 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 v2 - -import ( - "fmt" - "testing" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -// Run e2e tests using the Ginkgo runner. -func TestE2E(t *testing.T) { - RegisterFailHandler(Fail) - fmt.Fprintf(GinkgoWriter, "Starting kubebuilder suite\n") - RunSpecs(t, "Kubebuilder e2e suite") -} diff --git a/test/e2e/v2/e2e_suite.go b/test/e2e/v2_cluster_test.go similarity index 84% rename from test/e2e/v2/e2e_suite.go rename to test/e2e/v2_cluster_test.go index db0262ebf45..66e0bc580ec 100644 --- a/test/e2e/v2/e2e_suite.go +++ b/test/e2e/v2_cluster_test.go @@ -14,12 +14,12 @@ See the License for the specific language governing permissions and limitations under the License. */ -package v2 +//nolint:dupl +package e2e_test import ( "encoding/base64" "fmt" - "io/ioutil" "path/filepath" "strconv" "strings" @@ -33,30 +33,16 @@ import ( var _ = Describe("kubebuilder", func() { Context("with v2 scaffolding", func() { - var kbc *utils.TestContext + BeforeEach(func() { - var err error - kbc, err = utils.NewTestContext(utils.KubebuilderBinName, "GO111MODULE=on") - Expect(err).NotTo(HaveOccurred()) + By("creating the repository") Expect(kbc.Prepare()).To(Succeed()) - - By("installing cert manager bundle") - Expect(kbc.InstallCertManager()).To(Succeed()) - - By("installing prometheus operator") - Expect(kbc.InstallPrometheusOperManager()).To(Succeed()) }) AfterEach(func() { By("clean up created API objects during test process") kbc.CleanupManifests(filepath.Join("config", "default")) - By("uninstalling prometheus manager bundle") - kbc.UninstallPrometheusOperManager() - - By("uninstalling cert manager bundle") - kbc.UninstallCertManager() - By("remove container image and work dir") kbc.Destroy() }) @@ -100,7 +86,7 @@ var _ = Describe("kubebuilder", func() { Expect(err).Should(Succeed()) By("implementing the mutating and validating webhooks") - err = implementWebhooks(filepath.Join( + err = utils.ImplementWebhooks(filepath.Join( kbc.Dir, "api", kbc.Version, fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind)))) Expect(err).Should(Succeed()) @@ -208,6 +194,9 @@ var _ = Describe("kubebuilder", func() { Expect(len(token)).To(BeNumerically(">", 0)) By("creating a pod with curl image") + // todo: the flag --generator=run-pod/v1 is deprecated, however, shows that besides + // it should not make any difference and work locally successfully when the flag is removed + // travis has been failing and the curl pod is not found when the flag is not used cmdOpts := []string{ "run", "--generator=run-pod/v1", "curl", "--image=curlimages/curl:7.68.0", "--restart=OnFailure", "--", "curl", "-v", "-k", "-H", fmt.Sprintf(`Authorization: Bearer %s`, token), @@ -229,15 +218,15 @@ var _ = Describe("kubebuilder", func() { } return nil } - Eventually(verifyCurlUp, 30*time.Second, time.Second).Should(Succeed()) + Eventually(verifyCurlUp, time.Minute, time.Second).Should(Succeed()) - By("validating the metrics endpoint is serving as expected") + By("checking metrics endpoint serving as expected") getCurlLogs := func() string { logOutput, err := kbc.Kubectl.Logs("curl") Expect(err).NotTo(HaveOccurred()) return logOutput } - Eventually(getCurlLogs, 10*time.Second, time.Second).Should(ContainSubstring("< HTTP/2 200")) + Eventually(getCurlLogs, 3*time.Minute, time.Second).Should(ContainSubstring("< HTTP/2 200")) By("validate cert manager has provisioned the certificate secret") Eventually(func() error { @@ -330,61 +319,3 @@ var _ = Describe("kubebuilder", func() { }) }) }) - -func implementWebhooks(filename string) error { - bs, err := ioutil.ReadFile(filename) - if err != nil { - return err - } - str := string(bs) - - str, err = ensureExistAndReplace( - str, - "import (", - `import ( - "errors"`) - if err != nil { - return err - } - - // implement defaulting webhook logic - str, err = ensureExistAndReplace( - str, - "// TODO(user): fill in your defaulting logic.", - `if r.Spec.Count == 0 { - r.Spec.Count = 5 - }`) - if err != nil { - return err - } - - // implement validation webhook logic - str, err = ensureExistAndReplace( - str, - "// TODO(user): fill in your validation logic upon object creation.", - `if r.Spec.Count < 0 { - return errors.New(".spec.count must >= 0") - }`) - if err != nil { - return err - } - str, err = ensureExistAndReplace( - str, - "// TODO(user): fill in your validation logic upon object update.", - `if r.Spec.Count < 0 { - return errors.New(".spec.count must >= 0") - }`) - if err != nil { - return err - } - // false positive - // nolint:gosec - return ioutil.WriteFile(filename, []byte(str), 0644) -} - -func ensureExistAndReplace(input, match, replace string) (string, error) { - if !strings.Contains(input, match) { - return "", fmt.Errorf("can't find %q", match) - } - return strings.Replace(input, match, replace, -1), nil -} diff --git a/test/e2e/v3/e2e_test.go b/test/e2e/v3/e2e_test.go deleted file mode 100644 index 93e56624a95..00000000000 --- a/test/e2e/v3/e2e_test.go +++ /dev/null @@ -1,32 +0,0 @@ -/* -Copyright 2020 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 v3 - -import ( - "fmt" - "testing" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -// Run e2e tests using the Ginkgo runner. -func TestE2E(t *testing.T) { - RegisterFailHandler(Fail) - fmt.Fprintf(GinkgoWriter, "Starting kubebuilder suite\n") - RunSpecs(t, "Kubebuilder e2e suite") -} diff --git a/test/e2e/v3/e2e_suite.go b/test/e2e/v3_cluster_test.go similarity index 84% rename from test/e2e/v3/e2e_suite.go rename to test/e2e/v3_cluster_test.go index c4825febfa8..42f3d7a6f4f 100644 --- a/test/e2e/v3/e2e_suite.go +++ b/test/e2e/v3_cluster_test.go @@ -14,12 +14,15 @@ See the License for the specific language governing permissions and limitations under the License. */ -package v3 +// NOTE That the we are not centralizing the code from v2 and v3 to keep easier +// kept it manatained. The v3 represents the v3+ which can be changed as the v2 is kept +// to ensure its supportability. +//nolint:dupl +package e2e_test import ( "encoding/base64" "fmt" - "io/ioutil" "path/filepath" "strconv" "strings" @@ -33,30 +36,15 @@ import ( var _ = Describe("kubebuilder", func() { Context("with v3 scaffolding", func() { - var kbc *utils.TestContext BeforeEach(func() { - var err error - kbc, err = utils.NewTestContext(utils.KubebuilderBinName, "GO111MODULE=on") - Expect(err).NotTo(HaveOccurred()) + By("creating the repository") Expect(kbc.Prepare()).To(Succeed()) - - By("installing cert manager bundle") - Expect(kbc.InstallCertManager()).To(Succeed()) - - By("installing prometheus operator") - Expect(kbc.InstallPrometheusOperManager()).To(Succeed()) }) AfterEach(func() { By("clean up created API objects during test process") kbc.CleanupManifests(filepath.Join("config", "default")) - By("uninstalling prometheus manager bundle") - kbc.UninstallPrometheusOperManager() - - By("uninstalling cert manager bundle") - kbc.UninstallCertManager() - By("remove container image and work dir") kbc.Destroy() }) @@ -100,7 +88,7 @@ var _ = Describe("kubebuilder", func() { Expect(err).Should(Succeed()) By("implementing the mutating and validating webhooks") - err = implementWebhooks(filepath.Join( + err = utils.ImplementWebhooks(filepath.Join( kbc.Dir, "api", kbc.Version, fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind)))) Expect(err).Should(Succeed()) @@ -208,6 +196,9 @@ var _ = Describe("kubebuilder", func() { Expect(len(token)).To(BeNumerically(">", 0)) By("creating a pod with curl image") + // todo: the flag --generator=run-pod/v1 is deprecated, however, shows that besides + // it should not make any difference and work locally successfully when the flag is removed + // travis has been failing and the curl pod is not found when the flag is not used cmdOpts := []string{ "run", "--generator=run-pod/v1", "curl", "--image=curlimages/curl:7.68.0", "--restart=OnFailure", "--", "curl", "-v", "-k", "-H", fmt.Sprintf(`Authorization: Bearer %s`, token), @@ -229,15 +220,15 @@ var _ = Describe("kubebuilder", func() { } return nil } - Eventually(verifyCurlUp, 30*time.Second, time.Second).Should(Succeed()) + Eventually(verifyCurlUp, time.Minute, time.Second).Should(Succeed()) - By("validating the metrics endpoint is serving as expected") + By("checking metrics endpoint serving as expected") getCurlLogs := func() string { logOutput, err := kbc.Kubectl.Logs("curl") Expect(err).NotTo(HaveOccurred()) return logOutput } - Eventually(getCurlLogs, 10*time.Second, time.Second).Should(ContainSubstring("< HTTP/2 200")) + Eventually(getCurlLogs, 3*time.Minute, time.Second).Should(ContainSubstring("< HTTP/2 200")) By("validate cert manager has provisioned the certificate secret") Eventually(func() error { @@ -330,61 +321,3 @@ var _ = Describe("kubebuilder", func() { }) }) }) - -func implementWebhooks(filename string) error { - bs, err := ioutil.ReadFile(filename) - if err != nil { - return err - } - str := string(bs) - - str, err = ensureExistAndReplace( - str, - "import (", - `import ( - "errors"`) - if err != nil { - return err - } - - // implement defaulting webhook logic - str, err = ensureExistAndReplace( - str, - "// TODO(user): fill in your defaulting logic.", - `if r.Spec.Count == 0 { - r.Spec.Count = 5 - }`) - if err != nil { - return err - } - - // implement validation webhook logic - str, err = ensureExistAndReplace( - str, - "// TODO(user): fill in your validation logic upon object creation.", - `if r.Spec.Count < 0 { - return errors.New(".spec.count must >= 0") - }`) - if err != nil { - return err - } - str, err = ensureExistAndReplace( - str, - "// TODO(user): fill in your validation logic upon object update.", - `if r.Spec.Count < 0 { - return errors.New(".spec.count must >= 0") - }`) - if err != nil { - return err - } - // false positive - // nolint:gosec - return ioutil.WriteFile(filename, []byte(str), 0644) -} - -func ensureExistAndReplace(input, match, replace string) (string, error) { - if !strings.Contains(input, match) { - return "", fmt.Errorf("can't find %q", match) - } - return strings.Replace(input, match, replace, -1), nil -} diff --git a/test_e2e.sh b/test_e2e.sh index f7fe1804a2c..a56da2c5392 100755 --- a/test_e2e.sh +++ b/test_e2e.sh @@ -38,5 +38,4 @@ function cleanup() { } trap cleanup EXIT -go test ./test/e2e/v2 -go test ./test/e2e/v3 +go test ./test/e2e/ -v -ginkgo.v diff --git a/test_e2e_local.sh b/test_e2e_local.sh index 72776af3794..abc6e11b3ec 100755 --- a/test_e2e_local.sh +++ b/test_e2e_local.sh @@ -49,5 +49,4 @@ if [ -z "${SKIP_KIND_CLEANUP:-}" ]; then fi # when changing these commands, make sure to keep in sync with ./test_e2e.sh -go test ./test/e2e/v2 -v -ginkgo.v -go test ./test/e2e/v3 -v -ginkgo.v +go test ./test/e2e/ -v -ginkgo.v