|
| 1 | +/* |
| 2 | +Copyright 2020 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 common |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + |
| 22 | + v1 "k8s.io/api/core/v1" |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "k8s.io/apimachinery/pkg/types" |
| 25 | + "k8s.io/apimachinery/pkg/util/uuid" |
| 26 | + "k8s.io/kubernetes/test/e2e/framework" |
| 27 | + |
| 28 | + "github.com/onsi/ginkgo" |
| 29 | +) |
| 30 | + |
| 31 | +var _ = ginkgo.Describe("[sig-architecture] PodTemplates", func() { |
| 32 | + f := framework.NewDefaultFramework("podtemplate") |
| 33 | + |
| 34 | + ginkgo.It("should run the lifecycle of PodTemplates", func() { |
| 35 | + testNamespaceName := f.Namespace.Name |
| 36 | + podTemplateName := "nginx-pod-template-" + string(uuid.NewUUID()) |
| 37 | + |
| 38 | + // get a list of PodTemplates (in all namespaces to hit endpoint) |
| 39 | + podTemplateList, err := f.ClientSet.CoreV1().PodTemplates("").List(metav1.ListOptions{ |
| 40 | + LabelSelector: "podtemplate-static=true", |
| 41 | + }) |
| 42 | + framework.ExpectNoError(err, "failed to list all PodTemplates") |
| 43 | + framework.ExpectEqual(len(podTemplateList.Items), 0, "unable to find templates") |
| 44 | + |
| 45 | + // create a PodTemplate |
| 46 | + _, err = f.ClientSet.CoreV1().PodTemplates(testNamespaceName).Create(&v1.PodTemplate{ |
| 47 | + ObjectMeta: metav1.ObjectMeta{ |
| 48 | + Name: podTemplateName, |
| 49 | + Labels: map[string]string{ |
| 50 | + "podtemplate-static": "true", |
| 51 | + }, |
| 52 | + }, |
| 53 | + Template: v1.PodTemplateSpec{ |
| 54 | + Spec: v1.PodSpec{ |
| 55 | + Containers: []v1.Container{ |
| 56 | + {Name: "nginx", Image: "nginx"}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }) |
| 61 | + framework.ExpectNoError(err, "failed to create PodTemplate") |
| 62 | + |
| 63 | + // get template |
| 64 | + podTemplateRead, err := f.ClientSet.CoreV1().PodTemplates(testNamespaceName).Get(podTemplateName, metav1.GetOptions{}) |
| 65 | + framework.ExpectNoError(err, "failed to get created PodTemplate") |
| 66 | + framework.ExpectEqual(podTemplateRead.ObjectMeta.Name, podTemplateName) |
| 67 | + |
| 68 | + // patch template |
| 69 | + podTemplatePatch, err := json.Marshal(map[string]interface{}{ |
| 70 | + "metadata": map[string]interface{}{ |
| 71 | + "labels": map[string]string{ |
| 72 | + "podtemplate": "patched", |
| 73 | + }, |
| 74 | + }, |
| 75 | + }) |
| 76 | + framework.ExpectNoError(err, "failed to marshal patch data") |
| 77 | + _, err = f.ClientSet.CoreV1().PodTemplates(testNamespaceName).Patch(podTemplateName, types.StrategicMergePatchType, []byte(podTemplatePatch)) |
| 78 | + framework.ExpectNoError(err, "failed to patch PodTemplate") |
| 79 | + |
| 80 | + // get template (ensure label is there) |
| 81 | + podTemplateRead, err = f.ClientSet.CoreV1().PodTemplates(testNamespaceName).Get(podTemplateName, metav1.GetOptions{}) |
| 82 | + framework.ExpectNoError(err, "failed to get PodTemplate") |
| 83 | + framework.ExpectEqual(podTemplateRead.ObjectMeta.Labels["podtemplate"], "patched", "failed to patch template, new label not found") |
| 84 | + |
| 85 | + // delete the PodTemplate |
| 86 | + err = f.ClientSet.CoreV1().PodTemplates(testNamespaceName).Delete(podTemplateName, &metav1.DeleteOptions{}) |
| 87 | + framework.ExpectNoError(err, "failed to delete PodTemplate") |
| 88 | + |
| 89 | + // list the PodTemplates |
| 90 | + podTemplateList, err = f.ClientSet.CoreV1().PodTemplates("").List(metav1.ListOptions{ |
| 91 | + LabelSelector: "podtemplate-static=true", |
| 92 | + }) |
| 93 | + framework.ExpectNoError(err, "failed to list PodTemplate") |
| 94 | + framework.ExpectEqual(len(podTemplateList.Items), 0, "PodTemplate list returned items, failed to delete PodTemplate") |
| 95 | + }) |
| 96 | +}) |
0 commit comments