disk_usage_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package system // import "github.com/docker/docker/integration/system"
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/integration/internal/container"
  7. "github.com/docker/docker/testutil/daemon"
  8. "gotest.tools/v3/assert"
  9. "gotest.tools/v3/skip"
  10. )
  11. func TestDiskUsage(t *testing.T) {
  12. skip.If(t, testEnv.OSType == "windows") // d.Start fails on Windows with `protocol not available`
  13. t.Parallel()
  14. d := daemon.New(t)
  15. defer d.Cleanup(t)
  16. d.Start(t, "--iptables=false")
  17. defer d.Stop(t)
  18. client := d.NewClientT(t)
  19. ctx := context.Background()
  20. var stepDU types.DiskUsage
  21. for _, step := range []struct {
  22. doc string
  23. next func(t *testing.T, prev types.DiskUsage) types.DiskUsage
  24. }{
  25. {
  26. doc: "empty",
  27. next: func(t *testing.T, _ types.DiskUsage) types.DiskUsage {
  28. du, err := client.DiskUsage(ctx, types.DiskUsageOptions{})
  29. assert.NilError(t, err)
  30. assert.DeepEqual(t, du, types.DiskUsage{
  31. Images: []*types.ImageSummary{},
  32. Containers: []*types.Container{},
  33. Volumes: []*types.Volume{},
  34. BuildCache: []*types.BuildCache{},
  35. })
  36. return du
  37. },
  38. },
  39. {
  40. doc: "after LoadBusybox",
  41. next: func(t *testing.T, _ types.DiskUsage) types.DiskUsage {
  42. d.LoadBusybox(t)
  43. du, err := client.DiskUsage(ctx, types.DiskUsageOptions{})
  44. assert.NilError(t, err)
  45. assert.Assert(t, du.LayersSize > 0)
  46. assert.Equal(t, len(du.Images), 1)
  47. assert.DeepEqual(t, du, types.DiskUsage{
  48. LayersSize: du.LayersSize,
  49. Images: []*types.ImageSummary{
  50. {
  51. Created: du.Images[0].Created,
  52. ID: du.Images[0].ID,
  53. RepoTags: []string{"busybox:latest"},
  54. Size: du.LayersSize,
  55. VirtualSize: du.LayersSize,
  56. },
  57. },
  58. Containers: []*types.Container{},
  59. Volumes: []*types.Volume{},
  60. BuildCache: []*types.BuildCache{},
  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, du.Containers[0].Created >= prev.Images[0].Created)
  74. assert.DeepEqual(t, du, types.DiskUsage{
  75. LayersSize: prev.LayersSize,
  76. Images: []*types.ImageSummary{
  77. func() *types.ImageSummary {
  78. sum := *prev.Images[0]
  79. sum.Containers++
  80. return &sum
  81. }(),
  82. },
  83. Containers: []*types.Container{
  84. {
  85. ID: cID,
  86. Names: du.Containers[0].Names,
  87. Image: "busybox",
  88. ImageID: prev.Images[0].ID,
  89. Command: du.Containers[0].Command, // not relevant for the test
  90. Created: du.Containers[0].Created,
  91. Ports: du.Containers[0].Ports, // not relevant for the test
  92. SizeRootFs: prev.Images[0].Size,
  93. Labels: du.Containers[0].Labels, // not relevant for the test
  94. State: du.Containers[0].State, // not relevant for the test
  95. Status: du.Containers[0].Status, // not relevant for the test
  96. HostConfig: du.Containers[0].HostConfig, // not relevant for the test
  97. NetworkSettings: du.Containers[0].NetworkSettings, // not relevant for the test
  98. Mounts: du.Containers[0].Mounts, // not relevant for the test
  99. },
  100. },
  101. Volumes: []*types.Volume{},
  102. BuildCache: []*types.BuildCache{},
  103. })
  104. return du
  105. },
  106. },
  107. } {
  108. t.Run(step.doc, func(t *testing.T) {
  109. stepDU = step.next(t, stepDU)
  110. for _, tc := range []struct {
  111. doc string
  112. options types.DiskUsageOptions
  113. expected types.DiskUsage
  114. }{
  115. {
  116. doc: "container types",
  117. options: types.DiskUsageOptions{
  118. Types: []types.DiskUsageObject{
  119. types.ContainerObject,
  120. },
  121. },
  122. expected: types.DiskUsage{
  123. Containers: stepDU.Containers,
  124. },
  125. },
  126. {
  127. doc: "image types",
  128. options: types.DiskUsageOptions{
  129. Types: []types.DiskUsageObject{
  130. types.ImageObject,
  131. },
  132. },
  133. expected: types.DiskUsage{
  134. LayersSize: stepDU.LayersSize,
  135. Images: stepDU.Images,
  136. },
  137. },
  138. {
  139. doc: "volume types",
  140. options: types.DiskUsageOptions{
  141. Types: []types.DiskUsageObject{
  142. types.VolumeObject,
  143. },
  144. },
  145. expected: types.DiskUsage{
  146. Volumes: stepDU.Volumes,
  147. },
  148. },
  149. {
  150. doc: "build-cache types",
  151. options: types.DiskUsageOptions{
  152. Types: []types.DiskUsageObject{
  153. types.BuildCacheObject,
  154. },
  155. },
  156. expected: types.DiskUsage{
  157. BuildCache: stepDU.BuildCache,
  158. },
  159. },
  160. {
  161. doc: "container, volume types",
  162. options: types.DiskUsageOptions{
  163. Types: []types.DiskUsageObject{
  164. types.ContainerObject,
  165. types.VolumeObject,
  166. },
  167. },
  168. expected: types.DiskUsage{
  169. Containers: stepDU.Containers,
  170. Volumes: stepDU.Volumes,
  171. },
  172. },
  173. {
  174. doc: "image, build-cache types",
  175. options: types.DiskUsageOptions{
  176. Types: []types.DiskUsageObject{
  177. types.ImageObject,
  178. types.BuildCacheObject,
  179. },
  180. },
  181. expected: types.DiskUsage{
  182. LayersSize: stepDU.LayersSize,
  183. Images: stepDU.Images,
  184. BuildCache: stepDU.BuildCache,
  185. },
  186. },
  187. {
  188. doc: "container, volume, build-cache types",
  189. options: types.DiskUsageOptions{
  190. Types: []types.DiskUsageObject{
  191. types.ContainerObject,
  192. types.VolumeObject,
  193. types.BuildCacheObject,
  194. },
  195. },
  196. expected: types.DiskUsage{
  197. Containers: stepDU.Containers,
  198. Volumes: stepDU.Volumes,
  199. BuildCache: stepDU.BuildCache,
  200. },
  201. },
  202. {
  203. doc: "image, volume, build-cache types",
  204. options: types.DiskUsageOptions{
  205. Types: []types.DiskUsageObject{
  206. types.ImageObject,
  207. types.VolumeObject,
  208. types.BuildCacheObject,
  209. },
  210. },
  211. expected: types.DiskUsage{
  212. LayersSize: stepDU.LayersSize,
  213. Images: stepDU.Images,
  214. Volumes: stepDU.Volumes,
  215. BuildCache: stepDU.BuildCache,
  216. },
  217. },
  218. {
  219. doc: "container, image, volume types",
  220. options: types.DiskUsageOptions{
  221. Types: []types.DiskUsageObject{
  222. types.ContainerObject,
  223. types.ImageObject,
  224. types.VolumeObject,
  225. },
  226. },
  227. expected: types.DiskUsage{
  228. LayersSize: stepDU.LayersSize,
  229. Containers: stepDU.Containers,
  230. Images: stepDU.Images,
  231. Volumes: stepDU.Volumes,
  232. },
  233. },
  234. {
  235. doc: "container, image, volume, build-cache types",
  236. options: types.DiskUsageOptions{
  237. Types: []types.DiskUsageObject{
  238. types.ContainerObject,
  239. types.ImageObject,
  240. types.VolumeObject,
  241. types.BuildCacheObject,
  242. },
  243. },
  244. expected: types.DiskUsage{
  245. LayersSize: stepDU.LayersSize,
  246. Containers: stepDU.Containers,
  247. Images: stepDU.Images,
  248. Volumes: stepDU.Volumes,
  249. BuildCache: stepDU.BuildCache,
  250. },
  251. },
  252. } {
  253. tc := tc
  254. t.Run(tc.doc, func(t *testing.T) {
  255. // TODO: Run in parallel once https://github.com/moby/moby/pull/42560 is merged.
  256. du, err := client.DiskUsage(ctx, tc.options)
  257. assert.NilError(t, err)
  258. assert.DeepEqual(t, du, tc.expected)
  259. })
  260. }
  261. })
  262. }
  263. }