images_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 dockertools
  14. import (
  15. "testing"
  16. dockertypes "github.com/docker/engine-api/types"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func TestImageStatsNoImages(t *testing.T) {
  20. fakeDockerClient := NewFakeDockerClientWithVersion("1.2.3", "1.2")
  21. isp := newImageStatsProvider(fakeDockerClient)
  22. st, err := isp.ImageStats()
  23. as := assert.New(t)
  24. as.NoError(err)
  25. as.NoError(fakeDockerClient.AssertCalls([]string{"list_images"}))
  26. as.Equal(st.TotalStorageBytes, uint64(0))
  27. }
  28. func TestImageStatsWithImages(t *testing.T) {
  29. fakeDockerClient := NewFakeDockerClientWithVersion("1.2.3", "1.2")
  30. fakeHistoryData := map[string][]dockertypes.ImageHistory{
  31. "busybox": {
  32. {
  33. ID: "0123456",
  34. CreatedBy: "foo",
  35. Size: 100,
  36. },
  37. {
  38. ID: "0123457",
  39. CreatedBy: "duplicate",
  40. Size: 200,
  41. },
  42. {
  43. ID: "<missing>",
  44. CreatedBy: "baz",
  45. Size: 300,
  46. },
  47. },
  48. "kubelet": {
  49. {
  50. ID: "1123456",
  51. CreatedBy: "foo",
  52. Size: 200,
  53. },
  54. {
  55. ID: "<missing>",
  56. CreatedBy: "1baz",
  57. Size: 400,
  58. },
  59. },
  60. "busybox-new": {
  61. {
  62. ID: "01234567",
  63. CreatedBy: "foo",
  64. Size: 100,
  65. },
  66. {
  67. ID: "0123457",
  68. CreatedBy: "duplicate",
  69. Size: 200,
  70. },
  71. {
  72. ID: "<missing>",
  73. CreatedBy: "baz",
  74. Size: 300,
  75. },
  76. },
  77. }
  78. fakeDockerClient.InjectImageHistory(fakeHistoryData)
  79. fakeDockerClient.InjectImages([]dockertypes.Image{
  80. {
  81. ID: "busybox",
  82. },
  83. {
  84. ID: "kubelet",
  85. },
  86. {
  87. ID: "busybox-new",
  88. },
  89. })
  90. isp := newImageStatsProvider(fakeDockerClient)
  91. st, err := isp.ImageStats()
  92. as := assert.New(t)
  93. as.NoError(err)
  94. as.NoError(fakeDockerClient.AssertCalls([]string{"list_images", "image_history", "image_history", "image_history"}))
  95. const expectedOutput uint64 = 1300
  96. as.Equal(expectedOutput, st.TotalStorageBytes, "expected %d, got %d", expectedOutput, st.TotalStorageBytes)
  97. }
  98. func TestImageStatsWithCachedImages(t *testing.T) {
  99. for _, test := range []struct {
  100. oldLayers map[string]*dockertypes.ImageHistory
  101. oldImageToLayerIDs map[string][]string
  102. images []dockertypes.Image
  103. history map[string][]dockertypes.ImageHistory
  104. expectedCalls []string
  105. expectedLayers map[string]*dockertypes.ImageHistory
  106. expectedImageToLayerIDs map[string][]string
  107. expectedTotalStorageSize uint64
  108. }{
  109. {
  110. // No cache
  111. oldLayers: make(map[string]*dockertypes.ImageHistory),
  112. oldImageToLayerIDs: make(map[string][]string),
  113. images: []dockertypes.Image{
  114. {
  115. ID: "busybox",
  116. },
  117. {
  118. ID: "kubelet",
  119. },
  120. },
  121. history: map[string][]dockertypes.ImageHistory{
  122. "busybox": {
  123. {
  124. ID: "0123456",
  125. CreatedBy: "foo",
  126. Size: 100,
  127. },
  128. {
  129. ID: "<missing>",
  130. CreatedBy: "baz",
  131. Size: 300,
  132. },
  133. },
  134. "kubelet": {
  135. {
  136. ID: "1123456",
  137. CreatedBy: "foo",
  138. Size: 200,
  139. },
  140. {
  141. ID: "<missing>",
  142. CreatedBy: "1baz",
  143. Size: 400,
  144. },
  145. },
  146. },
  147. expectedCalls: []string{"list_images", "image_history", "image_history"},
  148. expectedLayers: map[string]*dockertypes.ImageHistory{
  149. "0123456": {
  150. ID: "0123456",
  151. CreatedBy: "foo",
  152. Size: 100,
  153. },
  154. "1123456": {
  155. ID: "1123456",
  156. CreatedBy: "foo",
  157. Size: 200,
  158. },
  159. "<missing>baz": {
  160. ID: "<missing>",
  161. CreatedBy: "baz",
  162. Size: 300,
  163. },
  164. "<missing>1baz": {
  165. ID: "<missing>",
  166. CreatedBy: "1baz",
  167. Size: 400,
  168. },
  169. },
  170. expectedImageToLayerIDs: map[string][]string{
  171. "busybox": {"0123456", "<missing>baz"},
  172. "kubelet": {"1123456", "<missing>1baz"},
  173. },
  174. expectedTotalStorageSize: 1000,
  175. },
  176. {
  177. // Use cache value
  178. oldLayers: map[string]*dockertypes.ImageHistory{
  179. "0123456": {
  180. ID: "0123456",
  181. CreatedBy: "foo",
  182. Size: 100,
  183. },
  184. "<missing>baz": {
  185. ID: "<missing>",
  186. CreatedBy: "baz",
  187. Size: 300,
  188. },
  189. },
  190. oldImageToLayerIDs: map[string][]string{
  191. "busybox": {"0123456", "<missing>baz"},
  192. },
  193. images: []dockertypes.Image{
  194. {
  195. ID: "busybox",
  196. },
  197. {
  198. ID: "kubelet",
  199. },
  200. },
  201. history: map[string][]dockertypes.ImageHistory{
  202. "busybox": {
  203. {
  204. ID: "0123456",
  205. CreatedBy: "foo",
  206. Size: 100,
  207. },
  208. {
  209. ID: "<missing>",
  210. CreatedBy: "baz",
  211. Size: 300,
  212. },
  213. },
  214. "kubelet": {
  215. {
  216. ID: "1123456",
  217. CreatedBy: "foo",
  218. Size: 200,
  219. },
  220. {
  221. ID: "<missing>",
  222. CreatedBy: "1baz",
  223. Size: 400,
  224. },
  225. },
  226. },
  227. expectedCalls: []string{"list_images", "image_history"},
  228. expectedLayers: map[string]*dockertypes.ImageHistory{
  229. "0123456": {
  230. ID: "0123456",
  231. CreatedBy: "foo",
  232. Size: 100,
  233. },
  234. "1123456": {
  235. ID: "1123456",
  236. CreatedBy: "foo",
  237. Size: 200,
  238. },
  239. "<missing>baz": {
  240. ID: "<missing>",
  241. CreatedBy: "baz",
  242. Size: 300,
  243. },
  244. "<missing>1baz": {
  245. ID: "<missing>",
  246. CreatedBy: "1baz",
  247. Size: 400,
  248. },
  249. },
  250. expectedImageToLayerIDs: map[string][]string{
  251. "busybox": {"0123456", "<missing>baz"},
  252. "kubelet": {"1123456", "<missing>1baz"},
  253. },
  254. expectedTotalStorageSize: 1000,
  255. },
  256. {
  257. // Unused cache value
  258. oldLayers: map[string]*dockertypes.ImageHistory{
  259. "0123456": {
  260. ID: "0123456",
  261. CreatedBy: "foo",
  262. Size: 100,
  263. },
  264. "<missing>baz": {
  265. ID: "<missing>",
  266. CreatedBy: "baz",
  267. Size: 300,
  268. },
  269. },
  270. oldImageToLayerIDs: map[string][]string{
  271. "busybox": {"0123456", "<missing>baz"},
  272. },
  273. images: []dockertypes.Image{
  274. {
  275. ID: "kubelet",
  276. },
  277. },
  278. history: map[string][]dockertypes.ImageHistory{
  279. "kubelet": {
  280. {
  281. ID: "1123456",
  282. CreatedBy: "foo",
  283. Size: 200,
  284. },
  285. {
  286. ID: "<missing>",
  287. CreatedBy: "1baz",
  288. Size: 400,
  289. },
  290. },
  291. },
  292. expectedCalls: []string{"list_images", "image_history"},
  293. expectedLayers: map[string]*dockertypes.ImageHistory{
  294. "1123456": {
  295. ID: "1123456",
  296. CreatedBy: "foo",
  297. Size: 200,
  298. },
  299. "<missing>1baz": {
  300. ID: "<missing>",
  301. CreatedBy: "1baz",
  302. Size: 400,
  303. },
  304. },
  305. expectedImageToLayerIDs: map[string][]string{
  306. "kubelet": {"1123456", "<missing>1baz"},
  307. },
  308. expectedTotalStorageSize: 600,
  309. },
  310. } {
  311. fakeDockerClient := NewFakeDockerClientWithVersion("1.2.3", "1.2")
  312. fakeDockerClient.InjectImages(test.images)
  313. fakeDockerClient.InjectImageHistory(test.history)
  314. isp := newImageStatsProvider(fakeDockerClient)
  315. isp.layers = test.oldLayers
  316. isp.imageToLayerIDs = test.oldImageToLayerIDs
  317. st, err := isp.ImageStats()
  318. as := assert.New(t)
  319. as.NoError(err)
  320. as.NoError(fakeDockerClient.AssertCalls(test.expectedCalls))
  321. as.Equal(test.expectedLayers, isp.layers, "expected %+v, got %+v", test.expectedLayers, isp.layers)
  322. as.Equal(test.expectedImageToLayerIDs, isp.imageToLayerIDs, "expected %+v, got %+v", test.expectedImageToLayerIDs, isp.imageToLayerIDs)
  323. as.Equal(test.expectedTotalStorageSize, st.TotalStorageBytes, "expected %d, got %d", test.expectedTotalStorageSize, st.TotalStorageBytes)
  324. }
  325. }