strategy_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. Copyright 2014 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. "reflect"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/api/errors"
  19. "k8s.io/kubernetes/pkg/api/testapi"
  20. apitesting "k8s.io/kubernetes/pkg/api/testing"
  21. "k8s.io/kubernetes/pkg/fields"
  22. "k8s.io/kubernetes/pkg/labels"
  23. "k8s.io/kubernetes/pkg/runtime"
  24. )
  25. func TestMatchPod(t *testing.T) {
  26. testCases := []struct {
  27. in *api.Pod
  28. fieldSelector fields.Selector
  29. expectMatch bool
  30. }{
  31. {
  32. in: &api.Pod{
  33. Spec: api.PodSpec{NodeName: "nodeA"},
  34. },
  35. fieldSelector: fields.ParseSelectorOrDie("spec.nodeName=nodeA"),
  36. expectMatch: true,
  37. },
  38. {
  39. in: &api.Pod{
  40. Spec: api.PodSpec{NodeName: "nodeB"},
  41. },
  42. fieldSelector: fields.ParseSelectorOrDie("spec.nodeName=nodeA"),
  43. expectMatch: false,
  44. },
  45. {
  46. in: &api.Pod{
  47. Spec: api.PodSpec{RestartPolicy: api.RestartPolicyAlways},
  48. },
  49. fieldSelector: fields.ParseSelectorOrDie("spec.restartPolicy=Always"),
  50. expectMatch: true,
  51. },
  52. {
  53. in: &api.Pod{
  54. Spec: api.PodSpec{RestartPolicy: api.RestartPolicyAlways},
  55. },
  56. fieldSelector: fields.ParseSelectorOrDie("spec.restartPolicy=Never"),
  57. expectMatch: false,
  58. },
  59. {
  60. in: &api.Pod{
  61. Status: api.PodStatus{Phase: api.PodRunning},
  62. },
  63. fieldSelector: fields.ParseSelectorOrDie("status.phase=Running"),
  64. expectMatch: true,
  65. },
  66. {
  67. in: &api.Pod{
  68. Status: api.PodStatus{Phase: api.PodRunning},
  69. },
  70. fieldSelector: fields.ParseSelectorOrDie("status.phase=Pending"),
  71. expectMatch: false,
  72. },
  73. }
  74. for _, testCase := range testCases {
  75. result, err := MatchPod(labels.Everything(), testCase.fieldSelector).Matches(testCase.in)
  76. if err != nil {
  77. t.Errorf("Unexpected error %v", err)
  78. }
  79. if result != testCase.expectMatch {
  80. t.Errorf("Result %v, Expected %v, Selector: %v, Pod: %v", result, testCase.expectMatch, testCase.fieldSelector.String(), testCase.in)
  81. }
  82. }
  83. }
  84. func TestCheckGracefulDelete(t *testing.T) {
  85. defaultGracePeriod := int64(30)
  86. tcs := []struct {
  87. in *api.Pod
  88. gracePeriod int64
  89. }{
  90. {
  91. in: &api.Pod{
  92. Spec: api.PodSpec{NodeName: "something"},
  93. Status: api.PodStatus{Phase: api.PodPending},
  94. },
  95. gracePeriod: defaultGracePeriod,
  96. },
  97. {
  98. in: &api.Pod{
  99. Spec: api.PodSpec{NodeName: "something"},
  100. Status: api.PodStatus{Phase: api.PodFailed},
  101. },
  102. gracePeriod: 0,
  103. },
  104. {
  105. in: &api.Pod{
  106. Spec: api.PodSpec{},
  107. Status: api.PodStatus{Phase: api.PodPending},
  108. },
  109. gracePeriod: 0,
  110. },
  111. {
  112. in: &api.Pod{
  113. Spec: api.PodSpec{},
  114. Status: api.PodStatus{Phase: api.PodSucceeded},
  115. },
  116. gracePeriod: 0,
  117. },
  118. {
  119. in: &api.Pod{
  120. Spec: api.PodSpec{},
  121. Status: api.PodStatus{},
  122. },
  123. gracePeriod: 0,
  124. },
  125. }
  126. for _, tc := range tcs {
  127. out := &api.DeleteOptions{GracePeriodSeconds: &defaultGracePeriod}
  128. Strategy.CheckGracefulDelete(api.NewContext(), tc.in, out)
  129. if out.GracePeriodSeconds == nil {
  130. t.Errorf("out grace period was nil but supposed to be %v", tc.gracePeriod)
  131. }
  132. if *(out.GracePeriodSeconds) != tc.gracePeriod {
  133. t.Errorf("out grace period was %v but was expected to be %v", *out, tc.gracePeriod)
  134. }
  135. }
  136. }
  137. type mockPodGetter struct {
  138. pod *api.Pod
  139. }
  140. func (g mockPodGetter) Get(api.Context, string) (runtime.Object, error) {
  141. return g.pod, nil
  142. }
  143. func TestCheckLogLocation(t *testing.T) {
  144. ctx := api.NewDefaultContext()
  145. tcs := []struct {
  146. in *api.Pod
  147. opts *api.PodLogOptions
  148. expectedErr error
  149. }{
  150. {
  151. in: &api.Pod{
  152. Spec: api.PodSpec{},
  153. Status: api.PodStatus{},
  154. },
  155. opts: &api.PodLogOptions{},
  156. expectedErr: errors.NewBadRequest("a container name must be specified for pod test"),
  157. },
  158. {
  159. in: &api.Pod{
  160. Spec: api.PodSpec{
  161. Containers: []api.Container{
  162. {Name: "mycontainer"},
  163. },
  164. },
  165. Status: api.PodStatus{},
  166. },
  167. opts: &api.PodLogOptions{},
  168. expectedErr: nil,
  169. },
  170. {
  171. in: &api.Pod{
  172. Spec: api.PodSpec{
  173. Containers: []api.Container{
  174. {Name: "container1"},
  175. {Name: "container2"},
  176. },
  177. },
  178. Status: api.PodStatus{},
  179. },
  180. opts: &api.PodLogOptions{},
  181. expectedErr: errors.NewBadRequest("a container name must be specified for pod test, choose one of: [container1 container2]"),
  182. },
  183. {
  184. in: &api.Pod{
  185. Spec: api.PodSpec{
  186. Containers: []api.Container{
  187. {Name: "container1"},
  188. {Name: "container2"},
  189. },
  190. InitContainers: []api.Container{
  191. {Name: "initcontainer1"},
  192. },
  193. },
  194. Status: api.PodStatus{},
  195. },
  196. opts: &api.PodLogOptions{},
  197. expectedErr: errors.NewBadRequest("a container name must be specified for pod test, choose one of: [container1 container2] or one of the init containers: [initcontainer1]"),
  198. },
  199. {
  200. in: &api.Pod{
  201. Spec: api.PodSpec{
  202. Containers: []api.Container{
  203. {Name: "container1"},
  204. {Name: "container2"},
  205. },
  206. },
  207. Status: api.PodStatus{},
  208. },
  209. opts: &api.PodLogOptions{
  210. Container: "unknown",
  211. },
  212. expectedErr: errors.NewBadRequest("container unknown is not valid for pod test"),
  213. },
  214. {
  215. in: &api.Pod{
  216. Spec: api.PodSpec{
  217. Containers: []api.Container{
  218. {Name: "container1"},
  219. {Name: "container2"},
  220. },
  221. },
  222. Status: api.PodStatus{},
  223. },
  224. opts: &api.PodLogOptions{
  225. Container: "container2",
  226. },
  227. expectedErr: nil,
  228. },
  229. }
  230. for _, tc := range tcs {
  231. getter := &mockPodGetter{tc.in}
  232. _, _, err := LogLocation(getter, nil, ctx, "test", tc.opts)
  233. if !reflect.DeepEqual(err, tc.expectedErr) {
  234. t.Errorf("expected %v, got %v", tc.expectedErr, err)
  235. }
  236. }
  237. }
  238. func TestSelectableFieldLabelConversions(t *testing.T) {
  239. apitesting.TestSelectableFieldLabelConversionsOfKind(t,
  240. testapi.Default.GroupVersion().String(),
  241. "Pod",
  242. PodToSelectableFields(&api.Pod{}),
  243. nil,
  244. )
  245. }