test: serialize Windows POSIX shell fixtures across packages
This commit is contained in:
1 parent
53627e5e1a
commit
dbf475d55c
12 files changed
+148
-18
No files matched your search
@@ -37,6 +37,7 @@ func TestGuestAgentClipboardScript(t *testing.T) {
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
lockShellFixture(t)
|
||||
tmp := t.TempDir()
|
||||
script := filepath.Join(tmp, "clipboard-test.sh")
|
||||
code := "set -eu\n" + agent[start:end] + clipboardShellFixture
|
||||
|
||||
@@ -18,6 +18,7 @@ func TestGuestAgentDriveMount(t *testing.T) {
|
||||
t.Skip("POSIX shell is unavailable")
|
||||
}
|
||||
}
|
||||
lockShellFixture(t)
|
||||
|
||||
dir := t.TempDir()
|
||||
helper := `#!/usr/bin/perl
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -23,6 +24,7 @@ func TestGuestAgentFolderMounts(t *testing.T) {
|
||||
run := func(t *testing.T, script string, args ...string) {
|
||||
t.Helper()
|
||||
t.Parallel()
|
||||
lockShellFixture(t)
|
||||
cmd := exec.Command(sh, args...)
|
||||
cmd.Dir = t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(cmd.Dir, "mount_webdav"), []byte(folderMountWebDAV), 0o755); err != nil {
|
||||
@@ -35,9 +37,12 @@ func TestGuestAgentFolderMounts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
agent, uninstall, install := string(GuestAgentScript()), string(GuestUninstallScript()), string(GuestInstallScript())
|
||||
if CurrentAgentVersion != 10 || !strings.Contains(agent, `"agent_version\":10,`) ||
|
||||
!strings.Contains(install, "Reims Guest Tools 8 installed and connected!") {
|
||||
t.Fatal("missing Guest Tools version 10")
|
||||
if CurrentAgentVersion != KeymapAgentVersion {
|
||||
t.Fatalf("CurrentAgentVersion = %d, want keymap version %d", CurrentAgentVersion, KeymapAgentVersion)
|
||||
}
|
||||
versionMarker := `"agent_version\":` + strconv.Itoa(CurrentAgentVersion) + `,`
|
||||
if !strings.Contains(agent, versionMarker) || !strings.Contains(install, "Reims Guest Tools installed") {
|
||||
t.Fatal("Guest Tools scripts do not advertise the current agent version")
|
||||
}
|
||||
mounts := scriptSection(t, agent, "urlencode() {", "\nhash_file() {") +
|
||||
scriptSection(t, agent, "\ndrive_mount_failures=", "\nreport_status() {")
|
||||
|
||||
@@ -640,6 +640,7 @@ func TestBuildToolsDiskImageAndScriptSyntax(t *testing.T) {
|
||||
if shBin == "" {
|
||||
return
|
||||
}
|
||||
lockShellFixture(t)
|
||||
for name, content := range map[string][]byte{
|
||||
"guest-tools.sh": GuestAgentScript(),
|
||||
"Install-Reims-Tools.command": GuestInstallScript(),
|
||||
|
||||
@@ -24,10 +24,9 @@ const (
|
||||
//go:embed file_sync.sh
|
||||
var guestFileSync string
|
||||
|
||||
// BuildToolsDiskImage creates a small read-only HFS+ disk image ("REIMSTOOLS")
|
||||
// containing the per-VM Bearer token config, Guest Tools 8 POSIX agent script
|
||||
// (text + PNG image clipboard with long-poll, WebDAV live mount with fallback
|
||||
// sync, and caffeinate prevent-sleep daemon), plus install/uninstall/pmset scripts.
|
||||
// BuildToolsDiskImage creates a read-only HFS+ disk image ("REIMSTOOLS")
|
||||
// containing the per-VM Bearer token config, the POSIX guest agent script, and
|
||||
// install/uninstall/pmset helper scripts.
|
||||
func BuildToolsDiskImage(outPath string, cfg VMConfig) error {
|
||||
if err := os.MkdirAll(filepath.Dir(outPath), 0o755); err != nil {
|
||||
return err
|
||||
@@ -74,16 +73,12 @@ func BuildToolsDiskImage(outPath string, cfg VMConfig) error {
|
||||
return installer.BuildHFSPlusDiskImage(outPath, ToolsVolumeName, files)
|
||||
}
|
||||
|
||||
// GuestAgentScript returns the Guest Tools 8 stock-macOS POSIX shell daemon
|
||||
// (no Xcode or Python required):
|
||||
// - Two-way UTF-8 text + PNG screenshot clipboard with server long-polling
|
||||
// - Zero-duplication WebDAV shared folder mount (~/Desktop/ReimsShare) with automatic sync fallback
|
||||
// - Host-controlled read-only Windows fixed-drive mounts with automatic unmount
|
||||
// - Host-controlled custom shared folder mounts
|
||||
// - Live host-controlled prevent-sleep guard via `caffeinate -dims` (no sudo required)
|
||||
// GuestAgentScript returns the stock-macOS POSIX shell daemon handling
|
||||
// clipboard sync, WebDAV and fallback folder shares, read-only host drive
|
||||
// mounts, and caffeinate prevent-sleep.
|
||||
func GuestAgentScript() []byte {
|
||||
return []byte(`#!/bin/sh
|
||||
# Reims Guest Tools 8: clipboard, WebDAV shares, read-only Windows drives, prevent-sleep.
|
||||
# Reims Guest Tools agent: clipboard, WebDAV shares, read-only Windows drives, prevent-sleep.
|
||||
set -u
|
||||
|
||||
# Force UTF-8 for macOS pbpaste/pbcopy under launchd (otherwise Simplified
|
||||
@@ -609,7 +604,7 @@ sync_clipboard() {
|
||||
fi
|
||||
;;
|
||||
503)
|
||||
# Feature disabled on host: back off instead of busy-looping osascript + curl (M11).
|
||||
# Feature disabled on host: back off instead of busy-looping osascript + curl.
|
||||
sleep 5
|
||||
;;
|
||||
000)
|
||||
@@ -769,12 +764,12 @@ launchctl bootstrap "gui/$uid" "$PLIST" 2>/dev/null || launchctl load -w "$PLIST
|
||||
# Verify host bridge connectivity immediately.
|
||||
. "$APP_DIR/config"
|
||||
if curl -fsS -m 5 -H "Authorization: Bearer $REIMS_TOKEN" "$REIMS_HOST_URL/v1/ping" >/dev/null 2>&1; then
|
||||
echo "Reims Guest Tools 8 installed and connected!"
|
||||
echo "Reims Guest Tools installed and connected!"
|
||||
echo "Clipboard: UTF-8 text + PNG screenshots (two-way)"
|
||||
echo "Shared folder: ~/Desktop/ReimsShare (WebDAV live mount, auto-fallback to sync)"
|
||||
echo "Prevent-sleep: controlled live from Reims-Go host UI"
|
||||
else
|
||||
echo "Reims Guest Tools 8 installed (waiting for Windows host bridge at $REIMS_HOST_URL)..."
|
||||
echo "Reims Guest Tools installed (waiting for Windows host bridge at $REIMS_HOST_URL)..."
|
||||
fi
|
||||
`)
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ func TestGuestAgentKeymapScript(t *testing.T) {
|
||||
for _, input := range []string{"(null)", "(\n)", "({ HIDKeyboardModifierMappingSrc = 30064771184; HIDKeyboardModifierMappingDst = 30064771299; })"} {
|
||||
t.Run(input, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
lockShellFixture(t)
|
||||
dir := t.TempDir()
|
||||
fixture := keymapShellFixture + strings.ReplaceAll(agent[start:end], "/usr/bin/hidutil", "hidutil")
|
||||
fixture += "\nprintf '%s\\n' \"$1\" > state\nsync_keymap_carrier '{}'\n"
|
||||
@@ -54,6 +55,7 @@ func TestGuestAgentKeymapScript(t *testing.T) {
|
||||
for _, teardown := range []string{"disable", "exit", "uninstall"} {
|
||||
t.Run(teardown, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
lockShellFixture(t)
|
||||
dir := t.TempDir()
|
||||
fixture := keymapShellFixture + strings.ReplaceAll(agent[start:end], "/usr/bin/hidutil", "hidutil") + keymapShellExercise
|
||||
if teardown == "uninstall" {
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package guesttools
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/testutil/shelllock"
|
||||
)
|
||||
|
||||
func lockShellFixture(t *testing.T) {
|
||||
t.Helper()
|
||||
release, err := shelllock.Acquire()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(release)
|
||||
}
|
||||
@@ -210,6 +210,7 @@ func TestHelperInstallScriptSyntaxAndDiskInspection(t *testing.T) {
|
||||
if err := os.WriteFile(scriptPath, []byte(script), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
lockPOSIXShell(t)
|
||||
// 1. POSIX sh syntax check.
|
||||
if out, err := exec.Command(shBin, "-n", scriptPath).CombinedOutput(); err != nil {
|
||||
t.Fatalf("sh -n install.sh failed: %v\n%s", err, out)
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
//go:build !windows
|
||||
|
||||
package installer
|
||||
|
||||
import "testing"
|
||||
|
||||
func lockPOSIXShell(t *testing.T) {
|
||||
t.Helper()
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
//go:build windows
|
||||
|
||||
package installer
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
const shellFixtureMutexName = `Local\ReimsGo-Test-MSYS`
|
||||
|
||||
func lockPOSIXShell(t *testing.T) {
|
||||
t.Helper()
|
||||
|
||||
runtime.LockOSThread()
|
||||
name, err := windows.UTF16PtrFromString(shellFixtureMutexName)
|
||||
if err != nil {
|
||||
runtime.UnlockOSThread()
|
||||
t.Fatal(err)
|
||||
}
|
||||
handle, err := windows.CreateMutex(nil, false, name)
|
||||
if err != nil && !errors.Is(err, windows.ERROR_ALREADY_EXISTS) {
|
||||
runtime.UnlockOSThread()
|
||||
t.Fatalf("create MSYS test mutex: %v", err)
|
||||
}
|
||||
event, err := windows.WaitForSingleObject(handle, windows.INFINITE)
|
||||
if err != nil {
|
||||
_ = windows.CloseHandle(handle)
|
||||
runtime.UnlockOSThread()
|
||||
t.Fatalf("wait for MSYS test mutex: %v", err)
|
||||
}
|
||||
if event != windows.WAIT_OBJECT_0 && event != windows.WAIT_ABANDONED {
|
||||
_ = windows.CloseHandle(handle)
|
||||
runtime.UnlockOSThread()
|
||||
t.Fatalf("unexpected MSYS test mutex result: %#x", event)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := windows.ReleaseMutex(handle); err != nil {
|
||||
t.Errorf("release MSYS test mutex: %v", err)
|
||||
}
|
||||
if err := windows.CloseHandle(handle); err != nil {
|
||||
t.Errorf("close MSYS test mutex: %v", err)
|
||||
}
|
||||
runtime.UnlockOSThread()
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
//go:build !windows
|
||||
|
||||
package shelllock
|
||||
|
||||
func Acquire() (func(), error) {
|
||||
return func() {}, nil
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
//go:build windows
|
||||
|
||||
package shelllock
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
const mutexName = `Local\ReimsGo-Test-MSYS`
|
||||
|
||||
func Acquire() (func(), error) {
|
||||
runtime.LockOSThread()
|
||||
name, err := windows.UTF16PtrFromString(mutexName)
|
||||
if err != nil {
|
||||
runtime.UnlockOSThread()
|
||||
return nil, err
|
||||
}
|
||||
handle, err := windows.CreateMutex(nil, false, name)
|
||||
if err != nil && !errors.Is(err, windows.ERROR_ALREADY_EXISTS) {
|
||||
runtime.UnlockOSThread()
|
||||
return nil, fmt.Errorf("create MSYS test mutex: %w", err)
|
||||
}
|
||||
event, err := windows.WaitForSingleObject(handle, windows.INFINITE)
|
||||
if err != nil {
|
||||
_ = windows.CloseHandle(handle)
|
||||
runtime.UnlockOSThread()
|
||||
return nil, fmt.Errorf("wait for MSYS test mutex: %w", err)
|
||||
}
|
||||
if event != windows.WAIT_OBJECT_0 && event != windows.WAIT_ABANDONED {
|
||||
_ = windows.CloseHandle(handle)
|
||||
runtime.UnlockOSThread()
|
||||
return nil, fmt.Errorf("unexpected MSYS test mutex result: %#x", event)
|
||||
}
|
||||
return func() {
|
||||
_ = windows.ReleaseMutex(handle)
|
||||
_ = windows.CloseHandle(handle)
|
||||
runtime.UnlockOSThread()
|
||||
}, nil
|
||||
}
|
||||
Reference in new issue
Block a user