|
| 1 | +//go:build e2e && vm |
| 2 | + |
| 3 | +package e2e |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/openbootdotdev/openboot/testutil" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// mockAPIConfig is the JSON response served by the in-VM mock API server. |
| 16 | +// It describes a remote config with oh-my-zsh: agnoster theme and git+docker plugins. |
| 17 | +const mockAPIConfig = `{ |
| 18 | + "username": "testuser", |
| 19 | + "slug": "myconfig", |
| 20 | + "packages": [], |
| 21 | + "casks": [], |
| 22 | + "taps": [], |
| 23 | + "npm": [], |
| 24 | + "shell": { |
| 25 | + "oh_my_zsh": true, |
| 26 | + "theme": "agnoster", |
| 27 | + "plugins": ["git", "docker"] |
| 28 | + } |
| 29 | +}` |
| 30 | + |
| 31 | +// startMockAPIServer starts a Python HTTP server inside the VM that serves |
| 32 | +// mockAPIConfig for any GET request. Returns the base URL (http://localhost:PORT). |
| 33 | +// The server is killed via t.Cleanup. |
| 34 | +func startMockAPIServer(t *testing.T, vm *testutil.TartVM, port int) string { |
| 35 | + t.Helper() |
| 36 | + |
| 37 | + // Write JSON to a file using printf (more reliable than heredoc over SSH) |
| 38 | + writeCmd := fmt.Sprintf(`printf '%%s' %s > /tmp/mock-config.json`, shellescape(mockAPIConfig)) |
| 39 | + _, err := vm.Run(writeCmd) |
| 40 | + require.NoError(t, err, "write mock config JSON") |
| 41 | + |
| 42 | + // Verify the file was written |
| 43 | + content, err := vm.Run("cat /tmp/mock-config.json") |
| 44 | + require.NoError(t, err, "read mock config JSON") |
| 45 | + require.Contains(t, content, "agnoster", "mock JSON should contain agnoster theme") |
| 46 | + t.Logf("mock config written: %s", content) |
| 47 | + |
| 48 | + // Python HTTP server as a script file (avoids shell quoting issues with -c) |
| 49 | + pyServer := fmt.Sprintf(`import http.server, pathlib |
| 50 | +class H(http.server.BaseHTTPRequestHandler): |
| 51 | + def do_GET(self): |
| 52 | + body = pathlib.Path('/tmp/mock-config.json').read_bytes() |
| 53 | + self.send_response(200) |
| 54 | + self.send_header('Content-Type', 'application/json') |
| 55 | + self.send_header('Content-Length', str(len(body))) |
| 56 | + self.end_headers() |
| 57 | + self.wfile.write(body) |
| 58 | + def log_message(self, *a): pass |
| 59 | +http.server.HTTPServer(('127.0.0.1', %d), H).serve_forever() |
| 60 | +`, port) |
| 61 | + |
| 62 | + _, err = vm.Run(fmt.Sprintf(`cat > /tmp/mock-server.py << 'PYEOF' |
| 63 | +%sPYEOF`, pyServer)) |
| 64 | + require.NoError(t, err, "write mock server script") |
| 65 | + |
| 66 | + // Start server with nohup so it persists after the SSH session exits |
| 67 | + pidOut, err := vm.Run(fmt.Sprintf("nohup python3 /tmp/mock-server.py >/tmp/mock-api.log 2>&1 & echo $!")) |
| 68 | + require.NoError(t, err, "start mock API server") |
| 69 | + pid := strings.TrimSpace(pidOut) |
| 70 | + t.Logf("mock API server started (pid=%s) on port %d", pid, port) |
| 71 | + |
| 72 | + // Poll until server responds (up to 10s) |
| 73 | + var curlOut string |
| 74 | + var curlErr error |
| 75 | + for i := 0; i < 10; i++ { |
| 76 | + _, _ = vm.Run("sleep 1") |
| 77 | + curlOut, curlErr = vm.Run(fmt.Sprintf("curl -s http://localhost:%d/testuser/myconfig/config", port)) |
| 78 | + if curlErr == nil && strings.Contains(curlOut, "agnoster") { |
| 79 | + break |
| 80 | + } |
| 81 | + t.Logf("waiting for mock server (attempt %d): err=%v", i+1, curlErr) |
| 82 | + } |
| 83 | + t.Logf("curl verification: err=%v, body=%s", curlErr, curlOut) |
| 84 | + if logOut, _ := vm.Run("cat /tmp/mock-api.log 2>/dev/null"); logOut != "" { |
| 85 | + t.Logf("mock server log:\n%s", logOut) |
| 86 | + } |
| 87 | + require.NoError(t, curlErr, "mock server should respond to curl") |
| 88 | + require.Contains(t, curlOut, "agnoster", "mock server should return our JSON") |
| 89 | + |
| 90 | + t.Cleanup(func() { |
| 91 | + if pid != "" { |
| 92 | + _, _ = vm.Run("kill " + pid + " 2>/dev/null || true") |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + return fmt.Sprintf("http://localhost:%d", port) |
| 97 | +} |
| 98 | + |
| 99 | +// shellescape wraps a string in single quotes for safe use in shell commands. |
| 100 | +// It escapes any single quotes within the string. |
| 101 | +func shellescape(s string) string { |
| 102 | + return "'" + strings.ReplaceAll(s, "'", "'\\''") + "'" |
| 103 | +} |
| 104 | + |
| 105 | +// writeSyncSource writes ~/.openboot/sync_source.json in the VM. |
| 106 | +func writeSyncSource(t *testing.T, vm *testutil.TartVM, userSlug string) { |
| 107 | + t.Helper() |
| 108 | + json := fmt.Sprintf(`{"user_slug":"%s"}`, userSlug) |
| 109 | + _, err := vm.Run(fmt.Sprintf("mkdir -p ~/.openboot && echo '%s' > ~/.openboot/sync_source.json", json)) |
| 110 | + require.NoError(t, err, "write sync source") |
| 111 | +} |
| 112 | + |
| 113 | +// TestE2E_Sync_Shell_DryRunShowsDiff verifies that `openboot sync --dry-run` |
| 114 | +// detects a shell config difference and displays it in the output. |
| 115 | +// |
| 116 | +// Setup: VM has Oh-My-Zsh with default theme (robbyrussell) and plugins (git). |
| 117 | +// Remote config (served by a local mock API) specifies theme=agnoster, plugins=git+docker. |
| 118 | +// Expected: diff output shows "Shell Changes" with theme and plugin arrows. |
| 119 | +func TestE2E_Sync_Shell_DryRunShowsDiff(t *testing.T) { |
| 120 | + if testing.Short() { |
| 121 | + t.Skip("skipping VM test in short mode") |
| 122 | + } |
| 123 | + |
| 124 | + vm := testutil.NewTartVM(t) |
| 125 | + installOhMyZsh(t, vm) |
| 126 | + bin := vmCopyDevBinary(t, vm) |
| 127 | + |
| 128 | + apiURL := startMockAPIServer(t, vm, 19876) |
| 129 | + writeSyncSource(t, vm, "testuser/myconfig") |
| 130 | + |
| 131 | + env := map[string]string{ |
| 132 | + "PATH": brewPath, |
| 133 | + "OPENBOOT_API_URL": apiURL, |
| 134 | + } |
| 135 | + out, err := vm.RunWithEnv(env, bin+" sync --dry-run") |
| 136 | + t.Logf("dry-run output:\n%s", out) |
| 137 | + // err is allowed (exit 1 if no changes applied), just verify output |
| 138 | + if err != nil { |
| 139 | + t.Logf("exit: %v", err) |
| 140 | + } |
| 141 | + |
| 142 | + assert.Contains(t, out, "Shell Changes", "should show Shell Changes section") |
| 143 | + assert.Contains(t, out, "agnoster", "should show target theme") |
| 144 | + // robbyrussell is the default Oh-My-Zsh theme |
| 145 | + assert.Contains(t, out, "→", "should show arrow between old and new values") |
| 146 | +} |
| 147 | + |
| 148 | +// TestE2E_Sync_Shell_AppliesTheme verifies that `openboot sync` (non-dry-run) |
| 149 | +// actually patches ~/.zshrc with the new theme and plugins. |
| 150 | +// |
| 151 | +// Setup: Same as DryRun test above. |
| 152 | +// Interaction: send Enter twice to accept both confirm prompts. |
| 153 | +// Expected: ~/.zshrc has ZSH_THEME="agnoster" and plugins=(git docker) afterwards. |
| 154 | +func TestE2E_Sync_Shell_AppliesTheme(t *testing.T) { |
| 155 | + if testing.Short() { |
| 156 | + t.Skip("skipping VM test in short mode") |
| 157 | + } |
| 158 | + |
| 159 | + vm := testutil.NewTartVM(t) |
| 160 | + installOhMyZsh(t, vm) |
| 161 | + bin := vmCopyDevBinary(t, vm) |
| 162 | + |
| 163 | + apiURL := startMockAPIServer(t, vm, 19877) |
| 164 | + writeSyncSource(t, vm, "testuser/myconfig") |
| 165 | + |
| 166 | + // Verify initial state |
| 167 | + zshrcBefore, err := vm.Run("cat ~/.zshrc") |
| 168 | + require.NoError(t, err) |
| 169 | + assert.Contains(t, zshrcBefore, "robbyrussell", "initial theme should be robbyrussell") |
| 170 | + |
| 171 | + // Write a shell script to avoid Tcl quoting issues in RunInteractive's expect script. |
| 172 | + // --install-only skips the "remove extra packages?" prompt from the base VM image. |
| 173 | + syncScript := fmt.Sprintf("#!/bin/sh\nexport PATH=%q\nexport OPENBOOT_API_URL=%q\nexec %s sync --install-only\n", |
| 174 | + brewPath, apiURL, bin) |
| 175 | + _, err = vm.Run(fmt.Sprintf("printf '%%s' %s > /tmp/run-sync.sh && chmod +x /tmp/run-sync.sh", shellescape(syncScript))) |
| 176 | + require.NoError(t, err, "write sync script") |
| 177 | + |
| 178 | + output, interactErr := vm.RunInteractive("/tmp/run-sync.sh", []testutil.ExpectStep{ |
| 179 | + {Expect: "shell config", Send: "\r"}, // Update shell config (theme and plugins)? → Yes |
| 180 | + {Expect: "Apply", Send: "\r"}, // Apply 1 changes? → Yes |
| 181 | + }, 60) |
| 182 | + t.Logf("interactive sync output:\n%s", output) |
| 183 | + if interactErr != nil { |
| 184 | + t.Logf("interactive exit: %v", interactErr) |
| 185 | + } |
| 186 | + |
| 187 | + // Verify .zshrc was updated |
| 188 | + zshrcAfter, err := vm.Run("cat ~/.zshrc") |
| 189 | + require.NoError(t, err) |
| 190 | + t.Logf("zshrc after sync:\n%s", zshrcAfter) |
| 191 | + |
| 192 | + assert.Contains(t, zshrcAfter, `ZSH_THEME="agnoster"`, "theme should be updated to agnoster") |
| 193 | + assert.Contains(t, zshrcAfter, "docker", "plugins should include docker") |
| 194 | +} |
0 commit comments