disk_usage_test.go 7.2 KB

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