|
| 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 | +} |
0 commit comments