Skip to content

Commit

Permalink
(test): Add sdk unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
justinkaseman committed Jan 9, 2025
1 parent dce3c30 commit ce74a60
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
9 changes: 9 additions & 0 deletions pkg/workflows/wasm/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,15 @@ func Test_createEmitFn(t *testing.T) {
assert.NoError(t, err)
})

t.Run("success if no labels are given", func(t *testing.T) {
hostEmit := func(respptr, resplenptr, reqptr unsafe.Pointer, reqptrlen int32) int32 {
return 0
}
runtimeEmit := createEmitFn(sdkConfig, l, hostEmit)
err := runtimeEmit(giveMsg, nil)
assert.NoError(t, err)
})

t.Run("successfully read error message when emit fails", func(t *testing.T) {
hostEmit := func(respptr, resplenptr, reqptr unsafe.Pointer, reqptrlen int32) int32 {
// marshall the protobufs
Expand Down
10 changes: 8 additions & 2 deletions pkg/workflows/wasm/runner_wasip1.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ func sendResponseFn(response *wasmpb.Response) {
os.Exit(CodeInvalidRequest)
}

ptr, ptrlen := bufferToPointerLen(pb)
ptr, ptrlen, err := bufferToPointerLen(pb)
if err != nil {
os.Exit(CodeInvalidResponse)
}
errno := sendResponse(ptr, ptrlen)
if errno != 0 {
os.Exit(CodeHostErr)
Expand All @@ -76,7 +79,10 @@ type wasmWriteSyncer struct{}

// Write is used to proxy log requests from the WASM binary back to the host
func (wws *wasmWriteSyncer) Write(p []byte) (n int, err error) {
ptr, ptrlen := bufferToPointerLen(p)
ptr, ptrlen, err := bufferToPointerLen(p)
if err != nil {
return int(ptrlen), err
}
log(ptr, ptrlen)
return int(ptrlen), nil
}
4 changes: 2 additions & 2 deletions pkg/workflows/wasm/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func createEmitFn(
}

if labels == nil {
return NewEmissionError(fmt.Errorf("labels must be provided"))
labels = map[string]string{}
}
labels, err := toEmitLabels(sdkConfig.Metadata, labels)
if err != nil {
Expand Down Expand Up @@ -272,7 +272,7 @@ func toEmitLabels(md *capabilities.RequestMetadata, labels map[string]string) (m
}

if md.WorkflowExecutionID == "" {
return nil, fmt.Errorf("must provide workflow owner to emit event")
return nil, fmt.Errorf("must provide workflow execution id to emit event")
}

labels[events.LabelWorkflowExecutionID] = md.WorkflowExecutionID
Expand Down
30 changes: 26 additions & 4 deletions pkg/workflows/wasm/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
func Test_toEmitLabels(t *testing.T) {
t.Run("successfully transforms metadata", func(t *testing.T) {
md := &capabilities.RequestMetadata{
WorkflowID: "workflow-id",
WorkflowName: "workflow-name",
WorkflowOwner: "workflow-owner",
WorkflowID: "workflow-id",
WorkflowName: "workflow-name",
WorkflowOwner: "workflow-owner",
WorkflowExecutionID: "6e2a46e3b6ae611bdb9bcc36ed3f46bb9a30babc3aabdd4eae7f35dd9af0f244",
}
empty := make(map[string]string, 0)

Expand All @@ -24,7 +25,7 @@ func Test_toEmitLabels(t *testing.T) {
"workflow_id": "workflow-id",
"workflow_name": "workflow-name",
"workflow_owner_address": "workflow-owner",
"workflow_execution_id": "",
"workflow_execution_id": "6e2a46e3b6ae611bdb9bcc36ed3f46bb9a30babc3aabdd4eae7f35dd9af0f244",
}, gotLabels)
})

Expand Down Expand Up @@ -63,4 +64,25 @@ func Test_toEmitLabels(t *testing.T) {
assert.Error(t, err)
assert.ErrorContains(t, err, "workflow owner")
})

t.Run("fails on missing workflow execution id", func(t *testing.T) {
md := &capabilities.RequestMetadata{
WorkflowID: "workflow-id",
WorkflowName: "workflow-name",
WorkflowOwner: "workflow-owner",
}
empty := make(map[string]string, 0)

_, err := toEmitLabels(md, empty)
assert.Error(t, err)
assert.ErrorContains(t, err, "workflow execution id")
})
}

func Test_bufferToPointerLen(t *testing.T) {
t.Run("fails when no buffer", func(t *testing.T) {
_, _, err := bufferToPointerLen([]byte{})
assert.Error(t, err)
assert.ErrorContains(t, err, "buffer cannot be empty")
})
}

0 comments on commit ce74a60

Please sign in to comment.