a73x

internal/agent/run/exposures_test.go

Ref:   Size: 1.9 KiB   History

package run

import (
	"testing"

	"github.com/a73x/eitri/internal/agent/state"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// TestGuestAddrAnswersFromTheAgentsOwnRecords pins that the proxy dials the
// same address the tunnel does — the one reconcile keeps current on each VM's
// record — rather than a second, independently drifting answer.
func TestGuestAddrAnswersFromTheAgentsOwnRecords(t *testing.T) {
	st, err := state.Open(t.TempDir())
	require.NoError(t, err)

	// A VM this host has never heard of has no address.
	if got := guestAddr(st, "vm1"); got != "" {
		t.Errorf("guestAddr(unknown) = %q, want empty", got)
	}

	require.NoError(t, st.SaveVM(state.Record{Spec: state.VMSpec{VMID: "vm1"}, IP: "10.77.1.5"}))
	if got := guestAddr(st, "vm1"); got != "10.77.1.5" {
		t.Errorf("guestAddr(vm1) = %q, want 10.77.1.5", got)
	}

	// A record with no address yet is "no answer", not an address of "".
	require.NoError(t, st.SaveVM(state.Record{Spec: state.VMSpec{VMID: "vm2"}}))
	if got := guestAddr(st, "vm2"); got != "" {
		t.Errorf("guestAddr(vm2) = %q, want empty", got)
	}
}

// TestGuestAddrIsTheNATAddressForANetworkedGuest pins which of a two-NIC
// guest's addresses this host reaches it by. Exposures and the SSH tunnel both
// run over the private bridge — that address exists before the guest boots and
// needs no site DHCP server to have answered — so a guest on the operator's LAN
// is published exactly like its NAT-only siblings. Answering with the LAN
// address instead would make a published port depend on someone else's network
// being up.
func TestGuestAddrIsTheNATAddressForANetworkedGuest(t *testing.T) {
	st, err := state.Open(t.TempDir())
	require.NoError(t, err)

	require.NoError(t, st.SaveVM(state.Record{
		Spec:      state.VMSpec{VMID: "vm-lan", Network: "lan"},
		IP:        "10.77.1.5",
		NetworkIP: "192.168.0.42",
	}))

	assert.Equal(t, "10.77.1.5", guestAddr(st, "vm-lan"))
}