21
21
package namespace
22
22
23
23
import (
24
+ "encoding/json"
24
25
"testing"
25
26
"time"
26
27
27
28
myspec "github.com/m3db/m3db-operator/pkg/apis/m3dboperator/v1alpha1"
28
29
30
+ pbtypes "github.com/gogo/protobuf/types"
29
31
m3ns "github.com/m3db/m3/src/dbnode/generated/proto/namespace"
30
32
"github.com/m3db/m3/src/query/generated/proto/admin"
31
-
32
33
"github.com/stretchr/testify/assert"
33
34
"github.com/stretchr/testify/require"
34
35
)
@@ -40,29 +41,25 @@ func TestRequestFromSpec(t *testing.T) {
40
41
require .NoError (t , err )
41
42
42
43
tests := []struct {
44
+ name string
43
45
ns myspec.Namespace
44
46
req * admin.NamespaceAddRequest
45
47
expErr bool
46
48
}{
47
49
{
50
+ name : "no fields" ,
48
51
ns : myspec.Namespace {},
49
52
expErr : true ,
50
53
},
51
54
{
55
+ name : "only name set" ,
52
56
ns : myspec.Namespace {
53
57
Name : "empty" ,
54
58
},
55
59
expErr : true ,
56
60
},
57
61
{
58
- ns : myspec.Namespace {
59
- Name : "badpreset" ,
60
- Preset : "a" ,
61
- Options : & myspec.NamespaceOptions {},
62
- },
63
- expErr : true ,
64
- },
65
- {
62
+ name : "valid custom" ,
66
63
ns : myspec.Namespace {
67
64
Name : "validcustom" ,
68
65
Options : & myspec.NamespaceOptions {
@@ -111,6 +108,7 @@ func TestRequestFromSpec(t *testing.T) {
111
108
},
112
109
},
113
110
{
111
+ name : "AggregatedOptions" ,
114
112
ns : myspec.Namespace {
115
113
Name : "aggregated" ,
116
114
Options : & myspec.NamespaceOptions {
@@ -170,6 +168,72 @@ func TestRequestFromSpec(t *testing.T) {
170
168
},
171
169
},
172
170
{
171
+ name : "ExtendedOptions" ,
172
+ ns : myspec.Namespace {
173
+ Name : "extended" ,
174
+ Options : & myspec.NamespaceOptions {
175
+ BootstrapEnabled : true ,
176
+ WritesToCommitLog : false ,
177
+ RetentionOptions : myspec.RetentionOptions {
178
+ RetentionPeriod : "1s" ,
179
+ BlockSize : "1s" ,
180
+ BufferFuture : "1s" ,
181
+ BufferPast : "1s" ,
182
+ BlockDataExpiry : true ,
183
+ BlockDataExpiryAfterNotAccessPeriod : "1s" ,
184
+ },
185
+ IndexOptions : myspec.IndexOptions {
186
+ BlockSize : "1s" ,
187
+ Enabled : true ,
188
+ },
189
+ ExtendedOptions : & myspec.ExtendedOptions {
190
+ Type : "testOpts" ,
191
+ Options : map [string ]json.RawMessage {
192
+ "key1" : json .RawMessage (`"str"` ),
193
+ "key2" : json .RawMessage (`123` ),
194
+ "key3" : json .RawMessage (`{
195
+ "subKey1": "foo",
196
+ "subKey2": "bar"
197
+ }` ),
198
+ },
199
+ },
200
+ },
201
+ },
202
+ req : & admin.NamespaceAddRequest {
203
+ Name : "extended" ,
204
+ Options : & m3ns.NamespaceOptions {
205
+ BootstrapEnabled : true ,
206
+ WritesToCommitLog : false ,
207
+ RetentionOptions : & m3ns.RetentionOptions {
208
+ RetentionPeriodNanos : 1000000000 ,
209
+ BlockSizeNanos : 1000000000 ,
210
+ BufferFutureNanos : 1000000000 ,
211
+ BufferPastNanos : 1000000000 ,
212
+ BlockDataExpiry : true ,
213
+ BlockDataExpiryAfterNotAccessPeriodNanos : 1000000000 ,
214
+ },
215
+ IndexOptions : & m3ns.IndexOptions {
216
+ BlockSizeNanos : 1000000000 ,
217
+ Enabled : true ,
218
+ },
219
+ ExtendedOptions : & m3ns.ExtendedOptions {
220
+ Type : "testOpts" ,
221
+ Options : & pbtypes.Struct {Fields : map [string ]* pbtypes.Value {
222
+ "key1" : {Kind : & pbtypes.Value_StringValue {StringValue : "str" }},
223
+ "key2" : {Kind : & pbtypes.Value_NumberValue {NumberValue : 123 }},
224
+ "key3" : {Kind : & pbtypes.Value_StructValue {StructValue : & pbtypes.Struct {
225
+ Fields : map [string ]* pbtypes.Value {
226
+ "subKey1" : {Kind : & pbtypes.Value_StringValue {StringValue : "foo" }},
227
+ "subKey2" : {Kind : & pbtypes.Value_StringValue {StringValue : "bar" }},
228
+ },
229
+ }}},
230
+ }},
231
+ },
232
+ },
233
+ },
234
+ },
235
+ {
236
+ name : "invalid custom" ,
173
237
ns : myspec.Namespace {
174
238
Name : "invalidcustom" ,
175
239
Options : & myspec.NamespaceOptions {
@@ -197,13 +261,24 @@ func TestRequestFromSpec(t *testing.T) {
197
261
expErr : true ,
198
262
},
199
263
{
264
+ name : "bad preset 1" ,
265
+ ns : myspec.Namespace {
266
+ Name : "badpreset" ,
267
+ Preset : "a" ,
268
+ Options : & myspec.NamespaceOptions {},
269
+ },
270
+ expErr : true ,
271
+ },
272
+ {
273
+ name : "bad preset 2" ,
200
274
ns : myspec.Namespace {
201
275
Name : "foo" ,
202
276
Preset : "a" ,
203
277
},
204
278
expErr : true ,
205
279
},
206
280
{
281
+ name : "preset 10s:2d" ,
207
282
ns : myspec.Namespace {
208
283
Name : "foo" ,
209
284
Preset : "10s:2d" ,
@@ -214,6 +289,7 @@ func TestRequestFromSpec(t *testing.T) {
214
289
},
215
290
},
216
291
{
292
+ name : "preset 1m:40d" ,
217
293
ns : myspec.Namespace {
218
294
Name : "foo" ,
219
295
Preset : "1m:40d" ,
@@ -226,24 +302,26 @@ func TestRequestFromSpec(t *testing.T) {
226
302
}
227
303
228
304
for _ , test := range tests {
229
- req , err := RequestFromSpec (test .ns )
230
- if test .expErr {
231
- assert .Error (t , err )
232
- } else {
233
- assert .NoError (t , err )
234
- assert .Equal (t , test .req , req )
235
- }
305
+ t .Run (test .name , func (t * testing.T ) {
306
+ req , err := RequestFromSpec (test .ns )
307
+ if test .expErr {
308
+ assert .Error (t , err )
309
+ } else {
310
+ assert .NoError (t , err )
311
+ assert .Equal (t , test .req , req )
312
+ }
313
+ })
236
314
}
237
315
}
238
316
239
317
func TestRetentionOptsFromAPI (t * testing.T ) {
240
318
opts := myspec.RetentionOptions {
241
- RetentionPeriod : time .Duration ( time . Second ) .String (),
242
- BlockSize : time . Duration (2 * time .Second ).String (),
243
- BufferFuture : time . Duration (3 * time .Second ).String (),
244
- BufferPast : time . Duration (4 * time .Second ).String (),
319
+ RetentionPeriod : time .Second .String (),
320
+ BlockSize : (2 * time .Second ).String (),
321
+ BufferFuture : (3 * time .Second ).String (),
322
+ BufferPast : (4 * time .Second ).String (),
245
323
BlockDataExpiry : true ,
246
- BlockDataExpiryAfterNotAccessPeriod : time . Duration (5 * time .Second ).String (),
324
+ BlockDataExpiryAfterNotAccessPeriod : (5 * time .Second ).String (),
247
325
}
248
326
249
327
nsOpts , err := m3dbRetentionOptsFromSpec (opts )
0 commit comments