generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GIT-49: enable paralle run of test features
GIT-49: enable basic unit test for Parallel feature run GIT-49: enable multi assessment test for parallel GIT-49: update test.Environment interface to include parallel tests GIT-49: add parallel feature run examples GIT-49: ability to run test in both parallel and sequence at the same time GIT-49: fix readme typo GIT-49: refactor porocessTestFeature function to be routine agnostic GIT-49: add additional UT for flag and config parse
- Loading branch information
1 parent
0fa3a81
commit 301dd09
Showing
10 changed files
with
377 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Parallel Run of Test Features | ||
|
||
This directory contains the example of how to run the features in parallel as part of your test run using | ||
the right flags to the test runtime | ||
|
||
# Running tests with flags | ||
|
||
These tests can be executed using the normal `go test` command by passing the right arguments | ||
|
||
```bash | ||
go test -v . -args --parallel | ||
``` | ||
|
||
One can also build a test binary and run that with the right arguments. | ||
|
||
```bash | ||
go test -c -o parallel.test . | ||
./parallel.test --parallel | ||
``` |
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,124 @@ | ||
/* | ||
Copyright 2021 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 parallel_features | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
log "k8s.io/klog/v2" | ||
|
||
"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/env" | ||
"sigs.k8s.io/e2e-framework/pkg/envconf" | ||
"sigs.k8s.io/e2e-framework/pkg/envfuncs" | ||
"sigs.k8s.io/e2e-framework/pkg/features" | ||
) | ||
|
||
var ( | ||
clusterName string | ||
namespace string | ||
testEnv env.Environment | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
cfg, _ := envconf.NewFromFlags() | ||
log.InfoS("Args", "flag", cfg) | ||
testEnv = env.NewWithConfig(cfg) | ||
|
||
clusterName = envconf.RandomName("kind-parallel", 16) | ||
namespace = envconf.RandomName("kind-ns", 16) | ||
|
||
testEnv.Setup( | ||
envfuncs.CreateKindCluster(clusterName), | ||
envfuncs.CreateNamespace(namespace), | ||
) | ||
|
||
testEnv.Finish( | ||
envfuncs.DeleteNamespace(namespace), | ||
envfuncs.DestroyKindCluster(clusterName), | ||
) | ||
os.Exit(testEnv.Run(m)) | ||
} | ||
|
||
func newDeployment(namespace string, name string, replicaCount int32) *appsv1.Deployment { | ||
return &appsv1.Deployment{ | ||
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace, Labels: map[string]string{"app": name}}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Replicas: &replicaCount, | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{"app": name}, | ||
}, | ||
Template: corev1.PodTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": name}}, | ||
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: name, Image: "nginx"}}}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func TestPodBringUp(t *testing.T) { | ||
featureOne := features.New("Feature One"). | ||
Assess("Create Nginx Deployment 1", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { | ||
deployment := newDeployment(namespace, "deployment-1", 2) | ||
err := config.Client().Resources().Create(ctx, deployment) | ||
if err != nil { | ||
t.Error("failed to create test pod for deployment-1") | ||
} | ||
ctx = context.WithValue(ctx, "DEPLOYMENT", deployment) | ||
return ctx | ||
}). | ||
Assess("Wait for Nginx Deployment 1 to be scaled up", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { | ||
deployment := ctx.Value("DEPLOYMENT").(*appsv1.Deployment) | ||
err := wait.For(conditions.New(config.Client().Resources()).ResourceScaled(deployment, func(object k8s.Object) int32 { | ||
return object.(*appsv1.Deployment).Status.ReadyReplicas | ||
}, 2)) | ||
if err != nil { | ||
t.Error("failed waiting for deployment to be scaled up") | ||
} | ||
return ctx | ||
}).Feature() | ||
|
||
featureTwo := features.New("Feature Two"). | ||
Assess("Create Nginx Deployment 2", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { | ||
deployment := newDeployment(namespace, "deployment-2", 2) | ||
err := config.Client().Resources().Create(ctx, deployment) | ||
if err != nil { | ||
t.Error("failed to create test pod for deployment-2") | ||
} | ||
ctx = context.WithValue(ctx, "DEPLOYMENT", deployment) | ||
return ctx | ||
}). | ||
Assess("Wait for Nginx Deployment 2 to be scaled up", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { | ||
deployment := ctx.Value("DEPLOYMENT").(*appsv1.Deployment) | ||
err := wait.For(conditions.New(config.Client().Resources()).ResourceScaled(deployment, func(object k8s.Object) int32 { | ||
return object.(*appsv1.Deployment).Status.ReadyReplicas | ||
}, 2)) | ||
if err != nil { | ||
t.Error("failed waiting for deployment to be scaled up") | ||
} | ||
return ctx | ||
}).Feature() | ||
|
||
testEnv.TestInParallel(t, featureOne, featureTwo) | ||
} |
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
Oops, something went wrong.