|
| 1 | +package checks |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "regexp" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +// RunRustIntegrationTests runs the Docker-backed SMB Rust integration tests. |
| 14 | +// |
| 15 | +// Flow: |
| 16 | +// 1. Start the core SMB Docker containers (guest, auth, both, readonly, flaky, slow) |
| 17 | +// via apps/desktop/test/smb-servers/start.sh. |
| 18 | +// 2. Poll until the expected containers report `running`. |
| 19 | +// 3. Invoke `cargo nextest run --release --run-ignored only -E 'test(smb_integration_)'` |
| 20 | +// in apps/desktop/src-tauri. The expression filter matches every |
| 21 | +// `smb_integration_*` test and skips other `#[ignore]` tests. |
| 22 | +// 4. Always tear the containers down afterwards (success or failure). |
| 23 | +func RunRustIntegrationTests(ctx *CheckContext) (CheckResult, error) { |
| 24 | + // Docker is a hard requirement. Surface a clear message instead of a cryptic error. |
| 25 | + if !CommandExists("docker") { |
| 26 | + return CheckResult{}, fmt.Errorf( |
| 27 | + "docker is required for SMB integration tests — install Docker or run without this check", |
| 28 | + ) |
| 29 | + } |
| 30 | + if _, err := RunCommand(exec.Command("docker", "info"), true); err != nil { |
| 31 | + return CheckResult{}, fmt.Errorf( |
| 32 | + "docker daemon is not running — start Docker or run without this check", |
| 33 | + ) |
| 34 | + } |
| 35 | + |
| 36 | + smbServersDir := filepath.Join(ctx.RootDir, "apps", "desktop", "test", "smb-servers") |
| 37 | + rustDir := filepath.Join(ctx.RootDir, "apps", "desktop", "src-tauri") |
| 38 | + |
| 39 | + // Start containers (core = guest, auth, both, readonly, flaky, slow). The |
| 40 | + // make_docker_volume helper in smb.rs only uses the guest port today, but |
| 41 | + // core matches what the default start.sh spins up and covers anything new |
| 42 | + // we add. |
| 43 | + startCmd := exec.Command("./start.sh", "core") |
| 44 | + startCmd.Dir = smbServersDir |
| 45 | + if startOutput, err := RunCommand(startCmd, true); err != nil { |
| 46 | + return CheckResult{}, fmt.Errorf("couldn't start SMB containers\n%s", indentOutput(startOutput)) |
| 47 | + } |
| 48 | + |
| 49 | + // Always stop containers when the check returns, regardless of outcome. |
| 50 | + defer func() { |
| 51 | + stopCmd := exec.Command("./stop.sh") |
| 52 | + stopCmd.Dir = smbServersDir |
| 53 | + _, _ = RunCommand(stopCmd, true) |
| 54 | + }() |
| 55 | + |
| 56 | + // Wait for the core services to be running. We don't require `healthy` |
| 57 | + // here because these images don't all ship healthchecks; `running` plus |
| 58 | + // a short settle is enough, and smb2 reconnects if the server isn't |
| 59 | + // ready on the first write. |
| 60 | + expected := []string{ |
| 61 | + "smb-consumer-guest", |
| 62 | + "smb-consumer-auth", |
| 63 | + "smb-consumer-both", |
| 64 | + "smb-consumer-readonly", |
| 65 | + "smb-consumer-flaky", |
| 66 | + "smb-consumer-slow", |
| 67 | + } |
| 68 | + if err := waitForSmbContainers(expected, 120*time.Second); err != nil { |
| 69 | + return CheckResult{}, err |
| 70 | + } |
| 71 | + |
| 72 | + // Make sure cargo-nextest is available (mirrors desktop-rust-tests.go). |
| 73 | + if !CommandExists("cargo-nextest") { |
| 74 | + installCmd := exec.Command("cargo", "install", "cargo-nextest", "--locked") |
| 75 | + if _, err := RunCommand(installCmd, true); err != nil { |
| 76 | + return CheckResult{}, fmt.Errorf("failed to install cargo-nextest: %w", err) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Use --release to match the perf profile of shipped code — compound reads |
| 81 | + // and writes are sensitive to -O settings. nextest's expression filter |
| 82 | + // matches only our `smb_integration_*` tests, so unrelated `#[ignore]` |
| 83 | + // tests are still skipped. |
| 84 | + cmd := exec.Command( |
| 85 | + "cargo", "nextest", "run", |
| 86 | + "--release", |
| 87 | + "--run-ignored", "only", |
| 88 | + "-E", "test(smb_integration_)", |
| 89 | + ) |
| 90 | + cmd.Dir = rustDir |
| 91 | + output, err := RunCommand(cmd, true) |
| 92 | + if err != nil { |
| 93 | + return CheckResult{}, fmt.Errorf("SMB integration tests failed\n%s", indentOutput(output)) |
| 94 | + } |
| 95 | + |
| 96 | + re := regexp.MustCompile(`(\d+) tests? run`) |
| 97 | + matches := re.FindStringSubmatch(output) |
| 98 | + if len(matches) > 1 { |
| 99 | + count, _ := strconv.Atoi(matches[1]) |
| 100 | + result := Success(fmt.Sprintf("%d %s passed", count, Pluralize(count, "test", "tests"))) |
| 101 | + result.Total = count |
| 102 | + return result, nil |
| 103 | + } |
| 104 | + return Success("All SMB integration tests passed"), nil |
| 105 | +} |
| 106 | + |
| 107 | +// waitForSmbContainers polls `docker compose -p smb-consumer ps` until every |
| 108 | +// expected service appears in the running set, or the timeout expires. |
| 109 | +func waitForSmbContainers(expected []string, timeout time.Duration) error { |
| 110 | + deadline := time.Now().Add(timeout) |
| 111 | + interval := 1 * time.Second |
| 112 | + |
| 113 | + for { |
| 114 | + psCmd := exec.Command( |
| 115 | + "docker", "compose", "-p", "smb-consumer", |
| 116 | + "ps", "--services", "--filter", "status=running", |
| 117 | + ) |
| 118 | + output, _ := RunCommand(psCmd, true) |
| 119 | + |
| 120 | + running := make(map[string]struct{}) |
| 121 | + for _, line := range strings.Split(strings.TrimSpace(output), "\n") { |
| 122 | + if line = strings.TrimSpace(line); line != "" { |
| 123 | + running[line] = struct{}{} |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + missing := []string{} |
| 128 | + for _, svc := range expected { |
| 129 | + if _, ok := running[svc]; !ok { |
| 130 | + missing = append(missing, svc) |
| 131 | + } |
| 132 | + } |
| 133 | + if len(missing) == 0 { |
| 134 | + return nil |
| 135 | + } |
| 136 | + |
| 137 | + if time.Now().After(deadline) { |
| 138 | + return fmt.Errorf( |
| 139 | + "SMB containers didn't reach running state within %s: still waiting for %s", |
| 140 | + timeout, strings.Join(missing, ", "), |
| 141 | + ) |
| 142 | + } |
| 143 | + time.Sleep(interval) |
| 144 | + } |
| 145 | +} |
0 commit comments