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

Commit 86f96f4

Browse files
authored
Merge pull request #627 from grafana/feat/414-force-browser-opts-in-cloud
Force browser opts in cloud
2 parents 0d1650a + 8bef8b9 commit 86f96f4

File tree

8 files changed

+76
-9
lines changed

8 files changed

+76
-9
lines changed

chromium/browser_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (b *BrowserType) Launch(opts goja.Value) api.Browser {
132132
k6ext.Panic(ctx, "setting up logger: %w", err)
133133
}
134134

135-
launchOpts := common.NewLaunchOptions()
135+
launchOpts := common.NewLaunchOptions(k6ext.OnCloud())
136136
if err := launchOpts.Parse(ctx, opts, b.logger); err != nil {
137137
k6ext.Panic(ctx, "parsing launch options: %w", err)
138138
}

common/browser_options.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/dop251/goja"
9+
"golang.org/x/exp/slices"
910

1011
"github.com/grafana/xk6-browser/k6ext"
1112
"github.com/grafana/xk6-browser/log"
@@ -32,6 +33,8 @@ type LaunchOptions struct {
3233
Proxy ProxyOptions
3334
SlowMo time.Duration
3435
Timeout time.Duration
36+
37+
onCloud bool // some options will be ignored when running in the cloud
3538
}
3639

3740
// LaunchPersistentContextOptions stores browser launch options for persistent context.
@@ -41,12 +44,13 @@ type LaunchPersistentContextOptions struct {
4144
}
4245

4346
// NewLaunchOptions returns a new LaunchOptions.
44-
func NewLaunchOptions() *LaunchOptions {
47+
func NewLaunchOptions(onCloud bool) *LaunchOptions {
4548
return &LaunchOptions{
4649
Env: make(map[string]string),
4750
Headless: true,
4851
LogCategoryFilter: ".*",
4952
Timeout: DefaultTimeout,
53+
onCloud: onCloud,
5054
}
5155
}
5256

@@ -66,6 +70,10 @@ func (l *LaunchOptions) Parse(ctx context.Context, opts goja.Value, logger *log.
6670
}
6771
)
6872
for _, k := range o.Keys() {
73+
if l.shouldIgnoreOnCloud(k) {
74+
logger.Warnf("LaunchOptions", "setting %s option is disallowed on cloud.", k)
75+
continue
76+
}
6977
v := o.Get(k)
7078
if v.Export() == nil {
7179
if dv, ok := defaults[k]; ok {
@@ -105,3 +113,10 @@ func (l *LaunchOptions) Parse(ctx context.Context, opts goja.Value, logger *log.
105113

106114
return nil
107115
}
116+
117+
func (l *LaunchOptions) shouldIgnoreOnCloud(opt string) bool {
118+
if !l.onCloud {
119+
return false
120+
}
121+
return slices.Contains([]string{"devtools", "executablePath", "headless"}, opt)
122+
}

common/browser_options_test.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ func TestBrowserLaunchOptionsParse(t *testing.T) {
1515
t.Parallel()
1616

1717
for name, tt := range map[string]struct {
18-
opts map[string]any
19-
assert func(testing.TB, *LaunchOptions)
20-
err string
18+
opts map[string]any
19+
assert func(testing.TB, *LaunchOptions)
20+
err string
21+
onCloud bool
2122
}{
2223
"defaults": {
2324
opts: map[string]any{},
@@ -31,6 +32,44 @@ func TestBrowserLaunchOptionsParse(t *testing.T) {
3132
}, lo)
3233
},
3334
},
35+
"defaults_on_cloud": {
36+
onCloud: true,
37+
opts: map[string]any{
38+
// disallow changing the following opts
39+
"headless": false,
40+
"devtools": true,
41+
"executablePath": "something else",
42+
// allow changing the following opts
43+
"args": []string{"any"},
44+
"debug": true,
45+
"env": map[string]string{"some": "thing"},
46+
"ignoreDefaultArgs": []string{"any"},
47+
"logCategoryFilter": "...",
48+
"proxy": ProxyOptions{Server: "srv"},
49+
"slowMo": time.Second,
50+
"timeout": time.Second,
51+
},
52+
assert: func(tb testing.TB, lo *LaunchOptions) {
53+
tb.Helper()
54+
assert.Equal(t, &LaunchOptions{
55+
// disallowed:
56+
Headless: true,
57+
Devtools: false,
58+
ExecutablePath: "",
59+
// allowed:
60+
Args: []string{"any"},
61+
Debug: true,
62+
Env: map[string]string{"some": "thing"},
63+
IgnoreDefaultArgs: []string{"any"},
64+
LogCategoryFilter: "...",
65+
Proxy: ProxyOptions{Server: "srv"},
66+
SlowMo: time.Second,
67+
Timeout: time.Second,
68+
69+
onCloud: true,
70+
}, lo)
71+
},
72+
},
3473
"nulls": { // don't override the defaults on `null`
3574
opts: map[string]any{
3675
"env": nil,
@@ -212,7 +251,7 @@ func TestBrowserLaunchOptionsParse(t *testing.T) {
212251
t.Parallel()
213252
var (
214253
vu = k6test.NewVU(t)
215-
lo = NewLaunchOptions()
254+
lo = NewLaunchOptions(tt.onCloud)
216255
)
217256
err := lo.Parse(vu.Context(), vu.ToGojaValue(tt.opts), log.NewNullLogger())
218257
if tt.err != "" {

common/browser_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestBrowserNewPageInContext(t *testing.T) {
2424
newTestCase := func(id cdp.BrowserContextID) *testCase {
2525
ctx, cancel := context.WithCancel(context.Background())
2626
logger := log.NewNullLogger()
27-
b := newBrowser(ctx, cancel, nil, NewLaunchOptions(), logger)
27+
b := newBrowser(ctx, cancel, nil, NewLaunchOptions(false), logger)
2828
// set a new browser context in the browser with `id`, so that newPageInContext can find it.
2929
b.contexts[id] = NewBrowserContext(ctx, b, id, nil, nil)
3030
return &testCase{

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/sirupsen/logrus v1.9.0
1515
github.com/stretchr/testify v1.8.0
1616
go.k6.io/k6 v0.41.0
17+
golang.org/x/exp v0.0.0-20221106115401-f9659909a136
1718
golang.org/x/net v0.1.0
1819
gopkg.in/guregu/null.v3 v3.5.0
1920
)

go.sum

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
138138
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
139139
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
140140
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
141-
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
141+
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
142142
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
143143
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
144144
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
@@ -282,6 +282,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
282282
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
283283
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
284284
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
285+
golang.org/x/exp v0.0.0-20221106115401-f9659909a136 h1:Fq7F/w7MAa1KJ5bt2aJ62ihqp9HDcRuyILskkpIAurw=
286+
golang.org/x/exp v0.0.0-20221106115401-f9659909a136/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
285287
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
286288
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
287289
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
@@ -479,7 +481,6 @@ golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
479481
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
480482
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
481483
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
482-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
483484
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
484485
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
485486
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=

k6ext/cloud.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package k6ext
2+
3+
import "os"
4+
5+
// OnCloud returns true if xk6-browser runs in the cloud.
6+
func OnCloud() bool {
7+
_, ok := os.LookupEnv("K6_CLOUDRUN_INSTANCE_ID")
8+
return ok
9+
}

k6ext/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package k6ext acts as an encapsulation layer between the k6 core and xk6-browser.
2+
package k6ext

0 commit comments

Comments
 (0)