custom_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package ps
  2. import (
  3. "bytes"
  4. "reflect"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/pkg/stringid"
  10. )
  11. func TestContainerPsContext(t *testing.T) {
  12. containerId := stringid.GenerateRandomID()
  13. unix := time.Now().Unix()
  14. var ctx containerContext
  15. cases := []struct {
  16. container types.Container
  17. trunc bool
  18. expValue string
  19. expHeader string
  20. call func() string
  21. }{
  22. {types.Container{ID: containerId}, true, stringid.TruncateID(containerId), idHeader, ctx.ID},
  23. {types.Container{Names: []string{"/foobar_baz"}}, true, "foobar_baz", namesHeader, ctx.Names},
  24. {types.Container{Image: "ubuntu"}, true, "ubuntu", imageHeader, ctx.Image},
  25. {types.Container{Image: ""}, true, "<no image>", imageHeader, ctx.Image},
  26. {types.Container{Command: "sh -c 'ls -la'"}, true, `"sh -c 'ls -la'"`, commandHeader, ctx.Command},
  27. {types.Container{Created: int(unix)}, true, time.Unix(unix, 0).String(), createdAtHeader, ctx.CreatedAt},
  28. {types.Container{Ports: []types.Port{{PrivatePort: 8080, PublicPort: 8080, Type: "tcp"}}}, true, "8080/tcp", portsHeader, ctx.Ports},
  29. {types.Container{Status: "RUNNING"}, true, "RUNNING", statusHeader, ctx.Status},
  30. {types.Container{SizeRw: 10}, true, "10 B", sizeHeader, ctx.Size},
  31. {types.Container{SizeRw: 10, SizeRootFs: 20}, true, "10 B (virtual 20 B)", sizeHeader, ctx.Size},
  32. {types.Container{Labels: map[string]string{"cpu": "6", "storage": "ssd"}}, true, "cpu=6,storage=ssd", labelsHeader, ctx.Labels},
  33. }
  34. for _, c := range cases {
  35. ctx = containerContext{c: c.container, trunc: c.trunc}
  36. v := c.call()
  37. if strings.Contains(v, ",") {
  38. // comma-separated values means probably a map input, which won't
  39. // be guaranteed to have the same order as our expected value
  40. // We'll create maps and use reflect.DeepEquals to check instead:
  41. entriesMap := make(map[string]string)
  42. expMap := make(map[string]string)
  43. entries := strings.Split(v, ",")
  44. expectedEntries := strings.Split(c.expValue, ",")
  45. for _, entry := range entries {
  46. keyval := strings.Split(entry, "=")
  47. entriesMap[keyval[0]] = keyval[1]
  48. }
  49. for _, expected := range expectedEntries {
  50. keyval := strings.Split(expected, "=")
  51. expMap[keyval[0]] = keyval[1]
  52. }
  53. if !reflect.DeepEqual(expMap, entriesMap) {
  54. t.Fatalf("Expected entries: %v, got: %v", c.expValue, v)
  55. }
  56. } else if v != c.expValue {
  57. t.Fatalf("Expected %s, was %s\n", c.expValue, v)
  58. }
  59. h := ctx.fullHeader()
  60. if h != c.expHeader {
  61. t.Fatalf("Expected %s, was %s\n", c.expHeader, h)
  62. }
  63. }
  64. c := types.Container{Labels: map[string]string{"com.docker.swarm.swarm-id": "33", "com.docker.swarm.node_name": "ubuntu"}}
  65. ctx = containerContext{c: c, trunc: true}
  66. sid := ctx.Label("com.docker.swarm.swarm-id")
  67. node := ctx.Label("com.docker.swarm.node_name")
  68. if sid != "33" {
  69. t.Fatalf("Expected 33, was %s\n", sid)
  70. }
  71. if node != "ubuntu" {
  72. t.Fatalf("Expected ubuntu, was %s\n", node)
  73. }
  74. h := ctx.fullHeader()
  75. if h != "SWARM ID\tNODE NAME" {
  76. t.Fatalf("Expected %s, was %s\n", "SWARM ID\tNODE NAME", h)
  77. }
  78. }
  79. func TestContainerPsFormatError(t *testing.T) {
  80. out := bytes.NewBufferString("")
  81. ctx := Context{
  82. Format: "{{InvalidFunction}}",
  83. Output: out,
  84. }
  85. customFormat(ctx, make([]types.Container, 0))
  86. if out.String() != "Template parsing error: template: :1: function \"InvalidFunction\" not defined\n" {
  87. t.Fatalf("Expected format error, got `%v`\n", out.String())
  88. }
  89. }