-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
monstore_test.go
269 lines (234 loc) · 9.31 KB
/
monstore_test.go
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
Copyright 2019 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"reflect"
"strings"
"testing"
"time"
"github.com/pkg/errors"
"github.com/rook/rook/pkg/clusterd"
"github.com/rook/rook/pkg/daemon/ceph/client"
testop "github.com/rook/rook/pkg/operator/test"
exectest "github.com/rook/rook/pkg/util/exec/test"
"github.com/stretchr/testify/assert"
)
func TestMonStore_Set(t *testing.T) {
executor := &exectest.MockExecutor{}
clientset := testop.New(t, 1)
ctx := &clusterd.Context{
Clientset: clientset,
Executor: executor,
}
// create a mock command runner which creates a simple string of the command it ran, and allow
// us to cause it to return an error when it detects a keyword.
execedCmd := ""
execInjectErr := false
<<<<<<< HEAD
executor.MockExecuteCommandWithOutputFile =
func(command string, outfile string, args ...string) (string, error) {
=======
executor.MockExecuteCommandWithTimeout =
func(timeout time.Duration, command string, args ...string) (string, error) {
>>>>>>> 8da68bfb7 (mon: run ceph commands to mon with timeout)
execedCmd = command + " " + strings.Join(args, " ")
if execInjectErr {
return "output from cmd with error", errors.New("mocked error")
}
return "", nil
}
monStore := GetMonStore(ctx, &client.ClusterInfo{Namespace: "ns"})
// setting with spaces converts to underscores
e := monStore.Set("global", "debug ms", "10")
assert.NoError(t, e)
assert.Contains(t, execedCmd, "config set global debug_ms 10")
// setting with dashes converts to underscores
e = monStore.Set("osd.0", "debug-osd", "20")
assert.NoError(t, e)
assert.Contains(t, execedCmd, " config set osd.0 debug_osd 20 ")
// setting with underscores stays the same
e = monStore.Set("mds.*", "debug_mds", "15")
assert.NoError(t, e)
assert.Contains(t, execedCmd, " config set mds.* debug_mds 15 ")
// errors returned as expected
execInjectErr = true
e = monStore.Set("mon.*", "unknown_setting", "10")
assert.Error(t, e)
assert.Contains(t, execedCmd, " config set mon.* unknown_setting 10 ")
}
func TestMonStore_Delete(t *testing.T) {
executor := &exectest.MockExecutor{}
clientset := testop.New(t, 1)
ctx := &clusterd.Context{
Clientset: clientset,
Executor: executor,
}
// create a mock command runner which creates a simple string of the command it ran, and allow
// us to cause it to return an error when it detects a keyword.
execedCmd := ""
execInjectErr := false
<<<<<<< HEAD
executor.MockExecuteCommandWithOutputFile =
func(command string, outfile string, args ...string) (string, error) {
=======
executor.MockExecuteCommandWithTimeout =
func(timeout time.Duration, command string, args ...string) (string, error) {
>>>>>>> 8da68bfb7 (mon: run ceph commands to mon with timeout)
execedCmd = command + " " + strings.Join(args, " ")
if execInjectErr {
return "output from cmd with error", errors.New("mocked error")
}
return "", nil
}
monStore := GetMonStore(ctx, &client.ClusterInfo{Namespace: "ns"})
// ceph config rm called as expected
e := monStore.Delete("global", "debug ms")
assert.NoError(t, e)
assert.Contains(t, execedCmd, "config rm global debug_ms")
// errors returned as expected
execInjectErr = true
e = monStore.Delete("mon.*", "unknown_setting")
assert.Error(t, e)
assert.Contains(t, execedCmd, " config rm mon.* unknown_setting ")
}
func TestMonStore_GetDaemon(t *testing.T) {
executor := &exectest.MockExecutor{}
clientset := testop.New(t, 1)
ctx := &clusterd.Context{
Clientset: clientset,
Executor: executor,
}
// create a mock command runner which creates a simple string of the command it ran, and allow
// us to cause it to return an error when it detects a keyword and to return a specific string
execedCmd := ""
execReturn := "{\"rbd_default_features\":{\"value\":\"3\",\"section\":\"global\",\"mask\":{}," +
"\"can_update_at_runtime\":true}," +
"\"rgw_enable_usage_log\":{\"value\":\"true\",\"section\":\"client.rgw.test.a\",\"mask\":{}," +
"\"can_update_at_runtime\":true}}"
execInjectErr := false
<<<<<<< HEAD
executor.MockExecuteCommandWithOutputFile =
func(command string, outfile string, args ...string) (string, error) {
=======
executor.MockExecuteCommandWithTimeout =
func(timeout time.Duration, command string, args ...string) (string, error) {
>>>>>>> 8da68bfb7 (mon: run ceph commands to mon with timeout)
execedCmd = command + " " + strings.Join(args, " ")
if execInjectErr {
return "output from cmd with error", errors.New("mocked error")
}
return execReturn, nil
}
monStore := GetMonStore(ctx, &client.ClusterInfo{Namespace: "ns"})
// ceph config get called as expected
options, e := monStore.GetDaemon("client.rgw.test.a")
assert.NoError(t, e)
assert.Contains(t, execedCmd, "ceph config get client.rgw.test.a")
assert.True(t, reflect.DeepEqual(options, []Option{{"client.rgw.test.a", "rgw_enable_usage_log", "true"}}))
// json parse exception return as expected
execReturn = "bad json output"
_, e = monStore.GetDaemon("client.rgw.test.a")
assert.Error(t, e)
assert.Contains(t, e.Error(), "failed to parse json config for daemon \"client.rgw.test.a\". json: "+
"bad json output")
// errors returned as expected
execInjectErr = true
_, e = monStore.GetDaemon("mon.*")
assert.Error(t, e)
assert.Contains(t, execedCmd, " config get mon.* ")
}
func TestMonStore_DeleteDaemon(t *testing.T) {
executor := &exectest.MockExecutor{}
clientset := testop.New(t, 1)
ctx := &clusterd.Context{
Clientset: clientset,
Executor: executor,
}
// create a mock command runner which creates a simple string of the command it ran, and allow
// us to cause it to return an error when it detects a keyword and to return a specific string
execedCmd := ""
execReturn := "{\"rbd_default_features\":{\"value\":\"3\",\"section\":\"global\",\"mask\":{}," +
"\"can_update_at_runtime\":true}," +
"\"rgw_enable_usage_log\":{\"value\":\"true\",\"section\":\"client.rgw.test.a\",\"mask\":{}," +
"\"can_update_at_runtime\":true}}"
<<<<<<< HEAD
executor.MockExecuteCommandWithOutputFile =
func(command string, outfile string, args ...string) (string, error) {
=======
executor.MockExecuteCommandWithTimeout =
func(timeout time.Duration, command string, args ...string) (string, error) {
>>>>>>> 8da68bfb7 (mon: run ceph commands to mon with timeout)
execedCmd = command + " " + strings.Join(args, " ")
return execReturn, nil
}
monStore := GetMonStore(ctx, &client.ClusterInfo{Namespace: "ns"})
// ceph config rm rgw_enable_usage_log called as expected
e := monStore.DeleteDaemon("client.rgw.test.a")
assert.NoError(t, e)
assert.Contains(t, execedCmd, "ceph config rm client.rgw.test.a rgw_enable_usage_log")
}
func TestMonStore_SetAll(t *testing.T) {
clientset := testop.New(t, 1)
executor := &exectest.MockExecutor{}
ctx := &clusterd.Context{
Clientset: clientset,
Executor: executor,
}
// create a mock command runner which creates a simple string of the command it ran, and allow
// us to cause it to return an error when it detects a keyword.
execedCmds := []string{}
execInjectErrOnKeyword := "donotinjectanerror"
<<<<<<< HEAD
executor.MockExecuteCommandWithOutputFile =
func(command string, outfile string, args ...string) (string, error) {
=======
executor.MockExecuteCommandWithTimeout =
func(timeout time.Duration, command string, args ...string) (string, error) {
>>>>>>> 8da68bfb7 (mon: run ceph commands to mon with timeout)
execedCmd := command + " " + strings.Join(args, " ")
execedCmds = append(execedCmds, execedCmd)
k := execInjectErrOnKeyword
if strings.Contains(execedCmd, k) {
return "output from cmd with error on keyword: " + k, errors.Errorf("mocked error on keyword: " + k)
}
return "", nil
}
monStore := GetMonStore(ctx, &client.ClusterInfo{Namespace: "ns"})
cfgOverrides := []Option{
configOverride("global", "debug ms", "10"), // setting w/ spaces converts to underscores
configOverride("osd.0", "debug-osd", "20"), // setting w/ dashes converts to underscores
configOverride("mds.*", "debug_mds", "15"), // setting w/ underscores remains the same
}
// commands w/ no error
e := monStore.SetAll(cfgOverrides...)
assert.NoError(t, e)
assert.Len(t, execedCmds, 3)
assert.Contains(t, execedCmds[0], " global debug_ms 10 ")
assert.Contains(t, execedCmds[1], " osd.0 debug_osd 20 ")
assert.Contains(t, execedCmds[2], " mds.* debug_mds 15 ")
// commands w/ one error
// keep cfgOverrides from last test
execInjectErrOnKeyword = "debug_osd"
execedCmds = execedCmds[:0] // empty execedCmds slice
e = monStore.SetAll(cfgOverrides...)
assert.Error(t, e)
// Rook should not return error before trying to set all config overrides
assert.Len(t, execedCmds, 3)
// all commands return error
// keep cfgOverrides
execInjectErrOnKeyword = "debug"
execedCmds = execedCmds[:0]
e = monStore.SetAll(cfgOverrides...)
assert.Error(t, e)
assert.Len(t, execedCmds, 3)
}