|
| 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 kind |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + "time" |
| 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 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 28 | + "sigs.k8s.io/e2e-framework/pkg/features" |
| 29 | +) |
| 30 | + |
| 31 | +func TestKindCluster(t *testing.T) { |
| 32 | + |
| 33 | + deploymentFeature := features.New("appsv1/deployment"). |
| 34 | + Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 35 | + // start a deployment |
| 36 | + deployment := newDeployment(cfg.Namespace(), "test-deployment", 1) |
| 37 | + if err := cfg.Client().Resources().Create(ctx, deployment); err != nil { |
| 38 | + t.Fatal(err) |
| 39 | + } |
| 40 | + time.Sleep(2 * time.Second) |
| 41 | + return ctx |
| 42 | + }). |
| 43 | + Assess("deployment creation", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 44 | + var dep appsv1.Deployment |
| 45 | + if err := cfg.Client().Resources().Get(ctx, "test-deployment", cfg.Namespace(), &dep); err != nil { |
| 46 | + t.Fatal(err) |
| 47 | + } |
| 48 | + if &dep != nil { |
| 49 | + t.Logf("deployment found: %s", dep.Name) |
| 50 | + } |
| 51 | + return context.WithValue(ctx, "test-deployment", &dep) |
| 52 | + }). |
| 53 | + Teardown(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 54 | + dep := ctx.Value("test-deployment").(*appsv1.Deployment) |
| 55 | + if err := cfg.Client().Resources().Delete(ctx, dep); err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + return ctx |
| 59 | + }).Feature() |
| 60 | + |
| 61 | + testenv.Test(t, deploymentFeature) |
| 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": "test-app"}}, |
| 67 | + Spec: appsv1.DeploymentSpec{ |
| 68 | + Replicas: &replicaCount, |
| 69 | + Selector: &metav1.LabelSelector{ |
| 70 | + MatchLabels: map[string]string{"app": "test-app"}, |
| 71 | + }, |
| 72 | + Template: corev1.PodTemplateSpec{ |
| 73 | + ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "test-app"}}, |
| 74 | + Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}}}, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | +} |
0 commit comments