-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathzboxcli_create_dir_test.go
327 lines (254 loc) · 13.8 KB
/
zboxcli_create_dir_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
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
package cli_tests
import (
"encoding/json"
"strings"
"testing"
"time"
"github.com/0chain/system_test/internal/api/util/test"
climodel "github.com/0chain/system_test/internal/cli/model"
cliutils "github.com/0chain/system_test/internal/cli/util"
"github.com/stretchr/testify/require"
)
func TestCreateDir(testSetup *testing.T) {
t := test.NewSystemTest(testSetup)
t.SetSmokeTests("create root dir")
t.Parallel()
t.Run("create root dir", func(t *test.SystemTest) {
allocID := setupAllocation(t, configPath)
dirname := "/rootdir"
output, err := createDir(t, configPath, allocID, "/rootdir", true)
require.Nil(t, err, "Unexpected create dir failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, dirname+" directory created", output[0])
output, err = listAll(t, configPath, allocID, true)
require.Nil(t, err, "Unexpected list all failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
var files []climodel.AllocationFile
err = json.Unmarshal([]byte(output[0]), &files)
require.Nil(t, err, "Error deserializing JSON string `%s`: %v", strings.Join(output, "\n"), err)
wantFile := climodel.AllocationFile{Name: "rootdir", Path: "/rootdir", Type: "d"}
require.Len(t, files, 1, "Expecting directories created. Possibly `createdir` failed to create on blobbers (error suppressed) or unable to `list-all` from 3/4 blobbers")
require.Equal(t, wantFile, files[0])
})
t.Run("create nested dir", func(t *test.SystemTest) {
allocID := setupAllocation(t, configPath)
dirname := "/parent"
output, err := createDir(t, configPath, allocID, dirname, true)
require.Nil(t, err, "Unexpected create dir failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, dirname+" directory created", output[0])
dirname = "/parent/child"
output, err = createDir(t, configPath, allocID, dirname, true)
require.Nil(t, err, "Unexpected create dir failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, dirname+" directory created", output[0])
output, err = listAll(t, configPath, allocID, true)
require.Nil(t, err, "Unexpected list all failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
var files []climodel.AllocationFile
err = json.Unmarshal([]byte(output[0]), &files)
require.Nil(t, err, "Error deserializing JSON string `%s`: %v", strings.Join(output, "\n"), err)
require.Len(t, files, 2, "Expecting directories created. Possibly `createdir` failed to create on blobbers (error suppressed) or unable to `list-all` from 3/4 blobbers")
require.Contains(t, files, climodel.AllocationFile{Name: "parent", Path: "/parent", Type: "d"})
require.Contains(t, files, climodel.AllocationFile{Name: "child", Path: "/parent/child", Type: "d"})
})
t.Run("create with 100-char dir", func(t *test.SystemTest) {
allocID := setupAllocation(t, configPath)
longDirName := "/"
for i := 0; i < 99; i++ {
longDirName += "a"
}
output, err := createDir(t, configPath, allocID, longDirName, true)
require.Nil(t, err, "Unexpected create dir failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, longDirName+" directory created", output[0])
output, err = listAll(t, configPath, allocID, true)
require.Nil(t, err, "Unexpected list all failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
var files []climodel.AllocationFile
err = json.Unmarshal([]byte(output[0]), &files)
require.Nil(t, err, "Error deserializing JSON string `%s`: %v", strings.Join(output, "\n"), err)
wantFile := climodel.AllocationFile{Name: longDirName[1:], Path: longDirName, Type: "d"}
require.Len(t, files, 1, "Expecting directories created. Possibly `createdir` failed to create on blobbers (error suppressed) or unable to `list-all` from 3/4 blobbers")
require.Equal(t, wantFile, files[0])
})
t.Run("create attempt with 151-char dirname", func(t *test.SystemTest) {
allocID := setupAllocation(t, configPath)
longDirName := "/"
for i := 0; i < 151; i++ {
longDirName += "a"
}
output, err := createDir(t, configPath, allocID, longDirName, false)
require.NotNil(t, err, "expected create dir failure command executed with output: ", strings.Join(output, "\n"))
require.Len(t, output, 2)
aggregatedOutput := strings.Join(output, " ")
require.Contains(t, aggregatedOutput, "Directory creation failed")
require.Contains(t, aggregatedOutput, "ERROR: value too long for type character varying(150)")
output, err = listAll(t, configPath, allocID, true)
require.Nil(t, err, "Unexpected list all failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1, "unexpected output"+strings.Join(output, ", "))
require.Equal(t, "[]", output[0], "unexpected output"+strings.Join(output, ", "))
var files []climodel.AllocationFile
err = json.Unmarshal([]byte(output[0]), &files)
require.Nil(t, err, "Error deserializing JSON string `%s`: %v", strings.Join(output, "\n"), err)
require.Len(t, files, 0)
})
t.Run("create dir with existing dirname should work", func(t *test.SystemTest) {
allocID := setupAllocation(t, configPath)
dirname := "/existingdir"
output, err := createDir(t, configPath, allocID, dirname, true)
require.Nil(t, err, "Unexpected create dir failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, dirname+" directory created", output[0])
output, err = createDir(t, configPath, allocID, dirname, false)
require.Nil(t, err, "Unexpected create dir failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, dirname+" directory created", output[0])
})
t.Run("create dir with no leading slash should not work", func(t *test.SystemTest) {
allocID := setupAllocation(t, configPath)
dirname := "noleadingslash"
output, err := createDir(t, configPath, allocID, dirname, false)
require.Error(t, err)
aggregatedOutput := strings.Join(output, " ")
require.Contains(t, aggregatedOutput, "not absolute")
})
t.Run("create with existing dir but different case", func(t *test.SystemTest) {
allocID := setupAllocation(t, configPath)
dirname := "/existingdir"
output, err := createDir(t, configPath, allocID, dirname, true)
require.Nil(t, err, "Unexpected create dir failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, dirname+" directory created", output[0])
dirname = "/existingDir"
output, err = createDir(t, configPath, allocID, dirname, true)
require.Nil(t, err, "Unexpected create dir failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, dirname+" directory created", output[0])
output, err = listAll(t, configPath, allocID, true)
require.Nil(t, err, "Unexpected list all failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
var files []climodel.AllocationFile
err = json.Unmarshal([]byte(output[0]), &files)
require.Nil(t, err, "Error deserializing JSON string `%s`: %v", strings.Join(output, "\n"), err)
require.Len(t, files, 2, "Expecting directories created. Possibly `createdir` failed to create on blobbers (error suppressed) or unable to `list-all` from 3/4 blobbers")
require.Contains(t, files, climodel.AllocationFile{Name: "existingdir", Path: "/existingdir", Type: "d"})
require.Contains(t, files, climodel.AllocationFile{Name: "existingDir", Path: "/existingDir", Type: "d"})
})
t.Run("create with non-existent parent dir", func(t *test.SystemTest) {
allocID := setupAllocation(t, configPath)
dirname := "/nonexistent/child"
output, err := createDir(t, configPath, allocID, dirname, true)
require.Nil(t, err, "Unexpected create dir failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, dirname+" directory created", output[0])
output, err = listAll(t, configPath, allocID, true)
require.Nil(t, err, "Unexpected list all failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
var files []climodel.AllocationFile
err = json.Unmarshal([]byte(output[0]), &files)
require.Nil(t, err, "Error deserializing JSON string `%s`: %v", strings.Join(output, "\n"), err)
require.Len(t, files, 2, "Expecting directories created. Possibly `createdir` failed to create on blobbers (error suppressed) or unable to `list-all` from 3/4 blobbers")
require.Contains(t, files, climodel.AllocationFile{Name: "nonexistent", Path: "/nonexistent", Type: "d"})
require.Contains(t, files, climodel.AllocationFile{Name: "child", Path: "/nonexistent/child", Type: "d"})
})
t.Run("create with dir containing special characters", func(t *test.SystemTest) {
allocID := setupAllocation(t, configPath)
dirname := "/abc!@#$%^&*()<>{}[]:;'?,."
output, err := createDir(t, configPath, allocID, dirname, true)
require.Nil(t, err, "Unexpected create dir failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, dirname+" directory created", output[0])
output, err = listAll(t, configPath, allocID, true)
require.Nil(t, err, "Unexpected list all failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
var files []climodel.AllocationFile
err = json.Unmarshal([]byte(output[0]), &files)
require.Nil(t, err, "Error deserializing JSON string `%s`: %v", strings.Join(output, "\n"), err)
wantFile := climodel.AllocationFile{Name: dirname[1:], Path: dirname, Type: "d"}
require.Len(t, files, 1, "Expecting directories created. Possibly `createdir` failed to create on blobbers (error suppressed) or unable to `list-all` from 3/4 blobbers")
require.Equal(t, wantFile, files[0])
})
t.Run("create attempt with missing dirname param", func(t *test.SystemTest) {
wallet := escapedTestName(t)
allocID := setupAllocation(t, configPath)
output, err := createDirForWallet(t, configPath, wallet, true, allocID, false, "", false)
require.NotNil(t, err, "Expecting create dir failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, "Error: dirname flag is missing", output[0])
})
t.Run("create attempt with empty dirname param", func(t *test.SystemTest) {
wallet := escapedTestName(t)
allocID := setupAllocation(t, configPath)
output, err := createDirForWallet(t, configPath, wallet, true, allocID, true, "", false)
require.NotNil(t, err, "Expecting create dir failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, "Directory creation failed. invalid_path: Path is not absolute", output[0])
})
t.Run("create attempt with missing allocation", func(t *test.SystemTest) {
wallet := escapedTestName(t)
createWallet(t)
output, err := createDirForWallet(t, configPath, wallet, false, "", true, "/root", false)
require.NotNil(t, err, "Expecting create dir failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, "Error: allocation flag is missing", output[0])
})
t.Run("create attempt with empty allocation", func(t *test.SystemTest) {
wallet := escapedTestName(t)
createWallet(t)
output, err := createDirForWallet(t, configPath, wallet, true, "", true, "/root", false)
require.NotNil(t, err, "Expecting create dir failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, "Error fetching the allocation allocation_fetch_error: "+
"Error fetching the allocation.internal_error: can't get allocation: error retrieving allocation: , error: record not found", output[0])
})
t.Run("create attempt with invalid allocation", func(t *test.SystemTest) {
createWallet(t)
output, err := createDir(t, configPath, "invalidallocation", "/root", false)
require.NotNil(t, err, "Expecting create dir failure %s", strings.Join(output, "\n"))
require.Len(t, output, 1)
require.Equal(t, "Error fetching the allocation allocation_fetch_error: Error fetching the allocation.internal_error: "+
"can't get allocation: error retrieving allocation: invalidallocation, error: record not found", output[0])
})
t.Run("create attempt with someone else's allocation", func(t *test.SystemTest) {
nonAllocOwnerWallet := escapedTestName(t) + "_NON_OWNER"
allocID := setupAllocation(t, configPath)
createWalletForName(nonAllocOwnerWallet)
output, err := createDirForWallet(t, configPath, nonAllocOwnerWallet, true, allocID, true, "/mydir", false)
require.NotNil(t, err, "Expected create dir failure but got output: ", strings.Join(output, "\n"))
require.Len(t, output, 1)
aggregatedOutput := strings.Join(output, " ")
require.Contains(t, aggregatedOutput, `consensus_not_met`)
})
}
func createDir(t *test.SystemTest, cliConfigFilename, allocationID, dirname string, retry bool) ([]string, error) {
return createDirForWallet(t, cliConfigFilename, escapedTestName(t), true, allocationID, true, dirname, retry)
}
func createDirForWallet(t *test.SystemTest, cliConfigFilename, wallet string, withAllocationFlag bool, allocationID string, withDirnameFlag bool, dirname string, retry bool) ([]string, error) {
cmd := "./zbox createdir --silent --wallet " + wallet + "_wallet.json --configDir ./config --config " + cliConfigFilename
if withAllocationFlag {
cmd += ` --allocation "` + allocationID + `"`
}
if withDirnameFlag {
cmd += ` --dirname "` + dirname + `"`
}
if retry {
return cliutils.RunCommand(t, cmd, 3, time.Second*2)
} else {
return cliutils.RunCommandWithoutRetry(cmd)
}
}
func listAll(t *test.SystemTest, cliConfigFilename, allocationID string, retry bool) ([]string, error) {
return listAllWithWallet(t, escapedTestName(t), cliConfigFilename, allocationID, retry)
}
func listAllWithWallet(t *test.SystemTest, wallet, cliConfigFilename, allocationID string, retry bool) ([]string, error) {
cliutils.Wait(t, 5*time.Second)
t.Logf("Listing all...")
cmd := "./zbox list-all --silent --allocation " + allocationID +
" --wallet " + wallet + "_wallet.json --configDir ./config --config " + cliConfigFilename
if retry {
return cliutils.RunCommand(t, cmd, 3, time.Second*2)
} else {
return cliutils.RunCommandWithoutRetry(cmd)
}
}