Skip to content

Commit

Permalink
Fix and test spec validation for non-YAML specs
Browse files Browse the repository at this point in the history
  • Loading branch information
nolag committed Sep 20, 2024
1 parent cfcfaf6 commit a0cd4bd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
7 changes: 1 addition & 6 deletions core/services/job/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

commonassets "github.com/smartcontractkit/chainlink-common/pkg/assets"
"github.com/smartcontractkit/chainlink-common/pkg/types"
pkgworkflows "github.com/smartcontractkit/chainlink-common/pkg/workflows"

"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/relay"
Expand Down Expand Up @@ -896,12 +895,8 @@ const (

// Validate checks the workflow spec for correctness
func (w *WorkflowSpec) Validate(ctx context.Context, logger logger.Logger) error {
s, err := pkgworkflows.ParseWorkflowSpecYaml(w.Workflow)
s, err := w.SDKSpec(ctx, logger)
if err != nil {
return fmt.Errorf("%w: failed to parse workflow spec %s: %w", ErrInvalidWorkflowYAMLSpec, w.Workflow, err)
}

if _, err = w.SDKSpec(ctx, logger); err != nil {
return err
}

Expand Down
37 changes: 30 additions & 7 deletions core/services/job/models_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package job
package job_test

import (
_ "embed"
"encoding/json"
"reflect"
"testing"
"time"

"github.com/pelletier/go-toml/v2"
"github.com/smartcontractkit/chainlink-common/pkg/workflows/sdk"

"github.com/smartcontractkit/chainlink-common/pkg/codec"
"github.com/smartcontractkit/chainlink-common/pkg/types"
pkgworkflows "github.com/smartcontractkit/chainlink-common/pkg/workflows"

"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/relay"

"github.com/stretchr/testify/assert"
Expand All @@ -28,7 +31,7 @@ func TestOCR2OracleSpec_RelayIdentifier(t *testing.T) {
type fields struct {
Relay string
ChainID string
RelayConfig JSONConfig
RelayConfig job.JSONConfig
}
tests := []struct {
name string
Expand Down Expand Up @@ -72,7 +75,7 @@ func TestOCR2OracleSpec_RelayIdentifier(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

s := &OCR2OracleSpec{
s := &job.OCR2OracleSpec{
Relay: tt.fields.Relay,
ChainID: tt.fields.ChainID,
RelayConfig: tt.fields.RelayConfig,
Expand All @@ -97,7 +100,7 @@ var (
)

func TestOCR2OracleSpec(t *testing.T) {
val := OCR2OracleSpec{
val := job.OCR2OracleSpec{
Relay: relay.NetworkEVM,
PluginType: types.Median,
ContractID: "foo",
Expand Down Expand Up @@ -260,13 +263,13 @@ func TestOCR2OracleSpec(t *testing.T) {
})

t.Run("round-trip", func(t *testing.T) {
var gotVal OCR2OracleSpec
var gotVal job.OCR2OracleSpec
require.NoError(t, toml.Unmarshal([]byte(compact), &gotVal))
gotB, err := toml.Marshal(gotVal)
require.NoError(t, err)
require.Equal(t, compact, string(gotB))
t.Run("pretty", func(t *testing.T) {
var gotVal OCR2OracleSpec
var gotVal job.OCR2OracleSpec
require.NoError(t, toml.Unmarshal([]byte(pretty), &gotVal))
gotB, err := toml.Marshal(gotVal)
require.NoError(t, err)
Expand Down Expand Up @@ -322,7 +325,7 @@ func TestWorkflowSpec_Validate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &WorkflowSpec{
w := &job.WorkflowSpec{
Workflow: tt.fields.Workflow,
}
err := w.Validate(testutils.Context(t), logger.NullLogger)
Expand All @@ -334,4 +337,24 @@ func TestWorkflowSpec_Validate(t *testing.T) {
}
})
}

t.Run("WASM can validate", func(t *testing.T) {
config, err := json.Marshal(sdk.NewWorkflowParams{
Owner: "owner",
Name: "name",
})
require.NoError(t, err)

w := &job.WorkflowSpec{
Workflow: createTestBinary(t),
SpecType: job.WASMFile,
Config: string(config),
}

err = w.Validate(testutils.Context(t), logger.NullLogger)
require.NoError(t, err)
assert.Equal(t, "owner", w.WorkflowOwner)
assert.Equal(t, "name", w.WorkflowName)
require.NotEmpty(t, w.WorkflowID)
})
}

0 comments on commit a0cd4bd

Please sign in to comment.