|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +// ───────────────────────────────────────────────────────────────────── |
| 14 | +// E2E Tests: Sandbox file injection |
| 15 | +// ───────────────────────────────────────────────────────────────────── |
| 16 | +// |
| 17 | +// These tests create a real Docker sandbox container and verify that |
| 18 | +// ctx files are correctly injected into it via docker cp. |
| 19 | +// |
| 20 | +// Gated by ODEK_E2E=true. |
| 21 | +// ───────────────────────────────────────────────────────────────────── |
| 22 | + |
| 23 | +// TestE2E_SandboxFileInjection verifies that --ctx files are copied |
| 24 | +// into the sandbox container and accessible via the container's shell. |
| 25 | +func TestE2E_SandboxFileInjection(t *testing.T) { |
| 26 | + skipIfNoE2E(t) |
| 27 | + |
| 28 | + // Create temp file to inject |
| 29 | + workDir := t.TempDir() |
| 30 | + testContent := "sandbox-injection-test-content" |
| 31 | + testFile := filepath.Join(workDir, "injected.txt") |
| 32 | + if err := os.WriteFile(testFile, []byte(testContent), 0644); err != nil { |
| 33 | + t.Fatalf("write test file: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + // Create a sandbox container using the same machinery as odek run |
| 37 | + containerName := fmt.Sprintf("odek-test-inject-%d", time.Now().UnixNano()) |
| 38 | + args := buildSandboxArgs(sandboxConfig{ |
| 39 | + Image: "alpine:latest", |
| 40 | + Network: "none", |
| 41 | + }, containerName, workDir, "alpine:latest") |
| 42 | + |
| 43 | + createCmd := exec.Command("docker", args...) |
| 44 | + createCmd.Stderr = os.Stderr |
| 45 | + if err := createCmd.Run(); err != nil { |
| 46 | + t.Fatalf("create sandbox container: %v", err) |
| 47 | + } |
| 48 | + defer func() { |
| 49 | + exec.Command("docker", "rm", "-f", containerName).Run() |
| 50 | + }() |
| 51 | + |
| 52 | + // Wait for container to be ready |
| 53 | + time.Sleep(500 * time.Millisecond) |
| 54 | + |
| 55 | + // Inject the file |
| 56 | + count, err := injectFilesToSandbox(containerName, []string{"injected.txt"}, workDir) |
| 57 | + if err != nil { |
| 58 | + t.Fatalf("injectFilesToSandbox: %v", err) |
| 59 | + } |
| 60 | + if count != 1 { |
| 61 | + t.Errorf("expected 1 file injected, got %d", count) |
| 62 | + } |
| 63 | + |
| 64 | + // Verify the file exists in the container at /workspace/injected.txt |
| 65 | + verifyCmd := exec.Command("docker", "exec", "-w", "/workspace", containerName, "cat", "injected.txt") |
| 66 | + output, err := verifyCmd.Output() |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("docker exec cat: %v", err) |
| 69 | + } |
| 70 | + if strings.TrimSpace(string(output)) != testContent { |
| 71 | + t.Errorf("file content = %q, want %q", strings.TrimSpace(string(output)), testContent) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// TestE2E_SandboxFileInjection_NestedPath verifies that --ctx files |
| 76 | +// in subdirectories preserve their relative path in the container. |
| 77 | +func TestE2E_SandboxFileInjection_NestedPath(t *testing.T) { |
| 78 | + skipIfNoE2E(t) |
| 79 | + |
| 80 | + workDir := t.TempDir() |
| 81 | + subDir := filepath.Join(workDir, "subdir") |
| 82 | + if err := os.Mkdir(subDir, 0755); err != nil { |
| 83 | + t.Fatalf("mkdir subdir: %v", err) |
| 84 | + } |
| 85 | + testContent := "nested-file-content" |
| 86 | + nestedFile := filepath.Join(subDir, "nested.txt") |
| 87 | + if err := os.WriteFile(nestedFile, []byte(testContent), 0644); err != nil { |
| 88 | + t.Fatalf("write nested file: %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + containerName := fmt.Sprintf("odek-test-nested-%d", time.Now().UnixNano()) |
| 92 | + args := buildSandboxArgs(sandboxConfig{ |
| 93 | + Image: "alpine:latest", |
| 94 | + Network: "none", |
| 95 | + }, containerName, workDir, "alpine:latest") |
| 96 | + |
| 97 | + createCmd := exec.Command("docker", args...) |
| 98 | + createCmd.Stderr = os.Stderr |
| 99 | + if err := createCmd.Run(); err != nil { |
| 100 | + t.Fatalf("create container: %v", err) |
| 101 | + } |
| 102 | + defer exec.Command("docker", "rm", "-f", containerName).Run() |
| 103 | + |
| 104 | + time.Sleep(500 * time.Millisecond) |
| 105 | + |
| 106 | + // Inject with relative path from cwd: subdir/nested.txt |
| 107 | + count, err := injectFilesToSandbox(containerName, []string{"subdir/nested.txt"}, workDir) |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("injectFilesToSandbox: %v", err) |
| 110 | + } |
| 111 | + if count != 1 { |
| 112 | + t.Errorf("expected 1 file injected, got %d", count) |
| 113 | + } |
| 114 | + |
| 115 | + // Verify file exists at /workspace/subdir/nested.txt |
| 116 | + verifyCmd := exec.Command("docker", "exec", "-w", "/workspace", containerName, "cat", "subdir/nested.txt") |
| 117 | + output, err := verifyCmd.Output() |
| 118 | + if err != nil { |
| 119 | + t.Fatalf("docker exec cat nested: %v", err) |
| 120 | + } |
| 121 | + if strings.TrimSpace(string(output)) != testContent { |
| 122 | + t.Errorf("nested file content = %q, want %q", strings.TrimSpace(string(output)), testContent) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// TestE2E_SandboxFileInjection_AbsolutePath verifies that absolute |
| 127 | +// path files outside cwd are injected by basename into /workspace/. |
| 128 | +func TestE2E_SandboxFileInjection_AbsolutePath(t *testing.T) { |
| 129 | + skipIfNoE2E(t) |
| 130 | + |
| 131 | + // Create a file outside the working directory |
| 132 | + externalDir := t.TempDir() |
| 133 | + absContent := "absolute-path-content" |
| 134 | + absFile := filepath.Join(externalDir, "external.txt") |
| 135 | + if err := os.WriteFile(absFile, []byte(absContent), 0644); err != nil { |
| 136 | + t.Fatalf("write external file: %v", err) |
| 137 | + } |
| 138 | + |
| 139 | + workDir := t.TempDir() |
| 140 | + |
| 141 | + containerName := fmt.Sprintf("odek-test-abs-%d", time.Now().UnixNano()) |
| 142 | + args := buildSandboxArgs(sandboxConfig{ |
| 143 | + Image: "alpine:latest", |
| 144 | + Network: "none", |
| 145 | + }, containerName, workDir, "alpine:latest") |
| 146 | + |
| 147 | + createCmd := exec.Command("docker", args...) |
| 148 | + createCmd.Stderr = os.Stderr |
| 149 | + if err := createCmd.Run(); err != nil { |
| 150 | + t.Fatalf("create container: %v", err) |
| 151 | + } |
| 152 | + defer exec.Command("docker", "rm", "-f", containerName).Run() |
| 153 | + |
| 154 | + time.Sleep(500 * time.Millisecond) |
| 155 | + |
| 156 | + // Inject with absolute path (outside cwd) — should use basename |
| 157 | + count, err := injectFilesToSandbox(containerName, []string{absFile}, workDir) |
| 158 | + if err != nil { |
| 159 | + t.Fatalf("injectFilesToSandbox: %v", err) |
| 160 | + } |
| 161 | + if count != 1 { |
| 162 | + t.Errorf("expected 1 file injected, got %d", count) |
| 163 | + } |
| 164 | + |
| 165 | + // Should exist at /workspace/external.txt (basename) |
| 166 | + verifyCmd := exec.Command("docker", "exec", "-w", "/workspace", containerName, "cat", "external.txt") |
| 167 | + output, err := verifyCmd.Output() |
| 168 | + if err != nil { |
| 169 | + t.Fatalf("docker exec cat external: %v", err) |
| 170 | + } |
| 171 | + if strings.TrimSpace(string(output)) != absContent { |
| 172 | + t.Errorf("external file content = %q, want %q", strings.TrimSpace(string(output)), absContent) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +// TestE2E_SandboxFileInjection_MultipleFiles verifies injecting |
| 177 | +// multiple files at once. |
| 178 | +func TestE2E_SandboxFileInjection_MultipleFiles(t *testing.T) { |
| 179 | + skipIfNoE2E(t) |
| 180 | + |
| 181 | + workDir := t.TempDir() |
| 182 | + os.WriteFile(filepath.Join(workDir, "a.txt"), []byte("file A"), 0644) |
| 183 | + os.WriteFile(filepath.Join(workDir, "b.txt"), []byte("file B"), 0644) |
| 184 | + |
| 185 | + containerName := fmt.Sprintf("odek-test-multi-%d", time.Now().UnixNano()) |
| 186 | + args := buildSandboxArgs(sandboxConfig{ |
| 187 | + Image: "alpine:latest", |
| 188 | + Network: "none", |
| 189 | + }, containerName, workDir, "alpine:latest") |
| 190 | + |
| 191 | + createCmd := exec.Command("docker", args...) |
| 192 | + createCmd.Stderr = os.Stderr |
| 193 | + if err := createCmd.Run(); err != nil { |
| 194 | + t.Fatalf("create container: %v", err) |
| 195 | + } |
| 196 | + defer exec.Command("docker", "rm", "-f", containerName).Run() |
| 197 | + |
| 198 | + time.Sleep(500 * time.Millisecond) |
| 199 | + |
| 200 | + count, err := injectFilesToSandbox(containerName, []string{"a.txt", "b.txt"}, workDir) |
| 201 | + if err != nil { |
| 202 | + t.Fatalf("injectFilesToSandbox: %v", err) |
| 203 | + } |
| 204 | + if count != 2 { |
| 205 | + t.Errorf("expected 2 files injected, got %d", count) |
| 206 | + } |
| 207 | + |
| 208 | + // Verify both files |
| 209 | + for _, name := range []string{"a.txt", "b.txt"} { |
| 210 | + out, err := exec.Command("docker", "exec", "-w", "/workspace", containerName, "cat", name).Output() |
| 211 | + if err != nil { |
| 212 | + t.Errorf("cat %s: %v", name, err) |
| 213 | + } |
| 214 | + if strings.TrimSpace(string(out)) == "" { |
| 215 | + t.Errorf("file %s is empty", name) |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments