Skip to content

Commit

Permalink
[TT-1423] reusable cleanup vrf (#13939)
Browse files Browse the repository at this point in the history
* reuse clean up functions in VRF, hopefully solve NPEs

* TT-1423: removing TODOs

* fixing NPE

* BHS test improvement

* GHA improvement

* GHA improvement

* GHA improvement

---------

Co-authored-by: Ilja Pavlovs <[email protected]>
Co-authored-by: Ilja Pavlovs <[email protected]>
  • Loading branch information
3 people authored Aug 30, 2024
1 parent 674eac3 commit 7e82972
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 311 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/on-demand-vrfv2plus-smoke-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ on:
test_list_regex:
description: "Regex for 'Selected Tests' to run"
required: false
default: "(TestVRFv2Plus$/(Link_Billing|Native_Billing|Direct_Funding)|TestVRFV2PlusWithBHS|TestVRFV2PlusWithBHF)"
default: "TestVRFv2Plus$/(Link_Billing|Native_Billing|Direct_Funding)|TestVRFV2PlusWithBHS"
test_secrets_override_key:
description: 'Key to run tests with custom test secrets'
required: false
Expand Down Expand Up @@ -81,7 +81,7 @@ jobs:
if: ${{ github.event.inputs.test_suite == 'Selected Tests' }}
uses: smartcontractkit/chainlink-github-actions/chainlink-testing-framework/run-tests@2967f2287bd3f3ddbac7b476e9568993df01796e # v2.3.27
with:
test_command_to_run: cd ./integration-tests/smoke && go test -v -count=1 -parallel=1 -timeout 2h -run "${{ inputs.test_list_regex }}"
test_command_to_run: cd ./integration-tests/smoke && go test -v -count=1 -parallel=1 -timeout 2h -run "${{ inputs.test_list_regex }}" 2>&1 | tee /tmp/gotest.log | gotestloghelper -ci -singlepackage -hidepassingtests=false -hidepassinglogs
test_download_vendor_packages_command: cd ./integration-tests && go mod download
test_secrets_override_base64: ${{ secrets[inputs.test_secrets_override_key] }}
cl_repo: ${{ env.CHAINLINK_IMAGE }}
Expand All @@ -99,7 +99,7 @@ jobs:
if: ${{ github.event.inputs.test_suite == 'All Tests' }}
uses: smartcontractkit/chainlink-github-actions/chainlink-testing-framework/run-tests@2967f2287bd3f3ddbac7b476e9568993df01796e # v2.3.27
with:
test_command_to_run: cd ./integration-tests/smoke && go test -v -count=1 -parallel=1 -timeout 3h vrfv2plus_test.go
test_command_to_run: cd ./integration-tests/smoke && go test -v -count=1 -parallel=1 -timeout 3h vrfv2plus_test.go 2>&1 | tee /tmp/gotest.log | gotestloghelper -ci -singlepackage -hidepassingtests=false -hidepassinglogs
test_download_vendor_packages_command: cd ./integration-tests && go mod download
test_secrets_override_base64: ${{ secrets[inputs.test_secrets_override_key] }}
cl_repo: ${{ env.CHAINLINK_IMAGE }}
Expand Down
173 changes: 60 additions & 113 deletions integration-tests/smoke/vrfv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,53 @@ const (
SethRootKeyIndex = 0
)

// vrfv2CleanUpFn is a cleanup function that captures pointers from context, in which it's called and uses them to clean up the test environment
var vrfv2CleanUpFn = func(
t **testing.T,
sethClient **seth.Client,
config **tc.TestConfig,
testEnv **test_env.CLClusterTestEnv,
vrfContracts **vrfcommon.VRFContracts,
subIDsForCancellingAfterTest *[]uint64,
walletAddress **string,
) func() {
return func() {
logger := logging.GetTestLogger(*t)
testConfig := **config
network := networks.MustGetSelectedNetworkConfig(testConfig.GetNetworkConfig())[0]
if network.Simulated {
logger.Info().
Str("Network Name", network.Name).
Msg("Network is a simulated network. Skipping fund return for Coordinator Subscriptions.")
} else {
if *vrfContracts != nil && *sethClient != nil {
if *testConfig.VRFv2.General.CancelSubsAfterTestRun {
client := *sethClient
var returnToAddress string
if walletAddress == nil || *walletAddress == nil {
returnToAddress = client.MustGetRootKeyAddress().Hex()
} else {
returnToAddress = **walletAddress
}
//cancel subs and return funds to sub owner
vrfv2.CancelSubsAndReturnFunds(testcontext.Get(*t), *vrfContracts, returnToAddress, *subIDsForCancellingAfterTest, logger)
}
} else {
logger.Error().Msg("VRF Contracts and/or Seth client are nil. Cannot execute cleanup")
}
}
if !*testConfig.VRFv2.General.UseExistingEnv {
if *testEnv == nil {
logger.Error().Msg("Test environment is nil. Cannot execute cleanup")
return
}
if err := (*testEnv).Cleanup(test_env.CleanupOpts{TestName: (*t).Name()}); err != nil {
logger.Error().Err(err).Msg("Error cleaning up test environment")
}
}
}
}

func TestVRFv2Basic(t *testing.T) {
t.Parallel()
var (
Expand All @@ -54,30 +101,13 @@ func TestVRFv2Basic(t *testing.T) {

config, err := tc.GetChainAndTestTypeSpecificConfig("Smoke", tc.VRFv2)
require.NoError(t, err, "Error getting config")
vrfv2Config := config.VRFv2
chainID := networks.MustGetSelectedNetworkConfig(config.GetNetworkConfig())[0].ChainID

cleanupFn := func() {
if sethClient.Cfg.IsSimulatedNetwork() {
l.Info().
Str("Network Name", sethClient.Cfg.Network.Name).
Msg("Network is a simulated network. Skipping fund return for Coordinator Subscriptions.")
} else {
if *vrfv2Config.General.CancelSubsAfterTestRun {
//cancel subs and return funds to sub owner
vrfv2.CancelSubsAndReturnFunds(testcontext.Get(t), vrfContracts, sethClient.MustGetRootKeyAddress().Hex(), subIDsForCancellingAfterTest, l)
}
}
if !*vrfv2Config.General.UseExistingEnv {
if err := testEnv.Cleanup(test_env.CleanupOpts{TestName: t.Name()}); err != nil {
l.Error().Err(err).Msg("Error cleaning up test environment")
}
}
}
configPtr := &config
vrfEnvConfig := vrfcommon.VRFEnvConfig{
TestConfig: config,
ChainID: chainID,
CleanupFn: cleanupFn,
CleanupFn: vrfv2CleanUpFn(&t, &sethClient, &configPtr, &testEnv, &vrfContracts, &subIDsForCancellingAfterTest, nil),
}
newEnvConfig := vrfcommon.NewEnvConfig{
NodesToCreate: []vrfcommon.VRFNodeType{vrfcommon.VRF},
Expand Down Expand Up @@ -574,29 +604,12 @@ func TestVRFv2MultipleSendingKeys(t *testing.T) {
t.Fatal(err)
}
chainID := networks.MustGetSelectedNetworkConfig(config.GetNetworkConfig())[0].ChainID
vrfv2Config := config.VRFv2

cleanupFn := func() {
if sethClient.Cfg.IsSimulatedNetwork() {
l.Info().
Str("Network Name", sethClient.Cfg.Network.Name).
Msg("Network is a simulated network. Skipping fund return for Coordinator Subscriptions.")
} else {
if *vrfv2Config.General.CancelSubsAfterTestRun {
//cancel subs and return funds to sub owner
vrfv2.CancelSubsAndReturnFunds(testcontext.Get(t), vrfContracts, sethClient.MustGetRootKeyAddress().Hex(), subIDsForCancellingAfterTest, l)
}
}
if !*vrfv2Config.General.UseExistingEnv {
if err := testEnv.Cleanup(test_env.CleanupOpts{TestName: t.Name()}); err != nil {
l.Error().Err(err).Msg("Error cleaning up test environment")
}
}
}
configPtr := &config
vrfEnvConfig := vrfcommon.VRFEnvConfig{
TestConfig: config,
ChainID: chainID,
CleanupFn: cleanupFn,
CleanupFn: vrfv2CleanUpFn(&t, &sethClient, &configPtr, &testEnv, &vrfContracts, &subIDsForCancellingAfterTest, nil),
}
newEnvConfig := vrfcommon.NewEnvConfig{
NodesToCreate: []vrfcommon.VRFNodeType{vrfcommon.VRF},
Expand Down Expand Up @@ -680,29 +693,12 @@ func TestVRFOwner(t *testing.T) {
config, err := tc.GetChainAndTestTypeSpecificConfig("Smoke", tc.VRFv2)
require.NoError(t, err, "Error getting config")
chainID := networks.MustGetSelectedNetworkConfig(config.GetNetworkConfig())[0].ChainID
vrfv2Config := config.VRFv2

cleanupFn := func() {
if sethClient.Cfg.IsSimulatedNetwork() {
l.Info().
Str("Network Name", sethClient.Cfg.Network.Name).
Msg("Network is a simulated network. Skipping fund return for Coordinator Subscriptions.")
} else {
if *vrfv2Config.General.CancelSubsAfterTestRun {
//cancel subs and return funds to sub owner
vrfv2.CancelSubsAndReturnFunds(testcontext.Get(t), vrfContracts, sethClient.MustGetRootKeyAddress().Hex(), subIDsForCancellingAfterTest, l)
}
}
if !*vrfv2Config.General.UseExistingEnv {
if err := testEnv.Cleanup(test_env.CleanupOpts{TestName: t.Name()}); err != nil {
l.Error().Err(err).Msg("Error cleaning up test environment")
}
}
}
configPtr := &config
vrfEnvConfig := vrfcommon.VRFEnvConfig{
TestConfig: config,
ChainID: chainID,
CleanupFn: cleanupFn,
CleanupFn: vrfv2CleanUpFn(&t, &sethClient, &configPtr, &testEnv, &vrfContracts, &subIDsForCancellingAfterTest, nil),
}
newEnvConfig := vrfcommon.NewEnvConfig{
NodesToCreate: []vrfcommon.VRFNodeType{vrfcommon.VRF},
Expand Down Expand Up @@ -818,31 +814,15 @@ func TestVRFV2WithBHS(t *testing.T) {
vrfv2Config := config.VRFv2
chainID := networks.MustGetSelectedNetworkConfig(config.GetNetworkConfig())[0].ChainID

cleanupFn := func() {
if sethClient.Cfg.IsSimulatedNetwork() {
l.Info().
Str("Network Name", sethClient.Cfg.Network.Name).
Msg("Network is a simulated network. Skipping fund return for Coordinator Subscriptions.")
} else {
if *vrfv2Config.General.CancelSubsAfterTestRun {
//cancel subs and return funds to sub owner
vrfv2.CancelSubsAndReturnFunds(testcontext.Get(t), vrfContracts, sethClient.MustGetRootKeyAddress().Hex(), subIDsForCancellingAfterTest, l)
}
}
if !*vrfv2Config.General.UseExistingEnv {
if err := testEnv.Cleanup(test_env.CleanupOpts{TestName: t.Name()}); err != nil {
l.Error().Err(err).Msg("Error cleaning up test environment")
}
}
}
configPtr := &config

//decrease default span for checking blockhashes for unfulfilled requests
vrfv2Config.General.BHSJobWaitBlocks = ptr.Ptr(2)
vrfv2Config.General.BHSJobLookBackBlocks = ptr.Ptr(20)
vrfEnvConfig := vrfcommon.VRFEnvConfig{
TestConfig: config,
ChainID: chainID,
CleanupFn: cleanupFn,
CleanupFn: vrfv2CleanUpFn(&t, &sethClient, &configPtr, &testEnv, &vrfContracts, &subIDsForCancellingAfterTest, nil),
}
newEnvConfig := vrfcommon.NewEnvConfig{
NodesToCreate: []vrfcommon.VRFNodeType{vrfcommon.VRF, vrfcommon.BHS},
Expand Down Expand Up @@ -1036,30 +1016,13 @@ func TestVRFV2NodeReorg(t *testing.T) {

config, err := tc.GetChainAndTestTypeSpecificConfig("Smoke", tc.VRFv2)
require.NoError(t, err, "Error getting config")
vrfv2Config := config.VRFv2
network := networks.MustGetSelectedNetworkConfig(config.GetNetworkConfig())[0]
if !network.Simulated {
t.Skip("Skipped since Reorg test could only be run on Simulated chain.")
}
chainID := network.ChainID
cleanupFn := func() {
if sethClient.Cfg.IsSimulatedNetwork() {
l.Info().
Str("Network Name", sethClient.Cfg.Network.Name).
Msg("Network is a simulated network. Skipping fund return for Coordinator Subscriptions.")
} else {
if *vrfv2Config.General.CancelSubsAfterTestRun {
//cancel subs and return funds to sub owner
vrfv2.CancelSubsAndReturnFunds(testcontext.Get(t), vrfContracts, sethClient.MustGetRootKeyAddress().Hex(), subIDsForCancellingAfterTest, l)
}
}
if !*vrfv2Config.General.UseExistingEnv {
if err := env.Cleanup(test_env.CleanupOpts{TestName: t.Name()}); err != nil {
l.Error().Err(err).Msg("Error cleaning up test environment")
}
}
}

configPtr := &config
chainlinkNodeLogScannerSettings := test_env.GetDefaultChainlinkNodeLogScannerSettingsWithExtraAllowedMessages(
testreporters.NewAllowedLogMessage(
"Got very old block.",
Expand All @@ -1075,7 +1038,7 @@ func TestVRFV2NodeReorg(t *testing.T) {
vrfEnvConfig := vrfcommon.VRFEnvConfig{
TestConfig: config,
ChainID: chainID,
CleanupFn: cleanupFn,
CleanupFn: vrfv2CleanUpFn(&t, &sethClient, &configPtr, &env, &vrfContracts, &subIDsForCancellingAfterTest, nil),
}
newEnvConfig := vrfcommon.NewEnvConfig{
NodesToCreate: []vrfcommon.VRFNodeType{vrfcommon.VRF},
Expand Down Expand Up @@ -1215,30 +1178,14 @@ func TestVRFv2BatchFulfillmentEnabledDisabled(t *testing.T) {

config, err := tc.GetChainAndTestTypeSpecificConfig("Smoke", tc.VRFv2)
require.NoError(t, err, "Error getting config")
vrfv2Config := config.VRFv2
network := networks.MustGetSelectedNetworkConfig(config.GetNetworkConfig())[0]
chainID := network.ChainID
cleanupFn := func() {
if sethClient.Cfg.IsSimulatedNetwork() {
l.Info().
Str("Network Name", sethClient.Cfg.Network.Name).
Msg("Network is a simulated network. Skipping fund return for Coordinator Subscriptions.")
} else {
if *vrfv2Config.General.CancelSubsAfterTestRun {
//cancel subs and return funds to sub owner
vrfv2.CancelSubsAndReturnFunds(testcontext.Get(t), vrfContracts, sethClient.MustGetRootKeyAddress().Hex(), subIDsForCancellingAfterTest, l)
}
}
if !*vrfv2Config.General.UseExistingEnv {
if err := env.Cleanup(test_env.CleanupOpts{TestName: t.Name()}); err != nil {
l.Error().Err(err).Msg("Error cleaning up test environment")
}
}
}

configPtr := &config
vrfEnvConfig := vrfcommon.VRFEnvConfig{
TestConfig: config,
ChainID: chainID,
CleanupFn: cleanupFn,
CleanupFn: vrfv2CleanUpFn(&t, &sethClient, &configPtr, &env, &vrfContracts, &subIDsForCancellingAfterTest, nil),
}
newEnvConfig := vrfcommon.NewEnvConfig{
NodesToCreate: []vrfcommon.VRFNodeType{vrfcommon.VRF},
Expand Down
Loading

0 comments on commit 7e82972

Please sign in to comment.