forked from docker/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_test.go
More file actions
183 lines (179 loc) · 5.54 KB
/
Copy pathupdate_test.go
File metadata and controls
183 lines (179 loc) · 5.54 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package node
import (
"errors"
"fmt"
"io"
"testing"
"github.com/docker/cli/internal/test"
"github.com/docker/cli/internal/test/builders"
"github.com/moby/moby/api/types/swarm"
"github.com/moby/moby/client"
"gotest.tools/v3/assert"
)
func TestNodeUpdateErrors(t *testing.T) {
testCases := []struct {
args []string
flags map[string]string
nodeInspectFunc func() (client.NodeInspectResult, error)
nodeUpdateFunc func(nodeID string, options client.NodeUpdateOptions) (client.NodeUpdateResult, error)
expectedError string
}{
{
expectedError: "requires 1 argument",
},
{
args: []string{"node1", "node2"},
expectedError: "requires 1 argument",
},
{
args: []string{"nodeID"},
nodeInspectFunc: func() (client.NodeInspectResult, error) {
return client.NodeInspectResult{}, errors.New("error inspecting the node")
},
expectedError: "error inspecting the node",
},
{
args: []string{"nodeID"},
nodeUpdateFunc: func(nodeID string, options client.NodeUpdateOptions) (client.NodeUpdateResult, error) {
return client.NodeUpdateResult{}, errors.New("error updating the node")
},
expectedError: "error updating the node",
},
{
args: []string{"nodeID"},
nodeInspectFunc: func() (client.NodeInspectResult, error) {
return client.NodeInspectResult{
Node: *builders.Node(builders.NodeLabels(map[string]string{
"key": "value",
})),
}, nil
},
flags: map[string]string{
"label-rm": "not-present",
},
expectedError: "key not-present doesn't exist in node's labels",
},
}
for _, tc := range testCases {
cmd := newUpdateCommand(
test.NewFakeCli(&fakeClient{
nodeInspectFunc: tc.nodeInspectFunc,
nodeUpdateFunc: tc.nodeUpdateFunc,
}))
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.ErrorContains(t, cmd.Execute(), tc.expectedError)
}
}
func TestNodeUpdate(t *testing.T) {
testCases := []struct {
args []string
flags map[string]string
nodeInspectFunc func() (client.NodeInspectResult, error)
nodeUpdateFunc func(nodeID string, options client.NodeUpdateOptions) (client.NodeUpdateResult, error)
}{
{
args: []string{"nodeID"},
flags: map[string]string{
"role": "manager",
},
nodeInspectFunc: func() (client.NodeInspectResult, error) {
return client.NodeInspectResult{
Node: *builders.Node(),
}, nil
},
nodeUpdateFunc: func(nodeID string, options client.NodeUpdateOptions) (client.NodeUpdateResult, error) {
if options.Spec.Role != swarm.NodeRoleManager {
return client.NodeUpdateResult{}, errors.New("expected role manager, got " + string(options.Spec.Role))
}
return client.NodeUpdateResult{}, nil
},
},
{
args: []string{"nodeID"},
flags: map[string]string{
"availability": "drain",
},
nodeInspectFunc: func() (client.NodeInspectResult, error) {
return client.NodeInspectResult{
Node: *builders.Node(),
}, nil
},
nodeUpdateFunc: func(nodeID string, options client.NodeUpdateOptions) (client.NodeUpdateResult, error) {
if options.Spec.Availability != swarm.NodeAvailabilityDrain {
return client.NodeUpdateResult{}, errors.New("expected drain availability, got " + string(options.Spec.Availability))
}
return client.NodeUpdateResult{}, nil
},
},
{
args: []string{"nodeID"},
flags: map[string]string{
"label-add": "lbl",
},
nodeInspectFunc: func() (client.NodeInspectResult, error) {
return client.NodeInspectResult{
Node: *builders.Node(),
}, nil
},
nodeUpdateFunc: func(nodeID string, options client.NodeUpdateOptions) (client.NodeUpdateResult, error) {
if _, present := options.Spec.Annotations.Labels["lbl"]; !present {
return client.NodeUpdateResult{}, fmt.Errorf("expected 'lbl' label, got %v", options.Spec.Annotations.Labels)
}
return client.NodeUpdateResult{}, nil
},
},
{
args: []string{"nodeID"},
flags: map[string]string{
"label-add": "key=value",
},
nodeInspectFunc: func() (client.NodeInspectResult, error) {
return client.NodeInspectResult{
Node: *builders.Node(),
}, nil
},
nodeUpdateFunc: func(nodeID string, options client.NodeUpdateOptions) (client.NodeUpdateResult, error) {
if value, present := options.Spec.Annotations.Labels["key"]; !present || value != "value" {
return client.NodeUpdateResult{}, fmt.Errorf("expected 'key' label to be 'value', got %v", options.Spec.Annotations.Labels)
}
return client.NodeUpdateResult{}, nil
},
},
{
args: []string{"nodeID"},
flags: map[string]string{
"label-rm": "key",
},
nodeInspectFunc: func() (client.NodeInspectResult, error) {
return client.NodeInspectResult{
Node: *builders.Node(builders.NodeLabels(map[string]string{
"key": "value",
})),
}, nil
},
nodeUpdateFunc: func(nodeID string, options client.NodeUpdateOptions) (client.NodeUpdateResult, error) {
if len(options.Spec.Annotations.Labels) > 0 {
return client.NodeUpdateResult{}, fmt.Errorf("expected no labels, got %v", options.Spec.Annotations.Labels)
}
return client.NodeUpdateResult{}, nil
},
},
}
for _, tc := range testCases {
cmd := newUpdateCommand(
test.NewFakeCli(&fakeClient{
nodeInspectFunc: tc.nodeInspectFunc,
nodeUpdateFunc: tc.nodeUpdateFunc,
}))
cmd.SetArgs(tc.args)
for key, value := range tc.flags {
assert.Check(t, cmd.Flags().Set(key, value))
}
assert.NilError(t, cmd.Execute())
}
}