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"
|
|
|
|
|
2024-06-13 01:20:46 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
|
2023-05-19 11:42:25 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
2024-08-07 10:52:31 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/telemetry"
|
2023-05-19 11:42:25 +02:00
|
|
|
|
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"
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
"github.com/netbirdio/management-integrations/integrations"
|
2024-05-16 18:28:37 +02: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
|
|
|
)
|
|
|
|
|
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()
|
2024-09-29 00:22:47 +02:00
|
|
|
srv, err := sig.NewServer(context.Background(), otel.Meter(""))
|
2024-06-13 01:20:46 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sigProto.RegisterSignalExchangeServer(s, srv)
|
2021-08-18 13:35:42 +02:00
|
|
|
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()
|
2024-08-07 10:52:31 +02:00
|
|
|
|
2021-08-18 13:35:42 +02:00
|
|
|
lis, err := net.Listen("tcp", ":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s := grpc.NewServer()
|
2024-07-03 11:33:02 +02:00
|
|
|
store, cleanUp, err := mgmt.NewTestStoreFromJson(context.Background(), config.Datadir)
|
2021-08-18 13:35:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-05-16 18:28:37 +02:00
|
|
|
t.Cleanup(cleanUp)
|
2021-08-18 13:35:42 +02:00
|
|
|
|
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
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
iv, _ := integrations.NewIntegratedValidator(context.Background(), eventStore)
|
2024-08-07 10:52:31 +02:00
|
|
|
|
|
|
|
metrics, err := telemetry.NewDefaultAppMetrics(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
accountManager, err := mgmt.BuildManager(context.Background(), store, peersUpdateManager, nil, "", "netbird.selfhosted", eventStore, nil, false, iv, metrics)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
|
|
|
|
secretsManager := mgmt.NewTimeBasedAuthSecretsManager(peersUpdateManager, config.TURNConfig, config.Relay)
|
|
|
|
mgmtServer, err := mgmt.NewServer(context.Background(), config, accountManager, peersUpdateManager, secretsManager, 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(
|
2024-06-13 11:38:38 +02:00
|
|
|
t *testing.T, ctx context.Context, _, configPath string,
|
2022-03-08 14:47:55 +01:00
|
|
|
) (*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
|
|
|
|
}
|