Skip to content

Commit

Permalink
Creating seperate test
Browse files Browse the repository at this point in the history
  • Loading branch information
b-gopalswami committed Sep 24, 2024
1 parent 8cd5458 commit f3787fc
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 146 deletions.
163 changes: 131 additions & 32 deletions integration-tests/ccip-tests/smoke/ccip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package smoke

import (
"fmt"
"github.com/ethereum/go-ethereum/core/types"
"github.com/smartcontractkit/chainlink-common/pkg/config"
"github.com/smartcontractkit/chainlink-testing-framework/lib/utils/osutil"
"math/big"
"strings"
"testing"
"time"

"github.com/ethereum/go-ethereum/core/types"

"github.com/AlekSi/pointer"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -56,40 +58,20 @@ func TestSmokeCCIPForBidirectionalLane(t *testing.T) {
require.NoError(t, setUpOutput.TearDown())
})

// Create test definitions for each lane.
var tests []testDefinition
lookBackDuration := TestCfg.TestGroupInput.SkipRequestIfAnotherRequestTriggeredWithin
var recentTxFound *types.Log
var err error

addLanesToTest := func(lane *actions.CCIPLane) {
// Create test definitions for given lane if no previous request has been triggered within the specified timeframe.
// By default, the timeframe is set to nil. To define a timeframe, assign a duration to the variable
// SkipRequestIfAnotherRequestTriggeredWithin.
if lookBackDuration != nil {
recentTxFound, err = lane.Source.IsPastRequestTriggeredWithinTimeframe(lane.Context, lookBackDuration)
require.NoError(t, err, "error while finding recent request for lane network %s to network %s",
lane.SourceNetworkName, lane.DestNetworkName)
}
if recentTxFound == nil {
for _, lane := range setUpOutput.Lanes {
tests = append(tests, testDefinition{
testName: fmt.Sprintf("CCIP message transfer from network %s to network %s",
lane.ForwardLane.SourceNetworkName, lane.ForwardLane.DestNetworkName),
lane: lane.ForwardLane,
})
if lane.ReverseLane != nil {
tests = append(tests, testDefinition{
testName: fmt.Sprintf("CCIP message transfer from network %s to network %s",
lane.SourceNetworkName, lane.DestNetworkName),
lane: lane,
lane.ReverseLane.SourceNetworkName, lane.ReverseLane.DestNetworkName),
lane: lane.ReverseLane,
})
} else {
log.Info().
Str("TX", recentTxFound.TxHash.Hex()).
Uint64("Block Number", recentTxFound.BlockNumber).
Str("Source", lane.SourceNetworkName).
Str("Dest", lane.DestNetworkName).
Msgf("Lane Skipped. Recent request found within %v minutes.", lookBackDuration.Duration().Minutes())
}
}
for _, lane := range setUpOutput.Lanes {
addLanesToTest(lane.ForwardLane)
if lane.ReverseLane != nil {
recentTxFound = nil
addLanesToTest(lane.ReverseLane)
}
}

Expand Down Expand Up @@ -928,6 +910,123 @@ func TestSmokeCCIPReorgAboveFinalityAtSource(t *testing.T) {
})
}

// TestSmokeCCIPForGivenNetworkPairs is designed specifically for scheduled mainnet testing. This test checks for recent
// transaction and skip the lanes accordingly. This test also has capability to take override input on network pairs and phase timeout.
func TestSmokeCCIPForGivenNetworkPairs(t *testing.T) {
t.Parallel()
log := logging.GetTestLogger(t)
TestCfg := testsetups.NewCCIPTestConfig(t, log, testconfig.Smoke)
// override network pairs
var temp []testsetups.NetworkPair
overrideNetworkPairs, err := osutil.GetEnv("OVERRIDE_NETWORK_PAIRS")
require.NoError(t, err, "Error getting OVERRIDE_NETWORK_PAIRS environment variable")
if overrideNetworkPairs != "" {
networkPairs := strings.Split(overrideNetworkPairs, ";")
for _, networkPair := range networkPairs {
// check for any malformed inputs
if !strings.Contains(networkPair, ",") || len(strings.Split(networkPair, ",")) != 2 {
log.Error().Msgf("malformed OVERRIDE_NETWORK_PAIRS environment variable for network pair: %s ", networkPair)
return
}
networkPair = strings.ToUpper(strings.ReplaceAll(networkPair, "_", " "))
for _, network := range TestCfg.NetworkPairs {
if strings.Contains(networkPair, strings.ToUpper(network.NetworkA.Name)) && strings.Contains(networkPair, strings.ToUpper(network.NetworkB.Name)) {
temp = append(temp, network)
break
}
}
}
log.Info().Int("Pairs", len(temp)).Msg("Number of lanes overridden in the test")
log.Info().Interface("Lanes", networkPairs).Msg("Lanes under test")
TestCfg.NetworkPairs = temp
}

// phase timeout override
phaseTimeout, err := osutil.GetEnv("OVERRIDE_PHASE_TIMEOUT")
require.NoError(t, err, "Error getting OVERRIDE_PHASE_TIMEOUT environment variable")
if phaseTimeout != "" {
configDuration, err := config.ParseDuration(phaseTimeout)
require.NoError(t, err, "Error parsing phase timeout value")
TestCfg.TestGroupInput.PhaseTimeout = &configDuration
log.Info().Float64("Timeout in minutes", configDuration.Duration().Minutes()).Msg("Phase timeout is overridden")
}

gasLimit := big.NewInt(*TestCfg.TestGroupInput.MsgDetails.DestGasLimit)
setUpOutput := testsetups.CCIPDefaultTestSetUp(t, &log, "smoke-ccip", nil, TestCfg)
if len(setUpOutput.Lanes) == 0 {
log.Error().Msg("No lanes found")
return
}

t.Cleanup(func() {
// If we are running a test that is a token transfer, we need to verify the balance.
// skip the balance check for existing deployment, there can be multiple external requests in progress for existing deployments
// other than token transfer initiated by the test, which can affect the balance check
// therefore we check the balance only for the ccip environment created by the test
if TestCfg.TestGroupInput.MsgDetails.IsTokenTransfer() &&
!pointer.GetBool(TestCfg.TestGroupInput.USDCMockDeployment) &&
!pointer.GetBool(TestCfg.TestGroupInput.ExistingDeployment) {
setUpOutput.Balance.Verify(t)
}
require.NoError(t, setUpOutput.TearDown(), "error in tear down step")
})

var tests []testDefinition
lookBackDuration := TestCfg.TestGroupInput.SkipRequestIfAnotherRequestTriggeredWithin
var recentTxFound *types.Log

addLanesToTest := func(lane *actions.CCIPLane) {
// Create test definitions for given lane if no previous request has been triggered within the specified timeframe.
// By default, the timeframe is set to nil. To define a timeframe, assign a duration to the variable
// SkipRequestIfAnotherRequestTriggeredWithin.
if lookBackDuration != nil {
recentTxFound, err = lane.Source.IsPastRequestTriggeredWithinTimeframe(lane.Context, lookBackDuration)
require.NoError(t, err, "error while finding recent request for lane network %s to network %s",
lane.SourceNetworkName, lane.DestNetworkName)
}
if recentTxFound == nil {
tests = append(tests, testDefinition{
testName: fmt.Sprintf("CCIP message transfer from network %s to network %s",
lane.SourceNetworkName, lane.DestNetworkName),
lane: lane,
})
} else {
log.Info().
Str("TX", recentTxFound.TxHash.Hex()).
Uint64("Block Number", recentTxFound.BlockNumber).
Str("Source", lane.SourceNetworkName).
Str("Dest", lane.DestNetworkName).
Msgf("Lane Skipped. Recent request found within %v minutes.", lookBackDuration.Duration().Minutes())
}
}
for _, lane := range setUpOutput.Lanes {
addLanesToTest(lane.ForwardLane)
if lane.ReverseLane != nil {
recentTxFound = nil
addLanesToTest(lane.ReverseLane)
}
}

// Execute tests.
log.Info().Int("Total Lanes", len(tests)).Msg("Starting CCIP test")
for _, test := range tests {
tc := test
t.Run(tc.testName, func(t *testing.T) {
t.Parallel()
tc.lane.Test = t
log.Info().
Str("Source", tc.lane.SourceNetworkName).
Str("Destination", tc.lane.DestNetworkName).
Msgf("Starting lane %s -> %s", tc.lane.SourceNetworkName, tc.lane.DestNetworkName)

tc.lane.RecordStateBeforeTransfer()
err = tc.lane.SendRequests(1, gasLimit)
require.NoError(t, err, "error sending requests")
tc.lane.ValidateRequests()
})
}
}

// performAboveFinalityReorgAndValidate is to perform the above finality reorg test
func performAboveFinalityReorgAndValidate(t *testing.T, network string) {
t.Helper()
Expand Down
Loading

0 comments on commit f3787fc

Please sign in to comment.