Skip to content

Commit

Permalink
GIT-49: enable paralle run of test features
Browse files Browse the repository at this point in the history
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
harshanarayana committed Jan 13, 2022
1 parent 0fa3a81 commit 301dd09
Show file tree
Hide file tree
Showing 10 changed files with 377 additions and 52 deletions.
19 changes: 19 additions & 0 deletions examples/parallel_features/README.md
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
```
124 changes: 124 additions & 0 deletions examples/parallel_features/parallel_features_test.go
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)
}
19 changes: 10 additions & 9 deletions examples/table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import (
)

var test = env.New()
func TestMain(m *testing.M){

func TestMain(m *testing.M) {
// Setup the rand number source and a limit
test.Setup(func(ctx context.Context, config *envconf.Config) (context.Context, error) {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
Expand All @@ -46,8 +47,8 @@ func TestTableDriven(t *testing.T) {
{
Name: "less than equal 64",
Assessment: func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
rnd := ctx.Value("randsrc").(*rand.Rand) // in real test, check asserted type
lim := ctx.Value("limit").(int32) // check type assertion
rnd := ctx.Value("randsrc").(*rand.Rand) // in real test, check asserted type
lim := ctx.Value("limit").(int32) // check type assertion
if rnd.Int31n(lim) > 64 {
t.Log("limit should be less than 64")
}
Expand All @@ -57,8 +58,8 @@ func TestTableDriven(t *testing.T) {
{
Name: "more than than equal 128",
Assessment: func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
rnd := ctx.Value("randsrc").(*rand.Rand) // in real test, check asserted type
lim := ctx.Value("limit").(int32) // check type assertion
rnd := ctx.Value("randsrc").(*rand.Rand) // in real test, check asserted type
lim := ctx.Value("limit").(int32) // check type assertion
if rnd.Int31n(lim) > 128 {
t.Log("limit should be less than 128")
}
Expand All @@ -67,8 +68,8 @@ func TestTableDriven(t *testing.T) {
},
{
Assessment: func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
rnd := ctx.Value("randsrc").(*rand.Rand) // in real test, check asserted type
lim := ctx.Value("limit").(int32) // check type assertion
rnd := ctx.Value("randsrc").(*rand.Rand) // in real test, check asserted type
lim := ctx.Value("limit").(int32) // check type assertion
if rnd.Int31n(lim) > 256 {
t.Log("limit should be less than 256")
}
Expand All @@ -91,5 +92,5 @@ func TestTableDriven(t *testing.T) {
},
}

test.Test(t, table0, table1.Build().Feature() )
}
test.Test(t, table0, table1.Build().Feature())
}
Loading

0 comments on commit 301dd09

Please sign in to comment.