Skip to content

Commit

Permalink
Fixes duplicate entries in docker node ps when multi nodes are passed…
Browse files Browse the repository at this point in the history
…. For example, 'docker ps node node1 node2'.

Signed-off-by: Ehsan Salamati <[email protected]>
  • Loading branch information
ehsan-salamati committed Aug 2, 2024
1 parent ddd4c39 commit 94b0eb8
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 23 deletions.
24 changes: 15 additions & 9 deletions cli/command/node/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,26 @@ import (

type fakeClient struct {
client.Client
infoFunc func() (system.Info, error)
nodeInspectFunc func() (swarm.Node, []byte, error)
nodeListFunc func() ([]swarm.Node, error)
nodeRemoveFunc func() error
nodeUpdateFunc func(nodeID string, version swarm.Version, node swarm.NodeSpec) error
taskInspectFunc func(taskID string) (swarm.Task, []byte, error)
taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error)
serviceInspectFunc func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error)
infoFunc func() (system.Info, error)
nodeInspectFunc func() (swarm.Node, []byte, error)
nodeInspectFuncWithArgs func(string) (swarm.Node, []byte, error)
nodeListFunc func() ([]swarm.Node, error)
nodeRemoveFunc func() error
nodeUpdateFunc func(nodeID string, version swarm.Version, node swarm.NodeSpec) error
taskInspectFunc func(taskID string) (swarm.Task, []byte, error)
taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error)
serviceInspectFunc func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error)
}

func (cli *fakeClient) NodeInspectWithRaw(context.Context, string) (swarm.Node, []byte, error) {
func (cli *fakeClient) NodeInspectWithRaw(ctx context.Context, nodeRef string) (swarm.Node, []byte, error) {
if cli.nodeInspectFunc != nil {
return cli.nodeInspectFunc()
}

if cli.nodeInspectFuncWithArgs != nil {
return cli.nodeInspectFuncWithArgs(nodeRef)
}

return swarm.Node{}, []byte{}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion cli/command/node/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func runPs(ctx context.Context, dockerCli command.Cli, options psOptions) error
continue
}

filter := options.filter.Value()
filter := options.filter.Value().Clone()
filter.Add("node", node.ID)

nodeTasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter})
Expand Down
74 changes: 61 additions & 13 deletions cli/command/node/ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ func TestNodePsErrors(t *testing.T) {

func TestNodePs(t *testing.T) {
testCases := []struct {
name string
args []string
flags map[string]string
infoFunc func() (system.Info, error)
nodeInspectFunc func() (swarm.Node, []byte, error)
taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error)
taskInspectFunc func(taskID string) (swarm.Task, []byte, error)
serviceInspectFunc func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error)
name string
args []string
flags map[string]string
infoFunc func() (system.Info, error)
nodeInspectFunc func() (swarm.Node, []byte, error)
nodeInspectFuncWithArgs func(string) (swarm.Node, []byte, error)
taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error)
taskInspectFunc func(taskID string) (swarm.Task, []byte, error)
serviceInspectFunc func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error)
}{
{
name: "simple",
Expand Down Expand Up @@ -132,17 +133,64 @@ func TestNodePs(t *testing.T) {
}, []byte{}, nil
},
},
{
name: "multiple-nodes-no-duplicates",
args: []string{"nodeID1", "nodeID2"},
nodeInspectFuncWithArgs: func(nodeRef string) (swarm.Node, []byte, error) {
switch nodeRef {
case "nodeID1":
return *builders.Node(builders.NodeID("nodeID1")), []byte{}, nil
case "nodeID2":
return *builders.Node(builders.NodeID("nodeID2")), []byte{}, nil
default:
return swarm.Node{}, []byte{}, errors.Errorf("unexpected nodeRef %q", nodeRef)
}
},
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
nodeFilter := options.Filters.Get("node")[0]
switch nodeFilter {
case "nodeID1":
return []swarm.Task{
*builders.Task(builders.TaskID("taskID1"), builders.TaskServiceID("service1"), builders.TaskNodeID("nodeID1"),
builders.WithStatus(builders.Timestamp(time.Now().Add(-2*time.Hour)), builders.TaskState(swarm.TaskStateRunning))),
*builders.Task(builders.TaskID("taskID2"), builders.TaskServiceID("service2"), builders.TaskNodeID("nodeID1"),
builders.WithStatus(builders.Timestamp(time.Now().Add(-3*time.Hour)), builders.TaskState(swarm.TaskStateRunning))),
}, nil
case "nodeID2":
return []swarm.Task{
*builders.Task(builders.TaskID("taskID3"), builders.TaskServiceID("service3"), builders.TaskNodeID("nodeID2"),
builders.WithStatus(builders.Timestamp(time.Now().Add(-2*time.Hour)), builders.TaskState(swarm.TaskStateRunning))),
*builders.Task(builders.TaskID("taskID4"), builders.TaskServiceID("service4"), builders.TaskNodeID("nodeID2"),
builders.WithStatus(builders.Timestamp(time.Now().Add(-3*time.Hour)), builders.TaskState(swarm.TaskStateRunning))),
}, nil
default:
return []swarm.Task{}, nil
}
},
serviceInspectFunc: func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error) {
return swarm.Service{
ID: serviceID,
Spec: swarm.ServiceSpec{
Annotations: swarm.Annotations{
Name: serviceID,
},
},
}, []byte{}, nil
},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
infoFunc: tc.infoFunc,
nodeInspectFunc: tc.nodeInspectFunc,
taskInspectFunc: tc.taskInspectFunc,
taskListFunc: tc.taskListFunc,
serviceInspectFunc: tc.serviceInspectFunc,
infoFunc: tc.infoFunc,
nodeInspectFunc: tc.nodeInspectFunc,
nodeInspectFuncWithArgs: tc.nodeInspectFuncWithArgs,
taskInspectFunc: tc.taskInspectFunc,
taskListFunc: tc.taskListFunc,
serviceInspectFunc: tc.serviceInspectFunc,
})

cmd := newPsCommand(cli)
cmd.SetArgs(tc.args)
for key, value := range tc.flags {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
taskID1 service1.1 myimage:mytag defaultNodeName Ready Running 2 hours ago
taskID2 service2.1 myimage:mytag defaultNodeName Ready Running 3 hours ago
taskID3 service3.1 myimage:mytag defaultNodeName Ready Running 2 hours ago
taskID4 service4.1 myimage:mytag defaultNodeName Ready Running 3 hours ago

0 comments on commit 94b0eb8

Please sign in to comment.