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