isle/go/daemon/network/network_it_test.go

154 lines
3.3 KiB
Go
Raw Normal View History

package network
import (
"context"
"fmt"
"os"
"path/filepath"
2024-10-06 15:15:40 +00:00
"sync"
"sync/atomic"
"testing"
"isle/bootstrap"
"isle/daemon/daecommon"
"isle/nebula"
"isle/toolkit"
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
"gopkg.in/yaml.v3"
)
var (
2024-10-06 15:15:40 +00:00
getEnvBinDirPath = sync.OnceValue(func() string {
appDirPath := os.Getenv("APPDIR")
if appDirPath == "" {
panic("APPDIR not set")
}
return filepath.Join(appDirPath, "bin")
2024-10-06 15:15:40 +00:00
})
ipNetCounter uint64
)
func newIPNet(t *testing.T) nebula.IPNet {
var (
ipNet nebula.IPNet
ipNetStr = fmt.Sprintf(
"172.16.%d.0/24", atomic.AddUint64(&ipNetCounter, 1),
)
)
if err := ipNet.UnmarshalText([]byte(ipNetStr)); err != nil {
t.Fatalf("parsing IPNet from %q: %v", ipNetStr, err)
}
return ipNet
}
func mustParseNetworkConfigf(str string, args ...any) daecommon.NetworkConfig {
str = fmt.Sprintf(str, args...)
var networkConfig daecommon.NetworkConfig
if err := yaml.Unmarshal([]byte(str), &networkConfig); err != nil {
panic(fmt.Sprintf("parsing network config: %v", err))
}
return networkConfig
}
type harness struct {
ctx context.Context
logger *mlog.Logger
rootDir toolkit.Dir
dirCounter uint64
}
func newHarness(t *testing.T) *harness {
return &harness{
ctx: context.Background(),
logger: mlog.NewLogger(nil),
rootDir: toolkit.Dir{Path: t.TempDir()},
}
}
func (h *harness) mkDir(t *testing.T, name string) toolkit.Dir {
fullName := fmt.Sprintf("%s-%d", name, atomic.AddUint64(&h.dirCounter, 1))
t.Logf("Creating directory %q", fullName)
d, err := h.rootDir.MkChildDir(fullName, false)
if err != nil {
t.Fatalf("creating %q: %v", fullName, err)
}
return d
}
func TestCreate(t *testing.T) {
2024-10-06 15:15:40 +00:00
toolkit.MarkIntegrationTest(t)
var (
h = newHarness(t)
creationParams = bootstrap.NewCreationParams("test", "test.localnet")
networkConfig = mustParseNetworkConfigf(`
vpn:
public_addr: "127.0.0.1:10000"
tun:
device: isle-test
storage:
allocations:
- data_path: %s
meta_path: %s
capacity: 1
- data_path: %s
meta_path: %s
capacity: 1
- data_path: %s
meta_path: %s
capacity: 1
`,
h.mkDir(t, "data").Path,
h.mkDir(t, "meta").Path,
h.mkDir(t, "data").Path,
h.mkDir(t, "meta").Path,
h.mkDir(t, "data").Path,
h.mkDir(t, "meta").Path,
)
stateDir = h.mkDir(t, "state")
runtimeDir = h.mkDir(t, "runtime")
ipNet = newIPNet(t)
hostName = nebula.HostName("primus")
)
network, err := Create(
h.ctx,
h.logger.WithNamespace("network"),
networkConfig,
2024-10-06 15:15:40 +00:00
getEnvBinDirPath(),
stateDir,
runtimeDir,
creationParams,
ipNet,
hostName,
nil,
)
if err != nil {
t.Fatalf("creating Network: %v", err)
}
t.Cleanup(func() {
t.Log("Shutting down Network")
if err := network.Shutdown(); err != nil {
t.Logf("Shutting down Network failed: %v", err)
}
})
gotCreationParams, err := network.GetNetworkCreationParams(h.ctx)
if err != nil {
t.Fatalf("calling GetNetworkCreationParams: %v", err)
} else if creationParams != gotCreationParams {
t.Fatalf(
"expected CreationParams %+v, got %+v",
creationParams,
gotCreationParams,
)
}
}