-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcreate_test.go
More file actions
515 lines (452 loc) · 15.4 KB
/
Copy pathcreate_test.go
File metadata and controls
515 lines (452 loc) · 15.4 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright 2025 PingCAP, Inc.
//
// 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 export
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"testing"
"github.com/tidbcloud/tidbcloud-cli/internal"
"github.com/tidbcloud/tidbcloud-cli/internal/iostream"
"github.com/tidbcloud/tidbcloud-cli/internal/mock"
"github.com/tidbcloud/tidbcloud-cli/internal/service/cloud"
"github.com/tidbcloud/tidbcloud-cli/pkg/tidbcloud/v1beta1/serverless/export"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type CreateExportSuite struct {
suite.Suite
h *internal.Helper
mockClient *mock.TiDBCloudClient
}
func (suite *CreateExportSuite) SetupTest() {
if err := os.Setenv("NO_COLOR", "true"); err != nil {
suite.T().Error(err)
}
var pageSize int64 = 10
suite.mockClient = new(mock.TiDBCloudClient)
suite.h = &internal.Helper{
Client: func() (cloud.TiDBCloudClient, error) {
return suite.mockClient, nil
},
QueryPageSize: pageSize,
IOStreams: iostream.Test(),
}
}
func (suite *CreateExportSuite) TestCreateExportToLocal() {
ctx := context.Background()
clusterId := "fake-cluster-id"
exportId := "fake-export-id"
suite.mockClient.On("CreateExport", ctx, clusterId, getDefaultCreateExportBody()).
Return(&export.Export{
ExportId: &exportId,
}, nil)
tests := []Test{
{
name: "export all data to local with force",
args: []string{"-c", clusterId, "--force"},
stdoutString: fmt.Sprintf("export %s is running now\n", exportId),
},
{
name: "export all data to local without force",
args: []string{"-c", clusterId},
err: errors.New("the terminal doesn't support prompt, please run with --force to create export"),
},
}
suite.AssertTest(ctx, tests)
}
func (suite *CreateExportSuite) TestCreateExportToS3WithRoleArn() {
ctx := context.Background()
clusterId := "fake-cluster-id"
exportId := "fake-export-id"
targetType := export.EXPORTTARGETTYPEENUM_S3
uri := "s3://fake-bucket/fake-prefix"
roleArn := "arn:aws:iam::123456789012:role/service-role/AmazonS3FullAccess"
body := getDefaultCreateExportBody()
body.Target = &export.ExportTarget{
Type: &targetType,
S3: &export.S3Target{
Uri: &uri,
AuthType: export.EXPORTS3AUTHTYPEENUM_ROLE_ARN,
RoleArn: &roleArn,
},
}
suite.mockClient.On("CreateExport", ctx, clusterId, body).
Return(&export.Export{
ExportId: &exportId,
}, nil)
tests := []Test{
{
name: "export all data to s3 using role arn",
args: []string{"-c", clusterId, "--target-type", "S3", "--s3.uri", uri, "--s3.role-arn", roleArn, "--force"},
stdoutString: fmt.Sprintf("export %s is running now\n", exportId),
},
{
name: "export all data to s3 without uri",
args: []string{"-c", clusterId, "--target-type", "S3", "--s3.role-arn", roleArn, "--force"},
err: errors.New("S3 URI is required when target type is S3"),
},
{
name: "export all data to s3 without auth",
args: []string{"-c", clusterId, "--target-type", "S3", "--s3.uri", uri, "--force"},
err: errors.New("missing S3 auth information, require either role arn or access key id and secret access key"),
},
}
suite.AssertTest(ctx, tests)
}
func (suite *CreateExportSuite) TestCreateExportToS3WithAccessKey() {
ctx := context.Background()
clusterId := "fake-cluster-id"
exportId := "fake-export-id"
targetType := export.EXPORTTARGETTYPEENUM_S3
uri := "s3://fake-bucket/fake-prefix"
accessKeyId := "fake-id"
secretAccess := "fake-secret"
body := getDefaultCreateExportBody()
body.Target = &export.ExportTarget{
Type: &targetType,
S3: &export.S3Target{
Uri: &uri,
AuthType: export.EXPORTS3AUTHTYPEENUM_ACCESS_KEY,
AccessKey: &export.S3TargetAccessKey{
Id: accessKeyId,
Secret: secretAccess,
},
},
}
suite.mockClient.On("CreateExport", ctx, clusterId, body).
Return(&export.Export{
ExportId: &exportId,
}, nil)
tests := []Test{
{
name: "export all data to s3 using access key",
args: []string{"-c", clusterId, "--target-type", "S3", "--s3.uri", uri, "--s3.access-key-id", accessKeyId, "--s3.secret-access-key", secretAccess, "--force"},
stdoutString: fmt.Sprintf("export %s is running now\n", exportId),
},
}
suite.AssertTest(ctx, tests)
}
func (suite *CreateExportSuite) TestCreateExportToGCS() {
ctx := context.Background()
clusterId := "fake-cluster-id"
exportId := "fake-export-id"
targetType := export.EXPORTTARGETTYPEENUM_GCS
uri := "s3://fake-bucket/fake-prefix"
serviceAccountKey := "fake-service-account-key"
body := getDefaultCreateExportBody()
body.Target = &export.ExportTarget{
Type: &targetType,
Gcs: &export.GCSTarget{
Uri: uri,
AuthType: export.EXPORTGCSAUTHTYPEENUM_SERVICE_ACCOUNT_KEY,
ServiceAccountKey: &serviceAccountKey,
},
}
suite.mockClient.On("CreateExport", ctx, clusterId, body).
Return(&export.Export{
ExportId: &exportId,
}, nil)
tests := []Test{
{
name: "export all data to gcs",
args: []string{"-c", clusterId, "--target-type", "GCS", "--gcs.uri", uri, "--gcs.service-account-key", serviceAccountKey, "--force"},
stdoutString: fmt.Sprintf("export %s is running now\n", exportId),
},
{
name: "export all data to gcs without auth",
args: []string{"-c", clusterId, "--target-type", "GCS", "--gcs.uri", uri, "--force"},
err: errors.New("GCS service account key is required when target type is GCS"),
},
{
name: "export all data to gcs without uri",
args: []string{"-c", clusterId, "--target-type", "GCS", "--gcs.service-account-key", serviceAccountKey, "--force"},
err: errors.New("GCS URI is required when target type is GCS"),
},
}
suite.AssertTest(ctx, tests)
}
func (suite *CreateExportSuite) TestCreateExportToAzure() {
ctx := context.Background()
clusterId := "fake-cluster-id"
exportId := "fake-export-id"
targetType := export.EXPORTTARGETTYPEENUM_AZURE_BLOB
uri := "s3://fake-bucket/fake-prefix"
sasToken := "fake-sas-token"
body := getDefaultCreateExportBody()
body.Target = &export.ExportTarget{
Type: &targetType,
AzureBlob: &export.AzureBlobTarget{
Uri: uri,
AuthType: export.EXPORTAZUREBLOBAUTHTYPEENUM_SAS_TOKEN,
SasToken: &sasToken,
},
}
suite.mockClient.On("CreateExport", ctx, clusterId, body).
Return(&export.Export{
ExportId: &exportId,
}, nil)
tests := []Test{
{
name: "export all data to azure blob",
args: []string{"-c", clusterId, "--target-type", "AZURE_BLOB", "--azblob.uri", uri, "--azblob.sas-token", sasToken, "--force"},
stdoutString: fmt.Sprintf("export %s is running now\n", exportId),
},
{
name: "export all data to azure blob without auth",
args: []string{"-c", clusterId, "--target-type", "AZURE_BLOB", "--azblob.uri", uri, "--force"},
err: errors.New("Azure Blob SAS token is required when target type is AZURE_BLOB"),
},
{
name: "export all data to azure blob without uri",
args: []string{"-c", clusterId, "--target-type", "AZURE_BLOB", "--azblob.sas-token", sasToken, "--force"},
err: errors.New("Azure Blob URI is required when target type is AZURE_BLOB"),
},
}
suite.AssertTest(ctx, tests)
}
func (suite *CreateExportSuite) TestCreateExportWithSQLFile() {
ctx := context.Background()
clusterId := "fake-cluster-id"
exportId := "fake-export-id"
fileType := export.EXPORTFILETYPEENUM_SQL
body := getDefaultCreateExportBody()
body.ExportOptions.FileType = &fileType
body.ExportOptions.CsvFormat = nil
suite.mockClient.On("CreateExport", ctx, clusterId, body).
Return(&export.Export{
ExportId: &exportId,
}, nil)
tests := []Test{
{
name: "export all data to sql file",
args: []string{"-c", clusterId, "--file-type", "SQL", "--force"},
stdoutString: fmt.Sprintf("export %s is running now\n", exportId),
},
}
suite.AssertTest(ctx, tests)
}
func (suite *CreateExportSuite) TestCreateExportWithParquetFile() {
ctx := context.Background()
clusterId := "fake-cluster-id"
exportId := "fake-export-id"
fileType := export.EXPORTFILETYPEENUM_PARQUET
parquetCompression := export.EXPORTPARQUETCOMPRESSIONTYPEENUM_ZSTD
body := getDefaultCreateExportBody()
body.ExportOptions.FileType = &fileType
body.ExportOptions.Compression = nil
body.ExportOptions.CsvFormat = nil
body.ExportOptions.ParquetFormat = &export.ExportOptionsParquetFormat{
Compression: &parquetCompression,
}
suite.mockClient.On("CreateExport", ctx, clusterId, body).
Return(&export.Export{
ExportId: &exportId,
}, nil)
tests := []Test{
{
name: "export all data to parquet file",
args: []string{"-c", clusterId, "--file-type", "PARQUET", "--force"},
stdoutString: fmt.Sprintf("export %s is running now\n", exportId),
},
{
name: "export all data to parquet file with compression",
args: []string{"-c", clusterId, "--file-type", "PARQUET", "--compression", "GZIP", "--force"},
err: errors.New("--compression is not supported when file type is parquet, please use --parquet.compression instead"),
},
}
suite.AssertTest(ctx, tests)
}
func (suite *CreateExportSuite) TestCreateExportWithSQLFilter() {
ctx := context.Background()
clusterId := "fake-cluster-id"
exportId := "fake-export-id"
sql := "SELECT * FROM db.table WHERE column = 'value'"
body := getDefaultCreateExportBody()
body.ExportOptions.Filter = &export.ExportOptionsFilter{
Sql: &sql,
}
suite.mockClient.On("CreateExport", ctx, clusterId, body).
Return(&export.Export{
ExportId: &exportId,
}, nil)
tests := []Test{
{
name: "export data with sql filter",
args: []string{"-c", clusterId, "--sql", sql},
stdoutString: fmt.Sprintf("export %s is running now\n", exportId),
},
}
suite.AssertTest(ctx, tests)
}
func (suite *CreateExportSuite) TestCreateExportWithTableFilter() {
ctx := context.Background()
clusterId := "fake-cluster-id"
exportId := "fake-export-id"
where := "column = 'value'"
pattern1 := "db.t\\.able"
pattern2 := "db.table"
body := getDefaultCreateExportBody()
body.ExportOptions.Filter = &export.ExportOptionsFilter{
Table: &export.ExportOptionsFilterTable{
Patterns: []string{pattern1, pattern2},
Where: &where,
Partitions: []string{},
},
}
suite.mockClient.On("CreateExport", ctx, clusterId, body).
Return(&export.Export{
ExportId: &exportId,
}, nil)
tests := []Test{
{
name: "export data with table filter",
args: []string{"-c", clusterId, "--where", where, "--filter", pattern1, "--filter", pattern2},
stdoutString: fmt.Sprintf("export %s is running now\n", exportId),
},
{
name: "export data with table filter2",
args: []string{"-c", clusterId, "--where", where, "--filter", fmt.Sprintf("%s,%s", pattern1, pattern2)},
stdoutString: fmt.Sprintf("export %s is running now\n", exportId),
},
}
suite.AssertTest(ctx, tests)
}
func (suite *CreateExportSuite) TestCreateExportToOSS() {
ctx := context.Background()
clusterId := "fake-cluster-id"
exportId := "fake-export-id"
targetType := export.EXPORTTARGETTYPEENUM_OSS
uri := "s3://fake-bucket/fake-prefix"
accessKeyId := "fake-id"
secretAccess := "fake-secret"
body := getDefaultCreateExportBody()
body.Target = &export.ExportTarget{
Type: &targetType,
Oss: &export.OSSTarget{
Uri: uri,
AuthType: export.EXPORTOSSAUTHTYPEENUM_ACCESS_KEY,
AccessKey: &export.OSSTargetAccessKey{
Id: accessKeyId,
Secret: secretAccess,
},
},
}
suite.mockClient.On("CreateExport", ctx, clusterId, body).
Return(&export.Export{
ExportId: &exportId,
}, nil)
tests := []Test{
{
name: "export data to oss without uri",
args: []string{"-c", clusterId, "--target-type", "OSS", "--oss.access-key-id", accessKeyId, "--oss.access-key-secret", secretAccess, "--force"},
err: errors.New("OSS URI is required when target type is OSS"),
},
{
name: "export data to oss without auth",
args: []string{"-c", clusterId, "--target-type", "OSS", "--oss.uri", uri, "--oss.access-key-id", accessKeyId, "--force"},
err: errors.New("OSS access key id and access key secret are required when target type is OSS"),
},
{
name: "export all data to oss using access key",
args: []string{"-c", clusterId, "--target-type", "OSS", "--oss.uri", uri, "--oss.access-key-id", accessKeyId, "--oss.access-key-secret", secretAccess, "--force"},
stdoutString: fmt.Sprintf("export %s is running now\n", exportId),
},
}
suite.AssertTest(ctx, tests)
}
func (suite *CreateExportSuite) TestAliClusterExportToS3WithRoleArn() {
ctx := context.Background()
clusterId := "fake-cluster-id"
targetType := export.EXPORTTARGETTYPEENUM_S3
uri := "s3://fake-bucket/fake-prefix"
roleArn := "fake-role-arn"
body := getDefaultCreateExportBody()
body.Target = &export.ExportTarget{
Type: &targetType,
S3: &export.S3Target{
Uri: &uri,
AuthType: export.EXPORTS3AUTHTYPEENUM_ROLE_ARN,
RoleArn: &roleArn,
},
}
errMsg := "export to s3 with role arn is not supported in alicloud"
suite.mockClient.On("CreateExport", ctx, clusterId, body).
Return(nil, errors.New(errMsg))
tests := []Test{
{
name: "export ali cluster with S3 role arn",
args: []string{"-c", clusterId, "--target-type", "S3", "--s3.uri", uri, "--s3.role-arn", roleArn, "--force"},
err: errors.New(errMsg),
},
}
suite.AssertTest(ctx, tests)
}
func getDefaultCreateExportBody() *export.ExportServiceCreateExportBody {
defaultFileType := export.EXPORTFILETYPEENUM_CSV
defaultTargetType := export.EXPORTTARGETTYPEENUM_LOCAL
defaultCompression := export.EXPORTCOMPRESSIONTYPEENUM_GZIP
separatorDefaultValue := ","
delimiterDefaultValue := "\""
nullValueDefaultValue := "\\N"
skipHeaderDefaultValue := false
return &export.ExportServiceCreateExportBody{
ExportOptions: &export.ExportOptions{
FileType: &defaultFileType,
Compression: &defaultCompression,
CsvFormat: &export.ExportOptionsCSVFormat{
Separator: &separatorDefaultValue,
Delimiter: *export.NewNullableString(&delimiterDefaultValue),
NullValue: *export.NewNullableString(&nullValueDefaultValue),
SkipHeader: &skipHeaderDefaultValue,
},
},
Target: &export.ExportTarget{
Type: &defaultTargetType,
},
}
}
func TestCreateExportSuite(t *testing.T) {
suite.Run(t, new(CreateExportSuite))
}
func (suite *CreateExportSuite) AssertTest(ctx context.Context, tests []Test) {
assert := require.New(suite.T())
for _, tt := range tests {
suite.T().Run(tt.name, func(t *testing.T) {
cmd := CreateCmd(suite.h)
cmd.SetContext(ctx)
suite.h.IOStreams.Out.(*bytes.Buffer).Reset()
suite.h.IOStreams.Err.(*bytes.Buffer).Reset()
cmd.SetArgs(tt.args)
err := cmd.Execute()
if err != nil {
assert.Equal(tt.err.Error(), err.Error())
}
assert.Equal(tt.stdoutString, suite.h.IOStreams.Out.(*bytes.Buffer).String())
assert.Equal(tt.stderrString, suite.h.IOStreams.Err.(*bytes.Buffer).String())
if tt.err == nil {
suite.mockClient.AssertExpectations(suite.T())
}
})
}
}
type Test struct {
name string
args []string
err error
stdoutString string
stderrString string
}