diff --git a/Tests/CLITests/Subcommands/Build/CLIBuilderTest.swift b/Tests/CLITests/Subcommands/Build/CLIBuilderTest.swift index 67dd27c25..6c18b4518 100644 --- a/Tests/CLITests/Subcommands/Build/CLIBuilderTest.swift +++ b/Tests/CLITests/Subcommands/Build/CLIBuilderTest.swift @@ -1351,5 +1351,65 @@ extension TestCLIBuildBase { ) #expect(try self.inspectImage(imageName) == imageName, "expected to have successfully built \(imageName)") } + + @Test func testCopyFromLocalImage() throws { + let baseTempDir: URL = try createTempDir() + let tempDir: URL = try createTempDir() + defer { + try! FileManager.default.removeItem(at: baseTempDir) + try! FileManager.default.removeItem(at: tempDir) + } + + let baseImageName = "local-base:\(UUID().uuidString)" + let baseDockerfile = + """ + FROM scratch + ADD hello.txt /hello.txt + """ + let baseContext: [FileSystemEntry] = [ + .file("hello.txt", content: .data("hello\n".data(using: .utf8)!)) + ] + try createContext(tempDir: baseTempDir, dockerfile: baseDockerfile, context: baseContext) + + try self.build(tag: baseImageName, tempDir: baseTempDir) + #expect(try self.inspectImage(baseImageName) == baseImageName, "expected to have successfully built \(baseImageName)") + + let dockerfile = + """ + FROM ghcr.io/linuxcontainers/alpine:3.20 + COPY --from=\(baseImageName) /hello.txt /copied.txt + RUN cat /copied.txt + """ + try createContext(tempDir: tempDir, dockerfile: dockerfile) + + let imageName = "registry.local/copy-from-local:\(UUID().uuidString)" + try self.build(tag: imageName, tempDir: tempDir) + #expect(try self.inspectImage(imageName) == imageName, "expected to have successfully built \(imageName)") + } + + @Test func testCopyFromBuildStage() throws { + let tempDir: URL = try createTempDir() + defer { + try! FileManager.default.removeItem(at: tempDir) + } + + let dockerfile = + """ + FROM scratch AS builder + ADD hello.txt /hello.txt + + FROM ghcr.io/linuxcontainers/alpine:3.20 + COPY --from=builder /hello.txt /copied.txt + RUN cat /copied.txt + """ + let context: [FileSystemEntry] = [ + .file("hello.txt", content: .data("hello\n".data(using: .utf8)!)) + ] + try createContext(tempDir: tempDir, dockerfile: dockerfile, context: context) + + let imageName = "registry.local/copy-from-stage:\(UUID().uuidString)" + try self.build(tag: imageName, tempDir: tempDir) + #expect(try self.inspectImage(imageName) == imageName, "expected to have successfully built \(imageName)") + } } }