|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package integration |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "path/filepath" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + BuggyAppHost = "buggyapp" |
| 17 | + MockServerURL = "http://mock-server:8080" |
| 18 | + defaultJavaHome = "/usr/lib/jvm/java-11-openjdk" |
| 19 | + TestTimeout = 120 * time.Second |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + // JavaHome is the path to Java installation, configurable via JAVA_HOME environment variable |
| 24 | + JavaHome = getJavaHome() |
| 25 | +) |
| 26 | + |
| 27 | +// getJavaHome returns the Java home directory from environment or default |
| 28 | +func getJavaHome() string { |
| 29 | + if javaHome := os.Getenv("JAVA_HOME"); javaHome != "" { |
| 30 | + return javaHome |
| 31 | + } |
| 32 | + return defaultJavaHome |
| 33 | +} |
| 34 | + |
| 35 | +// StartTestJVM launches a Java process for testing |
| 36 | +// Note: This function expects to run inside the Docker test environment |
| 37 | +// where it can communicate with the buggyapp container via the shared network |
| 38 | +func StartTestJVM(t *testing.T) (pid int, cleanup func()) { |
| 39 | + t.Helper() |
| 40 | + |
| 41 | + // When running in the test runner container, we need to get the PID |
| 42 | + // from within the buggyapp container. We use 'docker exec' for this. |
| 43 | + // Note: This requires Docker socket access OR we could use HTTP-based |
| 44 | + // process discovery if buggyapp exposed a /pid endpoint. |
| 45 | + cmd := exec.Command("docker", "exec", "yc-test-buggyapp", |
| 46 | + "sh", "-c", "pgrep -f 'java.*buggyapp'") |
| 47 | + out, err := cmd.Output() |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("Failed to get Java PID: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + _, err = fmt.Sscanf(string(out), "%d", &pid) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("Failed to parse PID: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + // Verify the process is still running |
| 58 | + cleanup = func() { |
| 59 | + // Optionally verify process is still running after test |
| 60 | + cmd := exec.Command("docker", "exec", "yc-test-buggyapp", |
| 61 | + "sh", "-c", fmt.Sprintf("kill -0 %d 2>/dev/null", pid)) |
| 62 | + if err := cmd.Run(); err != nil { |
| 63 | + t.Logf("Warning: Java process %d is no longer running", pid) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return pid, cleanup |
| 68 | +} |
| 69 | + |
| 70 | +// BuildYCBinary compiles the yc binary for testing |
| 71 | +func BuildYCBinary(t *testing.T) string { |
| 72 | + t.Helper() |
| 73 | + |
| 74 | + binPath := filepath.Join(t.TempDir(), "yc") |
| 75 | + cmd := exec.Command("go", "build", |
| 76 | + "-o", binPath, |
| 77 | + "-ldflags", "-s -w", |
| 78 | + "/workspace/cmd/yc") |
| 79 | + |
| 80 | + if out, err := cmd.CombinedOutput(); err != nil { |
| 81 | + t.Fatalf("Build failed: %v\n%s", err, out) |
| 82 | + } |
| 83 | + |
| 84 | + // Verify binary was created and is executable |
| 85 | + info, err := os.Stat(binPath) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("Binary not created at %s: %v", binPath, err) |
| 88 | + } |
| 89 | + if info.Mode()&0111 == 0 { |
| 90 | + t.Fatalf("Binary at %s is not executable (mode: %v)", binPath, info.Mode()) |
| 91 | + } |
| 92 | + |
| 93 | + return binPath |
| 94 | +} |
| 95 | + |
| 96 | +// WaitForFile polls for file existence |
| 97 | +func WaitForFile(path string, timeout time.Duration) error { |
| 98 | + ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 99 | + defer cancel() |
| 100 | + |
| 101 | + ticker := time.NewTicker(500 * time.Millisecond) |
| 102 | + defer ticker.Stop() |
| 103 | + |
| 104 | + for { |
| 105 | + select { |
| 106 | + case <-ctx.Done(): |
| 107 | + return fmt.Errorf("timeout waiting for %s", path) |
| 108 | + case <-ticker.C: |
| 109 | + if _, err := os.Stat(path); err == nil { |
| 110 | + return nil |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments