generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GIT-138: add example of multi cluster test workflow
- Loading branch information
1 parent
2aa1046
commit e1b7605
Showing
11 changed files
with
308 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Multi Cluster Test Runs | ||
|
||
This directory contains the example of how to run tests on multiple `kind` clusters at the same time using the same `env.Environment` | ||
|
||
## What does this test do ? | ||
|
||
1. Create two clusters | ||
1. One with prefix cluster-one | ||
2. One with prefix cluster-two | ||
2. Install A sample helm chart on both clusters | ||
3. Run an assessment to check if the chart has successfully been deployed by checking the pod status | ||
4. Teardown the Test Environments | ||
|
||
# Run Tests | ||
|
||
These test cases can be executed using the normal `go test` command by passing the right arguments | ||
|
||
```bash | ||
go test -v . | ||
``` | ||
|
||
```bash | ||
go test -c -o multi-cluster.test . | ||
|
||
./multi-cluster.test --v 4 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
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 multi_cluster | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/e2e-framework/klient" | ||
"sigs.k8s.io/e2e-framework/klient/k8s" | ||
"sigs.k8s.io/e2e-framework/klient/wait" | ||
"sigs.k8s.io/e2e-framework/klient/wait/conditions" | ||
"sigs.k8s.io/e2e-framework/pkg/envconf" | ||
"sigs.k8s.io/e2e-framework/pkg/envfuncs" | ||
"sigs.k8s.io/e2e-framework/pkg/features" | ||
"sigs.k8s.io/e2e-framework/third_party/helm" | ||
) | ||
|
||
var curDir, _ = os.Getwd() | ||
|
||
func checkPodStatus(t *testing.T, kubeConfig string, clusterName string) { | ||
t.Helper() | ||
client, err := klient.NewWithKubeConfigFile(kubeConfig) | ||
if err != nil { | ||
t.Errorf("ran into an error trying to create a client for Cluster %s", clusterName) | ||
} | ||
deployment := &appsv1.Deployment{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "example", | ||
Namespace: "default", | ||
}, | ||
Spec: appsv1.DeploymentSpec{}, | ||
} | ||
err = wait.For(conditions.New(client.Resources()).ResourceScaled(deployment, func(object k8s.Object) int32 { | ||
return object.(*appsv1.Deployment).Status.ReadyReplicas | ||
}, 1)) | ||
if err != nil { | ||
t.Fatal("failed waiting for the Deployment to reach a ready state") | ||
} | ||
} | ||
|
||
func TestScenarioOne(t *testing.T) { | ||
feature := features.New("Scenario One"). | ||
Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { | ||
for _, clusterName := range clusterNames { | ||
cluster, ok := envfuncs.GetKindClusterFromContext(ctx, clusterName) | ||
if !ok { | ||
t.Fatalf("Failed to extract kind cluster %s from context", clusterName) | ||
} | ||
manager := helm.New(cluster.GetKubeconfig()) | ||
err := manager.RunInstall(helm.WithName("example"), helm.WithNamespace("default"), helm.WithChart(filepath.Join(curDir, "testdata", "example_chart")), helm.WithWait(), helm.WithTimeout("10m")) | ||
if err != nil { | ||
t.Fatal("failed to invoke helm install operation due to an error", err) | ||
} | ||
} | ||
return ctx | ||
}). | ||
Assess(fmt.Sprintf("Deployment is running successfully - %s", clusterNames[0]), func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { | ||
cluster, ok := envfuncs.GetKindClusterFromContext(ctx, clusterNames[0]) | ||
if !ok { | ||
t.Fatalf("Failed to extract kind cluster %s from context", clusterNames[0]) | ||
} | ||
checkPodStatus(t, cluster.GetKubeconfig(), clusterNames[0]) | ||
return ctx | ||
}). | ||
Assess(fmt.Sprintf("Deployment is running successfully - %s", clusterNames[1]), func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { | ||
cluster, ok := envfuncs.GetKindClusterFromContext(ctx, clusterNames[1]) | ||
if !ok { | ||
t.Fatalf("Failed to extract kind cluster %s from context", clusterNames[1]) | ||
} | ||
checkPodStatus(t, cluster.GetKubeconfig(), clusterNames[1]) | ||
return ctx | ||
}). | ||
Feature() | ||
|
||
testEnv.Test(t, feature) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
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 multi_cluster | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"sigs.k8s.io/e2e-framework/pkg/env" | ||
"sigs.k8s.io/e2e-framework/pkg/envconf" | ||
"sigs.k8s.io/e2e-framework/pkg/envfuncs" | ||
) | ||
|
||
var ( | ||
testEnv env.Environment | ||
clusterNames []string | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
cfg, _ := envconf.NewFromFlags() | ||
testEnv = env.NewWithConfig(cfg) | ||
|
||
clusterNames = []string{ | ||
envconf.RandomName("cluster-one", 16), | ||
envconf.RandomName("cluster-two", 16), | ||
} | ||
|
||
testEnv.Setup( | ||
func(ctx context.Context, config *envconf.Config) (context.Context, error) { | ||
var err error | ||
for _, cluster := range clusterNames { | ||
ctx, err = envfuncs.CreateKindCluster(cluster)(ctx, config) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
} | ||
return ctx, nil | ||
}, | ||
).Finish( | ||
func(ctx context.Context, config *envconf.Config) (context.Context, error) { | ||
var err error | ||
for _, cluster := range clusterNames { | ||
ctx, err = envfuncs.DestroyKindCluster(cluster)(ctx, config) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
} | ||
return ctx, nil | ||
}, | ||
) | ||
|
||
os.Exit(testEnv.Run(m)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Patterns to ignore when building packages. | ||
# This supports shell glob matching, relative path matching, and | ||
# negation (prefixed with !). Only one pattern per line. | ||
.DS_Store | ||
# Common VCS dirs | ||
.git/ | ||
.gitignore | ||
.bzr/ | ||
.bzrignore | ||
.hg/ | ||
.hgignore | ||
.svn/ | ||
# Common backup files | ||
*.swp | ||
*.bak | ||
*.tmp | ||
*.orig | ||
*~ | ||
# Various IDEs | ||
.project | ||
.idea/ | ||
*.tmproj | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apiVersion: v2 | ||
name: example | ||
description: An example Helm chart for Kubernetes e2e-framework | ||
type: application | ||
version: 0.1.0 | ||
appVersion: "1.16.0" |
35 changes: 35 additions & 0 deletions
35
examples/multi_cluster/testdata/example_chart/templates/deployment.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: {{ .Release.Name }} | ||
namespace: {{ .Release.Namespace }} | ||
labels: | ||
app: {{ .Release.Name }} | ||
spec: | ||
replicas: {{ .Values.replicaCount }} | ||
selector: | ||
matchLabels: | ||
app: {{ .Release.Name }} | ||
template: | ||
metadata: | ||
labels: | ||
app: {{ .Release.Name }} | ||
spec: | ||
automountServiceAccountToken: true | ||
containers: | ||
- name: {{ .Chart.Name }} | ||
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" | ||
imagePullPolicy: {{ .Values.image.pullPolicy }} | ||
ports: | ||
- name: http | ||
containerPort: {{ .Values.service.port }} | ||
protocol: TCP | ||
livenessProbe: | ||
httpGet: | ||
path: / | ||
port: http | ||
readinessProbe: | ||
httpGet: | ||
path: / | ||
port: http | ||
|
16 changes: 16 additions & 0 deletions
16
examples/multi_cluster/testdata/example_chart/templates/service.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: {{ .Release.Name }} | ||
namespace: {{ .Release.Namespace }} | ||
labels: | ||
app: {{ .Release.Name }} | ||
spec: | ||
type: {{ .Values.service.type }} | ||
ports: | ||
- port: {{ .Values.service.port }} | ||
targetPort: http | ||
protocol: TCP | ||
name: http | ||
selector: | ||
app: {{ .Release.Name }} |
14 changes: 14 additions & 0 deletions
14
examples/multi_cluster/testdata/example_chart/templates/tests/test-connection.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: "{{ .Release.Name }}-test-connection" | ||
namespace: {{ .Release.Namespace }} | ||
annotations: | ||
"helm.sh/hook": test | ||
spec: | ||
containers: | ||
- name: wget | ||
image: busybox | ||
command: ['wget'] | ||
args: ['{{ .Release.Name }}:{{ .Values.service.port }}'] | ||
restartPolicy: Never |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
replicaCount: 1 | ||
|
||
|
||
image: | ||
repository: nginx | ||
pullPolicy: IfNotPresent | ||
tag: "" | ||
|
||
service: | ||
type: ClusterIP | ||
port: 80 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters