From bb6d5f602b8e472cc7257b650e0c771a82ec4714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?rich=CE=9Brd?= Date: Thu, 15 Aug 2024 14:43:37 -0400 Subject: [PATCH] chore_: fix `TestBasicWakuV2` (#5718) --- _assets/ci/Jenkinsfile.tests | 48 ++++++++++++++++++++++------- wakuv2/nwaku.go | 58 ++++++++++++++++++++++++++++++++++++ wakuv2/waku_test.go | 36 ++++++++++++++++++---- 3 files changed, 127 insertions(+), 15 deletions(-) create mode 100644 wakuv2/nwaku.go diff --git a/_assets/ci/Jenkinsfile.tests b/_assets/ci/Jenkinsfile.tests index 774d6787f..2a73c25aa 100644 --- a/_assets/ci/Jenkinsfile.tests +++ b/_assets/ci/Jenkinsfile.tests @@ -62,6 +62,11 @@ pipeline { REPO_SRC = "${GOPATH}/src/github.com/status-im/status-go" BASE_BRANCH = "${env.CHANGE_TARGET}" + NWAKU_CONT = "status-go-test-nwaku-${env.EXECUTOR_NUMBER.toInteger() + 1}" + NWAKU_TCP_PORT = "${60000 + env.EXECUTOR_NUMBER.toInteger()}" + NWAKU_UDP_PORT = "${9000 + env.EXECUTOR_NUMBER.toInteger()}" + NWAKU_REST_PORT = "${9645 + env.EXECUTOR_NUMBER.toInteger()}" + /* Hack-fix for params not being set in env on first job run. */ UNIT_TEST_FAILFAST = "${params.UNIT_TEST_FAILFAST}" UNIT_TEST_RERUN_FAILS = "${params.UNIT_TEST_RERUN_FAILS}" @@ -121,28 +126,51 @@ pipeline { stage('Unit Tests') { environment { TEST_POSTGRES_PORT = "${env.DB_PORT}" + NWAKU_REST_PORT = "${env.NWAKU_REST_PORT}" } steps { script { + def ipAddress = sh(script: "hostname -I | awk '{print \$1}'", returnStdout: true).trim() db = docker.image('postgres:9.6-alpine').withRun([ "--name=${DB_CONT}", "--env=POSTGRES_HOST_AUTH_METHOD=trust", "--publish=${DB_PORT}:${DB_PORT}", ].join(' '), "-p ${DB_PORT}") { c -> - nix.shell('make generate-handlers', pure: true) - withCredentials([ - string( - credentialsId: 'codeclimate-test-reporter-id', - variable: 'CC_TEST_REPORTER_ID' - ), - ]) { - nix.shell('make test-unit V=1', pure: false) + nwaku = docker.image('harbor.status.im/wakuorg/nwaku:latest').withRun([ + "--name=${NWAKU_CONT}", + "--publish=${NWAKU_TCP_PORT}:${NWAKU_TCP_PORT}/tcp", + "--publish=${NWAKU_UDP_PORT}:${NWAKU_UDP_PORT}/udp", + "--publish=${NWAKU_REST_PORT}:8645/tcp" + ].join(' '), [ + "--tcp-port=${NWAKU_TCP_PORT}", + "--discv5-discovery=true", + "--cluster-id=16", + "--pubsub-topic=/waku/2/rs/16/32", + "--pubsub-topic=/waku/2/rs/16/64", + "--nat=extip:${ipAddress}", + "--discv5-discovery", + "--discv5-udp-port=${NWAKU_UDP_PORT}", + "--rest-address=0.0.0.0", + "--store", + "--filter", + "--lightpush" + ].join(' ')) { c2 -> + nix.shell('make generate-handlers', pure: true) + withCredentials([ + string( + credentialsId: 'codeclimate-test-reporter-id', + variable: 'CC_TEST_REPORTER_ID' + ), + ]) { + nix.shell('make test-unit V=1', pure: false) + } + sh "mv c.out test-coverage.out" + archiveArtifacts('test-coverage.out, coverage/codeclimate.json, test-coverage.html') } - sh "mv c.out test-coverage.out" - archiveArtifacts('test-coverage.out, coverage/codeclimate.json, test-coverage.html') } } } post { cleanup { /* Leftover DB containers. */ sh "docker rm ${DB_CONT} || true" + sh "docker rm ${NWAKU_CONT} || true" } } } } // stages diff --git a/wakuv2/nwaku.go b/wakuv2/nwaku.go new file mode 100644 index 000000000..ed9f3e80c --- /dev/null +++ b/wakuv2/nwaku.go @@ -0,0 +1,58 @@ +package wakuv2 + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "os" + "strconv" +) + +type NwakuInfo struct { + ListenAddresses []string `json:"listenAddresses"` + EnrUri string `json:"enrUri"` +} + +func GetNwakuInfo(host *string, port *int) (NwakuInfo, error) { + nwakuRestPort := 8645 + if port != nil { + nwakuRestPort = *port + } + envNwakuRestPort := os.Getenv("NWAKU_REST_PORT") + if envNwakuRestPort != "" { + v, err := strconv.Atoi(envNwakuRestPort) + if err != nil { + return NwakuInfo{}, err + } + nwakuRestPort = v + } + + nwakuRestHost := "localhost" + if host != nil { + nwakuRestHost = *host + } + envNwakuRestHost := os.Getenv("NWAKU_REST_HOST") + if envNwakuRestHost != "" { + nwakuRestHost = envNwakuRestHost + } + + resp, err := http.Get(fmt.Sprintf("http://%s:%d/debug/v1/info", nwakuRestHost, nwakuRestPort)) + if err != nil { + return NwakuInfo{}, err + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return NwakuInfo{}, err + } + + var data NwakuInfo + err = json.Unmarshal(body, &data) + if err != nil { + return NwakuInfo{}, err + } + + return data, nil +} diff --git a/wakuv2/waku_test.go b/wakuv2/waku_test.go index e37f1a8d2..b00c20f6d 100644 --- a/wakuv2/waku_test.go +++ b/wakuv2/waku_test.go @@ -154,10 +154,35 @@ func TestRelayPeers(t *testing.T) { require.Error(t, err) } -func TestBasicWakuV2(t *testing.T) { - t.Skip("flaky test") +func parseNodes(rec []string) []*enode.Node { + var ns []*enode.Node + for _, r := range rec { + var n enode.Node + if err := n.UnmarshalText([]byte(r)); err != nil { + panic(err) + } + ns = append(ns, &n) + } + return ns +} - enrTreeAddress := testStoreENRBootstrap +// In order to run these tests, you must run an nwaku node +// +// Using Docker: +// +// IP_ADDRESS=$(hostname -I | awk '{print $1}'); +// docker run \ +// -p 60000:60000/tcp -p 9000:9000/udp -p 8645:8645/tcp harbor.status.im/wakuorg/nwaku:v0.31.0 \ +// --tcp-port=60000 --discv5-discovery=true --cluster-id=16 --pubsub-topic=/waku/2/rs/16/32 --pubsub-topic=/waku/2/rs/16/64 \ +// --nat=extip:${IP_ADDRESS} --discv5-discovery --discv5-udp-port=9000 --rest-address=0.0.0.0 --store + +func TestBasicWakuV2(t *testing.T) { + nwakuInfo, err := GetNwakuInfo(nil, nil) + require.NoError(t, err) + + // Creating a fake DNS Discovery ENRTree + tree, url := makeTestTree("n", parseNodes([]string{nwakuInfo.EnrUri}), nil) + enrTreeAddress := url envEnrTreeAddress := os.Getenv("ENRTREE_ADDRESS") if envEnrTreeAddress != "" { enrTreeAddress = envEnrTreeAddress @@ -166,6 +191,7 @@ func TestBasicWakuV2(t *testing.T) { config := &Config{} setDefaultConfig(config, false) config.Port = 0 + config.Resolver = mapResolver(tree.ToTXT("n")) config.DiscV5BootstrapNodes = []string{enrTreeAddress} config.DiscoveryLimit = 20 config.WakuNodes = []string{enrTreeAddress} @@ -177,7 +203,7 @@ func TestBasicWakuV2(t *testing.T) { ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second) defer cancel() - discoveredNodes, err := dnsdisc.RetrieveNodes(ctx, enrTreeAddress) + discoveredNodes, err := dnsdisc.RetrieveNodes(ctx, enrTreeAddress, dnsdisc.WithResolver(config.Resolver)) require.NoError(t, err) // Peer used for retrieving history @@ -192,7 +218,7 @@ func TestBasicWakuV2(t *testing.T) { // Sanity check, not great, but it's probably helpful err = tt.RetryWithBackOff(func() error { - if len(w.Peers()) < 2 { + if len(w.Peers()) < 1 { return errors.New("no peers discovered") } return nil