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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup tests e2e #1

Closed
wants to merge 1 commit into from
Closed
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
39 changes: 39 additions & 0 deletions test/e2e/suite_test.go
Original file line number Diff line number Diff line change
@@ -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()
})
63 changes: 62 additions & 1 deletion test/e2e/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package utils
import (
"bytes"
"crypto/rand"
"fmt"
"io/ioutil"
"math/big"
"strings"
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
32 changes: 0 additions & 32 deletions test/e2e/v2/e2e_test.go

This file was deleted.

91 changes: 11 additions & 80 deletions test/e2e/v2/e2e_suite.go → test/e2e/v2_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
})
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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),
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
32 changes: 0 additions & 32 deletions test/e2e/v3/e2e_test.go

This file was deleted.

Loading