forked from minio/madmin-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelta-sharing_test.go
More file actions
412 lines (357 loc) · 11.4 KB
/
Copy pathdelta-sharing_test.go
File metadata and controls
412 lines (357 loc) · 11.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
//
// Copyright (c) 2015-2025 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package madmin
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
)
func TestCreateShare(t *testing.T) {
// Setup mock server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("Expected POST request, got %s", r.Method)
}
if r.URL.Path != "/minio/admin/v4/delta-sharing/shares" {
t.Errorf("Expected path /minio/admin/v4/delta-sharing/shares, got %s", r.URL.Path)
}
var req CreateShareRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
t.Errorf("Failed to decode request body: %v", err)
}
if req.Name != "test-share" {
t.Errorf("Expected share name 'test-share', got %s", req.Name)
}
response := CreateShareResponse{
Share: DeltaSharingShare{
ID: "share-123",
Name: req.Name,
Description: req.Description,
Schemas: req.Schemas,
CreatedAt: time.Now(),
},
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
// Create client
client, err := New(server.URL[7:], "access", "secret", false)
if err != nil {
t.Fatal(err)
}
// Create share request
req := CreateShareRequest{
Name: "test-share",
Description: "Test share description",
Schemas: []DeltaSharingSchema{
{
Name: "default",
Description: "Default schema",
Tables: []DeltaSharingTable{
NewDeltaTable("sales", "test-bucket", "sales/"),
},
},
},
}
// Test create share
resp, err := client.CreateShare(context.Background(), req)
if err != nil {
t.Fatalf("CreateShare failed: %v", err)
}
if resp.Share.Name != "test-share" {
t.Errorf("Expected share name 'test-share', got %s", resp.Share.Name)
}
if resp.Share.ID != "share-123" {
t.Errorf("Expected share ID 'share-123', got %s", resp.Share.ID)
}
}
func TestListShares(t *testing.T) {
// Setup mock server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("Expected GET request, got %s", r.Method)
}
if r.URL.Path != "/minio/admin/v4/delta-sharing/shares" {
t.Errorf("Expected path /minio/admin/v4/delta-sharing/shares, got %s", r.URL.Path)
}
response := ListSharesResponse{
Shares: []DeltaSharingShare{
{
ID: "share-1",
Name: "share1",
Description: "First share",
CreatedAt: time.Now(),
},
{
ID: "share-2",
Name: "share2",
Description: "Second share",
CreatedAt: time.Now(),
},
},
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
// Create client
client, err := New(server.URL[7:], "access", "secret", false)
if err != nil {
t.Fatal(err)
}
// Test list shares
resp, err := client.ListShares(context.Background())
if err != nil {
t.Fatalf("ListShares failed: %v", err)
}
if len(resp.Shares) != 2 {
t.Errorf("Expected 2 shares, got %d", len(resp.Shares))
}
if resp.Shares[0].Name != "share1" {
t.Errorf("Expected first share name 'share1', got %s", resp.Shares[0].Name)
}
}
func TestCreateToken(t *testing.T) {
// Setup mock server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("Expected POST request, got %s", r.Method)
}
if r.URL.Path != "/minio/admin/v4/delta-sharing/shares/test-share/tokens" {
t.Errorf("Expected path /minio/admin/v4/delta-sharing/shares/test-share/tokens, got %s", r.URL.Path)
}
var req CreateTokenRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
t.Errorf("Failed to decode request body: %v", err)
}
response := CreateTokenResponse{
TokenID: "token-123",
Token: "dstkn_abcdefghijklmnopqrstuvwxyz",
Profile: &DeltaSharingProfile{
ShareCredentialsVersion: 1,
Endpoint: "https://minio.example.com/_delta-sharing",
BearerToken: "dstkn_abcdefghijklmnopqrstuvwxyz",
},
}
if req.ExpiresAt != nil {
response.ExpiresAt = req.ExpiresAt
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
// Create client
client, err := New(server.URL[7:], "access", "secret", false)
if err != nil {
t.Fatal(err)
}
// Create token request
expiresAt := time.Now().Add(24 * time.Hour)
req := CreateTokenRequest{
Description: "Test token",
ExpiresAt: &expiresAt,
}
// Test create token
resp, err := client.CreateToken(context.Background(), "test-share", req)
if err != nil {
t.Fatalf("CreateToken failed: %v", err)
}
if resp.TokenID != "token-123" {
t.Errorf("Expected token ID 'token-123', got %s", resp.TokenID)
}
if resp.Profile == nil {
t.Error("Expected profile to be non-nil")
} else {
if resp.Profile.ShareCredentialsVersion != 1 {
t.Errorf("Expected share credentials version 1, got %d", resp.Profile.ShareCredentialsVersion)
}
if resp.Profile.BearerToken == "" {
t.Error("Expected bearer token to be non-empty")
}
}
}
func TestDeleteShare(t *testing.T) {
// Setup mock server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
t.Errorf("Expected DELETE request, got %s", r.Method)
}
if r.URL.Path != "/minio/admin/v4/delta-sharing/shares/test-share" {
t.Errorf("Expected path /minio/admin/v4/delta-sharing/shares/test-share, got %s", r.URL.Path)
}
w.WriteHeader(http.StatusNoContent)
}))
defer server.Close()
// Create client
client, err := New(server.URL[7:], "access", "secret", false)
if err != nil {
t.Fatal(err)
}
// Test delete share
err = client.DeleteShare(context.Background(), "test-share")
if err != nil {
t.Fatalf("DeleteShare failed: %v", err)
}
}
func TestHelperFunctions(t *testing.T) {
// Test NewDeltaTable
deltaTable := NewDeltaTable("sales", "bucket1", "path/to/sales/")
if deltaTable.Name != "sales" {
t.Errorf("Expected table name 'sales', got %s", deltaTable.Name)
}
if deltaTable.SourceType != "delta" {
t.Errorf("Expected source type 'delta', got %s", deltaTable.SourceType)
}
if deltaTable.Bucket != "bucket1" {
t.Errorf("Expected bucket 'bucket1', got %s", deltaTable.Bucket)
}
if deltaTable.Location != "path/to/sales/" {
t.Errorf("Expected location 'path/to/sales/', got %s", deltaTable.Location)
}
// Test NewUniformTable
uniformTable := NewUniformTable("inventory", "warehouse1", "retail", "inventory_table")
if uniformTable.Name != "inventory" {
t.Errorf("Expected table name 'inventory', got %s", uniformTable.Name)
}
if uniformTable.SourceType != "uniform" {
t.Errorf("Expected source type 'uniform', got %s", uniformTable.SourceType)
}
if uniformTable.Warehouse != "warehouse1" {
t.Errorf("Expected warehouse 'warehouse1', got %s", uniformTable.Warehouse)
}
if uniformTable.Namespace != "retail" {
t.Errorf("Expected namespace 'retail', got %s", uniformTable.Namespace)
}
if uniformTable.Table != "inventory_table" {
t.Errorf("Expected table 'inventory_table', got %s", uniformTable.Table)
}
// Test NewSchema
schema := NewSchema("default", "Default schema", deltaTable, uniformTable)
if schema.Name != "default" {
t.Errorf("Expected schema name 'default', got %s", schema.Name)
}
if schema.Description != "Default schema" {
t.Errorf("Expected schema description 'Default schema', got %s", schema.Description)
}
if len(schema.Tables) != 2 {
t.Errorf("Expected 2 tables, got %d", len(schema.Tables))
}
}
func TestDeltaSharingError(t *testing.T) {
err := DeltaSharingError{
ErrorCode: "SHARE_NOT_FOUND",
Message: "Share 'test' not found",
}
expected := "SHARE_NOT_FOUND: Share 'test' not found"
if err.Error() != expected {
t.Errorf("Expected error string '%s', got '%s'", expected, err.Error())
}
}
func TestUpdateShare(t *testing.T) {
// Setup mock server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut {
t.Errorf("Expected PUT request, got %s", r.Method)
}
if r.URL.Path != "/minio/admin/v4/delta-sharing/shares/test-share" {
t.Errorf("Expected path /minio/admin/v4/delta-sharing/shares/test-share, got %s", r.URL.Path)
}
var req map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
t.Errorf("Failed to decode request body: %v", err)
}
share := DeltaSharingShare{
ID: "share-123",
Name: "test-share",
Description: "Updated description",
UpdatedAt: time.Now(),
}
if desc, ok := req["description"].(string); ok {
share.Description = desc
}
response := GetShareResponse{
Share: share,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
// Create client
client, err := New(server.URL[7:], "access", "secret", false)
if err != nil {
t.Fatal(err)
}
// Test update share
newDesc := "New description"
resp, err := client.UpdateShare(context.Background(), "test-share", &newDesc, nil)
if err != nil {
t.Fatalf("UpdateShare failed: %v", err)
}
if resp.Description != newDesc {
t.Errorf("Expected description '%s', got '%s'", newDesc, resp.Description)
}
}
func TestProfileMarshaling(t *testing.T) {
// Test v1 profile (Bearer token)
v1Profile := DeltaSharingProfile{
ShareCredentialsVersion: 1,
Endpoint: "https://minio.example.com/_delta-sharing",
BearerToken: "dstkn_abcdefg",
}
data, err := json.Marshal(v1Profile)
if err != nil {
t.Fatalf("Failed to marshal v1 profile: %v", err)
}
var decoded DeltaSharingProfile
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatalf("Failed to unmarshal v1 profile: %v", err)
}
if !reflect.DeepEqual(v1Profile, decoded) {
t.Error("v1 profile marshaling/unmarshaling mismatch")
}
// Test v2 profile (OAuth)
v2Profile := DeltaSharingProfile{
ShareCredentialsVersion: 2,
Endpoint: "https://minio.example.com/_delta-sharing",
TokenEndpoint: "https://minio.example.com/oauth/token",
ClientID: "client-123",
ClientSecret: "secret-456",
Scope: "delta-sharing:read",
}
data, err = json.Marshal(v2Profile)
if err != nil {
t.Fatalf("Failed to marshal v2 profile: %v", err)
}
var decodedV2 DeltaSharingProfile
if err := json.Unmarshal(data, &decodedV2); err != nil {
t.Fatalf("Failed to unmarshal v2 profile: %v", err)
}
if !reflect.DeepEqual(v2Profile, decodedV2) {
t.Error("v2 profile marshaling/unmarshaling mismatch")
}
}