|
| 1 | +// Copyright 2020 The Operator-SDK Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Modified from https://github.com/kubernetes-sigs/kubebuilder/tree/39224f0/test/e2e/v3 |
| 16 | + |
| 17 | +package e2e |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "path/filepath" |
| 22 | + "strings" |
| 23 | + "time" |
| 24 | + |
| 25 | + . "github.com/onsi/ginkgo" //nolint:golint |
| 26 | + . "github.com/onsi/gomega" //nolint:golint |
| 27 | + |
| 28 | + "github.com/operator-framework/operator-sdk/test/e2e-new/utils" |
| 29 | +) |
| 30 | + |
| 31 | +var _ = Describe("operator-sdk", func() { |
| 32 | + Context("with the new project layout", func() { |
| 33 | + var tc *utils.TestContext |
| 34 | + BeforeEach(func() { |
| 35 | + |
| 36 | + By("creating a new test context") |
| 37 | + var err error |
| 38 | + tc, err = utils.NewTestContext("operator-sdk", "GO111MODULE=on") |
| 39 | + Expect(err).NotTo(HaveOccurred()) |
| 40 | + Expect(tc.Prepare()).To(Succeed()) |
| 41 | + }) |
| 42 | + |
| 43 | + AfterEach(func() { |
| 44 | + By("cleaning up created API objects during test process") |
| 45 | + tc.CleanupManifests(filepath.Join("config", "default")) |
| 46 | + |
| 47 | + By("removing container image and work dir") |
| 48 | + tc.Destroy() |
| 49 | + }) |
| 50 | + |
| 51 | + It("should generate a runnable project", func() { |
| 52 | + var controllerPodName string |
| 53 | + By("initializing a project") |
| 54 | + err := tc.Init( |
| 55 | + "--project-version", "3-alpha", |
| 56 | + "--domain", tc.Domain, |
| 57 | + "--fetch-deps=false") |
| 58 | + Expect(err).Should(Succeed()) |
| 59 | + |
| 60 | + By("creating an API definition") |
| 61 | + err = tc.CreateAPI( |
| 62 | + "--group", tc.Group, |
| 63 | + "--version", tc.Version, |
| 64 | + "--kind", tc.Kind, |
| 65 | + "--namespaced", |
| 66 | + "--resource", |
| 67 | + "--controller", |
| 68 | + "--make=false") |
| 69 | + Expect(err).Should(Succeed()) |
| 70 | + |
| 71 | + By("implementing the API") |
| 72 | + Expect(utils.InsertCode( |
| 73 | + filepath.Join(tc.Dir, "api", tc.Version, fmt.Sprintf("%s_types.go", strings.ToLower(tc.Kind))), |
| 74 | + fmt.Sprintf(`type %sSpec struct { |
| 75 | +`, tc.Kind), |
| 76 | + ` // +optional |
| 77 | + Count int `+"`"+`json:"count,omitempty"`+"`"+` |
| 78 | +`)).Should(Succeed()) |
| 79 | + |
| 80 | + By("building the operator image") |
| 81 | + err = tc.Make("docker-build", "IMG="+tc.ImageName) |
| 82 | + Expect(err).Should(Succeed()) |
| 83 | + |
| 84 | + By("loading the operator image into the test cluster") |
| 85 | + err = tc.LoadImageToKindCluster() |
| 86 | + Expect(err).Should(Succeed()) |
| 87 | + |
| 88 | + By("deploying the controller manager") |
| 89 | + err = tc.Make("deploy", "IMG="+tc.ImageName) |
| 90 | + Expect(err).Should(Succeed()) |
| 91 | + |
| 92 | + By("ensuring the controller-manager pod is running as expected") |
| 93 | + verifyControllerUp := func() error { |
| 94 | + // Get pod name |
| 95 | + podOutput, err := tc.Kubectl.Get( |
| 96 | + true, |
| 97 | + "pods", "-l", "control-plane=controller-manager", |
| 98 | + "-o", "go-template={{ range .items }}{{ if not .metadata.deletionTimestamp }}{{ .metadata.name }}"+ |
| 99 | + "{{ \"\\n\" }}{{ end }}{{ end }}") |
| 100 | + Expect(err).NotTo(HaveOccurred()) |
| 101 | + podNames := utils.GetNonEmptyLines(podOutput) |
| 102 | + if len(podNames) != 1 { |
| 103 | + return fmt.Errorf("expect 1 controller pods running, but got %d", len(podNames)) |
| 104 | + } |
| 105 | + controllerPodName = podNames[0] |
| 106 | + Expect(controllerPodName).Should(ContainSubstring("controller-manager")) |
| 107 | + |
| 108 | + // Validate pod status |
| 109 | + status, err := tc.Kubectl.Get( |
| 110 | + true, |
| 111 | + "pods", controllerPodName, "-o", "jsonpath={.status.phase}") |
| 112 | + Expect(err).NotTo(HaveOccurred()) |
| 113 | + if status != "Running" { |
| 114 | + return fmt.Errorf("controller pod in %s status", status) |
| 115 | + } |
| 116 | + return nil |
| 117 | + } |
| 118 | + Eventually(verifyControllerUp, time.Minute, time.Second).Should(Succeed()) |
| 119 | + |
| 120 | + By("creating an instance of CR") |
| 121 | + // currently controller-runtime doesn't provide a readiness probe, we retry a few times |
| 122 | + // we can change it to probe the readiness endpoint after CR supports it. |
| 123 | + sampleFile := filepath.Join("config", "samples", |
| 124 | + fmt.Sprintf("%s_%s_%s.yaml", tc.Group, tc.Version, strings.ToLower(tc.Kind))) |
| 125 | + Eventually(func() error { |
| 126 | + _, err = tc.Kubectl.Apply(true, "-f", sampleFile) |
| 127 | + return err |
| 128 | + }, time.Minute, time.Second).Should(Succeed()) |
| 129 | + |
| 130 | + By("ensuring the created resource object gets reconciled in controller") |
| 131 | + managerContainerLogs := func() string { |
| 132 | + logOutput, err := tc.Kubectl.Logs(controllerPodName, "-c", "manager") |
| 133 | + Expect(err).NotTo(HaveOccurred()) |
| 134 | + return logOutput |
| 135 | + } |
| 136 | + Eventually(managerContainerLogs, time.Minute, time.Second).Should(ContainSubstring("Successfully Reconciled")) |
| 137 | + }) |
| 138 | + }) |
| 139 | +}) |
0 commit comments