2024-10-05 21:03:26 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2024-10-24 17:52:08 +00:00
|
|
|
"isle/bootstrap"
|
|
|
|
"isle/daemon/daecommon"
|
|
|
|
"isle/garage"
|
|
|
|
"isle/jsonutil"
|
|
|
|
"isle/nebula"
|
2024-10-05 21:03:26 +00:00
|
|
|
"testing"
|
2024-10-24 17:52:08 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-10-05 21:03:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreate(t *testing.T) {
|
|
|
|
var (
|
2024-10-06 17:38:35 +00:00
|
|
|
h = newIntegrationHarness(t)
|
|
|
|
network = h.createNetwork(t, "primus", nil)
|
2024-10-05 21:03:26 +00:00
|
|
|
)
|
|
|
|
|
2024-10-07 20:41:46 +00:00
|
|
|
gotCreationParams, err := LoadCreationParams(network.stateDir)
|
2024-10-24 17:52:08 +00:00
|
|
|
assert.NoError(t, err)
|
2024-10-31 12:04:19 +00:00
|
|
|
assert.Equal(
|
|
|
|
t, gotCreationParams, network.getBootstrap(t).NetworkCreationParams,
|
|
|
|
)
|
2024-10-05 21:03:26 +00:00
|
|
|
}
|
2024-10-07 20:41:46 +00:00
|
|
|
|
|
|
|
func TestLoad(t *testing.T) {
|
|
|
|
var (
|
|
|
|
h = newIntegrationHarness(t)
|
|
|
|
network = h.createNetwork(t, "primus", &createNetworkOpts{
|
|
|
|
manualShutdown: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
t.Log("Shutting down network")
|
2024-10-24 17:52:08 +00:00
|
|
|
assert.NoError(t, network.Shutdown())
|
2024-10-07 20:41:46 +00:00
|
|
|
|
|
|
|
t.Log("Calling Load")
|
|
|
|
loadedNetwork, err := Load(
|
|
|
|
h.ctx,
|
|
|
|
h.logger.WithNamespace("loadedNetwork"),
|
2024-10-31 12:04:19 +00:00
|
|
|
network.getConfig(t),
|
2024-10-07 20:41:46 +00:00
|
|
|
getEnvBinDirPath(),
|
|
|
|
network.stateDir,
|
|
|
|
h.mkDir(t, "runtime"),
|
|
|
|
network.opts,
|
|
|
|
)
|
2024-10-24 17:52:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-10-07 20:41:46 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
t.Log("Shutting down loadedNetwork")
|
2024-10-24 17:52:08 +00:00
|
|
|
assert.NoError(t, loadedNetwork.Shutdown())
|
2024-10-07 20:41:46 +00:00
|
|
|
})
|
|
|
|
}
|
2024-10-14 10:12:43 +00:00
|
|
|
|
|
|
|
func TestJoin(t *testing.T) {
|
2024-11-05 21:31:57 +00:00
|
|
|
t.Run("simple", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
h = newIntegrationHarness(t)
|
|
|
|
primus = h.createNetwork(t, "primus", nil)
|
|
|
|
secondus = h.joinNetwork(t, primus, "secondus", nil)
|
|
|
|
)
|
2024-10-14 10:12:43 +00:00
|
|
|
|
2024-11-05 21:31:57 +00:00
|
|
|
primusHosts, err := primus.GetHosts(h.ctx)
|
|
|
|
assert.NoError(t, err)
|
2024-10-14 10:12:43 +00:00
|
|
|
|
2024-11-05 21:31:57 +00:00
|
|
|
secondusHosts, err := secondus.GetHosts(h.ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, primusHosts, secondusHosts)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with alloc", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
h = newIntegrationHarness(t)
|
|
|
|
primus = h.createNetwork(t, "primus", nil)
|
|
|
|
secondus = h.joinNetwork(t, primus, "secondus", &joinNetworkOpts{
|
|
|
|
networkConfigOpts: &networkConfigOpts{
|
|
|
|
numStorageAllocs: 1,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
t.Log("reloading primus' hosts")
|
|
|
|
assert.NoError(t, primus.Network.(*network).reloadHosts(h.ctx))
|
|
|
|
|
|
|
|
primusHosts, err := primus.GetHosts(h.ctx)
|
|
|
|
assert.NoError(t, err)
|
2024-10-24 17:52:08 +00:00
|
|
|
|
2024-11-05 21:31:57 +00:00
|
|
|
secondusHosts, err := secondus.GetHosts(h.ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, primusHosts, secondusHosts)
|
|
|
|
})
|
2024-10-14 10:12:43 +00:00
|
|
|
}
|
2024-10-23 18:18:11 +00:00
|
|
|
|
|
|
|
func TestNetwork_GetConfig(t *testing.T) {
|
|
|
|
var (
|
|
|
|
h = newIntegrationHarness(t)
|
|
|
|
network = h.createNetwork(t, "primus", nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
config, err := network.GetConfig(h.ctx)
|
2024-10-24 17:52:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-10-31 12:04:19 +00:00
|
|
|
assert.Equal(t, config, network.getConfig(t))
|
2024-10-24 17:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetwork_SetConfig(t *testing.T) {
|
2024-11-08 16:46:44 +00:00
|
|
|
allocsToRoles := func(
|
2024-10-31 12:04:19 +00:00
|
|
|
hostName nebula.HostName, allocs []bootstrap.GarageHostInstance,
|
2024-11-08 16:46:44 +00:00
|
|
|
) []garage.Role {
|
|
|
|
roles := make([]garage.Role, len(allocs))
|
2024-10-31 12:04:19 +00:00
|
|
|
for i := range allocs {
|
2024-11-08 16:46:44 +00:00
|
|
|
roles[i] = garage.Role{
|
2024-10-31 12:04:19 +00:00
|
|
|
ID: allocs[i].ID,
|
|
|
|
Capacity: 1_000_000_000,
|
|
|
|
Zone: string(hostName),
|
|
|
|
Tags: []string{},
|
|
|
|
}
|
|
|
|
}
|
2024-11-08 16:46:44 +00:00
|
|
|
return roles
|
2024-10-31 12:04:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("add storage alloc", func(t *testing.T) {
|
2024-10-24 17:52:08 +00:00
|
|
|
var (
|
2024-10-31 12:04:19 +00:00
|
|
|
h = newIntegrationHarness(t)
|
|
|
|
network = h.createNetwork(t, "primus", nil)
|
|
|
|
networkConfig = network.getConfig(t)
|
2024-10-24 17:52:08 +00:00
|
|
|
)
|
|
|
|
|
2024-10-31 12:04:19 +00:00
|
|
|
networkConfig.Storage.Allocations = append(
|
|
|
|
networkConfig.Storage.Allocations,
|
2024-10-24 17:52:08 +00:00
|
|
|
daecommon.ConfigStorageAllocation{
|
|
|
|
DataPath: h.mkDir(t, "data").Path,
|
|
|
|
MetaPath: h.mkDir(t, "meta").Path,
|
|
|
|
Capacity: 1,
|
|
|
|
S3APIPort: 4900,
|
|
|
|
RPCPort: 4901,
|
|
|
|
AdminPort: 4902,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-10-31 12:04:19 +00:00
|
|
|
assert.NoError(t, network.SetConfig(h.ctx, networkConfig))
|
2024-10-24 17:52:08 +00:00
|
|
|
|
2024-10-31 12:04:19 +00:00
|
|
|
t.Log("Checking that the Host information was updated")
|
|
|
|
newHostsByName := network.getHostsByName(t)
|
2024-10-24 17:52:08 +00:00
|
|
|
newHost, ok := newHostsByName[network.hostName]
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
|
|
|
allocs := newHost.HostConfigured.Garage.Instances
|
|
|
|
assert.Len(t, allocs, 4)
|
|
|
|
|
|
|
|
newAlloc := allocs[3]
|
|
|
|
assert.NotEmpty(t, newAlloc.ID)
|
|
|
|
newAlloc.ID = ""
|
|
|
|
assert.Equal(t, bootstrap.GarageHostInstance{
|
|
|
|
S3APIPort: 4900,
|
|
|
|
RPCPort: 4901,
|
|
|
|
}, newAlloc)
|
|
|
|
|
2024-10-31 12:04:19 +00:00
|
|
|
t.Log("Checking that the bootstrap file was written with the new host config")
|
2024-10-24 17:52:08 +00:00
|
|
|
var storedBootstrap bootstrap.Bootstrap
|
|
|
|
assert.NoError(t, jsonutil.LoadFile(
|
|
|
|
&storedBootstrap, bootstrap.StateDirPath(network.stateDir.Path),
|
|
|
|
))
|
|
|
|
assert.Equal(t, newHostsByName, storedBootstrap.Hosts)
|
|
|
|
|
2024-10-31 12:04:19 +00:00
|
|
|
t.Log("Checking that garage layout contains the new allocation")
|
2024-11-08 16:46:44 +00:00
|
|
|
expRoles := allocsToRoles(network.hostName, allocs)
|
2024-10-31 12:04:19 +00:00
|
|
|
layout, err := network.garageAdminClient(t).GetLayout(h.ctx)
|
|
|
|
assert.NoError(t, err)
|
2024-11-08 16:46:44 +00:00
|
|
|
assert.ElementsMatch(t, expRoles, layout.Roles)
|
2024-10-31 12:04:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("remove storage alloc", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
h = newIntegrationHarness(t)
|
|
|
|
network = h.createNetwork(t, "primus", &createNetworkOpts{
|
|
|
|
numStorageAllocs: 4,
|
|
|
|
})
|
|
|
|
networkConfig = network.getConfig(t)
|
|
|
|
|
|
|
|
prevHost = network.getHostsByName(t)[network.hostName]
|
|
|
|
removedAlloc = networkConfig.Storage.Allocations[3]
|
|
|
|
removedGarageInst = daecommon.BootstrapGarageHostForAlloc(
|
|
|
|
prevHost, removedAlloc,
|
|
|
|
)
|
2024-10-23 18:18:11 +00:00
|
|
|
)
|
2024-10-24 17:52:08 +00:00
|
|
|
|
2024-10-31 12:04:19 +00:00
|
|
|
networkConfig.Storage.Allocations = networkConfig.Storage.Allocations[:3]
|
|
|
|
assert.NoError(t, network.SetConfig(h.ctx, networkConfig))
|
2024-10-24 17:52:08 +00:00
|
|
|
|
2024-10-31 12:04:19 +00:00
|
|
|
t.Log("Checking that the Host information was updated")
|
|
|
|
newHostsByName := network.getHostsByName(t)
|
|
|
|
newHost, ok := newHostsByName[network.hostName]
|
|
|
|
assert.True(t, ok)
|
2024-10-24 17:52:08 +00:00
|
|
|
|
2024-10-31 12:04:19 +00:00
|
|
|
allocs := newHost.HostConfigured.Garage.Instances
|
|
|
|
assert.Len(t, allocs, 3)
|
|
|
|
assert.NotContains(t, allocs, removedGarageInst)
|
|
|
|
|
|
|
|
t.Log("Checking that the bootstrap file was written with the new host config")
|
|
|
|
var storedBootstrap bootstrap.Bootstrap
|
|
|
|
assert.NoError(t, jsonutil.LoadFile(
|
|
|
|
&storedBootstrap, bootstrap.StateDirPath(network.stateDir.Path),
|
|
|
|
))
|
|
|
|
assert.Equal(t, newHostsByName, storedBootstrap.Hosts)
|
|
|
|
|
|
|
|
t.Log("Checking that garage layout contains the new allocation")
|
2024-11-08 16:46:44 +00:00
|
|
|
expRoles := allocsToRoles(network.hostName, allocs)
|
2024-10-31 12:04:19 +00:00
|
|
|
layout, err := network.garageAdminClient(t).GetLayout(h.ctx)
|
|
|
|
assert.NoError(t, err)
|
2024-11-08 16:46:44 +00:00
|
|
|
assert.ElementsMatch(t, expRoles, layout.Roles)
|
2024-10-24 17:52:08 +00:00
|
|
|
})
|
2024-10-31 12:04:19 +00:00
|
|
|
|
2024-11-05 21:31:57 +00:00
|
|
|
t.Run("remove all storage allocs", func(t *testing.T) {
|
|
|
|
var (
|
|
|
|
h = newIntegrationHarness(t)
|
|
|
|
primus = h.createNetwork(t, "primus", nil)
|
|
|
|
secondus = h.joinNetwork(t, primus, "secondus", &joinNetworkOpts{
|
|
|
|
networkConfigOpts: &networkConfigOpts{
|
|
|
|
numStorageAllocs: 1,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
networkConfig = secondus.getConfig(t)
|
|
|
|
|
|
|
|
prevHost = secondus.getHostsByName(t)[secondus.hostName]
|
|
|
|
removedAlloc = networkConfig.Storage.Allocations[0]
|
|
|
|
removedRole = allocsToRoles(
|
|
|
|
secondus.hostName, prevHost.Garage.Instances,
|
|
|
|
)[0]
|
|
|
|
removedGarageInst = daecommon.BootstrapGarageHostForAlloc(
|
|
|
|
prevHost, removedAlloc,
|
|
|
|
)
|
|
|
|
|
|
|
|
primusGarageAdminClient = primus.garageAdminClient(t)
|
|
|
|
)
|
|
|
|
|
|
|
|
networkConfig.Storage.Allocations = nil
|
|
|
|
assert.NoError(t, secondus.SetConfig(h.ctx, networkConfig))
|
|
|
|
|
|
|
|
t.Log("Checking that the Host information was updated")
|
|
|
|
newHostsByName := primus.getHostsByName(t)
|
|
|
|
newHost, ok := newHostsByName[secondus.hostName]
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
|
|
|
allocs := newHost.HostConfigured.Garage.Instances
|
|
|
|
assert.Len(t, allocs, 3)
|
|
|
|
assert.NotContains(t, allocs, removedGarageInst)
|
|
|
|
|
|
|
|
t.Log("Checking that garage layout still contains the old allocation")
|
|
|
|
layout, err := primusGarageAdminClient.GetLayout(h.ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, layout.Roles, removedRole)
|
|
|
|
|
|
|
|
t.Log("Removing orphan garage nodes with primus")
|
|
|
|
assert.NoError(
|
|
|
|
t, primus.Network.(*network).removeOrphanGarageNodes(h.ctx),
|
|
|
|
)
|
|
|
|
|
|
|
|
t.Log("Checking that garage layout no longer contains the old allocation")
|
|
|
|
layout, err = primusGarageAdminClient.GetLayout(h.ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotContains(t, layout.Roles, removedRole)
|
|
|
|
})
|
2024-10-23 18:18:11 +00:00
|
|
|
}
|