Skip to content

Commit

Permalink
testutil/compose: integrate p2p fuzz to compose (#2504)
Browse files Browse the repository at this point in the history
Integrates p2p fuzz to compose.

category: test
ticket: #2375
  • Loading branch information
dB2510 authored Aug 4, 2023
1 parent ae45de8 commit 2b768e3
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:
- run: |
echo "CHARON_REPO=$(pwd)" >> $GITHUB_ENV
echo "DOCKER_BUILDKIT=1" >> $GITHUB_ENV
- run: go test -race github.com/obolnetwork/charon/testutil/compose/fuzz -v -fuzz -sudo-perms -timeout=20m -log-dir=.
- run: go test -race github.com/obolnetwork/charon/testutil/compose/fuzz -v -fuzzer -sudo-perms -timeout=45m -log-dir=.
- uses: actions/upload-artifact@v3
if: always()
with:
Expand Down
6 changes: 4 additions & 2 deletions testutil/compose/compose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ func newNewCmd() *cobra.Command {
nodes := cmd.Flags().Int("nodes", conf.NumNodes, "Number of charon nodes in the cluster.")
insecureKeys := cmd.Flags().Bool("insecure-keys", conf.InsecureKeys, "To generate keys quickly.")
slotDuration := cmd.Flags().Duration("simnet-slot-duration", time.Second, "Configures slot duration in simnet beacon mock.")
fuzz := cmd.Flags().Bool("fuzz", false, "Configures simnet beaconmock to return fuzzed responses.")
beaconFuzz := cmd.Flags().Bool("beacon-fuzz", false, "Configures simnet beaconmock to return fuzzed responses.")
p2pFuzz := cmd.Flags().Bool("p2p-fuzz", false, "Configures charon p2p network to return fuzzed responses of one of the nodes in the cluster.")

cmd.RunE = func(cmd *cobra.Command, _ []string) error {
conf.KeyGen = compose.KeyGen(*keygen)
Expand All @@ -152,7 +153,8 @@ func newNewCmd() *cobra.Command {
conf.Threshold = cluster.Threshold(conf.NumNodes)
conf.InsecureKeys = *insecureKeys
conf.SlotDuration = *slotDuration
conf.Fuzz = *fuzz
conf.BeaconFuzz = *beaconFuzz
conf.P2PFuzz = *p2pFuzz

if conf.BuildLocal {
conf.ImageTag = "local"
Expand Down
8 changes: 6 additions & 2 deletions testutil/compose/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
charonImage = "obolnetwork/charon"
containerBinary = "/usr/local/bin/charon"
cmdRun = "run"
cmdUnsafeRun = "[unsafe,run]"
cmdDKG = "dkg"
cmdCreateCluster = "[create,cluster]"
cmdCreateDKG = "[create,dkg]"
Expand Down Expand Up @@ -109,8 +110,11 @@ type Config struct {
// SlotDuration configures slot duration on simnet beacon mock for all the nodes in the cluster.
SlotDuration time.Duration `json:"slot_duration"`

// Fuzz configures simnet beaconmock to return fuzzed responses.
Fuzz bool `json:"fuzz"`
// BeaconFuzz configures simnet beaconmock to return fuzzed responses.
BeaconFuzz bool `json:"beacon-fuzz"`

// P2PFuzz configures charon p2p network to send and receive fuzzed messages.
P2PFuzz bool `json:"p2p-fuzz"`

// SyntheticBlockProposals configures use of synthetic block proposals in simnet cluster.
SyntheticBlockProposals bool `json:"synthetic_block_proposals"`
Expand Down
60 changes: 0 additions & 60 deletions testutil/compose/fuzz/beacon_fuzz_test.go

This file was deleted.

86 changes: 86 additions & 0 deletions testutil/compose/fuzz/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright © 2022-2023 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1

package fuzz_test

import (
"context"
"flag"
"fmt"
"os"
"path"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/obolnetwork/charon/testutil"
"github.com/obolnetwork/charon/testutil/compose"
)

var (
fuzzer = flag.Bool("fuzzer", false, "Enables docker based fuzz tests")
sudoPerms = flag.Bool("sudo-perms", false, "Enables changing all compose artefacts file permissions using sudo.")
logDir = flag.String("log-dir", "", "Specifies the directory to store test docker-compose logs. Empty defaults to stdout.")
)

func TestFuzzers(t *testing.T) {
if !*fuzzer {
t.Skip("Skipping fuzz tests")
}

defaultConfig := compose.NewDefaultConfig()
defaultConfig.DisableMonitoringPorts = true
defaultConfig.BuildLocal = true
defaultConfig.ImageTag = "local"
defaultConfig.InsecureKeys = true

tests := []struct {
name string
configFunc func(compose.Config) compose.Config
timeout time.Duration
}{
{
name: "beacon_fuzz_tests",
configFunc: func(config compose.Config) compose.Config {
config.BeaconFuzz = true

return config
},
timeout: time.Minute * 20,
},
{
name: "p2p_fuzz_tests",
configFunc: func(config compose.Config) compose.Config {
config.P2PFuzz = true

return config
},
timeout: time.Minute * 20,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
dir, err := os.MkdirTemp("", "")
require.NoError(t, err)

config := test.configFunc(defaultConfig)
require.NoError(t, compose.WriteConfig(dir, config))

os.Args = []string{"cobra.test"}

autoConfig := compose.AutoConfig{
Dir: dir,
AlertTimeout: test.timeout,
SudoPerms: *sudoPerms,
}

if *logDir != "" {
autoConfig.LogFile = path.Join(*logDir, fmt.Sprintf("%s.log", test.name))
}

err = compose.Auto(context.Background(), autoConfig)
testutil.RequireNoError(t, err)
})
}
}
2 changes: 1 addition & 1 deletion testutil/compose/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func newNodeEnvs(index int, conf Config, vcType VCType) []kv {
kv{"simnet-validator-mock", fmt.Sprintf(`"%v"`, vcType == VCMock)},
kv{"simnet-slot-duration", conf.SlotDuration.String()},
kv{"simnet-validator-keys-dir", fmt.Sprintf("/compose/node%d/validator_keys", index)},
kv{"simnet-beacon-mock-fuzz", fmt.Sprintf(`"%v"`, conf.Fuzz)},
kv{"simnet-beacon-mock-fuzz", fmt.Sprintf(`"%v"`, conf.BeaconFuzz)},
kv{"loki-addresses", "http://loki:3100/loki/api/v1/push"},
kv{"loki-service", fmt.Sprintf("node%d", index)},
kv{"synthetic-block-proposals", fmt.Sprintf(`"%v"`, conf.SyntheticBlockProposals)},
Expand Down
8 changes: 7 additions & 1 deletion testutil/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ func Run(ctx context.Context, dir string, conf Config) (TmplData, error) {
nodes = append(nodes, n)
}

charonCmd := cmdRun
if conf.P2PFuzz {
nodes[0].EnvVars = append(nodes[0].EnvVars, kv{"p2p-fuzz", fmt.Sprintf(`"%v"`, conf.P2PFuzz)})
charonCmd = cmdUnsafeRun
}

data := TmplData{
ComposeDir: dir,
CharonImageTag: conf.ImageTag,
CharonCommand: cmdRun,
CharonCommand: charonCmd,
Nodes: nodes,
Relay: true,
Monitoring: conf.Monitoring,
Expand Down
3 changes: 2 additions & 1 deletion testutil/compose/testdata/TestNewDefaultConfig.golden
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"disable_monitoring_ports": false,
"insecure_keys": false,
"slot_duration": 1000000000,
"fuzz": false,
"beacon-fuzz": false,
"p2p-fuzz": false,
"synthetic_block_proposals": true,
"monitoring": true,
"builder_api": false
Expand Down

0 comments on commit 2b768e3

Please sign in to comment.