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"
|
|
|
|
|
2023-05-19 11:42:25 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
|
|
|
|
2022-03-26 12:08:54 +01:00
|
|
|
"github.com/netbirdio/netbird/util"
|
2022-03-25 13:21:04 +01:00
|
|
|
|
2023-05-19 11:42:25 +02:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2022-03-10 11:53:09 +01:00
|
|
|
func startTestingServices(t *testing.T) string {
|
2023-11-10 16:33:13 +01:00
|
|
|
t.Helper()
|
2022-03-10 11:53:09 +01:00
|
|
|
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) {
|
2023-11-10 16:33:13 +01:00
|
|
|
t.Helper()
|
2021-08-18 13:35:42 +02:00
|
|
|
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) {
|
2023-11-10 16:33:13 +01:00
|
|
|
t.Helper()
|
2021-08-18 13:35:42 +02:00
|
|
|
lis, err := net.Listen("tcp", ":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s := grpc.NewServer()
|
2023-10-12 15:42:36 +02:00
|
|
|
store, err := mgmt.NewStoreFromJson(config.Datadir, nil)
|
2021-08-18 13:35:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-11-16 18:21:52 +01:00
|
|
|
peersUpdateManager := mgmt.NewPeersUpdateManager(nil)
|
2023-01-02 15:11:32 +01:00
|
|
|
eventStore := &activity.InMemoryEventStore{}
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
accountManager, err := mgmt.BuildManager(store, peersUpdateManager, nil, "", "",
|
2023-09-19 18:08:40 +02:00
|
|
|
eventStore, false)
|
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)
|
2023-09-04 11:37:39 +02:00
|
|
|
mgmtServer, err := mgmt.NewServer(config, accountManager, peersUpdateManager, turnManager, nil, nil)
|
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) {
|
2023-11-10 16:33:13 +01:00
|
|
|
t.Helper()
|
2022-03-08 14:47:55 +01:00
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s := grpc.NewServer()
|
|
|
|
|
2023-02-07 11:40:05 +01:00
|
|
|
server := client.New(ctx,
|
|
|
|
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
|
|
|
|
}
|