Skip to content

Commit

Permalink
pass in new module fn
Browse files Browse the repository at this point in the history
  • Loading branch information
ettec committed Jan 21, 2025
1 parent c9bd411 commit e1fa917
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
4 changes: 2 additions & 2 deletions pkg/workflows/wasm/host/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func WithDeterminism() func(*ModuleConfig) {
}
}

func NewModule(modCfg *ModuleConfig, binary []byte, opts ...func(*ModuleConfig)) (*Module, error) {
func NewModule(modCfg *ModuleConfig, binary []byte, newWasmModule func(engine *wasmtime.Engine, wasm []byte) (*wasmtime.Module, error), opts ...func(*ModuleConfig)) (*Module, error) {
// Apply options to the module config.
for _, opt := range opts {
opt(modCfg)
Expand Down Expand Up @@ -192,7 +192,7 @@ func NewModule(modCfg *ModuleConfig, binary []byte, opts ...func(*ModuleConfig))
binary = decompedBinary
}

mod, err := wasmtime.NewModule(engine, binary)
mod, err := newWasmModule(engine, binary)
if err != nil {
return nil, fmt.Errorf("error creating wasmtime module: %w", err)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/workflows/wasm/host/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"

"github.com/bytecodealliance/wasmtime-go/v23"
"github.com/google/uuid"

"google.golang.org/protobuf/types/known/emptypb"
Expand All @@ -13,8 +14,9 @@ import (
wasmpb "github.com/smartcontractkit/chainlink-common/pkg/workflows/wasm/pb"
)

func GetWorkflowSpec(ctx context.Context, modCfg *ModuleConfig, binary []byte, config []byte) (*sdk.WorkflowSpec, error) {
m, err := NewModule(modCfg, binary, WithDeterminism())
func GetWorkflowSpec(ctx context.Context, modCfg *ModuleConfig, binary []byte,
newWasmModule func(engine *wasmtime.Engine, wasm []byte) (*wasmtime.Module, error), config []byte) (*sdk.WorkflowSpec, error) {
m, err := NewModule(modCfg, binary, newWasmModule, WithDeterminism())
if err != nil {
return nil, fmt.Errorf("could not instantiate module: %w", err)
}
Expand Down
54 changes: 30 additions & 24 deletions pkg/workflows/wasm/host/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/andybalholm/brotli"
"github.com/bytecodealliance/wasmtime-go/v23"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -98,6 +99,7 @@ func Test_GetWorkflowSpec(t *testing.T) {
IsUncompressed: true,
},
binary,
wasmtime.NewModule,
[]byte(""),
)
require.NoError(t, err)
Expand All @@ -115,6 +117,7 @@ func Test_GetWorkflowSpec_UncompressedBinary(t *testing.T) {
IsUncompressed: false,
},
binary,
wasmtime.NewModule,
[]byte(""),
)
require.NoError(t, err)
Expand All @@ -131,6 +134,7 @@ func Test_GetWorkflowSpec_BinaryErrors(t *testing.T) {
IsUncompressed: true,
},
failBinary,
wasmtime.NewModule,
[]byte(""),
)
// panic
Expand All @@ -151,6 +155,7 @@ func Test_GetWorkflowSpec_Timeout(t *testing.T) {
IsUncompressed: true,
},
binary, // use the success binary with a zero timeout
wasmtime.NewModule,
[]byte(""),
)
// panic
Expand All @@ -169,6 +174,7 @@ func Test_GetWorkflowSpec_BuildError(t *testing.T) {
IsUncompressed: true,
},
binary,
wasmtime.NewModule,
[]byte(""),
)
assert.ErrorContains(t, err, "oops")
Expand All @@ -186,7 +192,7 @@ func Test_Compute_Logs(t *testing.T) {
Fetch: func(ctx context.Context, req *wasmpb.FetchRequest) (*wasmpb.FetchResponse, error) {
return nil, nil
},
}, binary)
}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -277,7 +283,7 @@ func Test_Compute_Emit(t *testing.T) {
assert.Equal(t, "workflow-execution-id", kvs["workflow_execution_id"])
return nil
}),
}, binary)
}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -305,7 +311,7 @@ func Test_Compute_Emit(t *testing.T) {

return assert.AnError
}),
}, binary)
}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -338,7 +344,7 @@ func Test_Compute_Emit(t *testing.T) {
Labeler: newMockMessageEmitter(func(_ context.Context, msg string, labels map[string]string) error {
return nil
}), // never called
}, binary)
}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -373,7 +379,7 @@ func Test_Compute_PanicIsRecovered(t *testing.T) {
m, err := NewModule(&ModuleConfig{
Logger: logger.Test(t),
IsUncompressed: true,
}, binary)
}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -420,7 +426,7 @@ func Test_Compute_Fetch(t *testing.T) {
StatusCode: uint32(expected.StatusCode),
}, nil
},
}, binary)
}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -471,7 +477,7 @@ func Test_Compute_Fetch(t *testing.T) {
StatusCode: uint32(expected.StatusCode),
}, nil
},
}, binary)
}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -516,7 +522,7 @@ func Test_Compute_Fetch(t *testing.T) {
Fetch: func(ctx context.Context, req *wasmpb.FetchRequest) (*wasmpb.FetchResponse, error) {
return nil, assert.AnError
},
}, binary)
}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -574,7 +580,7 @@ func Test_Compute_Fetch(t *testing.T) {
StatusCode: uint32(expected.StatusCode),
}, nil
},
}, binary)
}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -623,7 +629,7 @@ func Test_Compute_Fetch(t *testing.T) {
return &wasmpb.FetchResponse{}, nil
}
},
}, binary)
}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -675,7 +681,7 @@ func Test_Compute_Fetch(t *testing.T) {
}, nil
},
MaxFetchRequests: 1,
}, binary)
}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -719,7 +725,7 @@ func Test_Compute_Fetch(t *testing.T) {
StatusCode: uint32(expected.StatusCode),
}, nil
},
}, binary)
}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -764,7 +770,7 @@ func Test_Compute_Fetch(t *testing.T) {
}, nil
},
MaxFetchRequests: 6,
}, binary)
}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -809,7 +815,7 @@ func Test_Compute_Fetch(t *testing.T) {
}, nil
},
MaxFetchRequests: 6,
}, binary)
}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -843,7 +849,7 @@ func TestModule_Errors(t *testing.T) {
ctx := tests.Context(t)
binary := createTestBinary(successBinaryCmd, successBinaryLocation, true, t)

m, err := NewModule(&ModuleConfig{IsUncompressed: true, Logger: logger.Test(t)}, binary)
m, err := NewModule(&ModuleConfig{IsUncompressed: true, Logger: logger.Test(t)}, binary, wasmtime.NewModule)
require.NoError(t, err)

_, err = m.Run(ctx, nil)
Expand Down Expand Up @@ -890,7 +896,7 @@ func TestModule_Sandbox_Memory(t *testing.T) {
ctx := tests.Context(t)
binary := createTestBinary(oomBinaryCmd, oomBinaryLocation, true, t)

m, err := NewModule(&ModuleConfig{IsUncompressed: true, Logger: logger.Test(t)}, binary)
m, err := NewModule(&ModuleConfig{IsUncompressed: true, Logger: logger.Test(t)}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand All @@ -909,7 +915,7 @@ func TestModule_Sandbox_SleepIsStubbedOut(t *testing.T) {
binary := createTestBinary(sleepBinaryCmd, sleepBinaryLocation, true, t)

d := 1 * time.Millisecond
m, err := NewModule(&ModuleConfig{Timeout: &d, IsUncompressed: true, Logger: logger.Test(t)}, binary)
m, err := NewModule(&ModuleConfig{Timeout: &d, IsUncompressed: true, Logger: logger.Test(t)}, binary, wasmtime.NewModule)

Check failure on line 918 in pkg/workflows/wasm/host/wasm_test.go

View workflow job for this annotation

GitHub Actions / benchmark

not enough arguments in call to NewModule
require.NoError(t, err)

m.Start()
Expand All @@ -935,7 +941,7 @@ func TestModule_Sandbox_Timeout(t *testing.T) {
binary := createTestBinary(sleepBinaryCmd, sleepBinaryLocation, true, t)

tmt := 10 * time.Millisecond
m, err := NewModule(&ModuleConfig{IsUncompressed: true, Logger: logger.Test(t), Timeout: &tmt}, binary)
m, err := NewModule(&ModuleConfig{IsUncompressed: true, Logger: logger.Test(t), Timeout: &tmt}, binary, wasmtime.NewModule)
require.NoError(t, err)

Check failure on line 946 in pkg/workflows/wasm/host/wasm_test.go

View workflow job for this annotation

GitHub Actions / benchmark

not enough arguments in call to NewModule
m.Start()
Expand All @@ -955,7 +961,7 @@ func TestModule_Sandbox_CantReadFiles(t *testing.T) {
ctx := tests.Context(t)
binary := createTestBinary(filesBinaryCmd, filesBinaryLocation, true, t)

m, err := NewModule(&ModuleConfig{IsUncompressed: true, Logger: logger.Test(t)}, binary)
m, err := NewModule(&ModuleConfig{IsUncompressed: true, Logger: logger.Test(t)}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -983,7 +989,7 @@ func TestModule_Sandbox_CantCreateDir(t *testing.T) {
ctx := tests.Context(t)
binary := createTestBinary(dirsBinaryCmd, dirsBinaryLocation, true, t)

m, err := NewModule(&ModuleConfig{IsUncompressed: true, Logger: logger.Test(t)}, binary)
m, err := NewModule(&ModuleConfig{IsUncompressed: true, Logger: logger.Test(t)}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -1011,7 +1017,7 @@ func TestModule_Sandbox_HTTPRequest(t *testing.T) {
ctx := tests.Context(t)
binary := createTestBinary(httpBinaryCmd, httpBinaryLocation, true, t)

m, err := NewModule(&ModuleConfig{IsUncompressed: true, Logger: logger.Test(t)}, binary)
m, err := NewModule(&ModuleConfig{IsUncompressed: true, Logger: logger.Test(t)}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -1039,7 +1045,7 @@ func TestModule_Sandbox_ReadEnv(t *testing.T) {
ctx := tests.Context(t)
binary := createTestBinary(envBinaryCmd, envBinaryLocation, true, t)

m, err := NewModule(&ModuleConfig{IsUncompressed: true, Logger: logger.Test(t)}, binary)
m, err := NewModule(&ModuleConfig{IsUncompressed: true, Logger: logger.Test(t)}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down Expand Up @@ -1092,7 +1098,7 @@ func TestModule_Sandbox_RandomGet(t *testing.T) {
Determinism: &DeterminismConfig{
Seed: 42,
},
}, binary)
}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand All @@ -1108,7 +1114,7 @@ func TestModule_Sandbox_RandomGet(t *testing.T) {
m, err := NewModule(&ModuleConfig{
Logger: logger.Test(t),
IsUncompressed: true,
}, binary)
}, binary, wasmtime.NewModule)
require.NoError(t, err)

m.Start()
Expand Down

0 comments on commit e1fa917

Please sign in to comment.