pod_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. "context"
  16. "fmt"
  17. "testing"
  18. "reflect"
  19. v1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/types"
  22. "k8s.io/client-go/kubernetes/fake"
  23. )
  24. func TestPatchPodStatus(t *testing.T) {
  25. ns := "ns"
  26. name := "name"
  27. uid := types.UID("myuid")
  28. client := &fake.Clientset{}
  29. client.CoreV1().Pods(ns).Create(context.TODO(), &v1.Pod{
  30. ObjectMeta: metav1.ObjectMeta{
  31. Namespace: ns,
  32. Name: name,
  33. },
  34. }, metav1.CreateOptions{})
  35. testCases := []struct {
  36. description string
  37. mutate func(input v1.PodStatus) v1.PodStatus
  38. expectedPatchBytes []byte
  39. }{
  40. {
  41. "no change",
  42. func(input v1.PodStatus) v1.PodStatus { return input },
  43. []byte(fmt.Sprintf(`{"metadata":{"uid":"myuid"}}`)),
  44. },
  45. {
  46. "message change",
  47. func(input v1.PodStatus) v1.PodStatus {
  48. input.Message = "random message"
  49. return input
  50. },
  51. []byte(fmt.Sprintf(`{"metadata":{"uid":"myuid"},"status":{"message":"random message"}}`)),
  52. },
  53. {
  54. "pod condition change",
  55. func(input v1.PodStatus) v1.PodStatus {
  56. input.Conditions[0].Status = v1.ConditionFalse
  57. return input
  58. },
  59. []byte(fmt.Sprintf(`{"metadata":{"uid":"myuid"},"status":{"$setElementOrder/conditions":[{"type":"Ready"},{"type":"PodScheduled"}],"conditions":[{"status":"False","type":"Ready"}]}}`)),
  60. },
  61. {
  62. "additional init container condition",
  63. func(input v1.PodStatus) v1.PodStatus {
  64. input.InitContainerStatuses = []v1.ContainerStatus{
  65. {
  66. Name: "init-container",
  67. Ready: true,
  68. },
  69. }
  70. return input
  71. },
  72. []byte(fmt.Sprintf(`{"metadata":{"uid":"myuid"},"status":{"initContainerStatuses":[{"image":"","imageID":"","lastState":{},"name":"init-container","ready":true,"restartCount":0,"state":{}}]}}`)),
  73. },
  74. }
  75. for _, tc := range testCases {
  76. _, patchBytes, err := PatchPodStatus(client, ns, name, uid, getPodStatus(), tc.mutate(getPodStatus()))
  77. if err != nil {
  78. t.Errorf("unexpected error: %v", err)
  79. }
  80. if !reflect.DeepEqual(patchBytes, tc.expectedPatchBytes) {
  81. t.Errorf("for test case %q, expect patchBytes: %q, got: %q\n", tc.description, tc.expectedPatchBytes, patchBytes)
  82. }
  83. }
  84. }
  85. func getPodStatus() v1.PodStatus {
  86. return v1.PodStatus{
  87. Phase: v1.PodRunning,
  88. Conditions: []v1.PodCondition{
  89. {
  90. Type: v1.PodReady,
  91. Status: v1.ConditionTrue,
  92. },
  93. {
  94. Type: v1.PodScheduled,
  95. Status: v1.ConditionTrue,
  96. },
  97. },
  98. ContainerStatuses: []v1.ContainerStatus{
  99. {
  100. Name: "container1",
  101. Ready: true,
  102. },
  103. {
  104. Name: "container2",
  105. Ready: true,
  106. },
  107. },
  108. Message: "Message",
  109. }
  110. }