custom_test.go 3.8 KB

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