Test network.Load

This commit is contained in:
Brian Picciano 2024-10-07 22:41:46 +02:00
parent 7f3cbf628f
commit cb6c11acef
2 changed files with 53 additions and 12 deletions

View File

@ -2,21 +2,17 @@ package network
import ( import (
"testing" "testing"
"isle/toolkit"
) )
func TestCreate(t *testing.T) { func TestCreate(t *testing.T) {
toolkit.MarkIntegrationTest(t)
var ( var (
h = newIntegrationHarness(t) h = newIntegrationHarness(t)
network = h.createNetwork(t, "primus", nil) network = h.createNetwork(t, "primus", nil)
) )
gotCreationParams, err := network.GetNetworkCreationParams(h.ctx) gotCreationParams, err := LoadCreationParams(network.stateDir)
if err != nil { if err != nil {
t.Fatalf("calling GetNetworkCreationParams: %v", err) t.Fatalf("calling LoadCreationParams: %v", err)
} else if network.creationParams != gotCreationParams { } else if network.creationParams != gotCreationParams {
t.Fatalf( t.Fatalf(
"expected CreationParams %+v, got %+v", "expected CreationParams %+v, got %+v",
@ -25,3 +21,37 @@ func TestCreate(t *testing.T) {
) )
} }
} }
func TestLoad(t *testing.T) {
var (
h = newIntegrationHarness(t)
network = h.createNetwork(t, "primus", &createNetworkOpts{
manualShutdown: true,
})
)
t.Log("Shutting down network")
if err := network.Shutdown(); err != nil {
t.Fatal(err)
}
t.Log("Calling Load")
loadedNetwork, err := Load(
h.ctx,
h.logger.WithNamespace("loadedNetwork"),
network.networkConfig,
getEnvBinDirPath(),
network.stateDir,
h.mkDir(t, "runtime"),
network.opts,
)
if err != nil {
t.Fatalf("Load failed: %v", err)
}
t.Cleanup(func() {
t.Log("Shutting down loadedNetwork")
if err := loadedNetwork.Shutdown(); err != nil {
t.Logf("Shutting down loadedNetwork failed: %v", err)
}
})
}

View File

@ -78,6 +78,7 @@ type integrationHarness struct {
} }
func newIntegrationHarness(t *testing.T) *integrationHarness { func newIntegrationHarness(t *testing.T) *integrationHarness {
t.Parallel()
toolkit.MarkIntegrationTest(t) toolkit.MarkIntegrationTest(t)
rootDir, err := os.MkdirTemp("", "isle-network-it-test.*") rootDir, err := os.MkdirTemp("", "isle-network-it-test.*")
@ -119,7 +120,7 @@ func (h *integrationHarness) mkDir(t *testing.T, name string) toolkit.Dir {
type createNetworkOpts struct { type createNetworkOpts struct {
creationParams bootstrap.CreationParams creationParams bootstrap.CreationParams
noCleanup bool manualShutdown bool
} }
func (o *createNetworkOpts) withDefaults() *createNetworkOpts { func (o *createNetworkOpts) withDefaults() *createNetworkOpts {
@ -136,8 +137,11 @@ func (o *createNetworkOpts) withDefaults() *createNetworkOpts {
type integrationHarnessNetwork struct { type integrationHarnessNetwork struct {
Network Network
creationParams bootstrap.CreationParams creationParams bootstrap.CreationParams
networkConfig daecommon.NetworkConfig
stateDir, runtimeDir toolkit.Dir stateDir, runtimeDir toolkit.Dir
opts *Opts
} }
func (h *integrationHarness) createNetwork( func (h *integrationHarness) createNetwork(
@ -207,6 +211,10 @@ func (h *integrationHarness) createNetwork(
} }
} }
networkOpts := &Opts{
ChildrenOpts: childrenOpts,
}
network, err := Create( network, err := Create(
h.ctx, h.ctx,
h.logger.WithNamespace("network"), h.logger.WithNamespace("network"),
@ -217,15 +225,13 @@ func (h *integrationHarness) createNetwork(
opts.creationParams, opts.creationParams,
ipNet, ipNet,
hostName, hostName,
&Opts{ networkOpts,
ChildrenOpts: childrenOpts,
},
) )
if err != nil { if err != nil {
t.Fatalf("creating Network: %v", err) t.Fatalf("creating Network: %v", err)
} }
if !opts.noCleanup { if !opts.manualShutdown {
t.Cleanup(func() { t.Cleanup(func() {
t.Log("Shutting down Network") t.Log("Shutting down Network")
if err := network.Shutdown(); err != nil { if err := network.Shutdown(); err != nil {
@ -235,6 +241,11 @@ func (h *integrationHarness) createNetwork(
} }
return integrationHarnessNetwork{ return integrationHarnessNetwork{
network, opts.creationParams, stateDir, runtimeDir, network,
opts.creationParams,
networkConfig,
stateDir,
runtimeDir,
networkOpts,
} }
} }