2024-10-06 17:38:35 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"isle/bootstrap"
|
2024-12-12 21:02:00 +00:00
|
|
|
"isle/daemon/children"
|
2024-10-06 17:38:35 +00:00
|
|
|
"isle/daemon/daecommon"
|
2024-10-31 12:04:19 +00:00
|
|
|
"isle/garage"
|
2024-10-06 17:38:35 +00:00
|
|
|
"isle/nebula"
|
|
|
|
"isle/toolkit"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
2024-10-24 17:52:08 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-10-06 17:38:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Utilities related to running network integration tests
|
|
|
|
|
|
|
|
var (
|
|
|
|
getEnvBinDirPath = sync.OnceValue(func() string {
|
|
|
|
appDirPath := os.Getenv("APPDIR")
|
|
|
|
if appDirPath == "" {
|
|
|
|
panic("APPDIR not set")
|
|
|
|
}
|
2024-12-19 19:57:09 +00:00
|
|
|
return filepath.Join(appDirPath, "usr/libexec/isle")
|
2024-10-06 17:38:35 +00:00
|
|
|
})
|
|
|
|
|
2024-11-19 11:55:56 +00:00
|
|
|
ipNetCounter = new(atomic.Uint64)
|
|
|
|
publicAddrPortCounter = func() *atomic.Uint64 {
|
|
|
|
i := new(atomic.Uint64)
|
|
|
|
i.Store(1024)
|
|
|
|
return i
|
|
|
|
}()
|
2024-10-06 17:38:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func newIPNet() nebula.IPNet {
|
|
|
|
var (
|
|
|
|
ipNet nebula.IPNet
|
|
|
|
ipNetStr = fmt.Sprintf(
|
2024-11-19 11:55:56 +00:00
|
|
|
"172.16.%d.0/24", ipNetCounter.Add(1)-1,
|
2024-10-06 17:38:35 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := ipNet.UnmarshalText([]byte(ipNetStr)); err != nil {
|
|
|
|
panic(fmt.Sprintf("parsing IPNet from %q: %v", ipNetStr, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipNet
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPublicAddr() string {
|
|
|
|
return fmt.Sprintf(
|
2024-11-19 11:55:56 +00:00
|
|
|
"127.0.0.200:%d", publicAddrPortCounter.Add(1)-1,
|
2024-10-06 17:38:35 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
type integrationHarness struct {
|
2024-12-12 21:02:00 +00:00
|
|
|
ctx context.Context
|
|
|
|
logger *mlog.Logger
|
2024-12-17 15:05:41 +00:00
|
|
|
constructors constructors
|
2024-12-12 21:02:00 +00:00
|
|
|
rootDir toolkit.Dir
|
|
|
|
dirCounter atomic.Uint64
|
|
|
|
nebulaDeviceNamer *children.NebulaDeviceNamer
|
2024-10-06 17:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newIntegrationHarness(t *testing.T) *integrationHarness {
|
2024-10-07 20:41:46 +00:00
|
|
|
t.Parallel()
|
2024-10-06 17:38:35 +00:00
|
|
|
toolkit.MarkIntegrationTest(t)
|
|
|
|
|
|
|
|
rootDir, err := os.MkdirTemp("", "isle-network-it-test.*")
|
2024-10-24 17:52:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-10-07 19:12:47 +00:00
|
|
|
t.Logf("Temporary test directory: %q", rootDir)
|
2024-10-06 17:38:35 +00:00
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if t.Failed() {
|
|
|
|
t.Logf("Temp directory for failed test not deleted: %q", rootDir)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Deleting temp directory %q", rootDir)
|
2024-10-24 17:52:08 +00:00
|
|
|
assert.NoError(t, os.RemoveAll(rootDir))
|
2024-10-06 17:38:35 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return &integrationHarness{
|
2024-12-12 21:02:00 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
logger: toolkit.NewTestLogger(t),
|
2024-12-17 15:05:41 +00:00
|
|
|
constructors: newConstructors(),
|
2024-12-12 21:02:00 +00:00
|
|
|
rootDir: toolkit.Dir{Path: rootDir},
|
|
|
|
nebulaDeviceNamer: children.NewNebulaDeviceNamer(),
|
2024-10-06 17:38:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *integrationHarness) mkDir(t *testing.T, name string) toolkit.Dir {
|
2024-11-19 11:55:56 +00:00
|
|
|
fullName := fmt.Sprintf("%s-%d", name, h.dirCounter.Add(1)-1)
|
2024-10-06 17:38:35 +00:00
|
|
|
|
|
|
|
t.Logf("Creating directory %q", fullName)
|
|
|
|
d, err := h.rootDir.MkChildDir(fullName, false)
|
2024-10-24 17:52:08 +00:00
|
|
|
require.NoError(t, err)
|
2024-10-06 17:38:35 +00:00
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2024-10-14 10:12:43 +00:00
|
|
|
type networkConfigOpts struct {
|
|
|
|
hasPublicAddr bool
|
|
|
|
numStorageAllocs int
|
|
|
|
}
|
|
|
|
|
2024-10-27 13:31:10 +00:00
|
|
|
func (o *networkConfigOpts) withDefaults() *networkConfigOpts {
|
|
|
|
if o == nil {
|
|
|
|
o = new(networkConfigOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2024-10-14 10:12:43 +00:00
|
|
|
func (h *integrationHarness) mkNetworkConfig(
|
|
|
|
t *testing.T,
|
|
|
|
opts *networkConfigOpts,
|
|
|
|
) daecommon.NetworkConfig {
|
|
|
|
if opts == nil {
|
|
|
|
opts = new(networkConfigOpts)
|
|
|
|
}
|
|
|
|
|
2024-11-11 14:32:15 +00:00
|
|
|
return daecommon.NewNetworkConfig(func(c *daecommon.NetworkConfig) {
|
|
|
|
if opts.hasPublicAddr {
|
|
|
|
c.VPN.PublicAddr = newPublicAddr()
|
2024-10-14 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
2024-11-11 14:32:15 +00:00
|
|
|
c.Storage.Allocations = make(
|
|
|
|
[]daecommon.ConfigStorageAllocation, opts.numStorageAllocs,
|
|
|
|
)
|
|
|
|
for i := range c.Storage.Allocations {
|
|
|
|
c.Storage.Allocations[i] = daecommon.ConfigStorageAllocation{
|
|
|
|
DataPath: h.mkDir(t, "data").Path,
|
|
|
|
MetaPath: h.mkDir(t, "meta").Path,
|
|
|
|
Capacity: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2024-10-14 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
2024-10-06 17:38:35 +00:00
|
|
|
type createNetworkOpts struct {
|
2024-10-31 12:04:19 +00:00
|
|
|
creationParams bootstrap.CreationParams
|
|
|
|
manualShutdown bool
|
|
|
|
numStorageAllocs int
|
2024-10-06 17:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *createNetworkOpts) withDefaults() *createNetworkOpts {
|
|
|
|
if o == nil {
|
|
|
|
o = new(createNetworkOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.creationParams == (bootstrap.CreationParams{}) {
|
|
|
|
o.creationParams = bootstrap.NewCreationParams("test", "test.localnet")
|
|
|
|
}
|
|
|
|
|
2024-10-31 12:04:19 +00:00
|
|
|
if o.numStorageAllocs == 0 {
|
|
|
|
o.numStorageAllocs = 3
|
|
|
|
}
|
|
|
|
|
2024-10-06 17:38:35 +00:00
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
|
|
|
type integrationHarnessNetwork struct {
|
|
|
|
Network
|
2024-10-07 20:41:46 +00:00
|
|
|
|
2024-10-31 12:04:19 +00:00
|
|
|
ctx context.Context
|
|
|
|
logger *mlog.Logger
|
2024-12-17 15:05:41 +00:00
|
|
|
constructors constructors
|
2024-10-24 17:52:08 +00:00
|
|
|
hostName nebula.HostName
|
2024-10-06 17:38:35 +00:00
|
|
|
stateDir, runtimeDir toolkit.Dir
|
2024-12-12 21:02:00 +00:00
|
|
|
nebulaDeviceNamer *children.NebulaDeviceNamer
|
2024-10-07 20:41:46 +00:00
|
|
|
opts *Opts
|
2024-10-06 17:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *integrationHarness) createNetwork(
|
|
|
|
t *testing.T,
|
|
|
|
hostNameStr string,
|
|
|
|
opts *createNetworkOpts,
|
2024-11-11 14:32:15 +00:00
|
|
|
) *integrationHarnessNetwork {
|
2024-10-14 10:12:43 +00:00
|
|
|
t.Logf("Creating as %q", hostNameStr)
|
2024-10-06 17:38:35 +00:00
|
|
|
opts = opts.withDefaults()
|
|
|
|
|
|
|
|
var (
|
2024-10-31 12:04:19 +00:00
|
|
|
logger = h.logger.WithNamespace("network").WithNamespace(hostNameStr)
|
2024-10-14 10:12:43 +00:00
|
|
|
networkConfig = h.mkNetworkConfig(t, &networkConfigOpts{
|
|
|
|
hasPublicAddr: true,
|
2024-10-31 12:04:19 +00:00
|
|
|
numStorageAllocs: opts.numStorageAllocs,
|
2024-10-14 10:12:43 +00:00
|
|
|
})
|
2024-10-06 17:38:35 +00:00
|
|
|
|
2024-10-14 10:12:43 +00:00
|
|
|
stateDir = h.mkDir(t, "state")
|
|
|
|
runtimeDir = h.mkDir(t, "runtime")
|
2024-10-06 17:38:35 +00:00
|
|
|
|
|
|
|
ipNet = newIPNet()
|
|
|
|
hostName = nebula.HostName(hostNameStr)
|
2024-10-07 19:12:47 +00:00
|
|
|
|
2024-10-14 10:12:43 +00:00
|
|
|
networkOpts = &Opts{
|
2024-10-24 17:52:08 +00:00
|
|
|
GarageAdminToken: "admin_token",
|
2024-11-11 14:32:15 +00:00
|
|
|
Config: &networkConfig,
|
2024-10-06 17:38:35 +00:00
|
|
|
}
|
2024-10-14 10:12:43 +00:00
|
|
|
)
|
2024-10-07 20:41:46 +00:00
|
|
|
|
2024-12-17 15:05:41 +00:00
|
|
|
network, err := h.constructors.create(
|
2024-10-06 17:38:35 +00:00
|
|
|
h.ctx,
|
2024-10-31 12:04:19 +00:00
|
|
|
logger,
|
2024-10-06 17:38:35 +00:00
|
|
|
getEnvBinDirPath(),
|
2024-12-12 21:02:00 +00:00
|
|
|
h.nebulaDeviceNamer,
|
2024-10-06 17:38:35 +00:00
|
|
|
stateDir,
|
|
|
|
runtimeDir,
|
|
|
|
opts.creationParams,
|
|
|
|
ipNet,
|
|
|
|
hostName,
|
2024-10-07 20:41:46 +00:00
|
|
|
networkOpts,
|
2024-10-06 17:38:35 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating Network: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-11-11 14:32:15 +00:00
|
|
|
nh := &integrationHarnessNetwork{
|
2024-10-07 20:41:46 +00:00
|
|
|
network,
|
2024-10-31 12:04:19 +00:00
|
|
|
h.ctx,
|
|
|
|
logger,
|
2024-12-17 15:05:41 +00:00
|
|
|
h.constructors,
|
2024-10-24 17:52:08 +00:00
|
|
|
hostName,
|
2024-10-07 20:41:46 +00:00
|
|
|
stateDir,
|
|
|
|
runtimeDir,
|
2024-12-12 21:02:00 +00:00
|
|
|
h.nebulaDeviceNamer,
|
2024-10-07 20:41:46 +00:00
|
|
|
networkOpts,
|
2024-10-06 17:38:35 +00:00
|
|
|
}
|
2024-11-11 14:32:15 +00:00
|
|
|
|
|
|
|
if !opts.manualShutdown {
|
|
|
|
t.Cleanup(func() {
|
|
|
|
t.Logf("Shutting down Network %q", hostNameStr)
|
|
|
|
if err := nh.Shutdown(); err != nil {
|
|
|
|
t.Logf("Shutting down Network %q failed: %v", hostNameStr, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nh
|
2024-10-06 17:38:35 +00:00
|
|
|
}
|
2024-10-14 10:12:43 +00:00
|
|
|
|
|
|
|
type joinNetworkOpts struct {
|
2024-10-27 13:31:10 +00:00
|
|
|
*networkConfigOpts
|
2024-10-14 10:12:43 +00:00
|
|
|
canCreateHosts bool
|
|
|
|
manualShutdown bool
|
|
|
|
}
|
|
|
|
|
2024-10-27 13:31:10 +00:00
|
|
|
func (o *joinNetworkOpts) withDefaults() *joinNetworkOpts {
|
|
|
|
if o == nil {
|
|
|
|
o = new(joinNetworkOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
o.networkConfigOpts = o.networkConfigOpts.withDefaults()
|
|
|
|
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2024-10-14 10:12:43 +00:00
|
|
|
func (h *integrationHarness) joinNetwork(
|
|
|
|
t *testing.T,
|
2024-11-11 14:32:15 +00:00
|
|
|
network *integrationHarnessNetwork,
|
2024-10-14 10:12:43 +00:00
|
|
|
hostNameStr string,
|
|
|
|
opts *joinNetworkOpts,
|
2024-11-11 14:32:15 +00:00
|
|
|
) *integrationHarnessNetwork {
|
2024-10-27 13:31:10 +00:00
|
|
|
opts = opts.withDefaults()
|
|
|
|
hostName := nebula.HostName(hostNameStr)
|
|
|
|
|
|
|
|
t.Logf("Creating bootstrap for %q", hostNameStr)
|
|
|
|
joiningBootstrap, err := network.CreateHost(h.ctx, hostName, CreateHostOpts{
|
|
|
|
CanCreateHosts: opts.canCreateHosts,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating host joining bootstrap: %v", err)
|
|
|
|
}
|
2024-10-14 10:12:43 +00:00
|
|
|
|
|
|
|
var (
|
2024-10-31 12:04:19 +00:00
|
|
|
logger = h.logger.WithNamespace("network").WithNamespace(hostNameStr)
|
2024-10-27 13:31:10 +00:00
|
|
|
networkConfig = h.mkNetworkConfig(t, opts.networkConfigOpts)
|
2024-10-14 10:12:43 +00:00
|
|
|
stateDir = h.mkDir(t, "state")
|
|
|
|
runtimeDir = h.mkDir(t, "runtime")
|
|
|
|
networkOpts = &Opts{
|
2024-10-24 17:52:08 +00:00
|
|
|
GarageAdminToken: "admin_token",
|
2024-11-11 14:32:15 +00:00
|
|
|
Config: &networkConfig,
|
2024-10-14 10:12:43 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-10-27 13:31:10 +00:00
|
|
|
t.Logf("Joining as %q", hostNameStr)
|
2024-12-17 15:05:41 +00:00
|
|
|
joinedNetwork, err := h.constructors.join(
|
2024-10-14 10:12:43 +00:00
|
|
|
h.ctx,
|
2024-10-31 12:04:19 +00:00
|
|
|
logger,
|
2024-11-10 21:13:06 +00:00
|
|
|
getEnvBinDirPath(),
|
2024-12-12 21:02:00 +00:00
|
|
|
h.nebulaDeviceNamer,
|
2024-10-14 10:12:43 +00:00
|
|
|
joiningBootstrap,
|
|
|
|
stateDir,
|
|
|
|
runtimeDir,
|
|
|
|
networkOpts,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("joining network: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-11-11 14:32:15 +00:00
|
|
|
nh := &integrationHarnessNetwork{
|
2024-10-14 10:12:43 +00:00
|
|
|
joinedNetwork,
|
2024-10-31 12:04:19 +00:00
|
|
|
h.ctx,
|
|
|
|
logger,
|
2024-12-17 15:05:41 +00:00
|
|
|
h.constructors,
|
2024-10-24 17:52:08 +00:00
|
|
|
hostName,
|
2024-10-14 10:12:43 +00:00
|
|
|
stateDir,
|
|
|
|
runtimeDir,
|
2024-12-12 21:02:00 +00:00
|
|
|
h.nebulaDeviceNamer,
|
2024-10-14 10:12:43 +00:00
|
|
|
networkOpts,
|
|
|
|
}
|
2024-11-11 14:32:15 +00:00
|
|
|
|
|
|
|
if !opts.manualShutdown {
|
|
|
|
t.Cleanup(func() {
|
|
|
|
t.Logf("Shutting down Network %q", hostNameStr)
|
|
|
|
if err := nh.Shutdown(); err != nil {
|
|
|
|
t.Logf("Shutting down Network %q failed: %v", hostNameStr, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nh
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nh *integrationHarnessNetwork) restart(t *testing.T) {
|
|
|
|
t.Log("Shutting down network (restart)")
|
|
|
|
require.NoError(t, nh.Network.Shutdown())
|
|
|
|
|
|
|
|
t.Log("Loading network (restart)")
|
|
|
|
var err error
|
2024-12-17 15:05:41 +00:00
|
|
|
nh.Network, err = nh.constructors.load(
|
2024-11-11 14:32:15 +00:00
|
|
|
nh.ctx,
|
|
|
|
nh.logger,
|
|
|
|
getEnvBinDirPath(),
|
2024-12-12 21:02:00 +00:00
|
|
|
nh.nebulaDeviceNamer,
|
2024-11-11 14:32:15 +00:00
|
|
|
nh.stateDir,
|
|
|
|
nh.runtimeDir,
|
|
|
|
nh.opts,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2024-10-14 10:12:43 +00:00
|
|
|
}
|
2024-10-31 12:04:19 +00:00
|
|
|
|
|
|
|
func (nh *integrationHarnessNetwork) getConfig(t *testing.T) daecommon.NetworkConfig {
|
|
|
|
networkConfig, err := nh.Network.GetConfig(nh.ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return networkConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nh *integrationHarnessNetwork) getBootstrap(
|
|
|
|
t *testing.T,
|
|
|
|
) bootstrap.Bootstrap {
|
|
|
|
currBootstrap, err := nh.Network.(*network).getBootstrap()
|
|
|
|
require.NoError(t, err)
|
|
|
|
return currBootstrap
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nh *integrationHarnessNetwork) garageAdminClient(
|
|
|
|
t *testing.T,
|
|
|
|
) *garage.AdminClient {
|
|
|
|
c := newGarageAdminClient(
|
|
|
|
nh.logger,
|
|
|
|
nh.getConfig(t),
|
|
|
|
nh.opts.GarageAdminToken,
|
|
|
|
nh.getBootstrap(t).ThisHost(),
|
|
|
|
)
|
|
|
|
t.Cleanup(func() { assert.NoError(t, c.Close()) })
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nh *integrationHarnessNetwork) getHostsByName(
|
|
|
|
t *testing.T,
|
|
|
|
) map[nebula.HostName]bootstrap.Host {
|
2024-12-07 19:39:13 +00:00
|
|
|
currBootstrap, err := nh.Network.GetBootstrap(nh.ctx)
|
2024-10-31 12:04:19 +00:00
|
|
|
require.NoError(t, err)
|
2024-12-07 19:39:13 +00:00
|
|
|
return currBootstrap.Hosts
|
2024-10-31 12:04:19 +00:00
|
|
|
}
|