forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk6_test.go
86 lines (77 loc) · 1.86 KB
/
k6_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
package k6_test
import (
"context"
"net/url"
"path/filepath"
"strings"
"testing"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/k6"
)
func TestK6(t *testing.T) {
testCases := []struct {
title string
script string
expect int
}{
{
title: "Passing test",
script: "pass.js",
expect: 0,
},
{
title: "Failing test",
script: "fail.js",
expect: 108,
},
{
title: "Passing remote test",
script: "https://raw.githubusercontent.com/testcontainers/testcontainers-go/main/modules/k6/scripts/pass.js",
expect: 0,
},
{
title: "Failing remote test",
script: "https://raw.githubusercontent.com/testcontainers/testcontainers-go/main/modules/k6/scripts/fail.js",
expect: 108,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.title, func(t *testing.T) {
ctx := context.Background()
var options testcontainers.CustomizeRequestOption
if !strings.HasPrefix(tc.script, "http") {
absPath, err := filepath.Abs(filepath.Join("scripts", tc.script))
if err != nil {
t.Fatal(err)
}
options = k6.WithTestScript(absPath)
} else {
uri, err := url.Parse(tc.script)
if err != nil {
t.Fatal(err)
}
desc := k6.DownloadableFile{Uri: *uri, DownloadDir: t.TempDir()}
options = k6.WithRemoteTestScript(desc)
}
container, err := k6.RunContainer(ctx, k6.WithCache(), options)
if err != nil {
t.Fatal(err)
}
// Clean up the container after the test is complete
t.Cleanup(func() {
if err := container.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})
// assert the result of the test
state, err := container.State(ctx)
if err != nil {
t.Fatal(err)
}
if state.ExitCode != tc.expect {
t.Fatalf("expected %d got %d", tc.expect, state.ExitCode)
}
})
}
}