2021-08-18 13:35:42 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-03-08 14:47:55 +01:00
|
|
|
"context"
|
|
|
|
"net"
|
2022-03-10 11:53:09 +01:00
|
|
|
"path/filepath"
|
2022-03-08 14:47:55 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/util"
|
2022-03-25 13:21:04 +01:00
|
|
|
|
2022-03-26 12:08:54 +01:00
|
|
|
clientProto "github.com/netbirdio/netbird/client/proto"
|
|
|
|
client "github.com/netbirdio/netbird/client/server"
|
|
|
|
mgmtProto "github.com/netbirdio/netbird/management/proto"
|
|
|
|
mgmt "github.com/netbirdio/netbird/management/server"
|
|
|
|
sigProto "github.com/netbirdio/netbird/signal/proto"
|
|
|
|
sig "github.com/netbirdio/netbird/signal/server"
|
2021-08-18 13:35:42 +02:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2022-03-10 11:53:09 +01:00
|
|
|
func startTestingServices(t *testing.T) string {
|
|
|
|
config := &mgmt.Config{}
|
|
|
|
_, err := util.ReadJson("../testdata/management.json", config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testDir := t.TempDir()
|
|
|
|
config.Datadir = testDir
|
|
|
|
err = util.CopyFileContents("../testdata/store.json", filepath.Join(testDir, "store.json"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, signalLis := startSignal(t)
|
|
|
|
signalAddr := signalLis.Addr().String()
|
|
|
|
config.Signal.URI = signalAddr
|
|
|
|
|
|
|
|
_, mgmLis := startManagement(t, config)
|
|
|
|
mgmAddr := mgmLis.Addr().String()
|
|
|
|
return mgmAddr
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:35:42 +02:00
|
|
|
func startSignal(t *testing.T) (*grpc.Server, net.Listener) {
|
|
|
|
lis, err := net.Listen("tcp", ":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s := grpc.NewServer()
|
|
|
|
sigProto.RegisterSignalExchangeServer(s, sig.NewServer())
|
|
|
|
go func() {
|
|
|
|
if err := s.Serve(lis); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return s, lis
|
|
|
|
}
|
|
|
|
|
2022-03-08 14:47:55 +01:00
|
|
|
func startManagement(t *testing.T, config *mgmt.Config) (*grpc.Server, net.Listener) {
|
2021-08-18 13:35:42 +02:00
|
|
|
lis, err := net.Listen("tcp", ":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s := grpc.NewServer()
|
|
|
|
store, err := mgmt.NewStore(config.Datadir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-29 17:48:31 +02:00
|
|
|
peersUpdateManager := mgmt.NewPeersUpdateManager()
|
2022-10-19 17:43:28 +02:00
|
|
|
accountManager, err := mgmt.BuildManager(store, peersUpdateManager, nil, "")
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-09-02 14:41:54 +02:00
|
|
|
turnManager := mgmt.NewTimeBasedAuthSecretsManager(peersUpdateManager, config.TURNConfig)
|
|
|
|
mgmtServer, err := mgmt.NewServer(config, accountManager, peersUpdateManager, turnManager)
|
2021-08-18 13:35:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
mgmtProto.RegisterManagementServiceServer(s, mgmtServer)
|
|
|
|
go func() {
|
|
|
|
if err := s.Serve(lis); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return s, lis
|
|
|
|
}
|
2022-03-08 14:47:55 +01:00
|
|
|
|
|
|
|
func startClientDaemon(
|
|
|
|
t *testing.T, ctx context.Context, managementURL, configPath string,
|
|
|
|
) (*grpc.Server, net.Listener) {
|
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s := grpc.NewServer()
|
|
|
|
|
2022-04-15 17:30:12 +02:00
|
|
|
server := client.New(ctx, managementURL, adminURL, configPath, "")
|
2022-03-08 14:47:55 +01:00
|
|
|
if err := server.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
clientProto.RegisterDaemonServiceServer(s, server)
|
|
|
|
go func() {
|
|
|
|
if err := s.Serve(lis); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
return s, lis
|
|
|
|
}
|