Skip to content

Commit

Permalink
fix(k8s): don't throw error when test/task artifact is missing after run
Browse files Browse the repository at this point in the history
We were throwing a somewhat ugly error, and in many cases you'd expect
files to be missing after a run, e.g. after a failed run.
  • Loading branch information
edvald committed Feb 7, 2020
1 parent d0b025c commit 8a24623
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
15 changes: 12 additions & 3 deletions garden-service/src/plugins/kubernetes/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ export async function runAndCopy({
"-c", // create an archive
"-f",
"-", // pipe to stdout
sourcePath, // files to match
// Files to match. The .DS_Store file is a trick to avoid errors when no files are matched. The file is
// ignored later when copying from the temp directory. See https://github.com/sindresorhus/cpy#ignorejunk
`$(ls ${sourcePath} 2>/dev/null) .DS_Store`,
]

await new Promise((_resolve, reject) => {
Expand All @@ -257,7 +259,7 @@ export async function runAndCopy({
// Tarball the requested files and stream to the above extractor.
runner
.exec({
command: ["sh", "-c", "cd / && " + tarCmd.join(" ")],
command: ["sh", "-c", "cd / && touch .DS_Store && " + tarCmd.join(" ")],
container: mainContainerName,
ignoreError: false,
log,
Expand All @@ -272,7 +274,14 @@ export async function runAndCopy({
})

// Copy the resulting files to the artifacts directory
await cpy(sourcePath, targetPath, { cwd: tmpDir.path })
try {
await cpy("**/*", targetPath, { cwd: tmpDir.path, ignoreJunk: true })
} catch (err) {
// Ignore error thrown when the directory is empty
if (err.name !== "CpyError" || !err.message.includes("the file doesn't exist")) {
throw err
}
}
})
)
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,26 @@ describe("kubernetes container module handlers", () => {
expect(await pathExists(join(tmpDir.path, "output.txt"))).to.be.true
})

it("should not throw when an artifact is missing", async () => {
const task = await graph.getTask("artifacts-task")
const module = task.module
const image = await containerHelpers.getDeploymentImageId(module, provider.config.deploymentRegistry)

await runAndCopy({
ctx: garden.getPluginContext(provider),
log: garden.log,
command: ["sh", "-c", "echo ok"],
args: [],
interactive: false,
module,
namespace,
runtimeContext: { envVars: {}, dependencies: [] },
artifacts: task.spec.artifacts,
artifactsPath: tmpDir.path,
image,
})
})

it("should correctly copy a whole directory", async () => {
const task = await graph.getTask("dir-task")
const module = task.module
Expand Down

0 comments on commit 8a24623

Please sign in to comment.