ps_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. for _, c := range contexts {
  47. options, err := buildContainerListOptions(c.psOpts)
  48. assert.NilError(t, err)
  49. assert.Equal(t, c.expectedAll, options.All)
  50. assert.Equal(t, c.expectedSize, options.Size)
  51. assert.Equal(t, c.expectedLimit, options.Limit)
  52. assert.Equal(t, options.Filter.Len(), len(c.expectedFilters))
  53. for k, v := range c.expectedFilters {
  54. f := options.Filter
  55. if !f.ExactMatch(k, v) {
  56. t.Fatalf("Expected filter with key %s to be %s but got %s", k, v, f.Get(k))
  57. }
  58. }
  59. }
  60. }