-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
bundle_test.go
886 lines (829 loc) · 25.9 KB
/
bundle_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
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* 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 js
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/dop251/goja"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v3"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/consts"
"github.com/loadimpact/k6/lib/fsext"
"github.com/loadimpact/k6/lib/testutils"
"github.com/loadimpact/k6/lib/types"
"github.com/loadimpact/k6/loader"
)
const isWindows = runtime.GOOS == "windows"
func getSimpleBundle(tb testing.TB, filename, data string, opts ...interface{}) (*Bundle, error) {
var (
fs = afero.NewMemMapFs()
rtOpts = lib.RuntimeOptions{}
)
for _, o := range opts {
switch opt := o.(type) {
case afero.Fs:
fs = opt
case lib.RuntimeOptions:
rtOpts = opt
}
}
return NewBundle(
testutils.NewLogger(tb),
&loader.SourceData{
URL: &url.URL{Path: filename, Scheme: "file"},
Data: []byte(data),
},
map[string]afero.Fs{"file": fs, "https": afero.NewMemMapFs()},
rtOpts,
)
}
func TestNewBundle(t *testing.T) {
t.Run("Blank", func(t *testing.T) {
_, err := getSimpleBundle(t, "/script.js", "")
assert.EqualError(t, err, "no exported functions in script")
})
t.Run("Invalid", func(t *testing.T) {
_, err := getSimpleBundle(t, "/script.js", "\x00")
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "SyntaxError: file:///script.js: Unexpected character '\x00' (1:0)\n> 1 | \x00\n")
})
t.Run("Error", func(t *testing.T) {
_, err := getSimpleBundle(t, "/script.js", `throw new Error("aaaa");`)
assert.EqualError(t, err, "Error: aaaa at file:///script.js:1:7(3)")
})
t.Run("InvalidExports", func(t *testing.T) {
_, err := getSimpleBundle(t, "/script.js", `exports = null`)
assert.EqualError(t, err, "exports must be an object")
})
t.Run("DefaultUndefined", func(t *testing.T) {
_, err := getSimpleBundle(t, "/script.js", `export default undefined;`)
assert.EqualError(t, err, "no exported functions in script")
})
t.Run("DefaultNull", func(t *testing.T) {
_, err := getSimpleBundle(t, "/script.js", `export default null;`)
assert.EqualError(t, err, "no exported functions in script")
})
t.Run("DefaultWrongType", func(t *testing.T) {
_, err := getSimpleBundle(t, "/script.js", `export default 12345;`)
assert.EqualError(t, err, "no exported functions in script")
})
t.Run("Minimal", func(t *testing.T) {
_, err := getSimpleBundle(t, "/script.js", `export default function() {};`)
assert.NoError(t, err)
})
t.Run("stdin", func(t *testing.T) {
b, err := getSimpleBundle(t, "-", `export default function() {};`)
if assert.NoError(t, err) {
assert.Equal(t, "file://-", b.Filename.String())
assert.Equal(t, "file:///", b.BaseInitContext.pwd.String())
}
})
t.Run("CompatibilityMode", func(t *testing.T) {
t.Run("Extended/ok/CoreJS", func(t *testing.T) {
rtOpts := lib.RuntimeOptions{
CompatibilityMode: null.StringFrom(lib.CompatibilityModeExtended.String()),
}
_, err := getSimpleBundle(t, "/script.js",
`module.exports.default = function() {}; new Promise(function(resolve, reject){});`, rtOpts)
assert.NoError(t, err)
})
t.Run("Base/ok/Minimal", func(t *testing.T) {
rtOpts := lib.RuntimeOptions{
CompatibilityMode: null.StringFrom(lib.CompatibilityModeBase.String()),
}
_, err := getSimpleBundle(t, "/script.js",
`module.exports.default = function() {};`, rtOpts)
assert.NoError(t, err)
})
t.Run("Base/err", func(t *testing.T) {
testCases := []struct {
name string
compatMode string
code string
expErr string
}{
{
"InvalidCompat", "es1", `export default function() {};`,
`invalid compatibility mode "es1". Use: "extended", "base"`,
},
// ES2015 modules are not supported
{
"Modules", "base", `export default function() {};`,
"file:///script.js: Line 1:1 Unexpected reserved word",
},
// Arrow functions are not supported
{
"ArrowFuncs", "base",
`module.exports.default = function() {}; () => {};`,
"file:///script.js: Line 1:42 Unexpected token ) (and 1 more errors)",
},
// some ES2015 objects polyfilled by core.js are not supported
{
"CoreJS", "base",
`module.exports.default = function() {}; new Promise(function(resolve, reject){});`,
"ReferenceError: Promise is not defined at file:///script.js:1:45(5)",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
rtOpts := lib.RuntimeOptions{CompatibilityMode: null.StringFrom(tc.compatMode)}
_, err := getSimpleBundle(t, "/script.js", tc.code, rtOpts)
assert.EqualError(t, err, tc.expErr)
})
}
})
})
t.Run("Options", func(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
_, err := getSimpleBundle(t, "/script.js", `
export let options = {};
export default function() {};
`)
assert.NoError(t, err)
})
t.Run("Invalid", func(t *testing.T) {
invalidOptions := map[string]struct {
Expr, Error string
}{
"Array": {`[]`, "json: cannot unmarshal array into Go value of type lib.Options"},
"Function": {`function(){}`, "json: unsupported type: func(goja.FunctionCall) goja.Value"},
}
for name, data := range invalidOptions {
t.Run(name, func(t *testing.T) {
_, err := getSimpleBundle(t, "/script.js", fmt.Sprintf(`
export let options = %s;
export default function() {};
`, data.Expr))
assert.EqualError(t, err, data.Error)
})
}
})
t.Run("Paused", func(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", `
export let options = {
paused: true,
};
export default function() {};
`)
if assert.NoError(t, err) {
assert.Equal(t, null.BoolFrom(true), b.Options.Paused)
}
})
t.Run("VUs", func(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", `
export let options = {
vus: 100,
};
export default function() {};
`)
if assert.NoError(t, err) {
assert.Equal(t, null.IntFrom(100), b.Options.VUs)
}
})
t.Run("Duration", func(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", `
export let options = {
duration: "10s",
};
export default function() {};
`)
if assert.NoError(t, err) {
assert.Equal(t, types.NullDurationFrom(10*time.Second), b.Options.Duration)
}
})
t.Run("Iterations", func(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", `
export let options = {
iterations: 100,
};
export default function() {};
`)
if assert.NoError(t, err) {
assert.Equal(t, null.IntFrom(100), b.Options.Iterations)
}
})
t.Run("Stages", func(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", `
export let options = {
stages: [],
};
export default function() {};
`)
if assert.NoError(t, err) {
assert.Len(t, b.Options.Stages, 0)
}
t.Run("Empty", func(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", `
export let options = {
stages: [
{},
],
};
export default function() {};
`)
if assert.NoError(t, err) {
if assert.Len(t, b.Options.Stages, 1) {
assert.Equal(t, lib.Stage{}, b.Options.Stages[0])
}
}
})
t.Run("Target", func(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", `
export let options = {
stages: [
{target: 10},
],
};
export default function() {};
`)
if assert.NoError(t, err) {
if assert.Len(t, b.Options.Stages, 1) {
assert.Equal(t, lib.Stage{Target: null.IntFrom(10)}, b.Options.Stages[0])
}
}
})
t.Run("Duration", func(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", `
export let options = {
stages: [
{duration: "10s"},
],
};
export default function() {};
`)
if assert.NoError(t, err) {
if assert.Len(t, b.Options.Stages, 1) {
assert.Equal(t, lib.Stage{Duration: types.NullDurationFrom(10 * time.Second)}, b.Options.Stages[0])
}
}
})
t.Run("DurationAndTarget", func(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", `
export let options = {
stages: [
{duration: "10s", target: 10},
],
};
export default function() {};
`)
if assert.NoError(t, err) {
if assert.Len(t, b.Options.Stages, 1) {
assert.Equal(t, lib.Stage{Duration: types.NullDurationFrom(10 * time.Second), Target: null.IntFrom(10)}, b.Options.Stages[0])
}
}
})
t.Run("RampUpAndPlateau", func(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", `
export let options = {
stages: [
{duration: "10s", target: 10},
{duration: "5s"},
],
};
export default function() {};
`)
if assert.NoError(t, err) {
if assert.Len(t, b.Options.Stages, 2) {
assert.Equal(t, lib.Stage{Duration: types.NullDurationFrom(10 * time.Second), Target: null.IntFrom(10)}, b.Options.Stages[0])
assert.Equal(t, lib.Stage{Duration: types.NullDurationFrom(5 * time.Second)}, b.Options.Stages[1])
}
}
})
})
t.Run("MaxRedirects", func(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", `
export let options = {
maxRedirects: 10,
};
export default function() {};
`)
if assert.NoError(t, err) {
assert.Equal(t, null.IntFrom(10), b.Options.MaxRedirects)
}
})
t.Run("InsecureSkipTLSVerify", func(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", `
export let options = {
insecureSkipTLSVerify: true,
};
export default function() {};
`)
if assert.NoError(t, err) {
assert.Equal(t, null.BoolFrom(true), b.Options.InsecureSkipTLSVerify)
}
})
t.Run("TLSCipherSuites", func(t *testing.T) {
for suiteName, suiteID := range lib.SupportedTLSCipherSuites {
t.Run(suiteName, func(t *testing.T) {
script := `
export let options = {
tlsCipherSuites: ["%s"]
};
export default function() {};
`
script = fmt.Sprintf(script, suiteName)
b, err := getSimpleBundle(t, "/script.js", script)
if assert.NoError(t, err) {
if assert.Len(t, *b.Options.TLSCipherSuites, 1) {
assert.Equal(t, (*b.Options.TLSCipherSuites)[0], suiteID)
}
}
})
}
})
t.Run("TLSVersion", func(t *testing.T) {
t.Run("Object", func(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", `
export let options = {
tlsVersion: {
min: "ssl3.0",
max: "tls1.2"
}
};
export default function() {};
`)
if assert.NoError(t, err) {
assert.Equal(t, b.Options.TLSVersion.Min, lib.TLSVersion(tls.VersionSSL30))
assert.Equal(t, b.Options.TLSVersion.Max, lib.TLSVersion(tls.VersionTLS12))
}
})
t.Run("String", func(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", `
export let options = {
tlsVersion: "ssl3.0"
};
export default function() {};
`)
if assert.NoError(t, err) {
assert.Equal(t, b.Options.TLSVersion.Min, lib.TLSVersion(tls.VersionSSL30))
assert.Equal(t, b.Options.TLSVersion.Max, lib.TLSVersion(tls.VersionSSL30))
}
})
})
t.Run("Thresholds", func(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", `
export let options = {
thresholds: {
http_req_duration: ["avg<100"],
},
};
export default function() {};
`)
if assert.NoError(t, err) {
if assert.Len(t, b.Options.Thresholds["http_req_duration"].Thresholds, 1) {
assert.Equal(t, "avg<100", b.Options.Thresholds["http_req_duration"].Thresholds[0].Source)
}
}
})
})
}
func getArchive(tb testing.TB, data string, rtOpts lib.RuntimeOptions) (*lib.Archive, error) {
b, err := getSimpleBundle(tb, "script.js", data, rtOpts)
if err != nil {
return nil, err
}
return b.makeArchive(), nil
}
func TestNewBundleFromArchive(t *testing.T) {
t.Parallel()
es5Code := `module.exports.options = { vus: 12345 }; module.exports.default = function() { return "hi!" };`
es6Code := `export let options = { vus: 12345 }; export default function() { return "hi!"; };`
baseCompatModeRtOpts := lib.RuntimeOptions{CompatibilityMode: null.StringFrom(lib.CompatibilityModeBase.String())}
extCompatModeRtOpts := lib.RuntimeOptions{CompatibilityMode: null.StringFrom(lib.CompatibilityModeExtended.String())}
logger := testutils.NewLogger(t)
checkBundle := func(t *testing.T, b *Bundle) {
assert.Equal(t, lib.Options{VUs: null.IntFrom(12345)}, b.Options)
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
val, err := bi.exports[consts.DefaultFn](goja.Undefined())
require.NoError(t, err)
assert.Equal(t, "hi!", val.Export())
}
checkArchive := func(t *testing.T, arc *lib.Archive, rtOpts lib.RuntimeOptions, expError string) {
b, err := NewBundleFromArchive(logger, arc, rtOpts)
if expError != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), expError)
} else {
require.NoError(t, err)
checkBundle(t, b)
}
}
t.Run("es6_script_default", func(t *testing.T) {
t.Parallel()
arc, err := getArchive(t, es6Code, lib.RuntimeOptions{}) // default options
require.NoError(t, err)
require.Equal(t, lib.CompatibilityModeExtended.String(), arc.CompatibilityMode)
checkArchive(t, arc, lib.RuntimeOptions{}, "") // default options
checkArchive(t, arc, extCompatModeRtOpts, "")
checkArchive(t, arc, baseCompatModeRtOpts, "Unexpected reserved word")
})
t.Run("es6_script_explicit", func(t *testing.T) {
t.Parallel()
arc, err := getArchive(t, es6Code, extCompatModeRtOpts)
require.NoError(t, err)
require.Equal(t, lib.CompatibilityModeExtended.String(), arc.CompatibilityMode)
checkArchive(t, arc, lib.RuntimeOptions{}, "")
checkArchive(t, arc, extCompatModeRtOpts, "")
checkArchive(t, arc, baseCompatModeRtOpts, "Unexpected reserved word")
})
t.Run("es5_script_with_extended", func(t *testing.T) {
t.Parallel()
arc, err := getArchive(t, es5Code, lib.RuntimeOptions{})
require.NoError(t, err)
require.Equal(t, lib.CompatibilityModeExtended.String(), arc.CompatibilityMode)
checkArchive(t, arc, lib.RuntimeOptions{}, "")
checkArchive(t, arc, extCompatModeRtOpts, "")
checkArchive(t, arc, baseCompatModeRtOpts, "")
})
t.Run("es5_script", func(t *testing.T) {
t.Parallel()
arc, err := getArchive(t, es5Code, baseCompatModeRtOpts)
require.NoError(t, err)
require.Equal(t, lib.CompatibilityModeBase.String(), arc.CompatibilityMode)
checkArchive(t, arc, lib.RuntimeOptions{}, "")
checkArchive(t, arc, extCompatModeRtOpts, "")
checkArchive(t, arc, baseCompatModeRtOpts, "")
})
t.Run("es6_archive_with_wrong_compat_mode", func(t *testing.T) {
t.Parallel()
arc, err := getArchive(t, es6Code, baseCompatModeRtOpts)
require.Error(t, err)
require.Nil(t, arc)
})
t.Run("messed_up_archive", func(t *testing.T) {
t.Parallel()
arc, err := getArchive(t, es6Code, extCompatModeRtOpts)
require.NoError(t, err)
arc.CompatibilityMode = "blah" // intentionally break the archive
checkArchive(t, arc, lib.RuntimeOptions{}, "invalid compatibility mode") // fails when it uses the archive one
checkArchive(t, arc, extCompatModeRtOpts, "") // works when I force the compat mode
checkArchive(t, arc, baseCompatModeRtOpts, "Unexpected reserved word") // failes because of ES6
})
t.Run("script_options_dont_overwrite_metadata", func(t *testing.T) {
t.Parallel()
code := `export let options = { vus: 12345 }; export default function() { return options.vus; };`
arc := &lib.Archive{
Type: "js",
FilenameURL: &url.URL{Scheme: "file", Path: "/script"},
K6Version: consts.Version,
Data: []byte(code),
Options: lib.Options{VUs: null.IntFrom(999)},
PwdURL: &url.URL{Scheme: "file", Path: "/"},
Filesystems: nil,
}
b, err := NewBundleFromArchive(logger, arc, lib.RuntimeOptions{})
require.NoError(t, err)
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
val, err := bi.exports[consts.DefaultFn](goja.Undefined())
require.NoError(t, err)
assert.Equal(t, int64(999), val.Export())
})
}
func TestOpen(t *testing.T) {
testCases := [...]struct {
name string
openPath string
pwd string
isError bool
isArchiveError bool
}{
{
name: "notOpeningUrls",
openPath: "github.com",
isError: true,
pwd: "/path/to",
},
{
name: "simple",
openPath: "file.txt",
pwd: "/path/to",
},
{
name: "simple with dot",
openPath: "./file.txt",
pwd: "/path/to",
},
{
name: "simple with two dots",
openPath: "../to/file.txt",
pwd: "/path/not",
},
{
name: "fullpath",
openPath: "/path/to/file.txt",
pwd: "/path/to",
},
{
name: "fullpath2",
openPath: "/path/to/file.txt",
pwd: "/path",
},
{
name: "file is dir",
openPath: "/path/to/",
pwd: "/path/to",
isError: true,
},
{
name: "file is missing",
openPath: "/path/to/missing.txt",
isError: true,
},
{
name: "relative1",
openPath: "to/file.txt",
pwd: "/path",
},
{
name: "relative2",
openPath: "./path/to/file.txt",
pwd: "/",
},
{
name: "relative wonky",
openPath: "../path/to/file.txt",
pwd: "/path",
},
{
name: "empty open doesn't panic",
openPath: "",
pwd: "/path",
isError: true,
},
}
fss := map[string]func() (afero.Fs, string, func()){
"MemMapFS": func() (afero.Fs, string, func()) {
fs := afero.NewMemMapFs()
require.NoError(t, fs.MkdirAll("/path/to", 0o755))
require.NoError(t, afero.WriteFile(fs, "/path/to/file.txt", []byte(`hi`), 0o644))
return fs, "", func() {}
},
"OsFS": func() (afero.Fs, string, func()) {
prefix, err := ioutil.TempDir("", "k6_open_test")
require.NoError(t, err)
fs := afero.NewOsFs()
filePath := filepath.Join(prefix, "/path/to/file.txt")
require.NoError(t, fs.MkdirAll(filepath.Join(prefix, "/path/to"), 0o755))
require.NoError(t, afero.WriteFile(fs, filePath, []byte(`hi`), 0o644))
if isWindows {
fs = fsext.NewTrimFilePathSeparatorFs(fs)
}
return fs, prefix, func() { require.NoError(t, os.RemoveAll(prefix)) }
},
}
logger := testutils.NewLogger(t)
for name, fsInit := range fss {
fs, prefix, cleanUp := fsInit()
defer cleanUp()
fs = afero.NewReadOnlyFs(fs)
t.Run(name, func(t *testing.T) {
for _, tCase := range testCases {
tCase := tCase
testFunc := func(t *testing.T) {
openPath := tCase.openPath
// if fullpath prepend prefix
if openPath != "" && (openPath[0] == '/' || openPath[0] == '\\') {
openPath = filepath.Join(prefix, openPath)
}
if isWindows {
openPath = strings.Replace(openPath, `\`, `\\`, -1)
}
pwd := tCase.pwd
if pwd == "" {
pwd = "/path/to/"
}
data := `
export let file = open("` + openPath + `");
export default function() { return file };`
sourceBundle, err := getSimpleBundle(t, filepath.ToSlash(filepath.Join(prefix, pwd, "script.js")), data, fs)
if tCase.isError {
assert.Error(t, err)
return
}
require.NoError(t, err)
arcBundle, err := NewBundleFromArchive(logger, sourceBundle.makeArchive(), lib.RuntimeOptions{})
require.NoError(t, err)
for source, b := range map[string]*Bundle{"source": sourceBundle, "archive": arcBundle} {
b := b
t.Run(source, func(t *testing.T) {
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
v, err := bi.exports[consts.DefaultFn](goja.Undefined())
require.NoError(t, err)
assert.Equal(t, "hi", v.Export())
})
}
}
t.Run(tCase.name, testFunc)
if isWindows {
// windowsify the testcase
tCase.openPath = strings.Replace(tCase.openPath, `/`, `\`, -1)
tCase.pwd = strings.Replace(tCase.pwd, `/`, `\`, -1)
t.Run(tCase.name+" with windows slash", testFunc)
}
}
})
}
}
func TestBundleInstantiate(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", `
export let options = {
vus: 5,
teardownTimeout: '1s',
};
let val = true;
export default function() { return val; }
`)
if !assert.NoError(t, err) {
return
}
logger := testutils.NewLogger(t)
bi, err := b.Instantiate(logger, 0)
if !assert.NoError(t, err) {
return
}
t.Run("Run", func(t *testing.T) {
v, err := bi.exports[consts.DefaultFn](goja.Undefined())
if assert.NoError(t, err) {
assert.Equal(t, true, v.Export())
}
})
t.Run("SetAndRun", func(t *testing.T) {
bi.Runtime.Set("val", false)
v, err := bi.exports[consts.DefaultFn](goja.Undefined())
if assert.NoError(t, err) {
assert.Equal(t, false, v.Export())
}
})
t.Run("Options", func(t *testing.T) {
// Ensure `options` properties are correctly marshalled
jsOptions := bi.Runtime.Get("options").ToObject(bi.Runtime)
vus := jsOptions.Get("vus").Export()
assert.Equal(t, int64(5), vus)
tdt := jsOptions.Get("teardownTimeout").Export()
assert.Equal(t, "1s", tdt)
// Ensure options propagate correctly from outside to the script
optOrig := b.Options.VUs
b.Options.VUs = null.IntFrom(10)
bi2, err := b.Instantiate(logger, 0)
assert.NoError(t, err)
jsOptions = bi2.Runtime.Get("options").ToObject(bi2.Runtime)
vus = jsOptions.Get("vus").Export()
assert.Equal(t, int64(10), vus)
b.Options.VUs = optOrig
})
}
func TestBundleEnv(t *testing.T) {
rtOpts := lib.RuntimeOptions{Env: map[string]string{
"TEST_A": "1",
"TEST_B": "",
}}
data := `
export default function() {
if (__ENV.TEST_A !== "1") { throw new Error("Invalid TEST_A: " + __ENV.TEST_A); }
if (__ENV.TEST_B !== "") { throw new Error("Invalid TEST_B: " + __ENV.TEST_B); }
}
`
b1, err := getSimpleBundle(t, "/script.js", data, rtOpts)
if !assert.NoError(t, err) {
return
}
logger := testutils.NewLogger(t)
b2, err := NewBundleFromArchive(logger, b1.makeArchive(), lib.RuntimeOptions{})
if !assert.NoError(t, err) {
return
}
bundles := map[string]*Bundle{"Source": b1, "Archive": b2}
for name, b := range bundles {
b := b
t.Run(name, func(t *testing.T) {
assert.Equal(t, "1", b.Env["TEST_A"])
assert.Equal(t, "", b.Env["TEST_B"])
bi, err := b.Instantiate(logger, 0)
if assert.NoError(t, err) {
_, err := bi.exports[consts.DefaultFn](goja.Undefined())
assert.NoError(t, err)
}
})
}
}
func TestBundleNotSharable(t *testing.T) {
data := `
export default function() {
if (__ITER == 0) {
if (typeof __ENV.something !== "undefined") {
throw new Error("invalid something: " + __ENV.something + " should be undefined");
}
__ENV.something = __VU;
} else if (__ENV.something != __VU) {
throw new Error("invalid something: " + __ENV.something+ " should be "+ __VU);
}
}
`
b1, err := getSimpleBundle(t, "/script.js", data)
if !assert.NoError(t, err) {
return
}
logger := testutils.NewLogger(t)
b2, err := NewBundleFromArchive(logger, b1.makeArchive(), lib.RuntimeOptions{})
if !assert.NoError(t, err) {
return
}
bundles := map[string]*Bundle{"Source": b1, "Archive": b2}
vus, iters := 10, 1000
for name, b := range bundles {
b := b
t.Run(name, func(t *testing.T) {
for i := 0; i < vus; i++ {
bi, err := b.Instantiate(logger, int64(i))
require.NoError(t, err)
for j := 0; j < iters; j++ {
bi.Runtime.Set("__ITER", j)
_, err := bi.exports[consts.DefaultFn](goja.Undefined())
assert.NoError(t, err)
}
}
})
}
}
func TestBundleMakeArchive(t *testing.T) {
testCases := []struct {
cm lib.CompatibilityMode
script string
exclaim string
}{
{
lib.CompatibilityModeExtended, `
import exclaim from "./exclaim.js";
export let options = { vus: 12345 };
export let file = open("./file.txt");
export default function() { return exclaim(file); };`,
`export default function(s) { return s + "!" };`,
},
{
lib.CompatibilityModeBase, `
var exclaim = require("./exclaim.js");
module.exports.options = { vus: 12345 };
module.exports.file = open("./file.txt");
module.exports.default = function() { return exclaim(module.exports.file); };`,
`module.exports.default = function(s) { return s + "!" };`,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.cm.String(), func(t *testing.T) {
fs := afero.NewMemMapFs()
_ = fs.MkdirAll("/path/to", 0o755)
_ = afero.WriteFile(fs, "/path/to/file.txt", []byte(`hi`), 0o644)
_ = afero.WriteFile(fs, "/path/to/exclaim.js", []byte(tc.exclaim), 0o644)
rtOpts := lib.RuntimeOptions{CompatibilityMode: null.StringFrom(tc.cm.String())}
b, err := getSimpleBundle(t, "/path/to/script.js", tc.script, fs, rtOpts)
assert.NoError(t, err)
arc := b.makeArchive()
assert.Equal(t, "js", arc.Type)
assert.Equal(t, lib.Options{VUs: null.IntFrom(12345)}, arc.Options)
assert.Equal(t, "file:///path/to/script.js", arc.FilenameURL.String())
assert.Equal(t, tc.script, string(arc.Data))
assert.Equal(t, "file:///path/to/", arc.PwdURL.String())
exclaimData, err := afero.ReadFile(arc.Filesystems["file"], "/path/to/exclaim.js")
assert.NoError(t, err)
assert.Equal(t, tc.exclaim, string(exclaimData))
fileData, err := afero.ReadFile(arc.Filesystems["file"], "/path/to/file.txt")
assert.NoError(t, err)
assert.Equal(t, `hi`, string(fileData))
assert.Equal(t, consts.Version, arc.K6Version)
assert.Equal(t, tc.cm.String(), arc.CompatibilityMode)
})
}
}