Skip to content

Commit 5a00878

Browse files
committed
Add: PodTemplate lifecycle test
1 parent bb89c2b commit 5a00878

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

test/e2e/common/podtemplates.go

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

0 commit comments

Comments
 (0)