-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
🏃 Add common utilities for provider-based testing #1474
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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() { | ||
}) |
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to opt out of the before and after suites? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not that I'm aware of, but I wouldn't expect other providers to inherit this suite directly, but instead we'd continue to move common logic into the helpers, and have each provider implement their own suites. |
||
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) | ||
} |
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()) | ||
} |
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) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mainly added this to facilitate testing of the utilities, but after CAPD and CABPK are in tree I think we'd want to have E2E testing.
Not directly related, but after this is merged I want to refactor the integration tests to use these utilities as well, so I think I'll move them up a directory to avoid confusion for why the integration tests are importing a package from the e2e dir.