Skip to content

Commit 301dd09

Browse files
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
1 parent 0fa3a81 commit 301dd09

File tree

10 files changed

+377
-52
lines changed

10 files changed

+377
-52
lines changed

examples/parallel_features/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Parallel Run of Test Features
2+
3+
This directory contains the example of how to run the features in parallel as part of your test run using
4+
the right flags to the test runtime
5+
6+
# Running tests with flags
7+
8+
These tests can be executed using the normal `go test` command by passing the right arguments
9+
10+
```bash
11+
go test -v . -args --parallel
12+
```
13+
14+
One can also build a test binary and run that with the right arguments.
15+
16+
```bash
17+
go test -c -o parallel.test .
18+
./parallel.test --parallel
19+
```
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package parallel_features
18+
19+
import (
20+
"context"
21+
"os"
22+
"testing"
23+
24+
appsv1 "k8s.io/api/apps/v1"
25+
corev1 "k8s.io/api/core/v1"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
log "k8s.io/klog/v2"
28+
29+
"sigs.k8s.io/e2e-framework/klient/k8s"
30+
"sigs.k8s.io/e2e-framework/klient/wait"
31+
"sigs.k8s.io/e2e-framework/klient/wait/conditions"
32+
"sigs.k8s.io/e2e-framework/pkg/env"
33+
"sigs.k8s.io/e2e-framework/pkg/envconf"
34+
"sigs.k8s.io/e2e-framework/pkg/envfuncs"
35+
"sigs.k8s.io/e2e-framework/pkg/features"
36+
)
37+
38+
var (
39+
clusterName string
40+
namespace string
41+
testEnv env.Environment
42+
)
43+
44+
func TestMain(m *testing.M) {
45+
cfg, _ := envconf.NewFromFlags()
46+
log.InfoS("Args", "flag", cfg)
47+
testEnv = env.NewWithConfig(cfg)
48+
49+
clusterName = envconf.RandomName("kind-parallel", 16)
50+
namespace = envconf.RandomName("kind-ns", 16)
51+
52+
testEnv.Setup(
53+
envfuncs.CreateKindCluster(clusterName),
54+
envfuncs.CreateNamespace(namespace),
55+
)
56+
57+
testEnv.Finish(
58+
envfuncs.DeleteNamespace(namespace),
59+
envfuncs.DestroyKindCluster(clusterName),
60+
)
61+
os.Exit(testEnv.Run(m))
62+
}
63+
64+
func newDeployment(namespace string, name string, replicaCount int32) *appsv1.Deployment {
65+
return &appsv1.Deployment{
66+
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace, Labels: map[string]string{"app": name}},
67+
Spec: appsv1.DeploymentSpec{
68+
Replicas: &replicaCount,
69+
Selector: &metav1.LabelSelector{
70+
MatchLabels: map[string]string{"app": name},
71+
},
72+
Template: corev1.PodTemplateSpec{
73+
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": name}},
74+
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: name, Image: "nginx"}}},
75+
},
76+
},
77+
}
78+
}
79+
80+
func TestPodBringUp(t *testing.T) {
81+
featureOne := features.New("Feature One").
82+
Assess("Create Nginx Deployment 1", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
83+
deployment := newDeployment(namespace, "deployment-1", 2)
84+
err := config.Client().Resources().Create(ctx, deployment)
85+
if err != nil {
86+
t.Error("failed to create test pod for deployment-1")
87+
}
88+
ctx = context.WithValue(ctx, "DEPLOYMENT", deployment)
89+
return ctx
90+
}).
91+
Assess("Wait for Nginx Deployment 1 to be scaled up", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
92+
deployment := ctx.Value("DEPLOYMENT").(*appsv1.Deployment)
93+
err := wait.For(conditions.New(config.Client().Resources()).ResourceScaled(deployment, func(object k8s.Object) int32 {
94+
return object.(*appsv1.Deployment).Status.ReadyReplicas
95+
}, 2))
96+
if err != nil {
97+
t.Error("failed waiting for deployment to be scaled up")
98+
}
99+
return ctx
100+
}).Feature()
101+
102+
featureTwo := features.New("Feature Two").
103+
Assess("Create Nginx Deployment 2", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
104+
deployment := newDeployment(namespace, "deployment-2", 2)
105+
err := config.Client().Resources().Create(ctx, deployment)
106+
if err != nil {
107+
t.Error("failed to create test pod for deployment-2")
108+
}
109+
ctx = context.WithValue(ctx, "DEPLOYMENT", deployment)
110+
return ctx
111+
}).
112+
Assess("Wait for Nginx Deployment 2 to be scaled up", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
113+
deployment := ctx.Value("DEPLOYMENT").(*appsv1.Deployment)
114+
err := wait.For(conditions.New(config.Client().Resources()).ResourceScaled(deployment, func(object k8s.Object) int32 {
115+
return object.(*appsv1.Deployment).Status.ReadyReplicas
116+
}, 2))
117+
if err != nil {
118+
t.Error("failed waiting for deployment to be scaled up")
119+
}
120+
return ctx
121+
}).Feature()
122+
123+
testEnv.TestInParallel(t, featureOne, featureTwo)
124+
}

examples/table/table_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import (
2929
)
3030

3131
var test = env.New()
32-
func TestMain(m *testing.M){
32+
33+
func TestMain(m *testing.M) {
3334
// Setup the rand number source and a limit
3435
test.Setup(func(ctx context.Context, config *envconf.Config) (context.Context, error) {
3536
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
@@ -46,8 +47,8 @@ func TestTableDriven(t *testing.T) {
4647
{
4748
Name: "less than equal 64",
4849
Assessment: func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
49-
rnd := ctx.Value("randsrc").(*rand.Rand) // in real test, check asserted type
50-
lim := ctx.Value("limit").(int32) // check type assertion
50+
rnd := ctx.Value("randsrc").(*rand.Rand) // in real test, check asserted type
51+
lim := ctx.Value("limit").(int32) // check type assertion
5152
if rnd.Int31n(lim) > 64 {
5253
t.Log("limit should be less than 64")
5354
}
@@ -57,8 +58,8 @@ func TestTableDriven(t *testing.T) {
5758
{
5859
Name: "more than than equal 128",
5960
Assessment: func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
60-
rnd := ctx.Value("randsrc").(*rand.Rand) // in real test, check asserted type
61-
lim := ctx.Value("limit").(int32) // check type assertion
61+
rnd := ctx.Value("randsrc").(*rand.Rand) // in real test, check asserted type
62+
lim := ctx.Value("limit").(int32) // check type assertion
6263
if rnd.Int31n(lim) > 128 {
6364
t.Log("limit should be less than 128")
6465
}
@@ -67,8 +68,8 @@ func TestTableDriven(t *testing.T) {
6768
},
6869
{
6970
Assessment: func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
70-
rnd := ctx.Value("randsrc").(*rand.Rand) // in real test, check asserted type
71-
lim := ctx.Value("limit").(int32) // check type assertion
71+
rnd := ctx.Value("randsrc").(*rand.Rand) // in real test, check asserted type
72+
lim := ctx.Value("limit").(int32) // check type assertion
7273
if rnd.Int31n(lim) > 256 {
7374
t.Log("limit should be less than 256")
7475
}
@@ -91,5 +92,5 @@ func TestTableDriven(t *testing.T) {
9192
},
9293
}
9394

94-
test.Test(t, table0, table1.Build().Feature() )
95-
}
95+
test.Test(t, table0, table1.Build().Feature())
96+
}

0 commit comments

Comments
 (0)