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

Make newTestSetup rely on modulestest #59

Merged
merged 5 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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: 12 additions & 12 deletions webcrypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func TestGetRandomValues(t *testing.T) {

ts := newTestSetup(t)

gotScriptErr := ts.ev.Start(func() error {
_, err := ts.rt.RunString(`
gotScriptErr := ts.EventLoop.Start(func() error {
_, err := ts.VU.Runtime().RunString(`
var input = new Uint8Array(10);
oleiade marked this conversation as resolved.
Show resolved Hide resolved
var output = crypto.getRandomValues(input);

Expand Down Expand Up @@ -122,7 +122,7 @@ func TestGetRandomValuesSupportedTypedArrays(t *testing.T) {
}

for _, tc := range testCases {
gotScriptErr := ts.ev.Start(func() error {
gotScriptErr := ts.EventLoop.Start(func() error {
script := fmt.Sprintf(`
var buf = new %s(10);
crypto.getRandomValues(buf);
Expand All @@ -132,7 +132,7 @@ func TestGetRandomValuesSupportedTypedArrays(t *testing.T) {
}
`, tc.typedArray)

_, err := ts.rt.RunString(script)
_, err := ts.VU.Runtime().RunString(script)
return err
})

Expand All @@ -157,8 +157,8 @@ func TestGetRandomValuesQuotaExceeded(t *testing.T) {

ts := newTestSetup(t)

gotScriptErr := ts.ev.Start(func() error {
_, err := ts.rt.RunString(`
gotScriptErr := ts.EventLoop.Start(func() error {
_, err := ts.VU.Runtime().RunString(`
var buf = new Uint8Array(1000000000);
crypto.getRandomValues(buf);
`)
Expand All @@ -181,8 +181,8 @@ func TestRandomUUIDIsInTheNamespaceFormat(t *testing.T) {

ts := newTestSetup(t)

gotScriptErr := ts.ev.Start(func() error {
_, err := ts.rt.RunString(`
gotScriptErr := ts.EventLoop.Start(func() error {
_, err := ts.VU.Runtime().RunString(`
const iterations = 256;
const uuids = new Set();

Expand Down Expand Up @@ -221,8 +221,8 @@ func TestRandomUUIDVersion(t *testing.T) {

ts := newTestSetup(t)

gotScriptErr := ts.ev.Start(func() error {
_, err := ts.rt.RunString(`
gotScriptErr := ts.EventLoop.Start(func() error {
_, err := ts.VU.Runtime().RunString(`
const iterations = 256;
const uuids = new Set();

Expand Down Expand Up @@ -262,8 +262,8 @@ func TestRandomUUIDVariant(t *testing.T) {

ts := newTestSetup(t)

gotScriptErr := ts.ev.Start(func() error {
_, err := ts.rt.RunString(`
gotScriptErr := ts.EventLoop.Start(func() error {
_, err := ts.VU.Runtime().RunString(`
const iterations = 256;
const uuids = new Set();

Expand Down
56 changes: 23 additions & 33 deletions webcrypto/subtle_crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (
func TestSubtleDigest(t *testing.T) {
t.Parallel()

ts := newTestSetup(t)
digestTestScript, err := CompileFile("./tests", "digest.js")
assert.NoError(t, err)

gotScriptErr := ts.ev.Start(func() error {
_, err := ts.rt.RunProgram(digestTestScript)
ts := newTestSetup(t)
gotScriptErr := ts.EventLoop.Start(func() error {
_, err := ts.VU.Runtime().RunProgram(digestTestScript)
return err
})

Expand All @@ -36,14 +36,11 @@ func TestSubtleCryptoGenerateKey(t *testing.T) {
t.Parallel()

ts := newTestSetup(t)
err := ts.rt.GlobalObject().Set("CryptoKey", CryptoKey{})
require.NoError(t, err)

gotScriptErr := ts.ev.Start(func() error {
err := executeTestScripts(ts.rt, "./tests/generateKey", "successes.js")
gotScriptErr := ts.EventLoop.Start(func() error {
err := executeTestScripts(ts.VU.Runtime(), "./tests/generateKey", "successes.js")
require.NoError(t, err)

_, err = ts.rt.RunString(`run_test()`)
_, err = ts.VU.Runtime().RunString(`run_test()`)

return err
})
Expand All @@ -55,14 +52,11 @@ func TestSubtleCryptoGenerateKey(t *testing.T) {
t.Parallel()

ts := newTestSetup(t)
err := ts.rt.GlobalObject().Set("CryptoKey", CryptoKey{})
require.NoError(t, err)

gotScriptErr := ts.ev.Start(func() error {
err := executeTestScripts(ts.rt, "./tests/generateKey", "failures.js")
gotScriptErr := ts.EventLoop.Start(func() error {
err := executeTestScripts(ts.VU.Runtime(), "./tests/generateKey", "failures.js")
require.NoError(t, err)

_, err = ts.rt.RunString(`run_test()`)
_, err = ts.VU.Runtime().RunString(`run_test()`)

return err
})
Expand All @@ -78,11 +72,8 @@ func TestSubtleCryptoImportExportKey(t *testing.T) {
t.Parallel()

ts := newTestSetup(t)
err := ts.rt.GlobalObject().Set("CryptoKey", CryptoKey{})
require.NoError(t, err)

gotScriptErr := ts.ev.Start(func() error {
err := executeTestScripts(ts.rt, "./tests/import_export", "symmetric.js")
gotScriptErr := ts.EventLoop.Start(func() error {
err := executeTestScripts(ts.VU.Runtime(), "./tests/import_export", "symmetric.js")

return err
})
Expand All @@ -98,12 +89,11 @@ func TestSubtleCryptoEncryptDecrypt(t *testing.T) {
t.Parallel()

ts := newTestSetup(t)

gotScriptErr := ts.ev.Start(func() error {
err := executeTestScripts(ts.rt, "./tests/encrypt_decrypt", "aes_cbc_vectors.js", "aes.js")
gotScriptErr := ts.EventLoop.Start(func() error {
err := executeTestScripts(ts.VU.Runtime(), "./tests/encrypt_decrypt", "aes_cbc_vectors.js", "aes.js")
require.NoError(t, err)

_, err = ts.rt.RunString(`run_test()`)
_, err = ts.VU.Runtime().RunString(`run_test()`)

return err
})
Expand All @@ -116,11 +106,11 @@ func TestSubtleCryptoEncryptDecrypt(t *testing.T) {

ts := newTestSetup(t)

gotScriptErr := ts.ev.Start(func() error {
err := executeTestScripts(ts.rt, "./tests/encrypt_decrypt", "aes_ctr_vectors.js", "aes.js")
gotScriptErr := ts.EventLoop.Start(func() error {
err := executeTestScripts(ts.VU.Runtime(), "./tests/encrypt_decrypt", "aes_ctr_vectors.js", "aes.js")
require.NoError(t, err)

_, err = ts.rt.RunString(`run_test()`)
_, err = ts.VU.Runtime().RunString(`run_test()`)

return err
})
Expand All @@ -137,11 +127,11 @@ func TestSubtleCryptoEncryptDecrypt(t *testing.T) {

ts := newTestSetup(t)

gotScriptErr := ts.ev.Start(func() error {
err := executeTestScripts(ts.rt, "./tests/encrypt_decrypt", "aes_gcm_96_iv_fixtures.js", "aes_gcm_vectors.js", "aes.js")
gotScriptErr := ts.EventLoop.Start(func() error {
err := executeTestScripts(ts.VU.Runtime(), "./tests/encrypt_decrypt", "aes_gcm_96_iv_fixtures.js", "aes_gcm_vectors.js", "aes.js")
require.NoError(t, err)

_, err = ts.rt.RunString(`run_test()`)
_, err = ts.VU.Runtime().RunString(`run_test()`)

return err
})
Expand All @@ -158,11 +148,11 @@ func TestSubtleCryptoSignVerify(t *testing.T) {

ts := newTestSetup(t)

gotScriptErr := ts.ev.Start(func() error {
err := executeTestScripts(ts.rt, "./tests/sign_verify", "hmac_vectors.js", "hmac.js")
gotScriptErr := ts.EventLoop.Start(func() error {
err := executeTestScripts(ts.VU.Runtime(), "./tests/sign_verify", "hmac_vectors.js", "hmac.js")
require.NoError(t, err)

_, err = ts.rt.RunString(`run_test()`)
_, err = ts.VU.Runtime().RunString(`run_test()`)

return err
})
Expand Down
72 changes: 8 additions & 64 deletions webcrypto/test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,15 @@ import (

"github.com/dop251/goja"
"github.com/stretchr/testify/require"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/eventloop"
"go.k6.io/k6/js/modulestest"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/testutils/httpmultibin"
"go.k6.io/k6/metrics"
"gopkg.in/guregu/null.v3"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropping this dependancies is breaking the ci - running go mod tidy will fix it.

)

// testSetup is a helper struct holding components
// necessary to test the redis client, in the context
// of the execution of a k6 script.
type testSetup struct {
rt *goja.Runtime
state *lib.State
samples chan metrics.SampleContainer
ev *eventloop.EventLoop
}

// newTestSetup initializes a new test setup.
// It prepares a test setup with a mocked redis server and a goja runtime,
// and event loop, ready to execute scripts as if being executed in the
// main context of k6.
func newTestSetup(t testing.TB) testSetup {
tb := httpmultibin.NewHTTPMultiBin(t)

rt := goja.New()
rt.SetFieldNameMapper(common.FieldNameMapper{})
func newTestSetup(t testing.TB) *modulestest.Runtime {
ts := modulestest.NewRuntime(t)

// We compile the Web Platform testharness script into a goja.Program
harnessProgram, err := CompileFile("./tests/util", "testharness.js")
Expand All @@ -45,7 +26,7 @@ func newTestSetup(t testing.TB) testSetup {
// We execute the harness script in the goja runtime
// in order to make the Web Platform assertion functions available
// to the tests.
_, err = rt.RunProgram(harnessProgram)
_, err = ts.VU.Runtime().RunProgram(harnessProgram)
require.NoError(t, err)

// We compile the Web Platform helpers script into a goja.Program
Expand All @@ -54,51 +35,14 @@ func newTestSetup(t testing.TB) testSetup {
// We execute the helpers script in the goja runtime
// in order to make the Web Platform helpers available
// to the tests.
_, err = rt.RunProgram(helpersProgram)
_, err = ts.VU.Runtime().RunProgram(helpersProgram)
require.NoError(t, err)

root, err := lib.NewGroup("", nil)
require.NoError(t, err)

samples := make(chan metrics.SampleContainer, 1000)

state := &lib.State{
Group: root,
Dialer: tb.Dialer,
Options: lib.Options{
SystemTags: metrics.NewSystemTagSet(
metrics.TagURL,
metrics.TagProto,
metrics.TagStatus,
metrics.TagSubproto,
),
UserAgent: null.StringFrom("TestUserAgent"),
},
Samples: samples,
TLSConfig: tb.TLSClientConfig,
BuiltinMetrics: metrics.RegisterBuiltinMetrics(metrics.NewRegistry()),
Tags: lib.NewVUStateTags(metrics.NewRegistry().RootTagSet()),
}

vu := &modulestest.VU{
CtxField: tb.Context,
InitEnvField: &common.InitEnvironment{},
RuntimeField: rt,
StateField: state,
}
m := new(RootModule).NewModuleInstance(ts.VU)
require.NoError(t, ts.VU.Runtime().Set("crypto", m.Exports().Named["crypto"]))
require.NoError(t, ts.VU.Runtime().GlobalObject().Set("CryptoKey", CryptoKey{}))
oleiade marked this conversation as resolved.
Show resolved Hide resolved

m := new(RootModule).NewModuleInstance(vu)
require.NoError(t, rt.Set("crypto", m.Exports().Named["crypto"]))

ev := eventloop.New(vu)
vu.RegisterCallbackField = ev.RegisterCallback

return testSetup{
rt: rt,
state: state,
samples: samples,
ev: ev,
}
return ts
}

// CompileFile compiles a javascript file as a goja.Program.
Expand Down