|
| 1 | +//go:build integration |
| 2 | +// +build integration |
| 3 | + |
| 4 | +package leeway |
| 5 | + |
| 6 | +import ( |
| 7 | + "archive/tar" |
| 8 | + "compress/gzip" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "os" |
| 12 | + "os/exec" |
| 13 | + "path/filepath" |
| 14 | + "strings" |
| 15 | + "testing" |
| 16 | + |
| 17 | + "github.com/gitpod-io/leeway/pkg/leeway/cache/local" |
| 18 | +) |
| 19 | + |
| 20 | +func TestDockerPackage_ExportToCache_Integration(t *testing.T) { |
| 21 | + if testing.Short() { |
| 22 | + t.Skip("Skipping integration test in short mode") |
| 23 | + } |
| 24 | + |
| 25 | + // Ensure Docker is available |
| 26 | + if err := exec.Command("docker", "version").Run(); err != nil { |
| 27 | + t.Skip("Docker not available, skipping integration test") |
| 28 | + } |
| 29 | + |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + exportToCache bool |
| 33 | + hasImages bool |
| 34 | + expectFiles []string |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "legacy push behavior", |
| 38 | + exportToCache: false, |
| 39 | + hasImages: true, |
| 40 | + expectFiles: []string{"imgnames.txt", "metadata.yaml"}, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "new export behavior", |
| 44 | + exportToCache: true, |
| 45 | + hasImages: true, |
| 46 | + expectFiles: []string{"image.tar", "imgnames.txt", "docker-export-metadata.json"}, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "export without image config", |
| 50 | + exportToCache: true, |
| 51 | + hasImages: false, |
| 52 | + expectFiles: []string{"content/"}, |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + for _, tt := range tests { |
| 57 | + t.Run(tt.name, func(t *testing.T) { |
| 58 | + // Create temporary workspace |
| 59 | + tmpDir := t.TempDir() |
| 60 | + |
| 61 | + // Create a simple Dockerfile |
| 62 | + dockerfile := `FROM alpine:latest |
| 63 | +LABEL test="true" |
| 64 | +CMD ["echo", "test"]` |
| 65 | + |
| 66 | + dockerfilePath := filepath.Join(tmpDir, "Dockerfile") |
| 67 | + if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0644); err != nil { |
| 68 | + t.Fatal(err) |
| 69 | + } |
| 70 | + |
| 71 | + // Create WORKSPACE.yaml with proper formatting |
| 72 | + imageSection := "" |
| 73 | + if tt.hasImages { |
| 74 | + imageSection = ` |
| 75 | + image: |
| 76 | + - test-leeway:latest` |
| 77 | + } |
| 78 | + |
| 79 | + workspaceYAML := fmt.Sprintf(`defaultTarget: ":app" |
| 80 | +components: |
| 81 | + - name: "." |
| 82 | + packages: |
| 83 | + - name: app |
| 84 | + type: docker |
| 85 | + config: |
| 86 | + dockerfile: Dockerfile |
| 87 | + exportToCache: %t%s`, tt.exportToCache, imageSection) |
| 88 | + |
| 89 | + workspacePath := filepath.Join(tmpDir, "WORKSPACE.yaml") |
| 90 | + if err := os.WriteFile(workspacePath, []byte(workspaceYAML), 0644); err != nil { |
| 91 | + t.Fatal(err) |
| 92 | + } |
| 93 | + |
| 94 | + // Load workspace |
| 95 | + workspace, err := FindWorkspace(tmpDir, Arguments{}, "", "") |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("Failed to load workspace: %v", err) |
| 98 | + } |
| 99 | + |
| 100 | + // Get package |
| 101 | + pkg, ok := workspace.Packages[":app"] |
| 102 | + if !ok { |
| 103 | + t.Fatalf("Package :app not found in workspace") |
| 104 | + } |
| 105 | + |
| 106 | + // Create local cache |
| 107 | + cacheDir := filepath.Join(tmpDir, ".cache") |
| 108 | + if err := os.MkdirAll(cacheDir, 0755); err != nil { |
| 109 | + t.Fatal(err) |
| 110 | + } |
| 111 | + |
| 112 | + localCache, err := local.NewFilesystemCache(cacheDir) |
| 113 | + if err != nil { |
| 114 | + t.Fatalf("Failed to create local cache: %v", err) |
| 115 | + } |
| 116 | + |
| 117 | + // Build package using the Build function |
| 118 | + err = Build(pkg, |
| 119 | + WithLocalCache(localCache), |
| 120 | + WithDontTest(true), |
| 121 | + ) |
| 122 | + if err != nil { |
| 123 | + t.Fatalf("Build failed: %v", err) |
| 124 | + } |
| 125 | + |
| 126 | + // Verify cache artifact exists |
| 127 | + cachePath, exists := localCache.Location(pkg) |
| 128 | + if !exists { |
| 129 | + t.Fatal("Package not found in cache after build") |
| 130 | + } |
| 131 | + |
| 132 | + t.Logf("Cache artifact created at: %s", cachePath) |
| 133 | + |
| 134 | + // Verify artifact contents |
| 135 | + foundFiles, err := listTarGzContents(cachePath) |
| 136 | + if err != nil { |
| 137 | + t.Fatalf("Failed to list tar contents: %v", err) |
| 138 | + } |
| 139 | + |
| 140 | + t.Logf("Files in cache artifact: %v", foundFiles) |
| 141 | + |
| 142 | + // Check expected files are present |
| 143 | + for _, expectedFile := range tt.expectFiles { |
| 144 | + found := false |
| 145 | + for _, actualFile := range foundFiles { |
| 146 | + if filepath.Base(actualFile) == expectedFile || actualFile == expectedFile { |
| 147 | + found = true |
| 148 | + break |
| 149 | + } |
| 150 | + } |
| 151 | + if !found { |
| 152 | + t.Errorf("Expected file %s not found in cache artifact", expectedFile) |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // Verify artifact contents based on export mode |
| 157 | + if tt.exportToCache && tt.hasImages { |
| 158 | + // Should contain image.tar and metadata |
| 159 | + metadata, err := extractDockerMetadataFromCache(cachePath) |
| 160 | + if err != nil { |
| 161 | + t.Errorf("Failed to extract metadata: %v", err) |
| 162 | + } else { |
| 163 | + if len(metadata.ImageNames) == 0 { |
| 164 | + t.Error("Expected image names in metadata") |
| 165 | + } |
| 166 | + if metadata.ImageNames[0] != "test-leeway:latest" { |
| 167 | + t.Errorf("Unexpected image name: %s", metadata.ImageNames[0]) |
| 168 | + } |
| 169 | + t.Logf("Metadata: %+v", metadata) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // Cleanup |
| 174 | + if tt.hasImages && !tt.exportToCache { |
| 175 | + exec.Command("docker", "rmi", "test-leeway:latest").Run() |
| 176 | + } |
| 177 | + }) |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +// listTarGzContents lists all files in a tar.gz archive |
| 182 | +func listTarGzContents(path string) ([]string, error) { |
| 183 | + f, err := os.Open(path) |
| 184 | + if err != nil { |
| 185 | + return nil, err |
| 186 | + } |
| 187 | + defer f.Close() |
| 188 | + |
| 189 | + gzr, err := gzip.NewReader(f) |
| 190 | + if err != nil { |
| 191 | + return nil, err |
| 192 | + } |
| 193 | + defer gzr.Close() |
| 194 | + |
| 195 | + tr := tar.NewReader(gzr) |
| 196 | + var files []string |
| 197 | + |
| 198 | + for { |
| 199 | + hdr, err := tr.Next() |
| 200 | + if err == io.EOF { |
| 201 | + break |
| 202 | + } |
| 203 | + if err != nil { |
| 204 | + return nil, err |
| 205 | + } |
| 206 | + files = append(files, hdr.Name) |
| 207 | + } |
| 208 | + |
| 209 | + return files, nil |
| 210 | +} |
| 211 | + |
| 212 | +func TestDockerPackage_CacheRoundTrip_Integration(t *testing.T) { |
| 213 | + if testing.Short() { |
| 214 | + t.Skip("Skipping integration test in short mode") |
| 215 | + } |
| 216 | + |
| 217 | + // Ensure Docker is available |
| 218 | + if err := exec.Command("docker", "version").Run(); err != nil { |
| 219 | + t.Skip("Docker not available, skipping integration test") |
| 220 | + } |
| 221 | + |
| 222 | + // This test verifies that a Docker image can be: |
| 223 | + // 1. Built and exported to cache |
| 224 | + // 2. Extracted from cache |
| 225 | + // 3. Loaded back into Docker |
| 226 | + // 4. Still works correctly |
| 227 | + |
| 228 | + tmpDir := t.TempDir() |
| 229 | + testImage := "test-leeway-roundtrip:latest" |
| 230 | + |
| 231 | + // Create a simple Dockerfile with identifiable content |
| 232 | + dockerfile := `FROM alpine:latest |
| 233 | +RUN echo "test-content-12345" > /test-file.txt |
| 234 | +CMD ["cat", "/test-file.txt"]` |
| 235 | + |
| 236 | + dockerfilePath := filepath.Join(tmpDir, "Dockerfile") |
| 237 | + if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0644); err != nil { |
| 238 | + t.Fatal(err) |
| 239 | + } |
| 240 | + |
| 241 | + // Create WORKSPACE.yaml with exportToCache enabled |
| 242 | + workspaceYAML := fmt.Sprintf(`defaultTarget: ":app" |
| 243 | +components: |
| 244 | + - name: "." |
| 245 | + packages: |
| 246 | + - name: app |
| 247 | + type: docker |
| 248 | + config: |
| 249 | + dockerfile: Dockerfile |
| 250 | + exportToCache: true |
| 251 | + image: |
| 252 | + - %s`, testImage) |
| 253 | + |
| 254 | + workspacePath := filepath.Join(tmpDir, "WORKSPACE.yaml") |
| 255 | + if err := os.WriteFile(workspacePath, []byte(workspaceYAML), 0644); err != nil { |
| 256 | + t.Fatal(err) |
| 257 | + } |
| 258 | + |
| 259 | + // Step 1: Build with export mode |
| 260 | + t.Log("Step 1: Building Docker image with export mode") |
| 261 | + workspace, err := FindWorkspace(tmpDir, Arguments{}, "", "") |
| 262 | + if err != nil { |
| 263 | + t.Fatalf("Failed to load workspace: %v", err) |
| 264 | + } |
| 265 | + |
| 266 | + pkg, ok := workspace.Packages[":app"] |
| 267 | + if !ok { |
| 268 | + t.Fatal("Package :app not found in workspace") |
| 269 | + } |
| 270 | + |
| 271 | + cacheDir := filepath.Join(tmpDir, ".cache") |
| 272 | + if err := os.MkdirAll(cacheDir, 0755); err != nil { |
| 273 | + t.Fatal(err) |
| 274 | + } |
| 275 | + |
| 276 | + localCache, err := local.NewFilesystemCache(cacheDir) |
| 277 | + if err != nil { |
| 278 | + t.Fatalf("Failed to create local cache: %v", err) |
| 279 | + } |
| 280 | + |
| 281 | + err = Build(pkg, |
| 282 | + WithLocalCache(localCache), |
| 283 | + WithDontTest(true), |
| 284 | + ) |
| 285 | + if err != nil { |
| 286 | + t.Fatalf("Build failed: %v", err) |
| 287 | + } |
| 288 | + |
| 289 | + // Step 2: Verify cache artifact exists and contains image.tar |
| 290 | + t.Log("Step 2: Verifying cache artifact") |
| 291 | + cachePath, exists := localCache.Location(pkg) |
| 292 | + if !exists { |
| 293 | + t.Fatal("Package not found in cache after build") |
| 294 | + } |
| 295 | + |
| 296 | + files, err := listTarGzContents(cachePath) |
| 297 | + if err != nil { |
| 298 | + t.Fatalf("Failed to list cache contents: %v", err) |
| 299 | + } |
| 300 | + |
| 301 | + hasImageTar := false |
| 302 | + hasMetadata := false |
| 303 | + for _, file := range files { |
| 304 | + if filepath.Base(file) == "image.tar" { |
| 305 | + hasImageTar = true |
| 306 | + } |
| 307 | + if filepath.Base(file) == "docker-export-metadata.json" { |
| 308 | + hasMetadata = true |
| 309 | + } |
| 310 | + } |
| 311 | + |
| 312 | + if !hasImageTar { |
| 313 | + t.Error("Cache artifact missing image.tar") |
| 314 | + } |
| 315 | + if !hasMetadata { |
| 316 | + t.Error("Cache artifact missing docker-export-metadata.json") |
| 317 | + } |
| 318 | + |
| 319 | + // Step 3: Extract metadata and verify |
| 320 | + t.Log("Step 3: Extracting and verifying metadata") |
| 321 | + metadata, err := extractDockerMetadataFromCache(cachePath) |
| 322 | + if err != nil { |
| 323 | + t.Fatalf("Failed to extract metadata: %v", err) |
| 324 | + } |
| 325 | + |
| 326 | + if len(metadata.ImageNames) == 0 { |
| 327 | + t.Error("Metadata has no image names") |
| 328 | + } |
| 329 | + if metadata.ImageNames[0] != testImage { |
| 330 | + t.Errorf("Metadata image name = %s, want %s", metadata.ImageNames[0], testImage) |
| 331 | + } |
| 332 | + if metadata.Digest == "" { |
| 333 | + t.Error("Metadata missing digest") |
| 334 | + } |
| 335 | + |
| 336 | + t.Logf("Metadata: ImageNames=%v, Digest=%s, BuildTime=%v", |
| 337 | + metadata.ImageNames, metadata.Digest, metadata.BuildTime) |
| 338 | + |
| 339 | + // Step 4: Extract image.tar from cache and load into Docker |
| 340 | + t.Log("Step 4: Extracting image.tar and loading into Docker") |
| 341 | + |
| 342 | + // First, remove the image if it exists |
| 343 | + exec.Command("docker", "rmi", "-f", testImage).Run() |
| 344 | + |
| 345 | + // Extract image.tar from the cache bundle |
| 346 | + extractDir := filepath.Join(tmpDir, "extracted") |
| 347 | + if err := os.MkdirAll(extractDir, 0755); err != nil { |
| 348 | + t.Fatal(err) |
| 349 | + } |
| 350 | + |
| 351 | + // Extract the tar.gz |
| 352 | + extractCmd := exec.Command("tar", "-xzf", cachePath, "-C", extractDir) |
| 353 | + if output, err := extractCmd.CombinedOutput(); err != nil { |
| 354 | + t.Fatalf("Failed to extract cache: %v\nOutput: %s", err, string(output)) |
| 355 | + } |
| 356 | + |
| 357 | + imageTarPath := filepath.Join(extractDir, "image.tar") |
| 358 | + if _, err := os.Stat(imageTarPath); err != nil { |
| 359 | + t.Fatalf("image.tar not found after extraction: %v", err) |
| 360 | + } |
| 361 | + |
| 362 | + // Load the image back into Docker |
| 363 | + loadCmd := exec.Command("docker", "load", "-i", imageTarPath) |
| 364 | + if output, err := loadCmd.CombinedOutput(); err != nil { |
| 365 | + t.Fatalf("Failed to load image: %v\nOutput: %s", err, string(output)) |
| 366 | + } |
| 367 | + |
| 368 | + // Step 5: Verify the loaded image works |
| 369 | + t.Log("Step 5: Verifying loaded image works") |
| 370 | + |
| 371 | + // Get the digest of the loaded image |
| 372 | + inspectCmd := exec.Command("docker", "inspect", "--format={{index .Id}}", testImage) |
| 373 | + inspectOutput, err := inspectCmd.Output() |
| 374 | + if err != nil { |
| 375 | + t.Fatalf("Failed to inspect loaded image: %v", err) |
| 376 | + } |
| 377 | + loadedDigest := strings.TrimSpace(string(inspectOutput)) |
| 378 | + |
| 379 | + t.Logf("Loaded image digest: %s", loadedDigest) |
| 380 | + t.Logf("Original metadata digest: %s", metadata.Digest) |
| 381 | + |
| 382 | + // Run the container to verify it works |
| 383 | + runCmd := exec.Command("docker", "run", "--rm", testImage) |
| 384 | + runOutput, err := runCmd.Output() |
| 385 | + if err != nil { |
| 386 | + t.Fatalf("Failed to run container: %v", err) |
| 387 | + } |
| 388 | + |
| 389 | + expectedOutput := "test-content-12345\n" |
| 390 | + if string(runOutput) != expectedOutput { |
| 391 | + t.Errorf("Container output = %q, want %q", string(runOutput), expectedOutput) |
| 392 | + } |
| 393 | + |
| 394 | + // Cleanup |
| 395 | + exec.Command("docker", "rmi", "-f", testImage).Run() |
| 396 | + |
| 397 | + t.Log("✅ Round-trip test passed: image exported, cached, extracted, loaded, and executed successfully") |
| 398 | +} |
0 commit comments