Skip to content

Commit 5761e5a

Browse files
authored
feat: add option to set offchain client for test environment (#508)
Adds a new option to the test environment loader: - `WithOffchainClient` option to set an offchain client into the test environment This allows for more flexibility in the test environment setup where the user may have an existing offchain client they want to provide into the environment for testing.
1 parent 4a11d81 commit 5761e5a

5 files changed

Lines changed: 40 additions & 5 deletions

File tree

.changeset/pretty-cities-agree.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"chainlink-deployments-framework": minor
3+
---
4+
5+
Adds a new option to test engine environment loading for setting an offchain client.
6+
7+
- `WithOffchainClient` option to set an offchain client into the test environment

engine/test/environment/components.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ import (
88
fchain "github.com/smartcontractkit/chainlink-deployments-framework/chain"
99
fdatastore "github.com/smartcontractkit/chainlink-deployments-framework/datastore"
1010
fdeployment "github.com/smartcontractkit/chainlink-deployments-framework/deployment"
11+
foffchain "github.com/smartcontractkit/chainlink-deployments-framework/offchain"
1112
)
1213

1314
// components is a struct that contains the components of the environment.
1415
type components struct {
1516
mu sync.Mutex
1617

17-
Chains []fchain.BlockChain
18-
AddressBook fdeployment.AddressBook
19-
Datastore fdatastore.DataStore
20-
Logger logger.Logger
18+
Chains []fchain.BlockChain
19+
AddressBook fdeployment.AddressBook
20+
Datastore fdatastore.DataStore
21+
OffchainClient foffchain.Client
22+
Logger logger.Logger
2123
}
2224

2325
// newComponents creates a new components instance.

engine/test/environment/environment.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ func (l *Loader) Load(ctx context.Context, opts ...LoadOpt) (*fdeployment.Enviro
5656
ab = fdeployment.NewMemoryAddressBook()
5757
}
5858

59+
// We do not set any default offchain client as it is not required for all tests.
60+
// We may want to set a default memory based offchain client in the future.
61+
oc := cmps.OffchainClient
62+
5963
return &fdeployment.Environment{
6064
Name: environmentName,
6165
Logger: cmps.Logger,
@@ -64,7 +68,7 @@ func (l *Loader) Load(ctx context.Context, opts ...LoadOpt) (*fdeployment.Enviro
6468
DataStore: ds,
6569
Catalog: nil, // Unimplemented for now
6670
NodeIDs: []string{}, // Unimplemented for now
67-
Offchain: nil, // Unimplemented for now
71+
Offchain: oc,
6872
GetContext: getCtx,
6973
OCRSecrets: focr.XXXGenerateTestOCRSecrets(),
7074
OperationsBundle: foperations.NewBundle(getCtx, cmps.Logger, foperations.NewMemoryReporter()),

engine/test/environment/environment_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
fdatastore "github.com/smartcontractkit/chainlink-deployments-framework/datastore"
2121
fdeployment "github.com/smartcontractkit/chainlink-deployments-framework/deployment"
2222
"github.com/smartcontractkit/chainlink-deployments-framework/engine/test/onchain"
23+
foffchainjd "github.com/smartcontractkit/chainlink-deployments-framework/offchain/jd"
2324
)
2425

2526
func TestNew(t *testing.T) {
@@ -122,6 +123,18 @@ func TestLoader_Load_DatastoreOption(t *testing.T) {
122123
require.Equal(t, ds, env.DataStore)
123124
}
124125

126+
func TestLoader_Load_OffchainClientOption(t *testing.T) {
127+
t.Parallel()
128+
129+
oc := &foffchainjd.JobDistributor{}
130+
131+
loader := NewLoader()
132+
env, err := loader.Load(t.Context(), WithOffchainClient(oc))
133+
require.NoError(t, err)
134+
require.NotNil(t, env)
135+
require.Equal(t, oc, env.Offchain)
136+
}
137+
125138
func TestLoader_Load_AddressBookOption(t *testing.T) {
126139
t.Parallel()
127140

engine/test/environment/options.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
fdatastore "github.com/smartcontractkit/chainlink-deployments-framework/datastore"
99
fdeployment "github.com/smartcontractkit/chainlink-deployments-framework/deployment"
1010
"github.com/smartcontractkit/chainlink-deployments-framework/engine/test/onchain"
11+
"github.com/smartcontractkit/chainlink-deployments-framework/offchain"
1112
)
1213

1314
// Assign the chain container loader constructors to local variables to allow for stubbing in tests.
@@ -170,6 +171,14 @@ func WithAddressBook(ab fdeployment.AddressBook) LoadOpt {
170171
}
171172
}
172173

174+
// WithOffchainClient sets a custom offchain client for the environment.
175+
func WithOffchainClient(oc offchain.Client) LoadOpt {
176+
return func(cmps *components) error {
177+
cmps.OffchainClient = oc
178+
return nil
179+
}
180+
}
181+
173182
// withChainLoader creates a LoadOpt that loads chains using the provided loader and selectors.
174183
func withChainLoader(t *testing.T, loader *onchain.ChainLoader, selectors []uint64) LoadOpt {
175184
t.Helper()

0 commit comments

Comments
 (0)