-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delegate workflow spec creation (#14365)
* Delegate workflow spec creation * Use checked in common * exclude sourcegrapht * fix broken tests * imports fix * Parse workflow earlier and store spec type in the DB * lint * Remove unused variable * Rename SdkWorkflowSpec to SDKSpec --------- Co-authored-by: skudasov <[email protected]>
- Loading branch information
Showing
29 changed files
with
404 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package job | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/workflows/sdk" | ||
) | ||
|
||
var ErrInvalidWorkflowType = errors.New("invalid workflow type") | ||
|
||
type SDKWorkflowSpecFactory interface { | ||
Spec(ctx context.Context, rawSpec, config []byte) (sdk.WorkflowSpec, error) | ||
RawSpec(ctx context.Context, wf string) ([]byte, error) | ||
} | ||
|
||
type WorkflowSpecFactory map[WorkflowSpecType]SDKWorkflowSpecFactory | ||
|
||
func (wsf WorkflowSpecFactory) Spec( | ||
ctx context.Context, workflow string, config []byte, tpe WorkflowSpecType) (sdk.WorkflowSpec, string, error) { | ||
if tpe == "" { | ||
tpe = DefaultSpecType | ||
} | ||
|
||
factory, ok := wsf[tpe] | ||
if !ok { | ||
return sdk.WorkflowSpec{}, "", ErrInvalidWorkflowType | ||
} | ||
|
||
rawSpec, err := factory.RawSpec(ctx, workflow) | ||
if err != nil { | ||
return sdk.WorkflowSpec{}, "", err | ||
} | ||
|
||
spec, err := factory.Spec(ctx, rawSpec, config) | ||
if err != nil { | ||
return sdk.WorkflowSpec{}, "", err | ||
} | ||
|
||
sum := sha256.New() | ||
sum.Write(rawSpec) | ||
sum.Write(config) | ||
|
||
return spec, fmt.Sprintf("%x", sum.Sum(nil)), nil | ||
} | ||
|
||
var workflowSpecFactory = WorkflowSpecFactory{ | ||
YamlSpec: YAMLSpecFactory{}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package job_test | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/workflows/sdk" | ||
|
||
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/job" | ||
) | ||
|
||
func TestWorkflowSpecFactory_ToSpec(t *testing.T) { | ||
t.Parallel() | ||
|
||
anyData := "any data" | ||
anyConfig := []byte("any config") | ||
anySpec := sdk.WorkflowSpec{Name: "name", Owner: "owner"} | ||
|
||
t.Run("delegates to factory and calculates CID", func(t *testing.T) { | ||
runYamlSpecTest(t, anySpec, anyData, anyConfig, job.YamlSpec) | ||
}) | ||
|
||
t.Run("delegates default", func(t *testing.T) { | ||
runYamlSpecTest(t, anySpec, anyData, anyConfig, "") | ||
}) | ||
|
||
t.Run("CID without config matches", func(t *testing.T) { | ||
factory := job.WorkflowSpecFactory{ | ||
job.YamlSpec: mockSdkSpecFactory{t: t, noConfig: true, SpecVal: anySpec}, | ||
} | ||
results, cid, err := factory.Spec(testutils.Context(t), anyData, nil, job.YamlSpec) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, anySpec, results) | ||
|
||
sha256Hash := sha256.New() | ||
sha256Hash.Write([]byte(anyData)) | ||
expectedCid := fmt.Sprintf("%x", sha256Hash.Sum(nil)) | ||
assert.Equal(t, expectedCid, cid) | ||
}) | ||
|
||
t.Run("returns errors from sdk factory", func(t *testing.T) { | ||
anyErr := errors.New("nope") | ||
factory := job.WorkflowSpecFactory{ | ||
job.YamlSpec: mockSdkSpecFactory{t: t, Err: anyErr}, | ||
} | ||
|
||
_, _, err := factory.Spec(testutils.Context(t), anyData, anyConfig, job.YamlSpec) | ||
assert.Equal(t, anyErr, err) | ||
}) | ||
|
||
t.Run("returns an error if the type is not supported", func(t *testing.T) { | ||
factory := job.WorkflowSpecFactory{ | ||
job.YamlSpec: mockSdkSpecFactory{t: t, SpecVal: anySpec}, | ||
} | ||
|
||
_, _, err := factory.Spec(testutils.Context(t), anyData, anyConfig, "unsupported") | ||
assert.Error(t, err) | ||
}) | ||
} | ||
|
||
func runYamlSpecTest(t *testing.T, anySpec sdk.WorkflowSpec, anyData string, anyConfig []byte, specType job.WorkflowSpecType) { | ||
factory := job.WorkflowSpecFactory{ | ||
job.YamlSpec: mockSdkSpecFactory{t: t, SpecVal: anySpec}, | ||
} | ||
|
||
results, cid, err := factory.Spec(testutils.Context(t), anyData, anyConfig, specType) | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, anySpec, results) | ||
|
||
sha256Hash := sha256.New() | ||
sha256Hash.Write([]byte(anyData)) | ||
sha256Hash.Write(anyConfig) | ||
expectedCid := fmt.Sprintf("%x", sha256Hash.Sum(nil)) | ||
assert.Equal(t, expectedCid, cid) | ||
} | ||
|
||
type mockSdkSpecFactory struct { | ||
t *testing.T | ||
noConfig bool | ||
SpecVal sdk.WorkflowSpec | ||
Err error | ||
} | ||
|
||
func (f mockSdkSpecFactory) RawSpec(_ context.Context, wf string) ([]byte, error) { | ||
return []byte(wf), nil | ||
} | ||
|
||
func (f mockSdkSpecFactory) Spec(_ context.Context, rawSpec, config []byte) (sdk.WorkflowSpec, error) { | ||
assert.ElementsMatch(f.t, rawSpec, []byte("any data")) | ||
if f.noConfig { | ||
assert.Nil(f.t, config) | ||
} else { | ||
assert.ElementsMatch(f.t, config, []byte("any config")) | ||
} | ||
|
||
return f.SpecVal, f.Err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package job | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/workflows" | ||
"github.com/smartcontractkit/chainlink-common/pkg/workflows/sdk" | ||
) | ||
|
||
type YAMLSpecFactory struct{} | ||
|
||
var _ SDKWorkflowSpecFactory = (*YAMLSpecFactory)(nil) | ||
|
||
func (y YAMLSpecFactory) Spec(_ context.Context, rawSpec, _ []byte) (sdk.WorkflowSpec, error) { | ||
return workflows.ParseWorkflowSpecYaml(string(rawSpec)) | ||
} | ||
|
||
func (y YAMLSpecFactory) RawSpec(_ context.Context, wf string) ([]byte, error) { | ||
return []byte(wf), nil | ||
} |
Oops, something went wrong.