logsforobject_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. Copyright 2017 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 polymorphichelpers
  14. import (
  15. "reflect"
  16. "testing"
  17. "time"
  18. appsv1 "k8s.io/api/apps/v1"
  19. batchv1 "k8s.io/api/batch/v1"
  20. corev1 "k8s.io/api/core/v1"
  21. extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. "k8s.io/apimachinery/pkg/runtime/schema"
  25. "k8s.io/apimachinery/pkg/util/diff"
  26. fakeexternal "k8s.io/client-go/kubernetes/fake"
  27. testclient "k8s.io/client-go/testing"
  28. )
  29. var (
  30. podsResource = schema.GroupVersionResource{Version: "v1", Resource: "pods"}
  31. podsKind = schema.GroupVersionKind{Version: "v1", Kind: "Pod"}
  32. )
  33. func TestLogsForObject(t *testing.T) {
  34. tests := []struct {
  35. name string
  36. obj runtime.Object
  37. opts *corev1.PodLogOptions
  38. allContainers bool
  39. pods []runtime.Object
  40. actions []testclient.Action
  41. }{
  42. {
  43. name: "pod logs",
  44. obj: &corev1.Pod{
  45. ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
  46. },
  47. pods: []runtime.Object{testPod()},
  48. actions: []testclient.Action{
  49. getLogsAction("test", nil),
  50. },
  51. },
  52. {
  53. name: "pod logs: all containers",
  54. obj: &corev1.Pod{
  55. ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
  56. Spec: corev1.PodSpec{
  57. InitContainers: []corev1.Container{
  58. {Name: "initc1"},
  59. {Name: "initc2"},
  60. },
  61. Containers: []corev1.Container{
  62. {Name: "c1"},
  63. {Name: "c2"},
  64. },
  65. },
  66. },
  67. opts: &corev1.PodLogOptions{},
  68. allContainers: true,
  69. pods: []runtime.Object{testPod()},
  70. actions: []testclient.Action{
  71. getLogsAction("test", &corev1.PodLogOptions{Container: "initc1"}),
  72. getLogsAction("test", &corev1.PodLogOptions{Container: "initc2"}),
  73. getLogsAction("test", &corev1.PodLogOptions{Container: "c1"}),
  74. getLogsAction("test", &corev1.PodLogOptions{Container: "c2"}),
  75. },
  76. },
  77. {
  78. name: "pods list logs",
  79. obj: &corev1.PodList{
  80. Items: []corev1.Pod{
  81. {
  82. ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
  83. Spec: corev1.PodSpec{
  84. InitContainers: []corev1.Container{
  85. {Name: "initc1"},
  86. {Name: "initc2"},
  87. },
  88. Containers: []corev1.Container{
  89. {Name: "c1"},
  90. {Name: "c2"},
  91. },
  92. },
  93. },
  94. },
  95. },
  96. pods: []runtime.Object{testPod()},
  97. actions: []testclient.Action{
  98. getLogsAction("test", nil),
  99. },
  100. },
  101. {
  102. name: "pods list logs: all containers",
  103. obj: &corev1.PodList{
  104. Items: []corev1.Pod{
  105. {
  106. ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
  107. Spec: corev1.PodSpec{
  108. InitContainers: []corev1.Container{
  109. {Name: "initc1"},
  110. {Name: "initc2"},
  111. },
  112. Containers: []corev1.Container{
  113. {Name: "c1"},
  114. {Name: "c2"},
  115. },
  116. },
  117. },
  118. },
  119. },
  120. opts: &corev1.PodLogOptions{},
  121. allContainers: true,
  122. pods: []runtime.Object{testPod()},
  123. actions: []testclient.Action{
  124. getLogsAction("test", &corev1.PodLogOptions{Container: "initc1"}),
  125. getLogsAction("test", &corev1.PodLogOptions{Container: "initc2"}),
  126. getLogsAction("test", &corev1.PodLogOptions{Container: "c1"}),
  127. getLogsAction("test", &corev1.PodLogOptions{Container: "c2"}),
  128. },
  129. },
  130. {
  131. name: "replication controller logs",
  132. obj: &corev1.ReplicationController{
  133. ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
  134. Spec: corev1.ReplicationControllerSpec{
  135. Selector: map[string]string{"foo": "bar"},
  136. },
  137. },
  138. pods: []runtime.Object{testPod()},
  139. actions: []testclient.Action{
  140. testclient.NewListAction(podsResource, podsKind, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
  141. getLogsAction("test", nil),
  142. },
  143. },
  144. {
  145. name: "replica set logs",
  146. obj: &extensionsv1beta1.ReplicaSet{
  147. ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
  148. Spec: extensionsv1beta1.ReplicaSetSpec{
  149. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
  150. },
  151. },
  152. pods: []runtime.Object{testPod()},
  153. actions: []testclient.Action{
  154. testclient.NewListAction(podsResource, podsKind, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
  155. getLogsAction("test", nil),
  156. },
  157. },
  158. {
  159. name: "deployment logs",
  160. obj: &extensionsv1beta1.Deployment{
  161. ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
  162. Spec: extensionsv1beta1.DeploymentSpec{
  163. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
  164. },
  165. },
  166. pods: []runtime.Object{testPod()},
  167. actions: []testclient.Action{
  168. testclient.NewListAction(podsResource, podsKind, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
  169. getLogsAction("test", nil),
  170. },
  171. },
  172. {
  173. name: "job logs",
  174. obj: &batchv1.Job{
  175. ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
  176. Spec: batchv1.JobSpec{
  177. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
  178. },
  179. },
  180. pods: []runtime.Object{testPod()},
  181. actions: []testclient.Action{
  182. testclient.NewListAction(podsResource, podsKind, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
  183. getLogsAction("test", nil),
  184. },
  185. },
  186. {
  187. name: "stateful set logs",
  188. obj: &appsv1.StatefulSet{
  189. ObjectMeta: metav1.ObjectMeta{Name: "hello", Namespace: "test"},
  190. Spec: appsv1.StatefulSetSpec{
  191. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
  192. },
  193. },
  194. pods: []runtime.Object{testPod()},
  195. actions: []testclient.Action{
  196. testclient.NewListAction(podsResource, podsKind, "test", metav1.ListOptions{LabelSelector: "foo=bar"}),
  197. getLogsAction("test", nil),
  198. },
  199. },
  200. }
  201. for _, test := range tests {
  202. fakeClientset := fakeexternal.NewSimpleClientset(test.pods...)
  203. _, err := logsForObjectWithClient(fakeClientset.CoreV1(), test.obj, test.opts, 20*time.Second, test.allContainers)
  204. if err != nil {
  205. t.Errorf("%s: unexpected error: %v", test.name, err)
  206. continue
  207. }
  208. for i := range test.actions {
  209. if len(fakeClientset.Actions()) < i {
  210. t.Errorf("%s: action %d does not exists in actual actions: %#v",
  211. test.name, i, fakeClientset.Actions())
  212. continue
  213. }
  214. got := fakeClientset.Actions()[i]
  215. want := test.actions[i]
  216. if !reflect.DeepEqual(got, want) {
  217. t.Errorf("%s: unexpected action: %s", test.name, diff.ObjectDiff(got, want))
  218. }
  219. }
  220. }
  221. }
  222. func testPod() runtime.Object {
  223. return &corev1.Pod{
  224. ObjectMeta: metav1.ObjectMeta{
  225. Name: "foo",
  226. Namespace: "test",
  227. Labels: map[string]string{"foo": "bar"},
  228. },
  229. Spec: corev1.PodSpec{
  230. RestartPolicy: corev1.RestartPolicyAlways,
  231. DNSPolicy: corev1.DNSClusterFirst,
  232. Containers: []corev1.Container{
  233. {Name: "c1"},
  234. {Name: "c2"},
  235. },
  236. },
  237. }
  238. }
  239. func getLogsAction(namespace string, opts *corev1.PodLogOptions) testclient.Action {
  240. action := testclient.GenericActionImpl{}
  241. action.Verb = "get"
  242. action.Namespace = namespace
  243. action.Resource = podsResource
  244. action.Subresource = "log"
  245. action.Value = opts
  246. return action
  247. }