|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +package e2e |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/openbootdotdev/openboot/testutil" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +func TestE2E_InstallSinglePackage_JQ(t *testing.T) { |
| 20 | + if testing.Short() { |
| 21 | + t.Skip("skipping real installation test in short mode") |
| 22 | + } |
| 23 | + |
| 24 | + // Given: system does not have jq installed |
| 25 | + testutil.EnsurePackageNotInstalled(t, "jq") |
| 26 | + binary := testutil.BuildTestBinary(t) |
| 27 | + |
| 28 | + // When: we install jq via openboot |
| 29 | + cmd := exec.Command(binary, "--packages-only", "--silent", "--preset", "minimal") |
| 30 | + cmd.Env = append(os.Environ(), |
| 31 | + "OPENBOOT_GIT_NAME=Test User", |
| 32 | + "OPENBOOT_GIT_EMAIL=test@example.com", |
| 33 | + ) |
| 34 | + |
| 35 | + tmpConfig := createTempConfig(t, `{ |
| 36 | + "packages": { |
| 37 | + "brew": ["jq"] |
| 38 | + } |
| 39 | + }`) |
| 40 | + defer os.Remove(tmpConfig) |
| 41 | + |
| 42 | + output, err := cmd.CombinedOutput() |
| 43 | + t.Logf("Install output: %s", string(output)) |
| 44 | + |
| 45 | + // Then: jq should be installed and executable |
| 46 | + assert.NoError(t, err, "installation should succeed") |
| 47 | + assert.True(t, testutil.IsPackageInstalled("jq"), "jq should be installed") |
| 48 | + |
| 49 | + jqPath, err := exec.Command("which", "jq").Output() |
| 50 | + require.NoError(t, err, "jq should be in PATH") |
| 51 | + assert.Contains(t, string(jqPath), "jq") |
| 52 | + |
| 53 | + jqVersion, err := exec.Command("jq", "--version").Output() |
| 54 | + require.NoError(t, err, "jq should be executable") |
| 55 | + assert.Contains(t, string(jqVersion), "jq-") |
| 56 | + |
| 57 | + // Cleanup |
| 58 | + testutil.UninstallPackage(t, "jq") |
| 59 | +} |
| 60 | + |
| 61 | +func TestE2E_InstallMultiplePackages(t *testing.T) { |
| 62 | + if testing.Short() { |
| 63 | + t.Skip("skipping real installation test in short mode") |
| 64 | + } |
| 65 | + |
| 66 | + packages := []string{"bat", "fd"} |
| 67 | + |
| 68 | + // Given: packages are not installed |
| 69 | + for _, pkg := range packages { |
| 70 | + testutil.EnsurePackageNotInstalled(t, pkg) |
| 71 | + } |
| 72 | + binary := testutil.BuildTestBinary(t) |
| 73 | + |
| 74 | + // When: we install multiple packages |
| 75 | + tmpConfig := createTempConfig(t, `{ |
| 76 | + "packages": { |
| 77 | + "brew": ["bat", "fd"] |
| 78 | + } |
| 79 | + }`) |
| 80 | + defer os.Remove(tmpConfig) |
| 81 | + |
| 82 | + cmd := exec.Command(binary, "--packages-only", "--silent", "--preset", "minimal") |
| 83 | + cmd.Env = append(os.Environ(), |
| 84 | + "OPENBOOT_GIT_NAME=Test User", |
| 85 | + "OPENBOOT_GIT_EMAIL=test@example.com", |
| 86 | + ) |
| 87 | + |
| 88 | + output, err := cmd.CombinedOutput() |
| 89 | + t.Logf("Install output: %s", string(output)) |
| 90 | + |
| 91 | + // Then: all packages should be installed |
| 92 | + assert.NoError(t, err, "installation should succeed") |
| 93 | + |
| 94 | + for _, pkg := range packages { |
| 95 | + assert.True(t, testutil.IsPackageInstalled(pkg), "%s should be installed", pkg) |
| 96 | + |
| 97 | + pkgPath, err := exec.Command("which", pkg).Output() |
| 98 | + require.NoError(t, err, "%s should be in PATH", pkg) |
| 99 | + assert.Contains(t, string(pkgPath), pkg) |
| 100 | + } |
| 101 | + |
| 102 | + // Cleanup |
| 103 | + for _, pkg := range packages { |
| 104 | + testutil.UninstallPackage(t, pkg) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestE2E_SnapshotRestoreRealPackages(t *testing.T) { |
| 109 | + if testing.Short() { |
| 110 | + t.Skip("skipping real installation test in short mode") |
| 111 | + } |
| 112 | + |
| 113 | + binary := testutil.BuildTestBinary(t) |
| 114 | + testPkg := "ripgrep" |
| 115 | + |
| 116 | + // Given: ripgrep is installed |
| 117 | + if !testutil.IsPackageInstalled(testPkg) { |
| 118 | + cmd := exec.Command("brew", "install", testPkg) |
| 119 | + require.NoError(t, cmd.Run(), "test setup: should install ripgrep") |
| 120 | + } |
| 121 | + |
| 122 | + // When: we capture a snapshot |
| 123 | + cmd := exec.Command(binary, "snapshot", "--json") |
| 124 | + var stdout, stderr strings.Builder |
| 125 | + cmd.Stdout = &stdout |
| 126 | + cmd.Stderr = &stderr |
| 127 | + |
| 128 | + err := cmd.Run() |
| 129 | + require.NoError(t, err, "snapshot capture should succeed, stderr: %s", stderr.String()) |
| 130 | + |
| 131 | + snapshotJSON := stdout.String() |
| 132 | + var snapshot map[string]interface{} |
| 133 | + err = json.Unmarshal([]byte(snapshotJSON), &snapshot) |
| 134 | + require.NoError(t, err, "snapshot should be valid JSON") |
| 135 | + |
| 136 | + // Then: snapshot should contain ripgrep |
| 137 | + packages, ok := snapshot["packages"].(map[string]interface{}) |
| 138 | + require.True(t, ok, "snapshot should have packages field") |
| 139 | + |
| 140 | + brew, ok := packages["brew"].([]interface{}) |
| 141 | + require.True(t, ok, "snapshot should have brew packages") |
| 142 | + |
| 143 | + foundRipgrep := false |
| 144 | + for _, pkg := range brew { |
| 145 | + if pkgStr, ok := pkg.(string); ok && strings.Contains(pkgStr, "ripgrep") { |
| 146 | + foundRipgrep = true |
| 147 | + break |
| 148 | + } |
| 149 | + } |
| 150 | + assert.True(t, foundRipgrep, "snapshot should contain ripgrep") |
| 151 | + |
| 152 | + // Save snapshot to file |
| 153 | + tmpDir := t.TempDir() |
| 154 | + snapshotPath := filepath.Join(tmpDir, "test-snapshot.json") |
| 155 | + err = os.WriteFile(snapshotPath, []byte(snapshotJSON), 0644) |
| 156 | + require.NoError(t, err) |
| 157 | + |
| 158 | + // When: we uninstall ripgrep and restore from snapshot |
| 159 | + testutil.UninstallPackage(t, testPkg) |
| 160 | + assert.False(t, testutil.IsPackageInstalled(testPkg), "ripgrep should be uninstalled") |
| 161 | + |
| 162 | + // Note: Actual restore would require implementing --restore flag |
| 163 | + // For now, we verify the snapshot format is correct |
| 164 | + content, err := os.ReadFile(snapshotPath) |
| 165 | + require.NoError(t, err) |
| 166 | + assert.Greater(t, len(content), 100, "snapshot should have substantial content") |
| 167 | +} |
| 168 | + |
| 169 | +func TestE2E_InstallWithInvalidPackage(t *testing.T) { |
| 170 | + if testing.Short() { |
| 171 | + t.Skip("skipping real installation test in short mode") |
| 172 | + } |
| 173 | + |
| 174 | + binary := testutil.BuildTestBinary(t) |
| 175 | + invalidPkg := "this-package-definitely-does-not-exist-12345" |
| 176 | + |
| 177 | + // Given: we have a config with an invalid package |
| 178 | + tmpConfig := createTempConfig(t, `{ |
| 179 | + "packages": { |
| 180 | + "brew": ["`+invalidPkg+`"] |
| 181 | + } |
| 182 | + }`) |
| 183 | + defer os.Remove(tmpConfig) |
| 184 | + |
| 185 | + // When: we try to install it |
| 186 | + cmd := exec.Command(binary, "--packages-only", "--silent", "--preset", "minimal") |
| 187 | + cmd.Env = append(os.Environ(), |
| 188 | + "OPENBOOT_GIT_NAME=Test User", |
| 189 | + "OPENBOOT_GIT_EMAIL=test@example.com", |
| 190 | + ) |
| 191 | + |
| 192 | + output, err := cmd.CombinedOutput() |
| 193 | + outStr := string(output) |
| 194 | + t.Logf("Error output: %s", outStr) |
| 195 | + |
| 196 | + // Then: command should fail gracefully |
| 197 | + // Note: OpenBoot may continue with valid packages, so we just check for error indication |
| 198 | + assert.True(t, err != nil || strings.Contains(outStr, "error") || strings.Contains(outStr, "failed"), |
| 199 | + "should indicate error for invalid package") |
| 200 | +} |
| 201 | + |
| 202 | +func TestE2E_DryRunDoesNotInstall(t *testing.T) { |
| 203 | + binary := testutil.BuildTestBinary(t) |
| 204 | + testPkg := "cowsay" |
| 205 | + |
| 206 | + // Given: cowsay is not installed |
| 207 | + testutil.EnsurePackageNotInstalled(t, testPkg) |
| 208 | + |
| 209 | + // When: we run with --dry-run |
| 210 | + tmpConfig := createTempConfig(t, `{ |
| 211 | + "packages": { |
| 212 | + "brew": ["`+testPkg+`"] |
| 213 | + } |
| 214 | + }`) |
| 215 | + defer os.Remove(tmpConfig) |
| 216 | + |
| 217 | + cmd := exec.Command(binary, "--dry-run", "--packages-only", "--silent", "--preset", "minimal") |
| 218 | + cmd.Env = append(os.Environ(), |
| 219 | + "OPENBOOT_GIT_NAME=Test User", |
| 220 | + "OPENBOOT_GIT_EMAIL=test@example.com", |
| 221 | + ) |
| 222 | + |
| 223 | + output, err := cmd.CombinedOutput() |
| 224 | + t.Logf("Dry-run output: %s", string(output)) |
| 225 | + |
| 226 | + // Then: cowsay should still not be installed |
| 227 | + assert.NoError(t, err, "dry-run should succeed") |
| 228 | + assert.False(t, testutil.IsPackageInstalled(testPkg), "dry-run should not actually install packages") |
| 229 | +} |
| 230 | + |
| 231 | +func TestE2E_BrewUpdateBeforeInstall(t *testing.T) { |
| 232 | + if testing.Short() { |
| 233 | + t.Skip("skipping brew update test in short mode") |
| 234 | + } |
| 235 | + |
| 236 | + binary := testutil.BuildTestBinary(t) |
| 237 | + |
| 238 | + // Given: we request brew update |
| 239 | + cmd := exec.Command(binary, "--update", "--dry-run", "--packages-only", "--silent", "--preset", "minimal") |
| 240 | + cmd.Env = append(os.Environ(), |
| 241 | + "OPENBOOT_GIT_NAME=Test User", |
| 242 | + "OPENBOOT_GIT_EMAIL=test@example.com", |
| 243 | + ) |
| 244 | + |
| 245 | + start := time.Now() |
| 246 | + output, err := cmd.CombinedOutput() |
| 247 | + duration := time.Since(start) |
| 248 | + |
| 249 | + t.Logf("Update output: %s", string(output)) |
| 250 | + t.Logf("Duration: %v", duration) |
| 251 | + |
| 252 | + // Then: command should succeed (update may happen or skip if recent) |
| 253 | + assert.NoError(t, err, "update command should succeed") |
| 254 | +} |
| 255 | + |
| 256 | +func TestE2E_GitConfigSetup(t *testing.T) { |
| 257 | + if testing.Short() { |
| 258 | + t.Skip("skipping git config test in short mode") |
| 259 | + } |
| 260 | + |
| 261 | + binary := testutil.BuildTestBinary(t) |
| 262 | + testName := "Test E2E User" |
| 263 | + testEmail := "e2e-test@example.com" |
| 264 | + |
| 265 | + // Given: we have test git credentials |
| 266 | + cmd := exec.Command(binary, "--packages-only", "--silent", "--preset", "minimal") |
| 267 | + cmd.Env = append(os.Environ(), |
| 268 | + "OPENBOOT_GIT_NAME="+testName, |
| 269 | + "OPENBOOT_GIT_EMAIL="+testEmail, |
| 270 | + ) |
| 271 | + |
| 272 | + // When: we run openboot |
| 273 | + output, err := cmd.CombinedOutput() |
| 274 | + t.Logf("Output: %s", string(output)) |
| 275 | + |
| 276 | + // Then: command should handle git config |
| 277 | + assert.NoError(t, err, "should succeed with git config") |
| 278 | + |
| 279 | + // Verify git config is accessible (system-level test) |
| 280 | + gitNameCheck := exec.Command("git", "config", "--global", "user.name") |
| 281 | + nameOutput, _ := gitNameCheck.Output() |
| 282 | + t.Logf("Git user.name: %s", string(nameOutput)) |
| 283 | + |
| 284 | + gitEmailCheck := exec.Command("git", "config", "--global", "user.email") |
| 285 | + emailOutput, _ := gitEmailCheck.Output() |
| 286 | + t.Logf("Git user.email: %s", string(emailOutput)) |
| 287 | +} |
| 288 | + |
| 289 | +func createTempConfig(t *testing.T, jsonContent string) string { |
| 290 | + tmpFile, err := os.CreateTemp("", "openboot-config-*.json") |
| 291 | + require.NoError(t, err) |
| 292 | + |
| 293 | + _, err = tmpFile.WriteString(jsonContent) |
| 294 | + require.NoError(t, err) |
| 295 | + |
| 296 | + err = tmpFile.Close() |
| 297 | + require.NoError(t, err) |
| 298 | + |
| 299 | + return tmpFile.Name() |
| 300 | +} |
0 commit comments