|
| 1 | +// +build e2e |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2018 The Kubernetes Authors. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package e2e_test |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "flag" |
| 24 | + "fmt" |
| 25 | + "io/ioutil" |
| 26 | + "os" |
| 27 | + "os/exec" |
| 28 | + "path" |
| 29 | + "testing" |
| 30 | + "time" |
| 31 | + |
| 32 | + "github.com/onsi/ginkgo" |
| 33 | + "github.com/onsi/gomega" |
| 34 | + appsv1 "k8s.io/api/apps/v1" |
| 35 | + apimachinerytypes "k8s.io/apimachinery/pkg/types" |
| 36 | + "sigs.k8s.io/cluster-api/test/helpers/kind" |
| 37 | + "sigs.k8s.io/cluster-api/test/helpers/scheme" |
| 38 | + "sigs.k8s.io/cluster-api/util" |
| 39 | + crclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 40 | +) |
| 41 | + |
| 42 | +func TestE2E(t *testing.T) { |
| 43 | + gomega.RegisterFailHandler(ginkgo.Fail) |
| 44 | + ginkgo.RunSpecs(t, "e2e Suite") |
| 45 | +} |
| 46 | + |
| 47 | +const ( |
| 48 | + capiNamespace = "capi-system" |
| 49 | + capiDeploymentName = "capi-controller-manager" |
| 50 | + setupTimeout = 10 * 60 |
| 51 | +) |
| 52 | + |
| 53 | +var ( |
| 54 | + managerImage = flag.String("managerImage", "", "Docker image to load into the kind cluster for testing") |
| 55 | + capiComponents = flag.String("capiComponents", "", "URL to CAPI components to load") |
| 56 | + kustomizeBinary = flag.String("kustomizeBinary", "kustomize", "path to the kustomize binary") |
| 57 | + |
| 58 | + kindCluster kind.Cluster |
| 59 | + kindClient crclient.Client |
| 60 | + suiteTmpDir string |
| 61 | +) |
| 62 | + |
| 63 | +var _ = ginkgo.BeforeSuite(func() { |
| 64 | + fmt.Fprintf(ginkgo.GinkgoWriter, "Setting up kind cluster\n") |
| 65 | + |
| 66 | + var err error |
| 67 | + suiteTmpDir, err = ioutil.TempDir("", "capi-e2e-suite") |
| 68 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 69 | + |
| 70 | + kindCluster = kind.Cluster{ |
| 71 | + Name: "capi-test-" + util.RandomString(6), |
| 72 | + } |
| 73 | + kindCluster.Setup() |
| 74 | + loadManagerImage(kindCluster) |
| 75 | + |
| 76 | + // Deploy the CAPA components |
| 77 | + deployCAPIComponents(kindCluster) |
| 78 | + |
| 79 | + kindClient, err = crclient.New(kindCluster.RestConfig(), crclient.Options{Scheme: scheme.SetupScheme()}) |
| 80 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 81 | + |
| 82 | + // Verify capi components are deployed |
| 83 | + waitDeployment(capiNamespace, capiDeploymentName) |
| 84 | +}, setupTimeout) |
| 85 | + |
| 86 | +var _ = ginkgo.AfterSuite(func() { |
| 87 | + fmt.Fprintf(ginkgo.GinkgoWriter, "Tearing down kind cluster\n") |
| 88 | + kindCluster.Teardown() |
| 89 | + os.RemoveAll(suiteTmpDir) |
| 90 | +}) |
| 91 | + |
| 92 | +func waitDeployment(namespace, name string) { |
| 93 | + fmt.Fprintf(ginkgo.GinkgoWriter, "Ensuring %s/%s is deployed\n", namespace, name) |
| 94 | + gomega.Eventually( |
| 95 | + func() (int32, error) { |
| 96 | + deployment := &appsv1.Deployment{} |
| 97 | + if err := kindClient.Get(context.TODO(), apimachinerytypes.NamespacedName{Namespace: namespace, Name: name}, deployment); err != nil { |
| 98 | + return 0, err |
| 99 | + } |
| 100 | + return deployment.Status.ReadyReplicas, nil |
| 101 | + }, 5*time.Minute, 15*time.Second, |
| 102 | + ).ShouldNot(gomega.BeZero()) |
| 103 | +} |
| 104 | + |
| 105 | +func loadManagerImage(kindCluster kind.Cluster) { |
| 106 | + if managerImage != nil && *managerImage != "" { |
| 107 | + kindCluster.LoadImage(*managerImage) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func applyManifests(kindCluster kind.Cluster, manifests *string) { |
| 112 | + gomega.Expect(manifests).ToNot(gomega.BeNil()) |
| 113 | + fmt.Fprintf(ginkgo.GinkgoWriter, "Applying manifests for %s\n", *manifests) |
| 114 | + gomega.Expect(*manifests).ToNot(gomega.BeEmpty()) |
| 115 | + kindCluster.ApplyYAML(*manifests) |
| 116 | +} |
| 117 | + |
| 118 | +func deployCAPIComponents(kindCluster kind.Cluster) { |
| 119 | + if capiComponents != nil && *capiComponents != "" { |
| 120 | + applyManifests(kindCluster, capiComponents) |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + fmt.Fprintf(ginkgo.GinkgoWriter, "Generating CAPI manifests\n") |
| 125 | + |
| 126 | + // Build the manifests using kustomize |
| 127 | + capiManifests, err := exec.Command(*kustomizeBinary, "build", "../../config/default").Output() |
| 128 | + if err != nil { |
| 129 | + if exitError, ok := err.(*exec.ExitError); ok { |
| 130 | + fmt.Fprintf(ginkgo.GinkgoWriter, "Error: %s\n", string(exitError.Stderr)) |
| 131 | + } |
| 132 | + } |
| 133 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 134 | + |
| 135 | + // write out the manifests |
| 136 | + manifestFile := path.Join(suiteTmpDir, "cluster-api-components.yaml") |
| 137 | + gomega.Expect(ioutil.WriteFile(manifestFile, capiManifests, 0644)).NotTo(gomega.HaveOccurred()) |
| 138 | + |
| 139 | + // apply generated manifests |
| 140 | + applyManifests(kindCluster, &manifestFile) |
| 141 | +} |
0 commit comments