ps_test.go 4.1 KB

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