custom_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package ps
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/pkg/stringid"
  7. )
  8. func TestContainerContextID(t *testing.T) {
  9. containerId := stringid.GenerateRandomID()
  10. unix := time.Now().Unix()
  11. var ctx containerContext
  12. cases := []struct {
  13. container types.Container
  14. trunc bool
  15. expValue string
  16. expHeader string
  17. call func() string
  18. }{
  19. {types.Container{ID: containerId}, true, stringid.TruncateID(containerId), idHeader, ctx.ID},
  20. {types.Container{Names: []string{"/foobar_baz"}}, true, "foobar_baz", namesHeader, ctx.Names},
  21. {types.Container{Image: "ubuntu"}, true, "ubuntu", imageHeader, ctx.Image},
  22. {types.Container{Image: ""}, true, "<no image>", imageHeader, ctx.Image},
  23. {types.Container{Command: "sh -c 'ls -la'"}, true, `"sh -c 'ls -la'"`, commandHeader, ctx.Command},
  24. {types.Container{Created: int(unix)}, true, time.Unix(unix, 0).String(), createdAtHeader, ctx.CreatedAt},
  25. {types.Container{Ports: []types.Port{types.Port{PrivatePort: 8080, PublicPort: 8080, Type: "tcp"}}}, true, "8080/tcp", portsHeader, ctx.Ports},
  26. {types.Container{Status: "RUNNING"}, true, "RUNNING", statusHeader, ctx.Status},
  27. {types.Container{SizeRw: 10}, true, "10 B", sizeHeader, ctx.Size},
  28. {types.Container{SizeRw: 10, SizeRootFs: 20}, true, "10 B (virtual 20 B)", sizeHeader, ctx.Size},
  29. {types.Container{Labels: map[string]string{"cpu": "6", "storage": "ssd"}}, true, "cpu=6,storage=ssd", labelsHeader, ctx.Labels},
  30. }
  31. for _, c := range cases {
  32. ctx = containerContext{c: c.container, trunc: c.trunc}
  33. v := c.call()
  34. if v != c.expValue {
  35. t.Fatalf("Expected %s, was %s\n", c.expValue, v)
  36. }
  37. h := ctx.fullHeader()
  38. if h != c.expHeader {
  39. t.Fatalf("Expected %s, was %s\n", c.expHeader, h)
  40. }
  41. }
  42. c := types.Container{Labels: map[string]string{"com.docker.swarm.swarm-id": "33", "com.docker.swarm.node_name": "ubuntu"}}
  43. ctx = containerContext{c: c, trunc: true}
  44. sid := ctx.Label("com.docker.swarm.swarm-id")
  45. node := ctx.Label("com.docker.swarm.node_name")
  46. if sid != "33" {
  47. t.Fatal("Expected 33, was %s\n", sid)
  48. }
  49. if node != "ubuntu" {
  50. t.Fatal("Expected ubuntu, was %s\n", node)
  51. }
  52. h := ctx.fullHeader()
  53. if h != "SWARM ID\tNODE NAME" {
  54. t.Fatal("Expected %s, was %s\n", "SWARM ID\tNODE NAME", h)
  55. }
  56. }