Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit aa0b43d

Browse files
authored
Merge pull request #671 from grafana/fix/k6-incompatibility-opts-hosts
Fix k6 Hosts option incompatibility
2 parents 65d53fe + 8f4aaa6 commit aa0b43d

File tree

5 files changed

+238
-28
lines changed

5 files changed

+238
-28
lines changed

chromium/browser_type.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package chromium
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"math/rand"
@@ -161,10 +162,11 @@ func (b *BrowserType) launch(ctx context.Context, opts *common.LaunchOptions) (*
161162
for k, v := range opts.Env {
162163
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
163164
}
164-
var (
165-
flags = prepareFlags(opts, &(b.vu.State()).Options)
166-
dataDir = b.storage
167-
)
165+
flags, err := prepareFlags(opts, &(b.vu.State()).Options)
166+
if err != nil {
167+
return nil, fmt.Errorf("%w", err)
168+
}
169+
dataDir := b.storage
168170
if err := dataDir.Make("", flags["user-data-dir"]); err != nil {
169171
return nil, fmt.Errorf("%w", err)
170172
}
@@ -279,7 +281,7 @@ func parseArgs(flags map[string]any) ([]string, error) {
279281
return args, nil
280282
}
281283

282-
func prepareFlags(lopts *common.LaunchOptions, k6opts *k6lib.Options) map[string]any {
284+
func prepareFlags(lopts *common.LaunchOptions, k6opts *k6lib.Options) (map[string]any, error) {
283285
// After Puppeteer's and Playwright's default behavior.
284286
f := map[string]any{
285287
"disable-background-networking": true,
@@ -327,9 +329,11 @@ func prepareFlags(lopts *common.LaunchOptions, k6opts *k6lib.Options) map[string
327329
ignoreDefaultArgsFlags(f, lopts.IgnoreDefaultArgs)
328330

329331
setFlagsFromArgs(f, lopts.Args)
330-
setFlagsFromK6Options(f, k6opts)
332+
if err := setFlagsFromK6Options(f, k6opts); err != nil {
333+
return nil, err
334+
}
331335

332-
return f
336+
return f, nil
333337
}
334338

335339
// ignoreDefaultArgsFlags ignores any flags in the provided slice.
@@ -356,24 +360,44 @@ func setFlagsFromArgs(flags map[string]any, args []string) {
356360

357361
// setFlagsFromK6Options adds additional data to flags considering the k6 options.
358362
// Such as: "host-resolver-rules" for blocking requests.
359-
func setFlagsFromK6Options(flags map[string]any, k6opts *k6lib.Options) {
363+
func setFlagsFromK6Options(flags map[string]any, k6opts *k6lib.Options) error {
360364
if k6opts == nil {
361-
return
365+
return nil
362366
}
363367

364368
hostResolver := []string{}
365369
if currHostResolver, ok := flags["host-resolver-rules"]; ok {
366370
hostResolver = append(hostResolver, fmt.Sprintf("%s", currHostResolver))
367371
}
368372

369-
for k, v := range k6opts.Hosts {
373+
// Add the host resolver rules.
374+
//
375+
// This is done by marshaling the k6 hosts option to JSON and then
376+
// unmarshaling it to a map[string]string. This is done because the
377+
// k6 v0.42 changed Hosts from a map to types.NullHosts and doesn't
378+
// expose the map anymore.
379+
//
380+
// TODO: A better way to do this would be to handle the resolver
381+
// rules by communicating with Chromium (and then using Hosts's
382+
// Match method) instead of passing the rules via the command line
383+
// to Chromium.
384+
var rules map[string]string
385+
b, err := json.Marshal(k6opts.Hosts)
386+
if err != nil {
387+
return fmt.Errorf("marshaling hosts option: %w", err)
388+
}
389+
if err := json.Unmarshal(b, &rules); err != nil {
390+
return fmt.Errorf("unmarshaling hosts option: %w", err)
391+
}
392+
for k, v := range rules {
370393
hostResolver = append(hostResolver, fmt.Sprintf("MAP %s %s", k, v))
371394
}
372-
373395
if len(hostResolver) > 0 {
374396
sort.Strings(hostResolver)
375397
flags["host-resolver-rules"] = strings.Join(hostResolver, ",")
376398
}
399+
400+
return nil
377401
}
378402

379403
// makeLogger makes and returns an extension wide logger.

chromium/browser_type_test.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/grafana/xk6-browser/common"
99

1010
k6lib "go.k6.io/k6/lib"
11+
"go.k6.io/k6/lib/types"
12+
k6types "go.k6.io/k6/lib/types"
1113

1214
"github.com/stretchr/testify/assert"
1315
"github.com/stretchr/testify/require"
@@ -16,8 +18,14 @@ import (
1618
func TestBrowserTypePrepareFlags(t *testing.T) {
1719
t.Parallel()
1820

19-
host, err := k6lib.NewHostAddress(net.ParseIP("127.0.0.1"), "8000")
20-
require.NoError(t, err)
21+
// to be used by the tests below
22+
host, err := k6types.NewHost(net.ParseIP("127.0.0.1"), "8000")
23+
require.NoError(t, err, "failed to set up test host")
24+
hosts, err := k6types.NewHosts(map[string]k6types.Host{
25+
"test.k6.io": *host,
26+
"httpbin.test.k6.io": *host,
27+
})
28+
require.NoError(t, err, "failed to set up test hosts")
2129

2230
testCases := []struct {
2331
flag string
@@ -91,10 +99,7 @@ func TestBrowserTypePrepareFlags(t *testing.T) {
9199
`host-resolver-rules="MAP * www.example.com, EXCLUDE *.youtube.*"`,
92100
}},
93101
changeK6Opts: &k6lib.Options{
94-
Hosts: map[string]*k6lib.HostAddress{
95-
"test.k6.io": host,
96-
"httpbin.test.k6.io": host,
97-
},
102+
Hosts: types.NullHosts{Trie: hosts, Valid: true},
98103
},
99104
expChangedVal: "MAP * www.example.com, EXCLUDE *.youtube.*," +
100105
"MAP httpbin.test.k6.io 127.0.0.1:8000,MAP test.k6.io 127.0.0.1:8000",
@@ -141,7 +146,8 @@ func TestBrowserTypePrepareFlags(t *testing.T) {
141146
tc.pre(t)
142147
}
143148

144-
flags := prepareFlags(&common.LaunchOptions{}, nil)
149+
flags, err := prepareFlags(&common.LaunchOptions{}, nil)
150+
require.NoError(t, err, "failed to prepare flags")
145151

146152
if tc.expInitVal != nil {
147153
require.Contains(t, flags, tc.flag)
@@ -151,7 +157,8 @@ func TestBrowserTypePrepareFlags(t *testing.T) {
151157
}
152158

153159
if tc.changeOpts != nil || tc.changeK6Opts != nil {
154-
flags = prepareFlags(tc.changeOpts, tc.changeK6Opts)
160+
flags, err = prepareFlags(tc.changeOpts, tc.changeK6Opts)
161+
require.NoError(t, err, "failed to prepare flags")
155162
if tc.expChangedVal != nil {
156163
assert.Equal(t, tc.expChangedVal, flags[tc.flag])
157164
} else {

go.mod

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.19
44

55
require (
66
github.com/chromedp/cdproto v0.0.0-20221023212508-67ada9507fb2
7-
github.com/dop251/goja v0.0.0-20221025095645-e66ebd086f45
7+
github.com/dop251/goja v0.0.0-20221106173738-3b8a68ca89b4
88
github.com/fatih/color v1.13.0
99
github.com/gorilla/websocket v1.5.0
1010
github.com/hashicorp/go-multierror v1.1.1
@@ -13,44 +13,69 @@ require (
1313
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
1414
github.com/sirupsen/logrus v1.9.0
1515
github.com/stretchr/testify v1.8.0
16-
go.k6.io/k6 v0.41.0
16+
go.k6.io/k6 v0.41.1-0.20221206194017-a2ab89abfdc8
1717
golang.org/x/exp v0.0.0-20221106115401-f9659909a136
1818
golang.org/x/net v0.1.0
1919
gopkg.in/guregu/null.v3 v3.5.0
2020
)
2121

2222
require (
2323
github.com/Azure/go-ntlmssp v0.0.0-20220621081337-cb9428e4ac1e // indirect
24+
github.com/DataDog/datadog-go v0.0.0-20180330214955-e67964b4021a // indirect
2425
github.com/PuerkitoBio/goquery v1.8.0 // indirect
2526
github.com/Soontao/goHttpDigestClient v0.0.0-20170320082612-6d28bb1415c5 // indirect
2627
github.com/andybalholm/brotli v1.0.4 // indirect
2728
github.com/andybalholm/cascadia v1.3.1 // indirect
29+
github.com/beorn7/perks v1.0.1 // indirect
30+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
2831
github.com/chromedp/sysutil v1.0.0 // indirect
2932
github.com/davecgh/go-spew v1.1.1 // indirect
33+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
3034
github.com/dlclark/regexp2 v1.7.0 // indirect
35+
github.com/go-redis/redis/v8 v8.11.5 // indirect
3136
github.com/go-sourcemap/sourcemap v2.1.4-0.20211119122758-180fcef48034+incompatible // indirect
3237
github.com/golang/protobuf v1.5.2 // indirect
38+
github.com/golang/snappy v0.0.4 // indirect
39+
github.com/grafana/xk6-output-prometheus-remote v0.0.8 // indirect
40+
github.com/grafana/xk6-redis v0.1.1 // indirect
41+
github.com/grafana/xk6-timers v0.1.2 // indirect
42+
github.com/grafana/xk6-websockets v0.1.6 // indirect
3343
github.com/hashicorp/errwrap v1.1.0 // indirect
44+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
45+
github.com/influxdata/influxdb1-client v0.0.0-20190402204710-8ff2fc3824fc // indirect
46+
github.com/jhump/protoreflect v1.13.0 // indirect
3447
github.com/josharian/intern v1.0.0 // indirect
3548
github.com/klauspost/compress v1.15.11 // indirect
3649
github.com/mattn/go-colorable v0.1.13 // indirect
3750
github.com/mattn/go-isatty v0.0.16 // indirect
51+
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
3852
github.com/mstoykov/atlas v0.0.0-20220811071828-388f114305dd // indirect
53+
github.com/mstoykov/envconfig v1.4.1-0.20220114105314-765c6d8c76f1 // indirect
54+
github.com/mstoykov/k6-taskqueue-lib v0.1.0 // indirect
3955
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
4056
github.com/onsi/ginkgo v1.16.5 // indirect
4157
github.com/onsi/gomega v1.18.1 // indirect
4258
github.com/pmezard/go-difflib v1.0.0 // indirect
59+
github.com/prometheus/client_golang v1.14.1-0.20221122130035-8b6e68085b10 // indirect
60+
github.com/prometheus/client_model v0.3.0 // indirect
61+
github.com/prometheus/common v0.37.0 // indirect
62+
github.com/prometheus/procfs v0.8.0 // indirect
4363
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e // indirect
4464
github.com/spf13/afero v1.9.2 // indirect
65+
github.com/spf13/cobra v1.4.0 // indirect
66+
github.com/spf13/pflag v1.0.5 // indirect
4567
github.com/tidwall/gjson v1.14.3 // indirect
4668
github.com/tidwall/match v1.1.1 // indirect
4769
github.com/tidwall/pretty v1.2.1 // indirect
70+
go.buf.build/grpc/go/gogo/protobuf v1.4.9 // indirect
71+
go.buf.build/grpc/go/prometheus/prometheus v1.4.4 // indirect
4872
golang.org/x/crypto v0.1.0 // indirect
4973
golang.org/x/sys v0.1.0 // indirect
74+
golang.org/x/term v0.1.0 // indirect
5075
golang.org/x/text v0.4.0 // indirect
5176
golang.org/x/time v0.1.0 // indirect
52-
google.golang.org/genproto v0.0.0-20220308174144-ae0e22291548 // indirect
77+
google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 // indirect
5378
google.golang.org/grpc v1.49.0 // indirect
54-
google.golang.org/protobuf v1.27.1 // indirect
79+
google.golang.org/protobuf v1.28.1 // indirect
5580
gopkg.in/yaml.v3 v3.0.1 // indirect
5681
)

0 commit comments

Comments
 (0)