From 4f1c7e1164491adf3583497137d00941932a8120 Mon Sep 17 00:00:00 2001 From: skudasov Date: Wed, 6 Nov 2024 15:51:10 +0100 Subject: [PATCH] try only static ports --- framework/components/clnode/clnode.go | 52 ++++++++------- .../components/simple_node_set/node_set.go | 31 +++++++-- framework/examples/myproject/.envrc | 3 +- framework/examples/myproject/chaos.toml | 23 +++++++ framework/examples/myproject/chaos_test.go | 46 ++++++++++++++ framework/examples/myproject/load.toml | 4 +- framework/examples/myproject/load_test.go | 63 ++++++++----------- 7 files changed, 152 insertions(+), 70 deletions(-) create mode 100644 framework/examples/myproject/chaos.toml create mode 100644 framework/examples/myproject/chaos_test.go diff --git a/framework/components/clnode/clnode.go b/framework/components/clnode/clnode.go index 66bb2465e..9131828a4 100644 --- a/framework/components/clnode/clnode.go +++ b/framework/components/clnode/clnode.go @@ -19,8 +19,8 @@ import ( ) const ( - Port = "6688" - P2PPort = "6690" + DefaultHTTPPort = "6688" + DefaultP2PPort = "6690" ) var ( @@ -144,8 +144,8 @@ func newNode(in *Input, pgOut *postgres.Output) (*NodeOut, error) { return nil, err } - httpPort := fmt.Sprintf("%s/tcp", Port) - p2pPort := fmt.Sprintf("%s/udp", P2PPort) + httpPort := fmt.Sprintf("%s/tcp", DefaultHTTPPort) + p2pPort := fmt.Sprintf("%s/udp", DefaultP2PPort) var containerName string if in.Node.Name != "" { containerName = in.Node.Name @@ -263,29 +263,33 @@ func newNode(in *Input, pgOut *postgres.Output) (*NodeOut, error) { if err != nil { return nil, err } - var ( - mp nat.Port - mpP2P nat.Port - ) - if in.Node.HTTPPort != 0 && in.Node.P2PPort != 0 { - mp = nat.Port(fmt.Sprintf("%d/tcp", in.Node.HTTPPort)) - mpP2P = nat.Port(fmt.Sprintf("%d/udp", in.Node.P2PPort)) - } else { - mp, err = c.MappedPort(ctx, nat.Port(httpPort)) - if err != nil { - return nil, err - } - mpP2P, err = c.MappedPort(ctx, nat.Port(p2pPort)) - if err != nil { - return nil, err - } - } + //var ( + // mp nat.Port + // mpP2P nat.Port + //) + //if in.Node.HTTPPort != 0 && in.Node.P2PPort != 0 { + // mp = nat.Port(fmt.Sprintf("%d/tcp", in.Node.HTTPPort)) + // mpP2P = nat.Port(fmt.Sprintf("%d/udp", in.Node.P2PPort)) + //} + //else { + // mp, err = c.MappedPort(ctx, nat.Port(httpPort)) + // if err != nil { + // return nil, err + // } + // mpP2P, err = c.MappedPort(ctx, nat.Port(p2pPort)) + // if err != nil { + // return nil, err + // } + //} + + mp := nat.Port(fmt.Sprintf("%d/tcp", in.Node.HTTPPort)) + mpP2P := nat.Port(fmt.Sprintf("%d/udp", in.Node.P2PPort)) return &NodeOut{ HostURL: fmt.Sprintf("http://%s:%s", host, mp.Port()), HostP2PURL: fmt.Sprintf("http://%s:%s", host, mpP2P.Port()), - DockerURL: fmt.Sprintf("http://%s:%s", containerName, Port), - DockerP2PUrl: fmt.Sprintf("http://%s:%s", containerName, P2PPort), + DockerURL: fmt.Sprintf("http://%s:%s", containerName, DefaultHTTPPort), + DockerP2PUrl: fmt.Sprintf("http://%s:%s", containerName, DefaultP2PPort), }, nil } @@ -296,7 +300,7 @@ type DefaultCLNodeConfig struct { func generateDefaultConfig(in *Input) (string, error) { config := DefaultCLNodeConfig{ - HTTPPort: Port, + HTTPPort: DefaultHTTPPort, SecureCookies: false, } tmpl, err := template.New("toml").Parse(defaultConfigTmpl) diff --git a/framework/components/simple_node_set/node_set.go b/framework/components/simple_node_set/node_set.go index 74f88fd57..c51b36f0c 100644 --- a/framework/components/simple_node_set/node_set.go +++ b/framework/components/simple_node_set/node_set.go @@ -11,11 +11,18 @@ import ( "sync" ) +const ( + DefaultHTTPPortStaticRangeStart = 10000 + DefaultP2PStaticRangeStart = 12000 +) + type Input struct { - Nodes int `toml:"nodes" validate:"required"` - OverrideMode string `toml:"override_mode" validate:"required,oneof=all each"` - NodeSpecs []*clnode.Input `toml:"node_specs"` - Out *Output `toml:"out"` + Nodes int `toml:"nodes" validate:"required"` + HTTPPortRangeStart int `toml:"http_port_range_start"` + P2PPortRangeStart int `toml:"p2p_port_range_start"` + OverrideMode string `toml:"override_mode" validate:"required,oneof=all each"` + NodeSpecs []*clnode.Input `toml:"node_specs"` + Out *Output `toml:"out"` } type Output struct { @@ -65,6 +72,18 @@ func sharedDBSetup(in *Input, bcOut *blockchain.Output, fakeUrl string, override return nil, err } nodeOuts := make([]*clnode.Output, 0) + + var ( + httpPortRangeStart = DefaultHTTPPortStaticRangeStart + p2pPortRangeStart = DefaultP2PStaticRangeStart + ) + if in.HTTPPortRangeStart != 0 { + httpPortRangeStart = in.HTTPPortRangeStart + } + if in.P2PPortRangeStart != 0 { + p2pPortRangeStart = in.P2PPortRangeStart + } + eg := &errgroup.Group{} mu := &sync.Mutex{} for i := 0; i < in.Nodes; i++ { @@ -89,8 +108,8 @@ func sharedDBSetup(in *Input, bcOut *blockchain.Output, fakeUrl string, override DataProviderURL: fakeUrl, DbInput: in.NodeSpecs[overrideIdx].DbInput, Node: &clnode.NodeInput{ - HTTPPort: in.NodeSpecs[overrideIdx].Node.HTTPPort + i, - P2PPort: in.NodeSpecs[overrideIdx].Node.P2PPort + i, + HTTPPort: httpPortRangeStart + i, + P2PPort: p2pPortRangeStart + i, Image: in.NodeSpecs[overrideIdx].Node.Image, Name: nodeName, PullImage: in.NodeSpecs[overrideIdx].Node.PullImage, diff --git a/framework/examples/myproject/.envrc b/framework/examples/myproject/.envrc index a9764aeb1..2bc67310e 100644 --- a/framework/examples/myproject/.envrc +++ b/framework/examples/myproject/.envrc @@ -2,4 +2,5 @@ export TESTCONTAINERS_RYUK_DISABLED=true export PRIVATE_KEY="..." # load test export LOKI_TENANT_ID=promtail -export LOKI_URL=http://localhost:3030/loki/api/v1/push \ No newline at end of file +export LOKI_URL=http://localhost:3030/loki/api/v1/push +export RESTY_DEBUG=true \ No newline at end of file diff --git a/framework/examples/myproject/chaos.toml b/framework/examples/myproject/chaos.toml new file mode 100644 index 000000000..7d1b727e2 --- /dev/null +++ b/framework/examples/myproject/chaos.toml @@ -0,0 +1,23 @@ + +[blockchain_a] + chain_id = "31337" + image = "f4hrenh9it/foundry:latest" + port = "8545" + type = "anvil" + +[data_provider] + port = 9111 + +[nodeset] + nodes = 5 + override_mode = "all" + + [[nodeset.node_specs]] + + [nodeset.node_specs.db] + image = "postgres:15.6" + pull_image = true + + [nodeset.node_specs.node] + image = "public.ecr.aws/chainlink/chainlink:v2.17.0" + pull_image = false \ No newline at end of file diff --git a/framework/examples/myproject/chaos_test.go b/framework/examples/myproject/chaos_test.go new file mode 100644 index 000000000..a3305ad4b --- /dev/null +++ b/framework/examples/myproject/chaos_test.go @@ -0,0 +1,46 @@ +package examples + +import ( + "github.com/smartcontractkit/chainlink-testing-framework/framework" + "github.com/smartcontractkit/chainlink-testing-framework/framework/chaos" + "github.com/smartcontractkit/chainlink-testing-framework/framework/clclient" + "github.com/smartcontractkit/chainlink-testing-framework/framework/components/blockchain" + "github.com/smartcontractkit/chainlink-testing-framework/framework/components/fake" + ns "github.com/smartcontractkit/chainlink-testing-framework/framework/components/simple_node_set" + "github.com/stretchr/testify/require" + "testing" + "time" +) + +type CfgChaos struct { + BlockchainA *blockchain.Input `toml:"blockchain_a" validate:"required"` + MockerDataProvider *fake.Input `toml:"data_provider" validate:"required"` + NodeSet *ns.Input `toml:"nodeset" validate:"required"` +} + +func TestChaos(t *testing.T) { + in, err := framework.Load[CfgChaos](t) + require.NoError(t, err) + + bc, err := blockchain.NewBlockchainNetwork(in.BlockchainA) + require.NoError(t, err) + dp, err := fake.NewFakeDataProvider(in.MockerDataProvider) + require.NoError(t, err) + out, err := ns.NewSharedDBNodeSet(in.NodeSet, bc, dp.BaseURLDocker) + require.NoError(t, err) + + c, err := clclient.NewCLDefaultClients(out.CLNodes, framework.L) + require.NoError(t, err) + + t.Run("run the cluster and simulate slow network", func(t *testing.T) { + // example commands for Pumba: + // stop --duration=1s --restart re2:node0 # stop one container for 1s and restart + // "netem --tc-image=gaiadocker/iproute2 --duration=1m delay --time=300 re2:node.* # slow network + _, err = chaos.ExecPumba("stop --duration=10s --restart re2:node0") + require.NoError(t, err) + time.Sleep(15 * time.Second) + // we need to reconnect since we've rebooted some containers + _, _, err = c[0].ReadBridges() + require.NoError(t, err) + }) +} diff --git a/framework/examples/myproject/load.toml b/framework/examples/myproject/load.toml index ab8a7c46d..7d1b727e2 100644 --- a/framework/examples/myproject/load.toml +++ b/framework/examples/myproject/load.toml @@ -20,6 +20,4 @@ [nodeset.node_specs.node] image = "public.ecr.aws/chainlink/chainlink:v2.17.0" - pull_image = false - port = 5000 - p2p_port = 5100 + pull_image = false \ No newline at end of file diff --git a/framework/examples/myproject/load_test.go b/framework/examples/myproject/load_test.go index 9b5e735d8..445931cfb 100644 --- a/framework/examples/myproject/load_test.go +++ b/framework/examples/myproject/load_test.go @@ -2,12 +2,13 @@ package examples import ( "github.com/smartcontractkit/chainlink-testing-framework/framework" - "github.com/smartcontractkit/chainlink-testing-framework/framework/chaos" "github.com/smartcontractkit/chainlink-testing-framework/framework/clclient" "github.com/smartcontractkit/chainlink-testing-framework/framework/components/blockchain" "github.com/smartcontractkit/chainlink-testing-framework/framework/components/fake" ns "github.com/smartcontractkit/chainlink-testing-framework/framework/components/simple_node_set" + "github.com/smartcontractkit/chainlink-testing-framework/wasp" "github.com/stretchr/testify/require" + "os" "testing" "time" ) @@ -29,44 +30,34 @@ func TestLoad(t *testing.T) { out, err := ns.NewSharedDBNodeSet(in.NodeSet, bc, dp.BaseURLDocker) require.NoError(t, err) - //var lokiCfg *wasp.LokiConfig - //// temp fix, we can't reach shared Loki instance in CI - //if os.Getenv("CI") != "true" { - // lokiCfg = wasp.NewEnvLokiConfig() - //} + var lokiCfg *wasp.LokiConfig + // temp fix, we can't reach shared Loki instance in CI + if os.Getenv("CI") != "true" { + lokiCfg = wasp.NewEnvLokiConfig() + } - _, err = clclient.NewCLDefaultClients(out.CLNodes, framework.L) + c, err := clclient.NewCLDefaultClients(out.CLNodes, framework.L) require.NoError(t, err) - t.Run("run the cluster and simulate slow network", func(t *testing.T) { - //p, err := wasp.NewProfile(). - // Add(wasp.NewGenerator(&wasp.Config{ - // T: t, - // LoadType: wasp.RPS, - // Schedule: wasp.Combine( - // wasp.Steps(1, 1, 9, 30*time.Second), - // wasp.Plain(10, 30*time.Second), - // wasp.Steps(10, -1, 10, 30*time.Second), - // ), - // Gun: NewCLNodeGun(c[0], "bridges"), - // Labels: map[string]string{ - // "gen_name": "cl_node_api_call", - // "branch": "example", - // "commit": "example", - // }, - // LokiConfig: lokiCfg, - // })). - // Run(false) - //require.NoError(t, err) - // example commands for Pumba: - // stop --duration=1s --restart re2:node0 # stop one container for 1s and restart - // "netem --tc-image=gaiadocker/iproute2 --duration=1m delay --time=300 re2:node.* # slow network - _, err = chaos.ExecPumba("stop --duration=1s --restart re2:node0") + t.Run("load test chainlink nodes", func(t *testing.T) { + _, err := wasp.NewProfile(). + Add(wasp.NewGenerator(&wasp.Config{ + T: t, + LoadType: wasp.RPS, + Schedule: wasp.Combine( + wasp.Steps(1, 1, 9, 30*time.Second), + wasp.Plain(10, 30*time.Second), + wasp.Steps(10, -1, 10, 30*time.Second), + ), + Gun: NewCLNodeGun(c[0], "bridges"), + Labels: map[string]string{ + "gen_name": "cl_node_api_call", + "branch": "example", + "commit": "example", + }, + LokiConfig: lokiCfg, + })). + Run(true) require.NoError(t, err) - time.Sleep(5 * time.Second) - _, err = clclient.NewCLDefaultClients(out.CLNodes, framework.L) - require.NoError(t, err) - - //p.Wait() }) }