internal/agent/cloudhv/console_test.go
Ref: Size: 1.3 KiB History
package cloudhv
import (
"net"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
// TestConsoleSourceOpensTheSerialSocket pins that ConsoleSource dials the
// socket cloud-hypervisor serves the serial line on, and that the resulting
// stream is readable — the shape serialpump.ConsoleSource.Open requires.
func TestConsoleSourceOpensTheSerialSocket(t *testing.T) {
dir := t.TempDir()
sock := filepath.Join(dir, "serial.sock")
ln, err := net.Listen("unix", sock)
require.NoError(t, err)
defer ln.Close()
go func() {
c, aerr := ln.Accept()
if aerr == nil {
_, _ = c.Write([]byte("boot"))
c.Close()
}
}()
src := ConsoleSource(func(vmID string) string { return sock })
rwc, err := src.Open("vm-1")
require.NoError(t, err)
defer rwc.Close()
buf := make([]byte, 4)
_, err = rwc.Read(buf)
require.NoError(t, err)
require.Equal(t, "boot", string(buf))
}
// TestConsoleSourceErrorsWhenSocketAbsent pins that Open surfaces the dial
// error rather than swallowing it — the pump's reopen loop depends on seeing
// a real error to drive its backoff.
func TestConsoleSourceErrorsWhenSocketAbsent(t *testing.T) {
src := ConsoleSource(func(vmID string) string {
return filepath.Join(t.TempDir(), "missing.sock")
})
_, err := src.Open("vm-1")
require.Error(t, err)
}