@@ -11,6 +11,7 @@ import (
1111 "github.com/smartcontractkit/chainlink-testing-framework/lib/blockchain"
1212 "github.com/smartcontractkit/chainlink-testing-framework/lib/config"
1313 "github.com/smartcontractkit/chainlink-testing-framework/lib/logging"
14+ "github.com/smartcontractkit/chainlink-testing-framework/lib/utils/ptr"
1415)
1516
1617func TestMain (m * testing.M ) {
@@ -79,6 +80,8 @@ func TestVariousNetworkConfig(t *testing.T) {
7980 "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" ,
8081 },
8182 }
83+ httpOnlyNetwork := newNetwork
84+ httpOnlyNetwork .URLs = nil
8285 forkedNetwork := newNetwork
8386 forkedNetwork .HTTPURLs = nil
8487 forkedNetwork .URLs = nil
@@ -445,7 +448,7 @@ evm_default_gas_limit = 6000000
445448 os .Unsetenv ("E2E_TEST_NEW_NETWORK_WALLET_KEY" )
446449 os .Unsetenv ("E2E_TEST_NEW_NETWORK_RPC_HTTP_URL" )
447450 },
448- isNetworkConfigError : true ,
451+ expNetworks : []blockchain. EVMNetwork { httpOnlyNetwork } ,
449452 },
450453 }
451454 for _ , tc := range testcases {
@@ -490,3 +493,181 @@ evm_default_gas_limit = 6000000
490493 })
491494 }
492495}
496+
497+ func TestSetNetworks (t * testing.T ) {
498+ // Helper to create a simple NetworkConfig for testing
499+ createNetworkConfig := func (selectedNetworks []string , evmNetworks map [string ]* blockchain.EVMNetwork ,
500+ rpcHttpUrls map [string ][]string , rpcWsUrls map [string ][]string , walletKeys map [string ][]string ,
501+ anvilConfigs map [string ]* config.AnvilConfig ) config.NetworkConfig {
502+
503+ return config.NetworkConfig {
504+ SelectedNetworks : selectedNetworks ,
505+ EVMNetworks : evmNetworks ,
506+ RpcHttpUrls : rpcHttpUrls ,
507+ RpcWsUrls : rpcWsUrls ,
508+ WalletKeys : walletKeys ,
509+ AnvilConfigs : anvilConfigs ,
510+ }
511+ }
512+
513+ // Define test cases
514+ testCases := []struct {
515+ name string
516+ networkCfg config.NetworkConfig
517+ expectedError bool
518+ expectedErrorMsg string
519+ expectedCount int // Expected number of valid networks returned
520+ }{
521+ {
522+ name : "Basic network setup with both HTTP and WS URLs" ,
523+ networkCfg : createNetworkConfig (
524+ []string {"ETHEREUM_MAINNET" },
525+ map [string ]* blockchain.EVMNetwork {
526+ "ETHEREUM_MAINNET" : {Name : "ETHEREUM_MAINNET" , ChainID : 1 },
527+ },
528+ map [string ][]string {"ETHEREUM_MAINNET" : {"http://localhost:8545" }},
529+ map [string ][]string {"ETHEREUM_MAINNET" : {"ws://localhost:8546" }},
530+ map [string ][]string {"ETHEREUM_MAINNET" : {"ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" }},
531+ nil ,
532+ ),
533+ expectedError : false ,
534+ expectedCount : 1 ,
535+ },
536+ {
537+ name : "Simulated network bypasses validations" ,
538+ networkCfg : createNetworkConfig (
539+ []string {"SIMULATED" },
540+ map [string ]* blockchain.EVMNetwork {
541+ "SIMULATED" : {Name : "Simulated" , Simulated : true , ChainID : 1337 },
542+ },
543+ nil , nil , nil , nil ,
544+ ),
545+ expectedError : false ,
546+ expectedCount : 1 ,
547+ },
548+ {
549+ name : "Forked network skips RPC and wallet validation" ,
550+ networkCfg : createNetworkConfig (
551+ []string {"ETHEREUM_MAINNET" },
552+ nil ,
553+ nil ,
554+ nil ,
555+ nil ,
556+ map [string ]* config.AnvilConfig {
557+ "ETHEREUM_MAINNET" : {URL : ptr .Ptr ("http://localhost:8545" )},
558+ },
559+ ),
560+ expectedError : false ,
561+ expectedCount : 1 ,
562+ },
563+ {
564+ name : "Only HTTP URLs, valid setup" ,
565+ networkCfg : createNetworkConfig (
566+ []string {"ETHEREUM_MAINNET" },
567+ nil ,
568+ map [string ][]string {"ETHEREUM_MAINNET" : {"http://localhost:8545" }},
569+ nil ,
570+ map [string ][]string {"ETHEREUM_MAINNET" : {"ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" }},
571+ nil ,
572+ ),
573+ expectedError : false ,
574+ expectedCount : 1 ,
575+ },
576+ {
577+ name : "Only WS URLs without HTTP, invalid setup" ,
578+ networkCfg : createNetworkConfig (
579+ []string {"MAINNET" },
580+ nil ,
581+ nil ,
582+ map [string ][]string {"MAINNET" : {"ws://localhost:8546" }},
583+ map [string ][]string {"MAINNET" : {"ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" }},
584+ nil ,
585+ ),
586+ expectedError : true ,
587+ expectedErrorMsg : "WS RPC endpoint for MAINNET network is set without an HTTP endpoint" ,
588+ },
589+ {
590+ name : "Multiple networks with valid configurations" ,
591+ networkCfg : createNetworkConfig (
592+ []string {"ETHEREUM_MAINNET" , "OPTIMISM_MAINNET" },
593+ map [string ]* blockchain.EVMNetwork {
594+ "ETHEREUM_MAINNET" : {Name : "ETHEREUM_MAINNET" , ChainID : 1 },
595+ "OPTIMISM_MAINNET" : {Name : "OPTIMISM_MAINNET" , ChainID : 10 },
596+ },
597+ map [string ][]string {
598+ "ETHEREUM_MAINNET" : {"http://localhost:8545" },
599+ "OPTIMISM_MAINNET" : {"http://localhost:8547" },
600+ },
601+ map [string ][]string {
602+ "ETHEREUM_MAINNET" : {"ws://localhost:8546" },
603+ "OPTIMISM_MAINNET" : {"ws://localhost:8548" },
604+ },
605+ map [string ][]string {
606+ "ETHEREUM_MAINNET" : {"ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" },
607+ "OPTIMISM_MAINNET" : {"59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d" },
608+ },
609+ nil ,
610+ ),
611+ expectedError : false ,
612+ expectedCount : 2 ,
613+ },
614+ {
615+ name : "Missing wallet keys, invalid setup" ,
616+ networkCfg : createNetworkConfig (
617+ []string {"ETHEREUM_MAINNET" },
618+ nil ,
619+ map [string ][]string {"ETHEREUM_MAINNET" : {"http://localhost:8545" }},
620+ nil ,
621+ nil ,
622+ nil ,
623+ ),
624+ expectedError : true ,
625+ expectedErrorMsg : "no wallet keys found in config for 'ETHEREUM_MAINNET' network" ,
626+ },
627+ {
628+ name : "Unknown network, invalid setup skips validation" ,
629+ networkCfg : createNetworkConfig (
630+ []string {"UNKNOWN_NETWORK" },
631+ nil ,
632+ nil ,
633+ nil ,
634+ nil ,
635+ nil ,
636+ ),
637+ expectedError : true ,
638+ expectedErrorMsg : "at least one HTTP RPC endpoint for UNKNOWN_NETWORK network must be set" ,
639+ },
640+ {
641+ name : "Valid known network from MappedNetworks" ,
642+ networkCfg : createNetworkConfig (
643+ []string {"SIMULATED_1" },
644+ nil ,
645+ nil ,
646+ nil ,
647+ nil ,
648+ nil ,
649+ ),
650+ expectedError : false ,
651+ expectedCount : 1 ,
652+ },
653+ }
654+
655+ // Run test cases
656+ for _ , tc := range testCases {
657+ tc := tc // capture range variable
658+ t .Run (tc .name , func (t * testing.T ) {
659+ nets , err := SetNetworks (tc .networkCfg )
660+
661+ // Check for expected error
662+ if tc .expectedError {
663+ require .Error (t , err )
664+ require .Contains (t , err .Error (), tc .expectedErrorMsg )
665+ return
666+ }
667+ require .NoError (t , err )
668+
669+ // Check the expected count of networks returned
670+ require .Equal (t , tc .expectedCount , len (nets ))
671+ })
672+ }
673+ }
0 commit comments