custom_test.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 TestContainerContextID(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{Names: []string{"/foobar_baz"}}, true, "foobar_baz", namesHeader, ctx.Names},
  23. {types.Container{Image: "ubuntu"}, true, "ubuntu", imageHeader, ctx.Image},
  24. {types.Container{Image: ""}, true, "<no image>", imageHeader, ctx.Image},
  25. {types.Container{Command: "sh -c 'ls -la'"}, true, `"sh -c 'ls -la'"`, commandHeader, ctx.Command},
  26. {types.Container{Created: int(unix)}, true, time.Unix(unix, 0).String(), createdAtHeader, ctx.CreatedAt},
  27. {types.Container{Ports: []types.Port{{PrivatePort: 8080, PublicPort: 8080, Type: "tcp"}}}, true, "8080/tcp", portsHeader, ctx.Ports},
  28. {types.Container{Status: "RUNNING"}, true, "RUNNING", statusHeader, ctx.Status},
  29. {types.Container{SizeRw: 10}, true, "10 B", sizeHeader, ctx.Size},
  30. {types.Container{SizeRw: 10, SizeRootFs: 20}, true, "10 B (virtual 20 B)", sizeHeader, ctx.Size},
  31. {types.Container{Labels: map[string]string{"cpu": "6", "storage": "ssd"}}, true, "cpu=6,storage=ssd", labelsHeader, ctx.Labels},
  32. }
  33. for _, c := range cases {
  34. ctx = containerContext{c: c.container, trunc: c.trunc}
  35. v := c.call()
  36. if strings.Contains(v, ",") {
  37. // comma-separated values means probably a map input, which won't
  38. // be guaranteed to have the same order as our expected value
  39. // We'll create maps and use reflect.DeepEquals to check instead:
  40. entriesMap := make(map[string]string)
  41. expMap := make(map[string]string)
  42. entries := strings.Split(v, ",")
  43. expectedEntries := strings.Split(c.expValue, ",")
  44. for _, entry := range entries {
  45. keyval := strings.Split(entry, "=")
  46. entriesMap[keyval[0]] = keyval[1]
  47. }
  48. for _, expected := range expectedEntries {
  49. keyval := strings.Split(expected, "=")
  50. expMap[keyval[0]] = keyval[1]
  51. }
  52. if !reflect.DeepEqual(expMap, entriesMap) {
  53. t.Fatalf("Expected entries: %v, got: %v", c.expValue, v)
  54. }
  55. } else if v != c.expValue {
  56. t.Fatalf("Expected %s, was %s\n", c.expValue, v)
  57. }
  58. h := ctx.fullHeader()
  59. if h != c.expHeader {
  60. t.Fatalf("Expected %s, was %s\n", c.expHeader, h)
  61. }
  62. }
  63. c := types.Container{Labels: map[string]string{"com.docker.swarm.swarm-id": "33", "com.docker.swarm.node_name": "ubuntu"}}
  64. ctx = containerContext{c: c, trunc: true}
  65. sid := ctx.Label("com.docker.swarm.swarm-id")
  66. node := ctx.Label("com.docker.swarm.node_name")
  67. if sid != "33" {
  68. t.Fatalf("Expected 33, was %s\n", sid)
  69. }
  70. if node != "ubuntu" {
  71. t.Fatalf("Expected ubuntu, was %s\n", node)
  72. }
  73. h := ctx.fullHeader()
  74. if h != "SWARM ID\tNODE NAME" {
  75. t.Fatalf("Expected %s, was %s\n", "SWARM ID\tNODE NAME", h)
  76. }
  77. }