ps_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package node
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "testing"
  7. "time"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/api/types/swarm"
  10. "github.com/docker/docker/cli/internal/test"
  11. // Import builders to get the builder function as package function
  12. . "github.com/docker/docker/cli/internal/test/builders"
  13. "github.com/docker/docker/pkg/testutil/assert"
  14. "github.com/docker/docker/pkg/testutil/golden"
  15. )
  16. func TestNodePsErrors(t *testing.T) {
  17. testCases := []struct {
  18. args []string
  19. flags map[string]string
  20. infoFunc func() (types.Info, error)
  21. nodeInspectFunc func() (swarm.Node, []byte, error)
  22. taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error)
  23. taskInspectFunc func(taskID string) (swarm.Task, []byte, error)
  24. expectedError string
  25. }{
  26. {
  27. infoFunc: func() (types.Info, error) {
  28. return types.Info{}, fmt.Errorf("error asking for node info")
  29. },
  30. expectedError: "error asking for node info",
  31. },
  32. {
  33. args: []string{"nodeID"},
  34. nodeInspectFunc: func() (swarm.Node, []byte, error) {
  35. return swarm.Node{}, []byte{}, fmt.Errorf("error inspecting the node")
  36. },
  37. expectedError: "error inspecting the node",
  38. },
  39. {
  40. args: []string{"nodeID"},
  41. taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
  42. return []swarm.Task{}, fmt.Errorf("error returning the task list")
  43. },
  44. expectedError: "error returning the task list",
  45. },
  46. }
  47. for _, tc := range testCases {
  48. buf := new(bytes.Buffer)
  49. cmd := newPsCommand(
  50. test.NewFakeCli(&fakeClient{
  51. infoFunc: tc.infoFunc,
  52. nodeInspectFunc: tc.nodeInspectFunc,
  53. taskInspectFunc: tc.taskInspectFunc,
  54. taskListFunc: tc.taskListFunc,
  55. }, buf))
  56. cmd.SetArgs(tc.args)
  57. for key, value := range tc.flags {
  58. cmd.Flags().Set(key, value)
  59. }
  60. cmd.SetOutput(ioutil.Discard)
  61. assert.Error(t, cmd.Execute(), tc.expectedError)
  62. }
  63. }
  64. func TestNodePs(t *testing.T) {
  65. testCases := []struct {
  66. name string
  67. args []string
  68. flags map[string]string
  69. infoFunc func() (types.Info, error)
  70. nodeInspectFunc func() (swarm.Node, []byte, error)
  71. taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error)
  72. taskInspectFunc func(taskID string) (swarm.Task, []byte, error)
  73. }{
  74. {
  75. name: "simple",
  76. args: []string{"nodeID"},
  77. nodeInspectFunc: func() (swarm.Node, []byte, error) {
  78. return *Node(), []byte{}, nil
  79. },
  80. taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
  81. return []swarm.Task{
  82. *Task(WithStatus(Timestamp(time.Now().Add(-2*time.Hour)), PortStatus([]swarm.PortConfig{
  83. {
  84. TargetPort: 80,
  85. PublishedPort: 80,
  86. Protocol: "tcp",
  87. },
  88. }))),
  89. }, nil
  90. },
  91. },
  92. {
  93. name: "with-errors",
  94. args: []string{"nodeID"},
  95. nodeInspectFunc: func() (swarm.Node, []byte, error) {
  96. return *Node(), []byte{}, nil
  97. },
  98. taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
  99. return []swarm.Task{
  100. *Task(TaskID("taskID1"), ServiceID("failure"),
  101. WithStatus(Timestamp(time.Now().Add(-2*time.Hour)), StatusErr("a task error"))),
  102. *Task(TaskID("taskID2"), ServiceID("failure"),
  103. WithStatus(Timestamp(time.Now().Add(-3*time.Hour)), StatusErr("a task error"))),
  104. *Task(TaskID("taskID3"), ServiceID("failure"),
  105. WithStatus(Timestamp(time.Now().Add(-4*time.Hour)), StatusErr("a task error"))),
  106. }, nil
  107. },
  108. },
  109. }
  110. for _, tc := range testCases {
  111. buf := new(bytes.Buffer)
  112. cmd := newPsCommand(
  113. test.NewFakeCli(&fakeClient{
  114. infoFunc: tc.infoFunc,
  115. nodeInspectFunc: tc.nodeInspectFunc,
  116. taskInspectFunc: tc.taskInspectFunc,
  117. taskListFunc: tc.taskListFunc,
  118. }, buf))
  119. cmd.SetArgs(tc.args)
  120. for key, value := range tc.flags {
  121. cmd.Flags().Set(key, value)
  122. }
  123. assert.NilError(t, cmd.Execute())
  124. actual := buf.String()
  125. expected := golden.Get(t, []byte(actual), fmt.Sprintf("node-ps.%s.golden", tc.name))
  126. assert.EqualNormalizedString(t, assert.RemoveSpace, actual, string(expected))
  127. }
  128. }