Skip to content

Commit

Permalink
Merge pull request #165 from jweite-amazon/e2e-toxiproxy
Browse files Browse the repository at this point in the history
E2e toxiproxy
  • Loading branch information
jweite-amazon authored Oct 10, 2022
2 parents d6f485f + d66275e commit 691a791
Show file tree
Hide file tree
Showing 26 changed files with 910 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ test/e2e/data/infrastructure-cloudstack/v1beta*/*yaml

# ACS Credentials file.
cloud-config
cloud-config.yaml

# Editor and IDE paraphernalia
.idea
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ tilt-up: cluster-api kind-cluster cluster-api/tilt-settings.json manifests ## Se

.PHONY: kind-cluster
kind-cluster: cluster-api ## Create a kind cluster with a local Docker repository.
-./cluster-api/hack/kind-install-for-capd.sh
./cluster-api/hack/kind-install-for-capd.sh

cluster-api: ## Clone cluster-api repository for tilt use.
git clone --branch v1.0.0 --depth 1 https://github.com/kubernetes-sigs/cluster-api.git
Expand Down Expand Up @@ -271,7 +271,7 @@ JOB ?= .*
run-e2e: e2e-essentials ## Run e2e testing. JOB is an optional REGEXP to select certainn test cases to run. e.g. JOB=PR-Blocking, JOB=Conformance
$(KUBECTL) apply -f cloud-config.yaml && \
cd test/e2e && \
$(REPO_ROOT)/$(GINKGO_V1) -v -trace -tags=e2e -focus=$(JOB) -skip=Conformance -nodes=1 -noColor=false ./... -- \
$(REPO_ROOT)/$(GINKGO_V1) -v -trace -tags=e2e -focus=$(JOB) -skip=Conformance -skipPackage=helpers -nodes=1 -noColor=false ./... -- \
-e2e.artifacts-folder=${REPO_ROOT}/_artifacts \
-e2e.config=${REPO_ROOT}/test/e2e/config/cloudstack.yaml \
-e2e.skip-resource-cleanup=false -e2e.use-existing-cluster=true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
controller-gen.kubebuilder.io/version: v0.8.0
creationTimestamp: null
name: cloudstackaffinitygroups.infrastructure.cluster.x-k8s.io
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
controller-gen.kubebuilder.io/version: v0.8.0
creationTimestamp: null
name: cloudstackclusters.infrastructure.cluster.x-k8s.io
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
controller-gen.kubebuilder.io/version: v0.8.0
creationTimestamp: null
name: cloudstackfailuredomains.infrastructure.cluster.x-k8s.io
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
controller-gen.kubebuilder.io/version: v0.8.0
creationTimestamp: null
name: cloudstackisolatednetworks.infrastructure.cluster.x-k8s.io
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
controller-gen.kubebuilder.io/version: v0.8.0
creationTimestamp: null
name: cloudstackmachines.infrastructure.cluster.x-k8s.io
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
controller-gen.kubebuilder.io/version: v0.8.0
creationTimestamp: null
name: cloudstackmachinestatecheckers.infrastructure.cluster.x-k8s.io
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
controller-gen.kubebuilder.io/version: v0.8.0
creationTimestamp: null
name: cloudstackmachinetemplates.infrastructure.cluster.x-k8s.io
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
controller-gen.kubebuilder.io/version: v0.8.0
creationTimestamp: null
name: cloudstackzones.infrastructure.cluster.x-k8s.io
spec:
Expand Down
1 change: 0 additions & 1 deletion config/rbac/role.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
Expand Down
2 changes: 0 additions & 2 deletions config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

---
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
Expand Down Expand Up @@ -46,7 +45,6 @@ webhooks:
resources:
- cloudstackmachines
sideEffects: None

---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
Expand Down
5 changes: 4 additions & 1 deletion controllers/utils/affinity_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package utils

import (
"fmt"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -91,6 +93,7 @@ func GenerateAffinityGroupName(csm infrav1.CloudStackMachine, capiMachine *clust
if managerOwnerRef == nil {
return "", errors.Errorf("could not find owner UID for %s/%s", csm.Namespace, csm.Name)
}
titleCaser := cases.Title(language.English)
return fmt.Sprintf("%sAffinity-%s-%s-%s",
strings.Title(csm.Spec.Affinity), managerOwnerRef.Name, managerOwnerRef.UID, csm.Spec.FailureDomainName), nil
titleCaser.String(csm.Spec.Affinity), managerOwnerRef.Name, managerOwnerRef.UID, csm.Spec.FailureDomainName), nil
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module sigs.k8s.io/cluster-api-provider-cloudstack
go 1.16

require (
github.com/ReneKroon/ttlcache v1.7.0 // indirect
github.com/ReneKroon/ttlcache v1.7.0
github.com/apache/cloudstack-go/v2 v2.13.0
github.com/go-logr/logr v1.2.3
github.com/golang/mock v1.6.0
Expand All @@ -14,6 +14,7 @@ require (
github.com/prometheus/client_golang v1.11.0
github.com/smallfish/simpleyaml v0.1.0
github.com/spf13/pflag v1.0.5
golang.org/x/text v0.3.7
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
k8s.io/api v0.23.0
k8s.io/apimachinery v0.23.0
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,15 @@ For example,
JOB=PR-Blocking make run-e2e
```
This command runs the e2e tests that contains `PR-Blocking` in their spec names.

### Debugging the e2e tests
The E2E tests can be debugged by attaching a debugger to the e2e process after it is launched (*i.e., make run-e2e*).
To facilitate this, the E2E tests can be run with environment variable PAUSE_FOR_DEBUGGER_ATTACH=true.
(This is only strictly needed when you want the debugger to break early in the test process, i.e., in SynchronizedBeforeSuite.
There's usually quite enough time to attach if you're not breaking until your actual test code runs.)

When this environment variable is set to *true* a 15s pause is inserted at the beginning of the test process
(i.e., in the SynchronizedBeforeSuite). The workflow is:
- Launch the e2e test: *PAUSE_FOR_DEBUGGER_ATTACH=true JOB=MyTest make run-e2e*
- Wait for console message: *Pausing 15s so you have a chance to attach a debugger to this process...*
- Quickly attach your debugger to the e2e process (i.e., e2e.test)
162 changes: 162 additions & 0 deletions test/e2e/deploy_app_toxi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
Copyright 2022 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

import (
"context"
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/pointer"
"os"
"path/filepath"
"runtime"
"sigs.k8s.io/cluster-api-provider-cloudstack-staging/test/e2e/helpers"
"sigs.k8s.io/cluster-api/test/framework/clusterctl"
"sigs.k8s.io/cluster-api/util"
)

// DeployAppToxiSpec implements a test that verifies that an app deployed to the workload cluster works.
func DeployAppToxiSpec(ctx context.Context, inputGetter func() CommonSpecInput) {
var (
specName = "deploy-app-toxi"
input CommonSpecInput
namespace *corev1.Namespace
cancelWatches context.CancelFunc
clusterResources *clusterctl.ApplyClusterTemplateAndWaitResult
bootstrapClusterToxicName string
cloudStackToxicName string
bootstrapClusterToxiProxyContext *helpers.ToxiProxyContext
cloudStackToxiProxyContext *helpers.ToxiProxyContext
appName = "httpd"
appManifestPath = "data/fixture/sample-application.yaml"
expectedHtmlPath = "data/fixture/expected-webpage.html"
appDeploymentReadyTimeout = 180
appPort = 8080
appDefaultHtmlPath = "/"
expectedHtml = ""
clusterName = fmt.Sprintf("%s-%s", specName, util.RandomString(6))
)

BeforeEach(func() {
// ToxiProxy running in a docker container requires docker host networking, only available in linux.
Expect(runtime.GOOS).To(Equal("linux"))

Expect(ctx).NotTo(BeNil(), "ctx is required for %s spec", specName)
input = inputGetter()
Expect(input.E2EConfig).ToNot(BeNil(), "Invalid argument. input.E2EConfig can't be nil when calling %s spec", specName)
Expect(input.ClusterctlConfigPath).To(BeAnExistingFile(), "Invalid argument. input.ClusterctlConfigPath must be an existing file when calling %s spec", specName)
Expect(input.BootstrapClusterProxy).ToNot(BeNil(), "Invalid argument. input.BootstrapClusterProxy can't be nil when calling %s spec", specName)
Expect(os.MkdirAll(input.ArtifactFolder, 0750)).To(Succeed(), "Invalid argument. input.ArtifactFolder can't be created for %s spec", specName)
Expect(input.E2EConfig.Variables).To(HaveKey(KubernetesVersion))

// Set up a toxiProxy for bootstrap server.
bootstrapClusterToxiProxyContext = helpers.SetupForToxiProxyTestingBootstrapCluster(input.BootstrapClusterProxy, clusterName)
const ToxicLatencyMs = 100
const ToxicJitterMs = 100
const ToxicToxicity = 1
bootstrapClusterToxicName = bootstrapClusterToxiProxyContext.AddLatencyToxic(ToxicLatencyMs, ToxicJitterMs, ToxicToxicity, false)

// Set up a toxiProxy for CloudStack
cloudStackToxiProxyContext = helpers.SetupForToxiProxyTestingACS(
ctx,
clusterName,
input.BootstrapClusterProxy,
input.E2EConfig,
input.ClusterctlConfigPath,
)
cloudStackToxicName = cloudStackToxiProxyContext.AddLatencyToxic(ToxicLatencyMs, ToxicJitterMs, ToxicToxicity, false)

// Set up a Namespace to host objects for this spec and create a watcher for the namespace events.
namespace, cancelWatches = setupSpecNamespace(ctx, specName, bootstrapClusterToxiProxyContext.ClusterProxy, input.ArtifactFolder)
clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)

fileContent, err := os.ReadFile(expectedHtmlPath)
Expect(err).To(BeNil(), "Failed to read "+expectedHtmlPath)
expectedHtml = string(fileContent)
})

It("Should be able to download an HTML from the app deployed to the workload cluster", func() {
By("Creating a workload cluster")

flavor := clusterctl.DefaultFlavor
if input.Flavor != nil {
flavor = *input.Flavor
}
namespace := namespace.Name

clusterctl.ApplyClusterTemplateAndWait(ctx, clusterctl.ApplyClusterTemplateAndWaitInput{
ClusterProxy: bootstrapClusterToxiProxyContext.ClusterProxy,
CNIManifestPath: input.E2EConfig.GetVariable(CNIPath),
ConfigCluster: clusterctl.ConfigClusterInput{
LogFolder: filepath.Join(input.ArtifactFolder, "clusters", bootstrapClusterToxiProxyContext.ClusterProxy.GetName()),
ClusterctlConfigPath: cloudStackToxiProxyContext.ConfigPath,
KubeconfigPath: bootstrapClusterToxiProxyContext.ClusterProxy.GetKubeconfigPath(),
InfrastructureProvider: clusterctl.DefaultInfrastructureProvider,
Flavor: flavor,
Namespace: namespace,
ClusterName: clusterName,
KubernetesVersion: input.E2EConfig.GetVariable(KubernetesVersion),
ControlPlaneMachineCount: pointer.Int64Ptr(1),
WorkerMachineCount: pointer.Int64Ptr(2),
},
WaitForClusterIntervals: input.E2EConfig.GetIntervals(specName, "wait-cluster"),
WaitForControlPlaneIntervals: input.E2EConfig.GetIntervals(specName, "wait-control-plane"),
WaitForMachineDeployments: input.E2EConfig.GetIntervals(specName, "wait-worker-nodes"),
}, clusterResources)

workloadClusterProxy := bootstrapClusterToxiProxyContext.ClusterProxy.GetWorkloadCluster(ctx, namespace, clusterName)
workloadKubeconfigPath := workloadClusterProxy.GetKubeconfigPath()

appManifestAbsolutePath, _ := filepath.Abs(appManifestPath)
Byf("Deploying a simple web server application to the workload cluster from %s", appManifestAbsolutePath)
Expect(DeployAppToWorkloadClusterAndWaitForDeploymentReady(ctx, workloadKubeconfigPath, appName, appManifestAbsolutePath, appDeploymentReadyTimeout)).To(Succeed())

By("Downloading the default html of the web server")
actualHtml, err := DownloadFromAppInWorkloadCluster(ctx, workloadKubeconfigPath, appName, appPort, appDefaultHtmlPath)
Expect(err).To(BeNil(), "Failed to download")

Expect(actualHtml).To(Equal(expectedHtml))

By("Confirming that the custom reconciliation error metric is scrape-able")
// TODO: Rebuild an E2E test designed purely to test this. Adding this requirement here is too flaky.
// Newer CloudStack instances return a different error code when the ZoneID is missing, and this test
// keeps us from fixing the additional error message that was present when reconciling the Isolated Network
// a bit too soon.
// BIG NOTE: The first reconciliation attempt of isolated_network!AssociatePublicIPAddress() returns
// a CloudStack error 9999. This test expects that to happen.
// No acs_reconciliation_errors appear in the scrape until logged.
// If that error ever gets fixed, this test will break.
// metricsScrape, err := DownloadMetricsFromCAPCManager(ctx, input.BootstrapClusterProxy.GetKubeconfigPath())
// Expect(err).To(BeNil())
// Expect(metricsScrape).To(MatchRegexp("acs_reconciliation_errors\\{acs_error_code=\"9999\"\\} [0-9]+"))
By("PASSED!")
})

AfterEach(func() {
// Dumps all the resources in the spec namespace, then cleanups the cluster object and the spec namespace itself.
dumpSpecResourcesAndCleanup(ctx, specName, bootstrapClusterToxiProxyContext.ClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)

// Tear down the ToxiProxies
cloudStackToxiProxyContext.RemoveToxic(cloudStackToxicName)
helpers.TearDownToxiProxyACS(ctx, input.BootstrapClusterProxy, cloudStackToxiProxyContext)

bootstrapClusterToxiProxyContext.RemoveToxic(bootstrapClusterToxicName)
helpers.TearDownToxiProxyBootstrap(bootstrapClusterToxiProxyContext)
})
}
39 changes: 39 additions & 0 deletions test/e2e/deploy_app_toxi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//go:build e2e
// +build e2e

/*
Copyright 2020 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

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

var _ = Describe("When testing app deployment to the workload cluster with slow network [ToxiProxy]", func() {

DeployAppToxiSpec(context.TODO(), func() CommonSpecInput {
return CommonSpecInput{
E2EConfig: e2eConfig,
ClusterctlConfigPath: clusterctlConfigPath,
BootstrapClusterProxy: bootstrapClusterProxy,
ArtifactFolder: artifactFolder,
SkipCleanup: skipCleanup,
}
})

})
Loading

0 comments on commit 691a791

Please sign in to comment.