disk_usage_test.go 7.4 KB

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