ps_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package container
  2. import (
  3. "testing"
  4. "github.com/docker/docker/opts"
  5. "github.com/docker/docker/pkg/testutil/assert"
  6. )
  7. func TestBuildContainerListOptions(t *testing.T) {
  8. filters := opts.NewFilterOpt()
  9. assert.NilError(t, filters.Set("foo=bar"))
  10. assert.NilError(t, filters.Set("baz=foo"))
  11. contexts := []struct {
  12. psOpts *psOptions
  13. expectedAll bool
  14. expectedSize bool
  15. expectedLimit int
  16. expectedFilters map[string]string
  17. }{
  18. {
  19. psOpts: &psOptions{
  20. all: true,
  21. size: true,
  22. last: 5,
  23. filter: filters,
  24. },
  25. expectedAll: true,
  26. expectedSize: true,
  27. expectedLimit: 5,
  28. expectedFilters: map[string]string{
  29. "foo": "bar",
  30. "baz": "foo",
  31. },
  32. },
  33. {
  34. psOpts: &psOptions{
  35. all: true,
  36. size: true,
  37. last: -1,
  38. nLatest: true,
  39. },
  40. expectedAll: true,
  41. expectedSize: true,
  42. expectedLimit: 1,
  43. expectedFilters: make(map[string]string),
  44. },
  45. {
  46. psOpts: &psOptions{
  47. all: true,
  48. size: false,
  49. last: 5,
  50. filter: filters,
  51. // With .Size, size should be true
  52. format: "{{.Size}}",
  53. },
  54. expectedAll: true,
  55. expectedSize: true,
  56. expectedLimit: 5,
  57. expectedFilters: map[string]string{
  58. "foo": "bar",
  59. "baz": "foo",
  60. },
  61. },
  62. {
  63. psOpts: &psOptions{
  64. all: true,
  65. size: false,
  66. last: 5,
  67. filter: filters,
  68. // With .Size, size should be true
  69. format: "{{.Size}} {{.CreatedAt}} {{.Networks}}",
  70. },
  71. expectedAll: true,
  72. expectedSize: true,
  73. expectedLimit: 5,
  74. expectedFilters: map[string]string{
  75. "foo": "bar",
  76. "baz": "foo",
  77. },
  78. },
  79. {
  80. psOpts: &psOptions{
  81. all: true,
  82. size: false,
  83. last: 5,
  84. filter: filters,
  85. // Without .Size, size should be false
  86. format: "{{.CreatedAt}} {{.Networks}}",
  87. },
  88. expectedAll: true,
  89. expectedSize: false,
  90. expectedLimit: 5,
  91. expectedFilters: map[string]string{
  92. "foo": "bar",
  93. "baz": "foo",
  94. },
  95. },
  96. }
  97. for _, c := range contexts {
  98. options, err := buildContainerListOptions(c.psOpts)
  99. assert.NilError(t, err)
  100. assert.Equal(t, c.expectedAll, options.All)
  101. assert.Equal(t, c.expectedSize, options.Size)
  102. assert.Equal(t, c.expectedLimit, options.Limit)
  103. assert.Equal(t, options.Filters.Len(), len(c.expectedFilters))
  104. for k, v := range c.expectedFilters {
  105. f := options.Filters
  106. if !f.ExactMatch(k, v) {
  107. t.Fatalf("Expected filter with key %s to be %s but got %s", k, v, f.Get(k))
  108. }
  109. }
  110. }
  111. }