Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to k6 lookupEnv and abstract env. var. lookup #918

Merged
merged 28 commits into from
Jun 6, 2023
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0d39d40
Rename RootModule.once to initOnce for clarity
inancgumus Jun 1, 2023
42fc913
Move RootModule init to a function
inancgumus Jun 2, 2023
cd861d7
Move debug server to NewModuleInstance
inancgumus Jun 2, 2023
cd85bb2
Fix abort if remote registry fails to register
inancgumus Jun 2, 2023
27c97e2
Add empty LookupEnv field to module test
inancgumus Jun 2, 2023
97d5c3a
Add LookupEnv to k6test.NewVU
inancgumus Jun 1, 2023
63bc0ec
Refactor NewBrowserType slightly
inancgumus Jun 2, 2023
bdb8727
Use k6 lookup in BrowserType and RootModule.init
inancgumus Jun 2, 2023
6d8dd09
Add withLookupFunc to testBrowser
inancgumus Jun 2, 2023
2049cad
Use env.LookupEnv in RootModule.initialize
inancgumus Jun 2, 2023
8871fbd
Remove SetEnvLookupper from BrowserType
inancgumus Jun 2, 2023
bb78b8a
Remove testBrowser.withBrowserOptions
inancgumus Jun 2, 2023
c784100
Remove testBrowser.setupEnvLookupper
inancgumus Jun 2, 2023
11f39a9
Remove browserOptions from tests
inancgumus Jun 2, 2023
34d2c16
Add testingT helper to test for failing tests
inancgumus Jun 2, 2023
6259634
Add TestTestBrowserWithLookupFunc
inancgumus Jun 2, 2023
30161e6
Use TMPDIR from k6 LookupEnv in BrowserType
inancgumus Jun 1, 2023
cb6572f
Use LookupEnv for USERPROFILE in ExecutablePath
inancgumus Jun 1, 2023
ad4ec6f
Use LookupEnv in makeLogger
inancgumus Jun 1, 2023
73b0199
Refactor TestIsRemoteBrowser
inancgumus Jun 1, 2023
a46ed22
Remove the old relic SKIP_FLAKKY
inancgumus Jun 1, 2023
ad81e72
Remove setenv from TestIsRemoteBrowser
inancgumus Jun 1, 2023
6825710
Refactor env.var. names to constants
inancgumus Jun 1, 2023
d2cc3c9
Add environment lookup func helpers
inancgumus Jun 1, 2023
e5fbe42
Remove unnecessary withLookup types
inancgumus Jun 1, 2023
1207e02
Update Dockerfile to use K6_BROWSER_HEADLESS
inancgumus Jun 2, 2023
6ffa156
Update workflows to use K6_BROWSER_HEADLESS
inancgumus Jun 2, 2023
3edd61b
Remove default env lookup from test browser
inancgumus Jun 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 22 additions & 2 deletions tests/test_browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type testBrowser struct {
// It automatically closes it when `t` returns.
//
// opts provides a way to customize the newTestBrowser.
// see: withBrowserOptions for an example.
// see: withFileServer for an example.
func newTestBrowser(tb testing.TB, opts ...any) *testBrowser {
tb.Helper()

Expand All @@ -61,6 +61,9 @@ func newTestBrowser(tb testing.TB, opts ...any) *testBrowser {
enableLogCache = false
skipClose = false
samples = make(chan k6metrics.SampleContainer, 1000)
// default lookup function is os.LookupEnv so that we can
// pass the environment variables while testing, i.e.: K6_BROWSER_LOG.
lookupFunc = os.LookupEnv
)
for _, opt := range opts {
switch opt := opt.(type) {
Expand All @@ -77,6 +80,16 @@ func newTestBrowser(tb testing.TB, opts ...any) *testBrowser {
skipClose = true
case withSamplesListener:
samples = opt
case withLookupFunc:
lookupFunc = func(key string) (string, bool) {
v, ok := opt(key)
if ok {
return v, ok
}
// return from the real environment lookup function
// so that we can debug (or other things) when we want it.
return os.LookupEnv(key)
inancgumus marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

Expand All @@ -89,9 +102,13 @@ func newTestBrowser(tb testing.TB, opts ...any) *testBrowser {
registry := k6metrics.NewRegistry()
k6m := k6ext.RegisterCustomMetrics(registry)
vu.CtxField = k6ext.WithCustomMetrics(vu.Context(), k6m)
vu.InitEnvField.LookupEnv = lookupFunc

bt := chromium.NewBrowserType(vu)

// Delete the pre-init stage data.
vu.MoveToVUContext()

// enable the HTTP test server only when necessary
var (
testServer *k6httpmultibin.HTTPMultiBin
Expand Down Expand Up @@ -131,7 +148,7 @@ func newTestBrowser(tb testing.TB, opts ...any) *testBrowser {

tbr := &testBrowser{
t: tb,
ctx: bt.Ctx, // This context has the additional wrapping of common.WithBrowserOptions
ctx: bt.Ctx,
http: testServer,
vu: vu,
logCache: lc,
Expand Down Expand Up @@ -496,6 +513,9 @@ func setupHTTPTestModuleInstance(tb testing.TB, samples chan k6metrics.SampleCon
return vu
}

// WithLookupFunc is a custom lookup function that returns test values.
type withLookupFunc func(string) (string, bool)

func setupEnvLookupper(tb testing.TB, opts browserOptions) env.LookupFunc {
tb.Helper()

Expand Down