disk_usage_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package system // import "github.com/docker/docker/integration/system"
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/api/types/image"
  7. "github.com/docker/docker/api/types/volume"
  8. "github.com/docker/docker/integration/internal/container"
  9. "github.com/docker/docker/testutil"
  10. "github.com/docker/docker/testutil/daemon"
  11. "gotest.tools/v3/assert"
  12. is "gotest.tools/v3/assert/cmp"
  13. "gotest.tools/v3/skip"
  14. )
  15. func TestDiskUsage(t *testing.T) {
  16. skip.If(t, testEnv.DaemonInfo.OSType == "windows") // d.Start fails on Windows with `protocol not available`
  17. t.Parallel()
  18. ctx := testutil.StartSpan(baseContext, t)
  19. d := daemon.New(t)
  20. defer d.Cleanup(t)
  21. d.Start(t, "--iptables=false")
  22. defer d.Stop(t)
  23. client := d.NewClientT(t)
  24. var stepDU types.DiskUsage
  25. for _, step := range []struct {
  26. doc string
  27. next func(t *testing.T, prev types.DiskUsage) types.DiskUsage
  28. }{
  29. {
  30. doc: "empty",
  31. next: func(t *testing.T, _ types.DiskUsage) types.DiskUsage {
  32. du, err := client.DiskUsage(ctx, types.DiskUsageOptions{})
  33. assert.NilError(t, err)
  34. assert.DeepEqual(t, du, types.DiskUsage{
  35. Images: []*image.Summary{},
  36. Containers: []*types.Container{},
  37. Volumes: []*volume.Volume{},
  38. BuildCache: []*types.BuildCache{},
  39. })
  40. return du
  41. },
  42. },
  43. {
  44. doc: "after LoadBusybox",
  45. next: func(t *testing.T, _ types.DiskUsage) types.DiskUsage {
  46. d.LoadBusybox(ctx, t)
  47. du, err := client.DiskUsage(ctx, types.DiskUsageOptions{})
  48. assert.NilError(t, err)
  49. assert.Assert(t, du.LayersSize > 0)
  50. assert.Equal(t, len(du.Images), 1)
  51. assert.Equal(t, len(du.Images[0].RepoTags), 1)
  52. assert.Check(t, is.Equal(du.Images[0].RepoTags[0], "busybox:latest"))
  53. // Image size is layer size + content size, should be greater than total layer size
  54. assert.Assert(t, du.Images[0].Size >= du.LayersSize)
  55. // If size is greater, than content exists and should have a repodigest
  56. if du.Images[0].Size > du.LayersSize {
  57. assert.Equal(t, len(du.Images[0].RepoDigests), 1)
  58. assert.Check(t, strings.HasPrefix(du.Images[0].RepoDigests[0], "busybox@"))
  59. }
  60. return du
  61. },
  62. },
  63. {
  64. doc: "after container.Run",
  65. next: func(t *testing.T, prev types.DiskUsage) types.DiskUsage {
  66. cID := container.Run(ctx, t, client)
  67. du, err := client.DiskUsage(ctx, types.DiskUsageOptions{})
  68. assert.NilError(t, err)
  69. assert.Equal(t, len(du.Containers), 1)
  70. assert.Equal(t, len(du.Containers[0].Names), 1)
  71. assert.Assert(t, len(prev.Images) > 0)
  72. assert.Check(t, du.Containers[0].Created >= prev.Images[0].Created)
  73. // Additional container layer could add to the size
  74. assert.Check(t, du.LayersSize >= prev.LayersSize)
  75. assert.Equal(t, len(du.Images), 1)
  76. assert.Equal(t, du.Images[0].Containers, prev.Images[0].Containers+1)
  77. assert.Check(t, is.Equal(du.Containers[0].ID, cID))
  78. assert.Check(t, is.Equal(du.Containers[0].Image, "busybox"))
  79. assert.Check(t, is.Equal(du.Containers[0].ImageID, prev.Images[0].ID))
  80. // The rootfs size should be equivalent to all the layers,
  81. // previously used prev.Images[0].Size, which may differ from content data
  82. assert.Check(t, is.Equal(du.Containers[0].SizeRootFs, du.LayersSize))
  83. return du
  84. },
  85. },
  86. } {
  87. t.Run(step.doc, func(t *testing.T) {
  88. ctx := testutil.StartSpan(ctx, t)
  89. stepDU = step.next(t, stepDU)
  90. for _, tc := range []struct {
  91. doc string
  92. options types.DiskUsageOptions
  93. expected types.DiskUsage
  94. }{
  95. {
  96. doc: "container types",
  97. options: types.DiskUsageOptions{
  98. Types: []types.DiskUsageObject{
  99. types.ContainerObject,
  100. },
  101. },
  102. expected: types.DiskUsage{
  103. Containers: stepDU.Containers,
  104. },
  105. },
  106. {
  107. doc: "image types",
  108. options: types.DiskUsageOptions{
  109. Types: []types.DiskUsageObject{
  110. types.ImageObject,
  111. },
  112. },
  113. expected: types.DiskUsage{
  114. LayersSize: stepDU.LayersSize,
  115. Images: stepDU.Images,
  116. },
  117. },
  118. {
  119. doc: "volume types",
  120. options: types.DiskUsageOptions{
  121. Types: []types.DiskUsageObject{
  122. types.VolumeObject,
  123. },
  124. },
  125. expected: types.DiskUsage{
  126. Volumes: stepDU.Volumes,
  127. },
  128. },
  129. {
  130. doc: "build-cache types",
  131. options: types.DiskUsageOptions{
  132. Types: []types.DiskUsageObject{
  133. types.BuildCacheObject,
  134. },
  135. },
  136. expected: types.DiskUsage{
  137. BuildCache: stepDU.BuildCache,
  138. },
  139. },
  140. {
  141. doc: "container, volume types",
  142. options: types.DiskUsageOptions{
  143. Types: []types.DiskUsageObject{
  144. types.ContainerObject,
  145. types.VolumeObject,
  146. },
  147. },
  148. expected: types.DiskUsage{
  149. Containers: stepDU.Containers,
  150. Volumes: stepDU.Volumes,
  151. },
  152. },
  153. {
  154. doc: "image, build-cache types",
  155. options: types.DiskUsageOptions{
  156. Types: []types.DiskUsageObject{
  157. types.ImageObject,
  158. types.BuildCacheObject,
  159. },
  160. },
  161. expected: types.DiskUsage{
  162. LayersSize: stepDU.LayersSize,
  163. Images: stepDU.Images,
  164. BuildCache: stepDU.BuildCache,
  165. },
  166. },
  167. {
  168. doc: "container, volume, build-cache types",
  169. options: types.DiskUsageOptions{
  170. Types: []types.DiskUsageObject{
  171. types.ContainerObject,
  172. types.VolumeObject,
  173. types.BuildCacheObject,
  174. },
  175. },
  176. expected: types.DiskUsage{
  177. Containers: stepDU.Containers,
  178. Volumes: stepDU.Volumes,
  179. BuildCache: stepDU.BuildCache,
  180. },
  181. },
  182. {
  183. doc: "image, volume, build-cache types",
  184. options: types.DiskUsageOptions{
  185. Types: []types.DiskUsageObject{
  186. types.ImageObject,
  187. types.VolumeObject,
  188. types.BuildCacheObject,
  189. },
  190. },
  191. expected: types.DiskUsage{
  192. LayersSize: stepDU.LayersSize,
  193. Images: stepDU.Images,
  194. Volumes: stepDU.Volumes,
  195. BuildCache: stepDU.BuildCache,
  196. },
  197. },
  198. {
  199. doc: "container, image, volume types",
  200. options: types.DiskUsageOptions{
  201. Types: []types.DiskUsageObject{
  202. types.ContainerObject,
  203. types.ImageObject,
  204. types.VolumeObject,
  205. },
  206. },
  207. expected: types.DiskUsage{
  208. LayersSize: stepDU.LayersSize,
  209. Containers: stepDU.Containers,
  210. Images: stepDU.Images,
  211. Volumes: stepDU.Volumes,
  212. },
  213. },
  214. {
  215. doc: "container, image, volume, build-cache types",
  216. options: types.DiskUsageOptions{
  217. Types: []types.DiskUsageObject{
  218. types.ContainerObject,
  219. types.ImageObject,
  220. types.VolumeObject,
  221. types.BuildCacheObject,
  222. },
  223. },
  224. expected: types.DiskUsage{
  225. LayersSize: stepDU.LayersSize,
  226. Containers: stepDU.Containers,
  227. Images: stepDU.Images,
  228. Volumes: stepDU.Volumes,
  229. BuildCache: stepDU.BuildCache,
  230. },
  231. },
  232. } {
  233. tc := tc
  234. t.Run(tc.doc, func(t *testing.T) {
  235. ctx := testutil.StartSpan(ctx, t)
  236. // TODO: Run in parallel once https://github.com/moby/moby/pull/42560 is merged.
  237. du, err := client.DiskUsage(ctx, tc.options)
  238. assert.NilError(t, err)
  239. assert.DeepEqual(t, du, tc.expected)
  240. })
  241. }
  242. })
  243. }
  244. }