go-waku/waku/v2/rpc/admin_test.go

73 lines
1.8 KiB
Go
Raw Normal View History

2021-11-10 13:36:51 +00:00
package rpc
import (
"bytes"
"context"
"crypto/rand"
"fmt"
"net/http"
"testing"
2021-11-17 16:19:42 +00:00
"time"
2021-11-10 13:36:51 +00:00
"github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
"github.com/waku-org/go-waku/tests"
"github.com/waku-org/go-waku/waku/v2/node"
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
2022-12-09 03:08:04 +00:00
"github.com/waku-org/go-waku/waku/v2/timesource"
"github.com/waku-org/go-waku/waku/v2/utils"
2021-11-10 13:36:51 +00:00
)
func makeAdminService(t *testing.T) *AdminService {
options := node.WithWakuRelay()
2023-01-06 22:37:57 +00:00
n, err := node.New(options)
2021-11-10 13:36:51 +00:00
require.NoError(t, err)
2023-01-06 22:37:57 +00:00
err = n.Start(context.Background())
2021-11-10 13:36:51 +00:00
require.NoError(t, err)
return &AdminService{n, utils.Logger()}
2021-11-10 13:36:51 +00:00
}
func TestV1Peers(t *testing.T) {
port, err := tests.FindFreePort(t, "", 5)
require.NoError(t, err)
host, err := tests.MakeHost(context.Background(), port, rand.Reader)
require.NoError(t, err)
2023-01-06 22:37:57 +00:00
relay := relay.NewWakuRelay(host, nil, 0, timesource.NewDefaultClock(), utils.Logger())
err = relay.Start(context.Background())
2021-11-17 16:19:42 +00:00
require.NoError(t, err)
defer relay.Stop()
2021-11-10 13:36:51 +00:00
var reply PeersReply
request, err := http.NewRequest(http.MethodPost, "url", bytes.NewReader([]byte("")))
require.NoError(t, err)
a := makeAdminService(t)
err = a.GetV1Peers(request, &GetPeersArgs{}, &reply)
require.NoError(t, err)
2022-07-28 18:29:35 +00:00
require.Len(t, reply, 0)
2021-11-10 13:36:51 +00:00
var reply2 SuccessReply
hostInfo, err := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", host.ID().Pretty()))
require.NoError(t, err)
var addr multiaddr.Multiaddr
for _, a := range host.Addrs() {
addr = a.Encapsulate(hostInfo)
break
}
err = a.PostV1Peers(request, &PeersArgs{Peers: []string{addr.String()}}, &reply2)
require.NoError(t, err)
2022-06-14 15:36:34 +00:00
require.True(t, reply2)
2021-11-10 13:36:51 +00:00
2021-11-17 16:19:42 +00:00
time.Sleep(2 * time.Second)
2021-11-10 13:36:51 +00:00
err = a.GetV1Peers(request, &GetPeersArgs{}, &reply)
require.NoError(t, err)
2022-07-28 18:29:35 +00:00
require.Len(t, reply, 2)
2021-11-10 13:36:51 +00:00
}