Skip to content

Commit

Permalink
Merge pull request #1474 from detiber/commonE2EUtils
Browse files Browse the repository at this point in the history
🏃 Add common utilities for provider-based testing
  • Loading branch information
k8s-ci-robot committed Oct 8, 2019
2 parents cf29f37 + fe32969 commit 7f1890e
Show file tree
Hide file tree
Showing 10 changed files with 467 additions and 1 deletion.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ TAG ?= dev
ARCH ?= amd64
ALL_ARCH = amd64 arm arm64 ppc64le s390x

# Allow overriding the imagePullPolicy
PULL_POLICY ?= Always

all: test manager clusterctl

help: ## Display this help
Expand All @@ -71,6 +74,11 @@ test: generate lint ## Run tests
test-integration: ## Run integration tests
go test -v -tags=integration ./test/integration/...

.PHONY: test-e2e
test-e2e: ## Run e2e tests
PULL_POLICY=IfNotPresent $(MAKE) docker-build
go test -v -tags=e2e -timeout=1h ./test/e2e/... -args --managerImage $(CONTROLLER_IMG)-$(ARCH):$(TAG)

## --------------------------------------
## Binaries
## --------------------------------------
Expand Down Expand Up @@ -147,6 +155,7 @@ modules: ## Runs go mod to ensure modules are up to date.
docker-build: ## Build the docker image for controller-manager
docker build --pull --build-arg ARCH=$(ARCH) . -t $(CONTROLLER_IMG)-$(ARCH):$(TAG)
MANIFEST_IMG=$(CONTROLLER_IMG)-$(ARCH) MANIFEST_TAG=$(TAG) $(MAKE) set-manifest-image
$(MAKE) set-manifest-pull-policy

.PHONY: docker-push
docker-push: ## Push the docker image
Expand Down Expand Up @@ -176,12 +185,18 @@ docker-push-manifest: ## Push the fat manifest docker image.
@for arch in $(ALL_ARCH); do docker manifest annotate --arch $${arch} ${CONTROLLER_IMG}:${TAG} ${CONTROLLER_IMG}-$${arch}:${TAG}; done
docker manifest push --purge $(CONTROLLER_IMG):$(TAG)
MANIFEST_IMG=$(CONTROLLER_IMG) MANIFEST_TAG=$(TAG) $(MAKE) set-manifest-image
$(MAKE) set-manifest-pull-policy

.PHONY: set-manifest-image
set-manifest-image:
$(info Updating kustomize image patch file for manager resource)
sed -i'' -e 's@image: .*@image: '"${MANIFEST_IMG}:$(MANIFEST_TAG)"'@' ./config/default/manager_image_patch.yaml

.PHONY: set-manifest-pull-policy
set-manifest-pull-policy:
$(info Updating kustomize pull policy file for manager resource)
sed -i'' -e 's@imagePullPolicy: .*@imagePullPolicy: '"$(PULL_POLICY)"'@' ./config/default/manager_pull_policy.yaml

## --------------------------------------
## Release
## --------------------------------------
Expand All @@ -203,6 +218,7 @@ release: clean-release ## Builds and push container images using the latest git
# Set the manifest image to the production bucket.
MANIFEST_IMG=$(PROD_REGISTRY)/$(IMAGE_NAME) MANIFEST_TAG=$(RELEASE_TAG) \
$(MAKE) set-manifest-image
PULL_POLICY=IfNotPresent $(MAKE) set-manifest-pull-policy
$(MAKE) release-manifests
$(MAKE) release-binaries

Expand Down
1 change: 1 addition & 0 deletions config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namePrefix: capi-
patchesStrategicMerge:
- manager_image_patch.yaml
- manager_label_patch.yaml
- manager_pull_policy.yaml

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in crd/kustomization.yaml
#- manager_webhook_patch.yaml
Expand Down
11 changes: 11 additions & 0 deletions config/default/manager_pull_policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: manager
imagePullPolicy: Always
2 changes: 1 addition & 1 deletion scripts/ci-integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ build_containers() {
export CONTROLLER_IMG="${CONTROLLER_REPO}"
export EXAMPLE_PROVIDER_IMG="${EXAMPLE_PROVIDER_REPO}"

"${MAKE}" docker-build TAG="${VERSION}" ARCH="${GOARCH}"
"${MAKE}" docker-build TAG="${VERSION}" ARCH="${GOARCH}" PULL_POLICY=IfNotPresent
"${MAKE}" docker-build-example-provider TAG="${VERSION}" ARCH="${GOARCH}"
}

Expand Down
26 changes: 26 additions & 0 deletions test/e2e/capi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// +build e2e

/*
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 e2e_test

import (
"github.com/onsi/ginkgo"
)

var _ = ginkgo.Describe("functional tests", func() {
})
141 changes: 141 additions & 0 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// +build e2e

/*
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 e2e_test

import (
"context"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"testing"
"time"

"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
apimachinerytypes "k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/cluster-api/test/helpers/kind"
"sigs.k8s.io/cluster-api/test/helpers/scheme"
"sigs.k8s.io/cluster-api/util"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
)

func TestE2E(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "e2e Suite")
}

const (
capiNamespace = "capi-system"
capiDeploymentName = "capi-controller-manager"
setupTimeout = 10 * 60
)

var (
managerImage = flag.String("managerImage", "", "Docker image to load into the kind cluster for testing")
capiComponents = flag.String("capiComponents", "", "URL to CAPI components to load")
kustomizeBinary = flag.String("kustomizeBinary", "kustomize", "path to the kustomize binary")

kindCluster kind.Cluster
kindClient crclient.Client
suiteTmpDir string
)

var _ = ginkgo.BeforeSuite(func() {
fmt.Fprintf(ginkgo.GinkgoWriter, "Setting up kind cluster\n")

var err error
suiteTmpDir, err = ioutil.TempDir("", "capi-e2e-suite")
gomega.Expect(err).NotTo(gomega.HaveOccurred())

kindCluster = kind.Cluster{
Name: "capi-test-" + util.RandomString(6),
}
kindCluster.Setup()
loadManagerImage(kindCluster)

// Deploy the CAPA components
deployCAPIComponents(kindCluster)

kindClient, err = crclient.New(kindCluster.RestConfig(), crclient.Options{Scheme: scheme.SetupScheme()})
gomega.Expect(err).NotTo(gomega.HaveOccurred())

// Verify capi components are deployed
waitDeployment(capiNamespace, capiDeploymentName)
}, setupTimeout)

var _ = ginkgo.AfterSuite(func() {
fmt.Fprintf(ginkgo.GinkgoWriter, "Tearing down kind cluster\n")
kindCluster.Teardown()
os.RemoveAll(suiteTmpDir)
})

func waitDeployment(namespace, name string) {
fmt.Fprintf(ginkgo.GinkgoWriter, "Ensuring %s/%s is deployed\n", namespace, name)
gomega.Eventually(
func() (int32, error) {
deployment := &appsv1.Deployment{}
if err := kindClient.Get(context.TODO(), apimachinerytypes.NamespacedName{Namespace: namespace, Name: name}, deployment); err != nil {
return 0, err
}
return deployment.Status.ReadyReplicas, nil
}, 5*time.Minute, 15*time.Second,
).ShouldNot(gomega.BeZero())
}

func loadManagerImage(kindCluster kind.Cluster) {
if managerImage != nil && *managerImage != "" {
kindCluster.LoadImage(*managerImage)
}
}

func applyManifests(kindCluster kind.Cluster, manifests *string) {
gomega.Expect(manifests).ToNot(gomega.BeNil())
fmt.Fprintf(ginkgo.GinkgoWriter, "Applying manifests for %s\n", *manifests)
gomega.Expect(*manifests).ToNot(gomega.BeEmpty())
kindCluster.ApplyYAML(*manifests)
}

func deployCAPIComponents(kindCluster kind.Cluster) {
if capiComponents != nil && *capiComponents != "" {
applyManifests(kindCluster, capiComponents)
return
}

fmt.Fprintf(ginkgo.GinkgoWriter, "Generating CAPI manifests\n")

// Build the manifests using kustomize
capiManifests, err := exec.Command(*kustomizeBinary, "build", "../../config/default").Output()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
fmt.Fprintf(ginkgo.GinkgoWriter, "Error: %s\n", string(exitError.Stderr))
}
}
gomega.Expect(err).NotTo(gomega.HaveOccurred())

// write out the manifests
manifestFile := path.Join(suiteTmpDir, "cluster-api-components.yaml")
gomega.Expect(ioutil.WriteFile(manifestFile, capiManifests, 0644)).NotTo(gomega.HaveOccurred())

// apply generated manifests
applyManifests(kindCluster, &manifestFile)
}
58 changes: 58 additions & 0 deletions test/helpers/components/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
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 common

import (
"context"
"fmt"
"time"

"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
"sigs.k8s.io/cluster-api/test/helpers/flag"
"sigs.k8s.io/cluster-api/test/helpers/kind"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
CAPIVersion = "v0.2.2"
)

var (
capiComponents = flag.DefineOrLookupStringFlag("capiComponents", "https://github.com/kubernetes-sigs/cluster-api/releases/download/"+CAPIVersion+"/cluster-api-components.yaml", "URL to CAPI components to load")
)

func DeployCAPIComponents(kindCluster kind.Cluster) {
gomega.Expect(capiComponents).ToNot(gomega.BeNil())
fmt.Fprintf(ginkgo.GinkgoWriter, "Applying cluster-api components\n")
gomega.Expect(*capiComponents).ToNot(gomega.BeEmpty())
kindCluster.ApplyYAML(*capiComponents)
}

func WaitDeployment(c client.Client, namespace, name string) {
fmt.Fprintf(ginkgo.GinkgoWriter, "Ensuring %s/%s is deployed\n", namespace, name)
gomega.Eventually(
func() (int32, error) {
deployment := &appsv1.Deployment{}
if err := c.Get(context.TODO(), client.ObjectKey{Namespace: namespace, Name: name}, deployment); err != nil {
return 0, err
}
return deployment.Status.ReadyReplicas, nil
}, 5*time.Minute, 15*time.Second,
).ShouldNot(gomega.BeZero())
}
30 changes: 30 additions & 0 deletions test/helpers/flag/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
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 flag

import (
"flag"
)

func DefineOrLookupStringFlag(name string, value string, usage string) *string {
f := flag.Lookup(name)
if f != nil {
v := f.Value.String()
return &v
}
return flag.String(name, value, usage)
}
Loading

0 comments on commit 7f1890e

Please sign in to comment.