internal/agent/run/guestcidr_test.go
Ref: Size: 2.5 KiB History
package run
import (
"strings"
"testing"
"github.com/a73x/eitri/internal/agent/state"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func storeWithIdentity(t *testing.T, cidr string) *state.Store {
t.Helper()
st, err := state.Open(t.TempDir())
require.NoError(t, err)
require.NoError(t, st.SaveIdentity(state.Identity{HostID: "h1", BridgeCIDR: cidr,
ServerQUICAddr: "10.0.0.1:8443", ServerCertSHA256: strings.Repeat("a", 64)}))
return st
}
// TestResolveGuestCIDRPrecedence pins D2's ladder, and in particular that a
// persisted identity outranks the flag. Letting a flag win would move the
// bridge under live guests, and every one of them would lose its address on the
// next restart — so a deliberate re-home is an edit to identity.json, not a
// flag.
func TestResolveGuestCIDRPrecedence(t *testing.T) {
t.Run("the durable record wins over everything", func(t *testing.T) {
st := storeWithIdentity(t, "10.20.0.0/24")
got := resolveGuestCIDR(st, Config{BridgeCIDR: "10.30.0.0/24"}, "10.40.0.0/24")
assert.Equal(t, "10.20.0.0/24", got)
})
t.Run("the flag wins over the suggestion", func(t *testing.T) {
st := storeWithIdentity(t, "")
got := resolveGuestCIDR(st, Config{BridgeCIDR: "10.30.0.0/24"}, "10.40.0.0/24")
assert.Equal(t, "10.30.0.0/24", got)
})
t.Run("the suggestion is taken when nothing else has an opinion", func(t *testing.T) {
st := storeWithIdentity(t, "")
assert.Equal(t, "10.40.0.0/24", resolveGuestCIDR(st, Config{}, "10.40.0.0/24"))
})
// "" is a real answer, not a failure: a platform whose OS owns the guest
// network takes no suggestion and goes and looks instead.
t.Run("nothing has an opinion", func(t *testing.T) {
st := storeWithIdentity(t, "")
assert.Empty(t, resolveGuestCIDR(st, Config{}, ""))
})
}
// TestResolveGuestCIDRPersists pins that join and serve cannot disagree. They
// are separate processes that used to source the subnet differently, so
// `eitri-agent join --bridge-cidr X` followed by a plain `eitri-agent` could
// re-home the host on its next start.
func TestResolveGuestCIDRPersists(t *testing.T) {
st := storeWithIdentity(t, "")
require.Equal(t, "10.30.0.0/24", resolveGuestCIDR(st, Config{BridgeCIDR: "10.30.0.0/24"}, ""))
id, ok := st.Identity()
require.True(t, ok)
assert.Equal(t, "10.30.0.0/24", id.BridgeCIDR, "the resolved subnet must be durable")
// A later start with no flag at all resolves the same, from rule 1.
assert.Equal(t, "10.30.0.0/24", resolveGuestCIDR(st, Config{}, ""))
}