pod_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package pod
  14. import (
  15. "testing"
  16. "fmt"
  17. "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/client-go/kubernetes/fake"
  20. "reflect"
  21. )
  22. func TestPatchPodStatus(t *testing.T) {
  23. ns := "ns"
  24. name := "name"
  25. client := &fake.Clientset{}
  26. client.CoreV1().Pods(ns).Create(&v1.Pod{
  27. ObjectMeta: metav1.ObjectMeta{
  28. Namespace: ns,
  29. Name: name,
  30. },
  31. })
  32. testCases := []struct {
  33. description string
  34. mutate func(input v1.PodStatus) v1.PodStatus
  35. expectedPatchBytes []byte
  36. }{
  37. {
  38. "no change",
  39. func(input v1.PodStatus) v1.PodStatus { return input },
  40. []byte(fmt.Sprintf(`{}`)),
  41. },
  42. {
  43. "message change",
  44. func(input v1.PodStatus) v1.PodStatus {
  45. input.Message = "random message"
  46. return input
  47. },
  48. []byte(fmt.Sprintf(`{"status":{"message":"random message"}}`)),
  49. },
  50. {
  51. "pod condition change",
  52. func(input v1.PodStatus) v1.PodStatus {
  53. input.Conditions[0].Status = v1.ConditionFalse
  54. return input
  55. },
  56. []byte(fmt.Sprintf(`{"status":{"$setElementOrder/conditions":[{"type":"Ready"},{"type":"PodScheduled"}],"conditions":[{"status":"False","type":"Ready"}]}}`)),
  57. },
  58. {
  59. "additional init container condition",
  60. func(input v1.PodStatus) v1.PodStatus {
  61. input.InitContainerStatuses = []v1.ContainerStatus{
  62. {
  63. Name: "init-container",
  64. Ready: true,
  65. },
  66. }
  67. return input
  68. },
  69. []byte(fmt.Sprintf(`{"status":{"initContainerStatuses":[{"image":"","imageID":"","lastState":{},"name":"init-container","ready":true,"restartCount":0,"state":{}}]}}`)),
  70. },
  71. }
  72. for _, tc := range testCases {
  73. _, patchBytes, err := PatchPodStatus(client, ns, name, getPodStatus(), tc.mutate(getPodStatus()))
  74. if err != nil {
  75. t.Errorf("unexpected error: %v", err)
  76. }
  77. if !reflect.DeepEqual(patchBytes, tc.expectedPatchBytes) {
  78. t.Errorf("for test case %q, expect patchBytes: %q, got: %q\n", tc.description, tc.expectedPatchBytes, patchBytes)
  79. }
  80. }
  81. }
  82. func getPodStatus() v1.PodStatus {
  83. return v1.PodStatus{
  84. Phase: v1.PodRunning,
  85. Conditions: []v1.PodCondition{
  86. {
  87. Type: v1.PodReady,
  88. Status: v1.ConditionTrue,
  89. },
  90. {
  91. Type: v1.PodScheduled,
  92. Status: v1.ConditionTrue,
  93. },
  94. },
  95. ContainerStatuses: []v1.ContainerStatus{
  96. {
  97. Name: "container1",
  98. Ready: true,
  99. },
  100. {
  101. Name: "container2",
  102. Ready: true,
  103. },
  104. },
  105. Message: "Message",
  106. }
  107. }