-
Notifications
You must be signed in to change notification settings - Fork 1.8k
cleanup go e2e tests and perform improvements #3499
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
Merged
camilamacedo86
merged 4 commits into
operator-framework:master
from
camilamacedo86:golang-cleanup-test
Sep 15, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
// Copyright 2020 The Operator-SDK 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. | ||
|
||
// Modified from https://github.com/kubernetes-sigs/kubebuilder/tree/39224f0/test/e2e/v3 | ||
|
||
package e2e_go_test | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo" //nolint:golint | ||
. "github.com/onsi/gomega" //nolint:golint | ||
kbtestutils "sigs.k8s.io/kubebuilder/test/e2e/utils" | ||
) | ||
|
||
var _ = Describe("operator-sdk", func() { | ||
var controllerPodName string | ||
|
||
Context("built with operator-sdk", func() { | ||
BeforeEach(func() { | ||
By("enabling Prometheus via the kustomization.yaml") | ||
Expect(kbtestutils.UncommentCode( | ||
filepath.Join(tc.Dir, "config", "default", "kustomization.yaml"), | ||
"#- ../prometheus", "#")).To(Succeed()) | ||
|
||
By("deploying project on the cluster") | ||
err := tc.Make("deploy", "IMG="+tc.ImageName) | ||
Expect(err).Should(Succeed()) | ||
}) | ||
AfterEach(func() { | ||
By("cleaning up the operator and resources") | ||
defaultOutput, err := tc.KustomizeBuild(filepath.Join("config", "default")) | ||
Expect(err).NotTo(HaveOccurred()) | ||
_, err = tc.Kubectl.WithInput(string(defaultOutput)).Command("delete", "-f", "-") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("deleting Curl Pod created") | ||
_, _ = tc.Kubectl.Delete(true, "pod", "curl") | ||
|
||
By("cleaning up permissions") | ||
_, _ = tc.Kubectl.Command("delete", "clusterrolebinding", | ||
joelanford marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fmt.Sprintf("metrics-%s", tc.TestSuffix)) | ||
|
||
By("undeploy project") | ||
_ = tc.Make("undeploy") | ||
joelanford marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
By("ensuring that the namespace was deleted") | ||
verifyNamespaceDeleted := func() error { | ||
_, err := tc.Kubectl.Command("get", "namespace", tc.Kubectl.Namespace) | ||
if strings.Contains(err.Error(), "(NotFound): namespaces") { | ||
return err | ||
} | ||
return nil | ||
} | ||
Eventually(verifyNamespaceDeleted, 2*time.Minute, time.Second).ShouldNot(Succeed()) | ||
}) | ||
|
||
It("should run correctly in a cluster", func() { | ||
By("checking if the Operator project Pod is running") | ||
verifyControllerUp := func() error { | ||
By("getting the controller-manager pod name") | ||
podOutput, err := tc.Kubectl.Get( | ||
true, | ||
"pods", "-l", "control-plane=controller-manager", | ||
"-o", "go-template={{ range .items }}{{ if not .metadata.deletionTimestamp }}{{ .metadata.name }}"+ | ||
"{{ \"\\n\" }}{{ end }}{{ end }}") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("ensuring the created controller-manager Pod") | ||
podNames := kbtestutils.GetNonEmptyLines(podOutput) | ||
if len(podNames) != 1 { | ||
return fmt.Errorf("expect 1 controller pods running, but got %d", len(podNames)) | ||
} | ||
controllerPodName = podNames[0] | ||
Expect(controllerPodName).Should(ContainSubstring("controller-manager")) | ||
|
||
By("checking the controller-manager Pod is running") | ||
status, err := tc.Kubectl.Get( | ||
true, | ||
"pods", controllerPodName, "-o", "jsonpath={.status.phase}") | ||
Expect(err).NotTo(HaveOccurred()) | ||
if status != "Running" { | ||
return fmt.Errorf("controller pod in %s status", status) | ||
} | ||
return nil | ||
} | ||
Eventually(verifyControllerUp, 2*time.Minute, time.Second).Should(Succeed()) | ||
|
||
By("ensuring the created ServiceMonitor for the manager") | ||
_, err := tc.Kubectl.Get( | ||
true, | ||
"ServiceMonitor", | ||
fmt.Sprintf("e2e-%s-controller-manager-metrics-monitor", tc.TestSuffix)) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("ensuring the created metrics Service for the manager") | ||
_, err = tc.Kubectl.Get( | ||
true, | ||
"Service", | ||
fmt.Sprintf("e2e-%s-controller-manager-metrics-service", tc.TestSuffix)) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("creating an instance of CR") | ||
// currently controller-runtime doesn't provide a readiness probe, we retry a few times | ||
// we can change it to probe the readiness endpoint after CR supports it. | ||
sampleFile := filepath.Join("config", "samples", | ||
fmt.Sprintf("%s_%s_%s.yaml", tc.Group, tc.Version, strings.ToLower(tc.Kind))) | ||
Eventually(func() error { | ||
_, err = tc.Kubectl.Apply(true, "-f", sampleFile) | ||
return err | ||
}, time.Minute, time.Second).Should(Succeed()) | ||
|
||
By("ensuring the created resource object gets reconciled in controller") | ||
managerContainerLogs := func() string { | ||
logOutput, err := tc.Kubectl.Logs(controllerPodName, "-c", "manager") | ||
Expect(err).NotTo(HaveOccurred()) | ||
return logOutput | ||
} | ||
Eventually(managerContainerLogs, time.Minute, time.Second).Should(ContainSubstring("Successfully Reconciled")) | ||
|
||
By("granting permissions to access the metrics and read the token") | ||
_, err = tc.Kubectl.Command( | ||
"create", | ||
"clusterrolebinding", | ||
fmt.Sprintf("metrics-%s", tc.TestSuffix), | ||
fmt.Sprintf("--clusterrole=e2e-%s-metrics-reader", tc.TestSuffix), | ||
fmt.Sprintf("--serviceaccount=%s:default", tc.Kubectl.Namespace)) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("getting the token") | ||
b64Token, err := tc.Kubectl.Get( | ||
true, | ||
"secrets", | ||
"-o=jsonpath={.items[0].data.token}") | ||
Expect(err).NotTo(HaveOccurred()) | ||
token, err := base64.StdEncoding.DecodeString(strings.TrimSpace(b64Token)) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(token).NotTo(HaveLen(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), | ||
fmt.Sprintf("https://e2e-%v-controller-manager-metrics-service.e2e-%v-system.svc:8443/metrics", | ||
tc.TestSuffix, tc.TestSuffix), | ||
} | ||
_, err = tc.Kubectl.CommandInNamespace(cmdOpts...) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("validating the curl pod running as expected") | ||
verifyCurlUp := func() error { | ||
// Validate pod status | ||
status, err := tc.Kubectl.Get( | ||
true, | ||
"pods", "curl", "-o", "jsonpath={.status.phase}") | ||
Expect(err).NotTo(HaveOccurred()) | ||
if status != "Completed" && status != "Succeeded" { | ||
return fmt.Errorf("curl pod in %s status", status) | ||
} | ||
return nil | ||
} | ||
Eventually(verifyCurlUp, 4*time.Minute, time.Second).Should(Succeed()) | ||
|
||
By("checking metrics endpoint serving as expected") | ||
getCurlLogs := func() string { | ||
logOutput, err := tc.Kubectl.Logs("curl") | ||
Expect(err).NotTo(HaveOccurred()) | ||
return logOutput | ||
} | ||
Eventually(getCurlLogs, time.Minute, time.Second).Should(ContainSubstring("< HTTP/2 200")) | ||
}) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2020 The Operator-SDK 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 e2e_go_test | ||
|
||
import ( | ||
"os/exec" | ||
|
||
. "github.com/onsi/ginkgo" //nolint:golint | ||
. "github.com/onsi/gomega" //nolint:golint | ||
) | ||
|
||
var _ = Describe("Running Go projects", func() { | ||
Context("built with operator-sdk", func() { | ||
|
||
BeforeEach(func() { | ||
By("installing CRD's") | ||
err := tc.Make("install") | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
AfterEach(func() { | ||
By("uninstalling CRD's") | ||
err := tc.Make("uninstall") | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("should run correctly locally", func() { | ||
By("running the project") | ||
cmd := exec.Command("make", "run") | ||
err := cmd.Start() | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("killing the project") | ||
err = cmd.Process.Kill() | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.