github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/node/ps_test.go (about) 1 package node 2 3 import ( 4 "context" 5 "fmt" 6 "io/ioutil" 7 "testing" 8 "time" 9 10 "github.com/docker/cli/internal/test" 11 . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function 12 "github.com/docker/docker/api/types" 13 "github.com/docker/docker/api/types/swarm" 14 "github.com/pkg/errors" 15 "gotest.tools/v3/assert" 16 "gotest.tools/v3/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{}, errors.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{}, errors.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{}, errors.Errorf("error returning the task list") 46 }, 47 expectedError: "error returning the task list", 48 }, 49 } 50 for _, tc := range testCases { 51 cli := test.NewFakeCli(&fakeClient{ 52 infoFunc: tc.infoFunc, 53 nodeInspectFunc: tc.nodeInspectFunc, 54 taskInspectFunc: tc.taskInspectFunc, 55 taskListFunc: tc.taskListFunc, 56 }) 57 cmd := newPsCommand(cli) 58 cmd.SetArgs(tc.args) 59 for key, value := range tc.flags { 60 cmd.Flags().Set(key, value) 61 } 62 cmd.SetOut(ioutil.Discard) 63 assert.Error(t, cmd.Execute(), tc.expectedError) 64 } 65 } 66 67 func TestNodePs(t *testing.T) { 68 testCases := []struct { 69 name string 70 args []string 71 flags map[string]string 72 infoFunc func() (types.Info, error) 73 nodeInspectFunc func() (swarm.Node, []byte, error) 74 taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error) 75 taskInspectFunc func(taskID string) (swarm.Task, []byte, error) 76 serviceInspectFunc func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []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 serviceInspectFunc: func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error) { 96 return swarm.Service{ 97 ID: serviceID, 98 Spec: swarm.ServiceSpec{ 99 Annotations: swarm.Annotations{ 100 Name: serviceID, 101 }, 102 }, 103 }, []byte{}, nil 104 }, 105 }, 106 { 107 name: "with-errors", 108 args: []string{"nodeID"}, 109 nodeInspectFunc: func() (swarm.Node, []byte, error) { 110 return *Node(), []byte{}, nil 111 }, 112 taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { 113 return []swarm.Task{ 114 *Task(TaskID("taskID1"), TaskServiceID("failure"), 115 WithStatus(Timestamp(time.Now().Add(-2*time.Hour)), StatusErr("a task error"))), 116 *Task(TaskID("taskID2"), TaskServiceID("failure"), 117 WithStatus(Timestamp(time.Now().Add(-3*time.Hour)), StatusErr("a task error"))), 118 *Task(TaskID("taskID3"), TaskServiceID("failure"), 119 WithStatus(Timestamp(time.Now().Add(-4*time.Hour)), StatusErr("a task error"))), 120 }, nil 121 }, 122 serviceInspectFunc: func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error) { 123 return swarm.Service{ 124 ID: serviceID, 125 Spec: swarm.ServiceSpec{ 126 Annotations: swarm.Annotations{ 127 Name: serviceID, 128 }, 129 }, 130 }, []byte{}, nil 131 }, 132 }, 133 } 134 for _, tc := range testCases { 135 cli := test.NewFakeCli(&fakeClient{ 136 infoFunc: tc.infoFunc, 137 nodeInspectFunc: tc.nodeInspectFunc, 138 taskInspectFunc: tc.taskInspectFunc, 139 taskListFunc: tc.taskListFunc, 140 serviceInspectFunc: tc.serviceInspectFunc, 141 }) 142 cmd := newPsCommand(cli) 143 cmd.SetArgs(tc.args) 144 for key, value := range tc.flags { 145 cmd.Flags().Set(key, value) 146 } 147 assert.NilError(t, cmd.Execute()) 148 golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("node-ps.%s.golden", tc.name)) 149 } 150 }