-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathps_test.go
More file actions
134 lines (117 loc) · 3.93 KB
/
ps_test.go
File metadata and controls
134 lines (117 loc) · 3.93 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
package service
import (
"context"
"testing"
"github.com/docker/cli/internal/test"
"github.com/docker/cli/opts"
"github.com/moby/moby/api/types/swarm"
"github.com/moby/moby/api/types/system"
"github.com/moby/moby/client"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestCreateFilter(t *testing.T) {
apiClient := &fakeClient{
serviceListFunc: func(ctx context.Context, options client.ServiceListOptions) (client.ServiceListResult, error) {
return client.ServiceListResult{
Items: []swarm.Service{
{ID: "idmatch"},
{ID: "idprefixmatch"},
newService("cccccccc", "namematch"),
newService("01010101", "notfoundprefix"),
},
}, nil
},
}
filter := opts.NewFilterOpt()
assert.NilError(t, filter.Set("node=somenode"))
options := psOptions{
services: []string{"idmatch", "idprefix", "namematch", "notfound"},
filter: filter,
}
actual, notfound, err := createFilter(context.Background(), apiClient, options)
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(notfound, []string{"no such service: notfound"}))
expected := make(client.Filters).Add("service", "idmatch").Add("service", "idprefixmatch").Add("service", "cccccccc").Add("node", "somenode")
assert.DeepEqual(t, expected, actual)
}
func TestCreateFilterWithAmbiguousIDPrefixError(t *testing.T) {
apiClient := &fakeClient{
serviceListFunc: func(ctx context.Context, options client.ServiceListOptions) (client.ServiceListResult, error) {
return client.ServiceListResult{
Items: []swarm.Service{
{ID: "aaaone"},
{ID: "aaatwo"},
},
}, nil
},
}
_, _, err := createFilter(context.Background(), apiClient, psOptions{
services: []string{"aaa"},
filter: opts.NewFilterOpt(),
})
assert.Error(t, err, "multiple services found with provided prefix: aaa")
}
func TestCreateFilterNoneFound(t *testing.T) {
apiClient := &fakeClient{}
options := psOptions{
services: []string{"foo", "notfound"},
filter: opts.NewFilterOpt(),
}
_, _, err := createFilter(context.Background(), apiClient, options)
assert.Error(t, err, "no such service: foo\nno such service: notfound")
}
func TestRunPSWarnsOnNotFound(t *testing.T) {
apiClient := &fakeClient{
serviceListFunc: func(ctx context.Context, options client.ServiceListOptions) (client.ServiceListResult, error) {
return client.ServiceListResult{
Items: []swarm.Service{{ID: "foo"}},
}, nil
},
}
cli := test.NewFakeCli(apiClient)
options := psOptions{
services: []string{"foo", "bar"},
filter: opts.NewFilterOpt(),
format: "{{.ID}}",
}
ctx := context.Background()
err := runPS(ctx, cli, options)
assert.Error(t, err, "no such service: bar")
}
func TestRunPSQuiet(t *testing.T) {
apiClient := &fakeClient{
serviceListFunc: func(ctx context.Context, options client.ServiceListOptions) (client.ServiceListResult, error) {
return client.ServiceListResult{
Items: []swarm.Service{{ID: "foo"}},
}, nil
},
taskListFunc: func(ctx context.Context, options client.TaskListOptions) (client.TaskListResult, error) {
return client.TaskListResult{
Items: []swarm.Task{{ID: "sxabyp0obqokwekpun4rjo0b3"}},
}, nil
},
}
cli := test.NewFakeCli(apiClient)
ctx := context.Background()
err := runPS(ctx, cli, psOptions{services: []string{"foo"}, quiet: true, filter: opts.NewFilterOpt()})
assert.NilError(t, err)
assert.Check(t, is.Equal("sxabyp0obqokwekpun4rjo0b3\n", cli.OutBuffer().String()))
}
func TestUpdateNodeFilter(t *testing.T) {
selfNodeID := "foofoo"
filter := make(client.Filters).Add("node", "one", "two", "self")
apiClient := &fakeClient{
infoFunc: func(_ context.Context) (client.SystemInfoResult, error) {
return client.SystemInfoResult{
Info: system.Info{
Swarm: swarm.Info{NodeID: selfNodeID},
},
}, nil
},
}
err := updateNodeFilter(context.Background(), apiClient, filter)
assert.NilError(t, err)
expected := make(client.Filters).Add("node", "one", "two", selfNodeID)
assert.DeepEqual(t, expected, filter)
}