forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_archive_test.go
276 lines (257 loc) · 8.4 KB
/
old_archive_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
package lib
import (
"archive/tar"
"bytes"
"io/fs"
"net/url"
"path"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"go.k6.io/k6/lib/fsext"
)
func dumpMemMapFsToBuf(fileSystem fsext.Fs) (*bytes.Buffer, error) {
b := bytes.NewBuffer(nil)
w := tar.NewWriter(b)
err := fsext.Walk(fileSystem, fsext.FilePathSeparator,
filepath.WalkFunc(func(filePath string, info fs.FileInfo, err error) error {
if filePath == fsext.FilePathSeparator {
return nil // skip the root
}
if err != nil {
return err
}
if info.IsDir() {
return w.WriteHeader(&tar.Header{
Name: path.Clean(filepath.ToSlash(filePath)[1:]),
Mode: 0o555,
Typeflag: tar.TypeDir,
})
}
var data []byte
data, err = fsext.ReadFile(fileSystem, filePath)
if err != nil {
return err
}
err = w.WriteHeader(&tar.Header{
Name: path.Clean(filepath.ToSlash(filePath)[1:]),
Mode: 0o644,
Size: int64(len(data)),
Typeflag: tar.TypeReg,
})
if err != nil {
return err
}
_, err = w.Write(data)
if err != nil {
return err
}
return nil
}))
if err != nil {
return nil, err
}
return b, w.Close()
}
func TestOldArchive(t *testing.T) {
t.Parallel()
testCases := map[string]string{
// map of filename to data for each main file tested
"github.com/k6io/k6/samples/example.js": `github file`,
"cdnjs.com/packages/Faker": `faker file`,
"C:/something/path2": `windows script`,
"/absolulte/path2": `unix script`,
}
for filename, data := range testCases {
filename, data := filename, data
t.Run(filename, func(t *testing.T) {
t.Parallel()
metadata := `{"filename": "` + filename + `", "options": {}}`
fs := makeMemMapFs(t, map[string][]byte{
// files
"/files/github.com/k6io/k6/samples/example.js": []byte(`github file`),
"/files/cdnjs.com/packages/Faker": []byte(`faker file`),
"/files/example.com/path/to.js": []byte(`example.com file`),
"/files/_/C/something/path": []byte(`windows file`),
"/files/_/absolulte/path": []byte(`unix file`),
// scripts
"/scripts/github.com/k6io/k6/samples/example.js2": []byte(`github script`),
"/scripts/cdnjs.com/packages/Faker2": []byte(`faker script`),
"/scripts/example.com/path/too.js": []byte(`example.com script`),
"/scripts/_/C/something/path2": []byte(`windows script`),
"/scripts/_/absolulte/path2": []byte(`unix script`),
"/data": []byte(data),
"/metadata.json": []byte(metadata),
})
buf, err := dumpMemMapFsToBuf(fs)
require.NoError(t, err)
expectedFilesystems := map[string]fsext.Fs{
"file": makeMemMapFs(t, map[string][]byte{
"/C:/something/path": []byte(`windows file`),
"/absolulte/path": []byte(`unix file`),
"/C:/something/path2": []byte(`windows script`),
"/absolulte/path2": []byte(`unix script`),
}),
"https": makeMemMapFs(t, map[string][]byte{
"/example.com/path/to.js": []byte(`example.com file`),
"/example.com/path/too.js": []byte(`example.com script`),
"/github.com/k6io/k6/samples/example.js": []byte(`github file`),
"/cdnjs.com/packages/Faker": []byte(`faker file`),
"/github.com/k6io/k6/samples/example.js2": []byte(`github script`),
"/cdnjs.com/packages/Faker2": []byte(`faker script`),
}),
}
arc, err := ReadArchive(buf)
require.NoError(t, err)
diffMapFilesystems(t, expectedFilesystems, arc.Filesystems)
})
}
}
func TestUnknownPrefix(t *testing.T) {
t.Parallel()
fs := makeMemMapFs(t, map[string][]byte{
"/strange/something": []byte(`github file`),
})
buf, err := dumpMemMapFsToBuf(fs)
require.NoError(t, err)
_, err = ReadArchive(buf)
require.Error(t, err)
require.Equal(t, err.Error(),
"unknown file prefix `strange` for file `strange/something`")
}
func TestFilenamePwdResolve(t *testing.T) {
t.Parallel()
tests := []struct {
Filename, Pwd, version string
expectedFilenameURL, expectedPwdURL *url.URL
expectedError string
}{
{
Filename: "/home/nobody/something.js",
Pwd: "/home/nobody",
expectedFilenameURL: &url.URL{Scheme: "file", Path: "/home/nobody/something.js"},
expectedPwdURL: &url.URL{Scheme: "file", Path: "/home/nobody"},
},
{
Filename: "github.com/k6io/k6/samples/http2.js",
Pwd: "github.com/k6io/k6/samples",
expectedFilenameURL: &url.URL{Opaque: "github.com/k6io/k6/samples/http2.js"},
expectedPwdURL: &url.URL{Opaque: "github.com/k6io/k6/samples"},
},
{
Filename: "cdnjs.com/libraries/Faker",
Pwd: "/home/nobody",
expectedFilenameURL: &url.URL{Opaque: "cdnjs.com/libraries/Faker"},
expectedPwdURL: &url.URL{Scheme: "file", Path: "/home/nobody"},
},
{
Filename: "example.com/something/dot.js",
Pwd: "example.com/something/",
expectedFilenameURL: &url.URL{Host: "example.com", Scheme: "", Path: "/something/dot.js"},
expectedPwdURL: &url.URL{Host: "example.com", Scheme: "", Path: "/something"},
},
{
Filename: "https://example.com/something/dot.js",
Pwd: "https://example.com/something",
expectedFilenameURL: &url.URL{Host: "example.com", Scheme: "https", Path: "/something/dot.js"},
expectedPwdURL: &url.URL{Host: "example.com", Scheme: "https", Path: "/something"},
version: "0.25.0",
},
{
Filename: "ftps://example.com/something/dot.js",
Pwd: "https://example.com/something",
expectedError: "only supported schemes for imports are file and https",
version: "0.25.0",
},
{
Filename: "https://example.com/something/dot.js",
Pwd: "ftps://example.com/something",
expectedError: "only supported schemes for imports are file and https",
version: "0.25.0",
},
}
for _, test := range tests {
metadata := `{
"filename": "` + test.Filename + `",
"pwd": "` + test.Pwd + `",
"k6version": "` + test.version + `",
"options": {}
}`
buf, err := dumpMemMapFsToBuf(makeMemMapFs(t, map[string][]byte{
"/metadata.json": []byte(metadata),
}))
require.NoError(t, err)
arc, err := ReadArchive(buf)
if test.expectedError != "" {
require.Error(t, err)
require.Contains(t, err.Error(), test.expectedError)
} else {
require.NoError(t, err)
require.Equal(t, test.expectedFilenameURL, arc.FilenameURL)
require.Equal(t, test.expectedPwdURL, arc.PwdURL)
}
}
}
func TestDerivedExecutionDiscarding(t *testing.T) {
t.Parallel()
var emptyConfigMap ScenarioConfigs
tests := []struct {
metadata string
expScenarios interface{}
expError string
}{
// Tests to make sure that "execution" in the options, the old name for
// "scenarios" before #1007 was merged, doesn't mess up the options...
{
metadata: `{
"filename": "/test.js", "pwd": "/",
"options": { "execution": { "something": "invalid" } }
}`,
expScenarios: emptyConfigMap,
},
{
metadata: `{
"filename": "/test.js", "pwd": "/",
"k6version": "0.24.0",
"options": { "execution": { "something": "invalid" } }
}`,
expScenarios: emptyConfigMap,
},
{
metadata: `blah`,
expError: "invalid character",
},
{
metadata: `{
"filename": "/test.js", "pwd": "/",
"k6version": "0.24.0",
"options": "something invalid"
}`,
expError: "cannot unmarshal string into Go struct field",
},
{
metadata: `{
"filename": "/test.js", "pwd": "/",
"k6version": "0.25.0",
"options": { "scenarios": { "something": "invalid" } }
}`,
expError: "cannot unmarshal string",
},
// TODO: test an actual scenarios unmarshalling, which is currently
// impossible due to import cycles...
}
for _, test := range tests {
buf, err := dumpMemMapFsToBuf(makeMemMapFs(t, map[string][]byte{
"/metadata.json": []byte(test.metadata),
}))
require.NoError(t, err)
arc, err := ReadArchive(buf)
if test.expError != "" {
require.Errorf(t, err, "expected error '%s' but got nil", test.expError)
require.Contains(t, err.Error(), test.expError)
} else {
require.NoError(t, err)
require.Equal(t, test.expScenarios, arc.Options.Scenarios)
}
}
}