kubelet_cadvisor_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. Copyright 2016 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 kubelet
  14. import (
  15. "fmt"
  16. "testing"
  17. cadvisorapi "github.com/google/cadvisor/info/v1"
  18. cadvisorapiv2 "github.com/google/cadvisor/info/v2"
  19. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  20. kubecontainertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
  21. )
  22. func TestGetContainerInfo(t *testing.T) {
  23. containerID := "ab2cdf"
  24. containerPath := fmt.Sprintf("/docker/%v", containerID)
  25. containerInfo := cadvisorapi.ContainerInfo{
  26. ContainerReference: cadvisorapi.ContainerReference{
  27. Name: containerPath,
  28. },
  29. }
  30. testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
  31. fakeRuntime := testKubelet.fakeRuntime
  32. kubelet := testKubelet.kubelet
  33. cadvisorReq := &cadvisorapi.ContainerInfoRequest{}
  34. mockCadvisor := testKubelet.fakeCadvisor
  35. mockCadvisor.On("DockerContainer", containerID, cadvisorReq).Return(containerInfo, nil)
  36. fakeRuntime.PodList = []*kubecontainertest.FakePod{
  37. {Pod: &kubecontainer.Pod{
  38. ID: "12345678",
  39. Name: "qux",
  40. Namespace: "ns",
  41. Containers: []*kubecontainer.Container{
  42. {
  43. Name: "foo",
  44. ID: kubecontainer.ContainerID{Type: "test", ID: containerID},
  45. },
  46. },
  47. }},
  48. }
  49. stats, err := kubelet.GetContainerInfo("qux_ns", "", "foo", cadvisorReq)
  50. if err != nil {
  51. t.Errorf("unexpected error: %v", err)
  52. }
  53. if stats == nil {
  54. t.Fatalf("stats should not be nil")
  55. }
  56. mockCadvisor.AssertExpectations(t)
  57. }
  58. func TestGetRawContainerInfoRoot(t *testing.T) {
  59. containerPath := "/"
  60. containerInfo := &cadvisorapi.ContainerInfo{
  61. ContainerReference: cadvisorapi.ContainerReference{
  62. Name: containerPath,
  63. },
  64. }
  65. testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
  66. kubelet := testKubelet.kubelet
  67. mockCadvisor := testKubelet.fakeCadvisor
  68. cadvisorReq := &cadvisorapi.ContainerInfoRequest{}
  69. mockCadvisor.On("ContainerInfo", containerPath, cadvisorReq).Return(containerInfo, nil)
  70. _, err := kubelet.GetRawContainerInfo(containerPath, cadvisorReq, false)
  71. if err != nil {
  72. t.Errorf("unexpected error: %v", err)
  73. }
  74. mockCadvisor.AssertExpectations(t)
  75. }
  76. func TestGetRawContainerInfoSubcontainers(t *testing.T) {
  77. containerPath := "/kubelet"
  78. containerInfo := map[string]*cadvisorapi.ContainerInfo{
  79. containerPath: {
  80. ContainerReference: cadvisorapi.ContainerReference{
  81. Name: containerPath,
  82. },
  83. },
  84. "/kubelet/sub": {
  85. ContainerReference: cadvisorapi.ContainerReference{
  86. Name: "/kubelet/sub",
  87. },
  88. },
  89. }
  90. testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
  91. kubelet := testKubelet.kubelet
  92. mockCadvisor := testKubelet.fakeCadvisor
  93. cadvisorReq := &cadvisorapi.ContainerInfoRequest{}
  94. mockCadvisor.On("SubcontainerInfo", containerPath, cadvisorReq).Return(containerInfo, nil)
  95. result, err := kubelet.GetRawContainerInfo(containerPath, cadvisorReq, true)
  96. if err != nil {
  97. t.Errorf("unexpected error: %v", err)
  98. }
  99. if len(result) != 2 {
  100. t.Errorf("Expected 2 elements, received: %#v", result)
  101. }
  102. mockCadvisor.AssertExpectations(t)
  103. }
  104. func TestGetContainerInfoWhenCadvisorFailed(t *testing.T) {
  105. containerID := "ab2cdf"
  106. testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
  107. kubelet := testKubelet.kubelet
  108. mockCadvisor := testKubelet.fakeCadvisor
  109. fakeRuntime := testKubelet.fakeRuntime
  110. cadvisorApiFailure := fmt.Errorf("cAdvisor failure")
  111. containerInfo := cadvisorapi.ContainerInfo{}
  112. cadvisorReq := &cadvisorapi.ContainerInfoRequest{}
  113. mockCadvisor.On("DockerContainer", containerID, cadvisorReq).Return(containerInfo, cadvisorApiFailure)
  114. fakeRuntime.PodList = []*kubecontainertest.FakePod{
  115. {Pod: &kubecontainer.Pod{
  116. ID: "uuid",
  117. Name: "qux",
  118. Namespace: "ns",
  119. Containers: []*kubecontainer.Container{
  120. {Name: "foo",
  121. ID: kubecontainer.ContainerID{Type: "test", ID: containerID},
  122. },
  123. },
  124. }},
  125. }
  126. stats, err := kubelet.GetContainerInfo("qux_ns", "uuid", "foo", cadvisorReq)
  127. if stats != nil {
  128. t.Errorf("non-nil stats on error")
  129. }
  130. if err == nil {
  131. t.Errorf("expect error but received nil error")
  132. return
  133. }
  134. if err.Error() != cadvisorApiFailure.Error() {
  135. t.Errorf("wrong error message. expect %v, got %v", cadvisorApiFailure, err)
  136. }
  137. mockCadvisor.AssertExpectations(t)
  138. }
  139. func TestGetContainerInfoOnNonExistContainer(t *testing.T) {
  140. testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
  141. kubelet := testKubelet.kubelet
  142. mockCadvisor := testKubelet.fakeCadvisor
  143. fakeRuntime := testKubelet.fakeRuntime
  144. fakeRuntime.PodList = []*kubecontainertest.FakePod{}
  145. stats, _ := kubelet.GetContainerInfo("qux", "", "foo", nil)
  146. if stats != nil {
  147. t.Errorf("non-nil stats on non exist container")
  148. }
  149. mockCadvisor.AssertExpectations(t)
  150. }
  151. func TestGetContainerInfoWhenContainerRuntimeFailed(t *testing.T) {
  152. testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
  153. kubelet := testKubelet.kubelet
  154. mockCadvisor := testKubelet.fakeCadvisor
  155. fakeRuntime := testKubelet.fakeRuntime
  156. expectedErr := fmt.Errorf("List containers error")
  157. fakeRuntime.Err = expectedErr
  158. stats, err := kubelet.GetContainerInfo("qux", "", "foo", nil)
  159. if err == nil {
  160. t.Errorf("expected error from dockertools, got none")
  161. }
  162. if err.Error() != expectedErr.Error() {
  163. t.Errorf("expected error %v got %v", expectedErr.Error(), err.Error())
  164. }
  165. if stats != nil {
  166. t.Errorf("non-nil stats when dockertools failed")
  167. }
  168. mockCadvisor.AssertExpectations(t)
  169. }
  170. func TestGetContainerInfoWithNoContainers(t *testing.T) {
  171. testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
  172. kubelet := testKubelet.kubelet
  173. mockCadvisor := testKubelet.fakeCadvisor
  174. stats, err := kubelet.GetContainerInfo("qux_ns", "", "foo", nil)
  175. if err == nil {
  176. t.Errorf("expected error from cadvisor client, got none")
  177. }
  178. if err != kubecontainer.ErrContainerNotFound {
  179. t.Errorf("expected error %v, got %v", kubecontainer.ErrContainerNotFound.Error(), err.Error())
  180. }
  181. if stats != nil {
  182. t.Errorf("non-nil stats when dockertools returned no containers")
  183. }
  184. mockCadvisor.AssertExpectations(t)
  185. }
  186. func TestGetContainerInfoWithNoMatchingContainers(t *testing.T) {
  187. testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
  188. fakeRuntime := testKubelet.fakeRuntime
  189. kubelet := testKubelet.kubelet
  190. mockCadvisor := testKubelet.fakeCadvisor
  191. fakeRuntime.PodList = []*kubecontainertest.FakePod{
  192. {Pod: &kubecontainer.Pod{
  193. ID: "12345678",
  194. Name: "qux",
  195. Namespace: "ns",
  196. Containers: []*kubecontainer.Container{
  197. {Name: "bar",
  198. ID: kubecontainer.ContainerID{Type: "test", ID: "fakeID"},
  199. },
  200. }},
  201. },
  202. }
  203. stats, err := kubelet.GetContainerInfo("qux_ns", "", "foo", nil)
  204. if err == nil {
  205. t.Errorf("Expected error from cadvisor client, got none")
  206. }
  207. if err != kubecontainer.ErrContainerNotFound {
  208. t.Errorf("Expected error %v, got %v", kubecontainer.ErrContainerNotFound.Error(), err.Error())
  209. }
  210. if stats != nil {
  211. t.Errorf("non-nil stats when dockertools returned no containers")
  212. }
  213. mockCadvisor.AssertExpectations(t)
  214. }
  215. func TestHasDedicatedImageFs(t *testing.T) {
  216. testCases := map[string]struct {
  217. imageFsInfo cadvisorapiv2.FsInfo
  218. rootFsInfo cadvisorapiv2.FsInfo
  219. expected bool
  220. }{
  221. "has-dedicated-image-fs": {
  222. imageFsInfo: cadvisorapiv2.FsInfo{Device: "123"},
  223. rootFsInfo: cadvisorapiv2.FsInfo{Device: "456"},
  224. expected: true,
  225. },
  226. "has-unified-image-fs": {
  227. imageFsInfo: cadvisorapiv2.FsInfo{Device: "123"},
  228. rootFsInfo: cadvisorapiv2.FsInfo{Device: "123"},
  229. expected: false,
  230. },
  231. }
  232. for testName, testCase := range testCases {
  233. testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
  234. kubelet := testKubelet.kubelet
  235. mockCadvisor := testKubelet.fakeCadvisor
  236. mockCadvisor.On("Start").Return(nil)
  237. mockCadvisor.On("ImagesFsInfo").Return(testCase.imageFsInfo, nil)
  238. mockCadvisor.On("RootFsInfo").Return(testCase.rootFsInfo, nil)
  239. actual, err := kubelet.HasDedicatedImageFs()
  240. if err != nil {
  241. t.Errorf("case: %s, unexpected error: %v", testName, err)
  242. }
  243. if actual != testCase.expected {
  244. t.Errorf("case: %s, expected: %v, actual: %v", testName, testCase.expected, actual)
  245. }
  246. }
  247. }