Fix test to use values from v1, wording; Update variables to be more templatable

This commit is contained in:
Caleb Woodbine 2020-04-08 15:42:50 +12:00
parent 47cd8dde56
commit c6a86b5fed

View File

@ -878,16 +878,17 @@ var _ = framework.KubeDescribe("Pods", func() {
ginkgo.It("should run through the lifecycle of Pods and PodStatus", func() { ginkgo.It("should run through the lifecycle of Pods and PodStatus", func() {
podResource := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"} podResource := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}
testNs := "default" testNs := f.Namespace.Name
testPodName := "pod-test" testPodName := "pod-test"
testPodImage := "nginx" testPodImage := "nginx"
testPodImage2 := "nginx" testPodImage2 := "httpd"
testPodLabels := map[string]string{"test-pod-static": "true"}
testPodLabelsFlat := "test-pod-static=true"
testPod := v1.Pod{ testPod := v1.Pod{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: testPodName, Name: testPodName,
Labels: map[string]string{ Labels: testPodLabels,
"test-pod-static": "true",
},
}, },
Spec: v1.PodSpec{ Spec: v1.PodSpec{
Containers: []v1.Container{ Containers: []v1.Container{
@ -904,40 +905,36 @@ var _ = framework.KubeDescribe("Pods", func() {
ginkgo.By("setting up a watch for the Pod") ginkgo.By("setting up a watch for the Pod")
podWatchTimeoutSeconds := int64(180) podWatchTimeoutSeconds := int64(180)
podWatch, err := f.ClientSet.CoreV1().Pods(testNs).Watch(context.TODO(), metav1.ListOptions{LabelSelector: "test-pod-static=true", TimeoutSeconds: &podWatchTimeoutSeconds}) podWatch, err := f.ClientSet.CoreV1().Pods(testNs).Watch(context.TODO(), metav1.ListOptions{LabelSelector: testPodLabelsFlat, TimeoutSeconds: &podWatchTimeoutSeconds})
framework.ExpectNoError(err, "failed to set up watch") framework.ExpectNoError(err, "failed to set up watch")
podWatchChan := podWatch.ResultChan() podWatchChan := podWatch.ResultChan()
ginkgo.By("watching for Pod to be ready") ginkgo.By("watching for Pod to be ready")
for watchEvent := range podWatchChan { for watchEvent := range podWatchChan {
podWatchEvent, ok := watchEvent.Object.(*v1.Pod) podWatchEvent, ok := watchEvent.Object.(*v1.Pod)
framework.ExpectEqual(ok, true, "unable to fix type") framework.ExpectEqual(ok, true, "unable to assert runtime object type to v1.Pod")
if podWatchEvent.Status.Phase == "Running" { if podWatchEvent.Status.Phase == v1.PodRunning {
break break
} }
} }
ginkgo.By("patching the Pod with a new Label and updated data") ginkgo.By("patching the Pod with a new Label and updated data")
podPatch, err := json.Marshal(map[string]interface{}{ podPatch, err := json.Marshal(v1.Pod{
"metadata": map[string]interface{}{ ObjectMeta: metav1.ObjectMeta{
"labels": map[string]string{ Labels: map[string]string{"podtemplate": "patched"},
"podtemplate": "patched",
},
},
"spec": map[string]interface{}{
"containers": []map[string]interface{}{
{
"name": testPodName,
"image": testPodImage2,
},
}, },
Spec: v1.PodSpec{
Containers: []v1.Container{{
Name: testPodName,
Image: testPodImage2,
}},
}, },
}) })
framework.ExpectNoError(err, "failed to marshal JSON patch for Pod") framework.ExpectNoError(err, "failed to marshal JSON patch for Pod")
_, err = f.ClientSet.CoreV1().Pods(testNs).Patch(context.TODO(), testPodName, types.StrategicMergePatchType, []byte(podPatch), metav1.PatchOptions{}) _, err = f.ClientSet.CoreV1().Pods(testNs).Patch(context.TODO(), testPodName, types.StrategicMergePatchType, []byte(podPatch), metav1.PatchOptions{})
framework.ExpectNoError(err, "failed to patch Pod") framework.ExpectNoError(err, "failed to patch Pod")
ginkgo.By("getting the Pod and ensure it's patched") ginkgo.By("getting the Pod and ensuring that it's patched")
pod, err := f.ClientSet.CoreV1().Pods(testNs).Get(context.TODO(), testPodName, metav1.GetOptions{}) pod, err := f.ClientSet.CoreV1().Pods(testNs).Get(context.TODO(), testPodName, metav1.GetOptions{})
framework.ExpectNoError(err, "failed to fetch Pod") framework.ExpectNoError(err, "failed to fetch Pod")
framework.ExpectEqual(pod.ObjectMeta.Labels["test-pod-static"], "true", "failed to patch Pod - missing label") framework.ExpectEqual(pod.ObjectMeta.Labels["test-pod-static"], "true", "failed to patch Pod - missing label")
@ -946,18 +943,18 @@ var _ = framework.KubeDescribe("Pods", func() {
ginkgo.By("getting the PodStatus") ginkgo.By("getting the PodStatus")
podStatusUnstructured, err := dc.Resource(podResource).Namespace(testNs).Get(context.TODO(), testPodName, metav1.GetOptions{}, "status") podStatusUnstructured, err := dc.Resource(podResource).Namespace(testNs).Get(context.TODO(), testPodName, metav1.GetOptions{}, "status")
framework.ExpectNoError(err, "failed to fetch PodStatus") framework.ExpectNoError(err, "failed to fetch PodStatus")
podStatusUjson, err := json.Marshal(podStatusUnstructured) podStatusBytes, err := json.Marshal(podStatusUnstructured)
framework.ExpectNoError(err, "failed to marshal unstructured response") framework.ExpectNoError(err, "failed to marshal unstructured response")
var podStatus v1.Pod var podStatus v1.Pod
json.Unmarshal(podStatusUjson, &podStatus) json.Unmarshal(podStatusBytes, &podStatus)
ginkgo.By("replacing the Pod's status Ready condition to False") ginkgo.By("replacing the Pod's status Ready condition to False")
podStatusUpdated := podStatus podStatusUpdated := podStatus
podStatusFieldPatchCount := 0 podStatusFieldPatchCount := 0
podStatusFieldPatchCountTotal := 2 podStatusFieldPatchCountTotal := 2
for pos, cond := range podStatusUpdated.Status.Conditions { for pos, cond := range podStatusUpdated.Status.Conditions {
if (cond.Type == "Ready" && cond.Status == "True") || (cond.Type == "ContainersReady" && cond.Status == "True") { if (cond.Type == v1.PodReady && cond.Status == v1.ConditionTrue) || (cond.Type == v1.ContainersReady && cond.Status == v1.ConditionTrue) {
podStatusUpdated.Status.Conditions[pos].Status = "False" podStatusUpdated.Status.Conditions[pos].Status = v1.ConditionFalse
podStatusFieldPatchCount++ podStatusFieldPatchCount++
} }
} }
@ -965,23 +962,23 @@ var _ = framework.KubeDescribe("Pods", func() {
podStatusUpdate, err := f.ClientSet.CoreV1().Pods(testNs).UpdateStatus(context.TODO(), &podStatusUpdated, metav1.UpdateOptions{}) podStatusUpdate, err := f.ClientSet.CoreV1().Pods(testNs).UpdateStatus(context.TODO(), &podStatusUpdated, metav1.UpdateOptions{})
framework.ExpectNoError(err, "failed to update PodStatus") framework.ExpectNoError(err, "failed to update PodStatus")
ginkgo.By("check the PodStatus updates's return status to ensure it's Ready condition is False") ginkgo.By("check the Pod again to ensure its Ready conditions are False")
podStatusFieldPatchCount = 0 podStatusFieldPatchCount = 0
podStatusFieldPatchCountTotal = 2 podStatusFieldPatchCountTotal = 2
for _, cond := range podStatusUpdate.Status.Conditions { for _, cond := range podStatusUpdate.Status.Conditions {
if (cond.Type == "Ready" && cond.Status == "False") || (cond.Type == "ContainersReady" && cond.Status == "False") { if (cond.Type == v1.PodReady && cond.Status == v1.ConditionFalse) || (cond.Type == v1.ContainersReady && cond.Status == v1.ConditionFalse) {
podStatusFieldPatchCount++ podStatusFieldPatchCount++
} }
} }
framework.ExpectEqual(podStatusFieldPatchCount, podStatusFieldPatchCountTotal, "failed to update PodStatus - field patch count doesn't match the total") framework.ExpectEqual(podStatusFieldPatchCount, podStatusFieldPatchCountTotal, "failed to update PodStatus - field patch count doesn't match the total")
ginkgo.By("deleting the Pod via a Collection with a LabelSelector") ginkgo.By("deleting the Pod via a Collection with a LabelSelector")
err = f.ClientSet.CoreV1().Pods(testNs).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: "test-pod-static=true"}) err = f.ClientSet.CoreV1().Pods(testNs).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: testPodLabelsFlat})
framework.ExpectNoError(err, "failed to delete Pod by collection") framework.ExpectNoError(err, "failed to delete Pod by collection")
ginkgo.By("watching for Pod to be deleted") ginkgo.By("watching for the Pod to be deleted")
for watchEvent := range podWatchChan { for watchEvent := range podWatchChan {
if watchEvent.Type == "DELETED" { if watchEvent.Type == watch.Deleted {
break break
} }
} }