forked from docker/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathps_test.go
More file actions
154 lines (150 loc) · 4.91 KB
/
Copy pathps_test.go
File metadata and controls
154 lines (150 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package node
import (
"context"
"errors"
"fmt"
"io"
"testing"
"time"
"github.com/docker/cli/internal/test"
"github.com/docker/cli/internal/test/builders"
"github.com/moby/moby/api/types/swarm"
"github.com/moby/moby/api/types/system"
"github.com/moby/moby/client"
"gotest.tools/v3/assert"
"gotest.tools/v3/golden"
)
func TestNodePsErrors(t *testing.T) {
testCases := []struct {
args []string
flags map[string]string
infoFunc func() (system.Info, error)
nodeInspectFunc func() (swarm.Node, []byte, error)
taskListFunc func(options client.TaskListOptions) ([]swarm.Task, error)
taskInspectFunc func(taskID string) (swarm.Task, []byte, error)
expectedError string
}{
{
infoFunc: func() (system.Info, error) {
return system.Info{}, errors.New("error asking for node info")
},
expectedError: "error asking for node info",
},
{
args: []string{"nodeID"},
nodeInspectFunc: func() (swarm.Node, []byte, error) {
return swarm.Node{}, []byte{}, errors.New("error inspecting the node")
},
expectedError: "error inspecting the node",
},
{
args: []string{"nodeID"},
taskListFunc: func(options client.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{}, errors.New("error returning the task list")
},
expectedError: "error returning the task list",
},
}
for _, tc := range testCases {
cli := test.NewFakeCli(&fakeClient{
infoFunc: tc.infoFunc,
nodeInspectFunc: tc.nodeInspectFunc,
taskInspectFunc: tc.taskInspectFunc,
taskListFunc: tc.taskListFunc,
})
cmd := newPsCommand(cli)
cmd.SetArgs(tc.args)
for key, value := range tc.flags {
assert.Check(t, cmd.Flags().Set(key, value))
}
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
assert.Error(t, cmd.Execute(), tc.expectedError)
}
}
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 client.TaskListOptions) ([]swarm.Task, error)
taskInspectFunc func(taskID string) (swarm.Task, []byte, error)
serviceInspectFunc func(ctx context.Context, serviceID string, opts client.ServiceInspectOptions) (swarm.Service, []byte, error)
}{
{
name: "simple",
args: []string{"nodeID"},
nodeInspectFunc: func() (swarm.Node, []byte, error) {
return *builders.Node(), []byte{}, nil
},
taskListFunc: func(options client.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{
*builders.Task(builders.WithStatus(builders.Timestamp(time.Now().Add(-2*time.Hour)), builders.PortStatus([]swarm.PortConfig{
{
TargetPort: 80,
PublishedPort: 80,
Protocol: "tcp",
},
}))),
}, nil
},
serviceInspectFunc: func(ctx context.Context, serviceID string, opts client.ServiceInspectOptions) (swarm.Service, []byte, error) {
return swarm.Service{
ID: serviceID,
Spec: swarm.ServiceSpec{
Annotations: swarm.Annotations{
Name: serviceID,
},
},
}, []byte{}, nil
},
},
{
name: "with-errors",
args: []string{"nodeID"},
nodeInspectFunc: func() (swarm.Node, []byte, error) {
return *builders.Node(), []byte{}, nil
},
taskListFunc: func(options client.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{
*builders.Task(builders.TaskID("taskID1"), builders.TaskServiceID("failure"),
builders.WithStatus(builders.Timestamp(time.Now().Add(-2*time.Hour)), builders.StatusErr("a task error"))),
*builders.Task(builders.TaskID("taskID2"), builders.TaskServiceID("failure"),
builders.WithStatus(builders.Timestamp(time.Now().Add(-3*time.Hour)), builders.StatusErr("a task error"))),
*builders.Task(builders.TaskID("taskID3"), builders.TaskServiceID("failure"),
builders.WithStatus(builders.Timestamp(time.Now().Add(-4*time.Hour)), builders.StatusErr("a task error"))),
}, nil
},
serviceInspectFunc: func(ctx context.Context, serviceID string, opts client.ServiceInspectOptions) (swarm.Service, []byte, error) {
return swarm.Service{
ID: serviceID,
Spec: swarm.ServiceSpec{
Annotations: swarm.Annotations{
Name: serviceID,
},
},
}, []byte{}, nil
},
},
}
for _, tc := range testCases {
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,
})
cmd := newPsCommand(cli)
cmd.SetArgs(tc.args)
for key, value := range tc.flags {
assert.Check(t, cmd.Flags().Set(key, value))
}
assert.NilError(t, cmd.Execute())
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("node-ps.%s.golden", tc.name))
})
}
}