From 66153c6194bcc1cd7783da7bbb8ddf516227bb82 Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Thu, 31 Mar 2022 19:31:14 -0300 Subject: [PATCH] Prevents files to be used as input of dagger.#FS Errors out in the PreRun phase of the clientfilesystemreader task since otherwise, dagger execution would hang Fixes #1977 Signed-off-by: Marcos Lilljedahl --- plan/task/clientfilesystemread.go | 7 ++++--- tests/plan.bats | 9 +++++++++ tests/plan/client/filesystem/read/fs/invalid/test.cue | 11 +++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 tests/plan/client/filesystem/read/fs/invalid/test.cue diff --git a/plan/task/clientfilesystemread.go b/plan/task/clientfilesystemread.go index 0ec37f1a..6ef5a32d 100644 --- a/plan/task/clientfilesystemread.go +++ b/plan/task/clientfilesystemread.go @@ -18,8 +18,7 @@ func init() { Register("ClientFilesystemRead", func() Task { return &clientFilesystemReadTask{} }) } -type clientFilesystemReadTask struct { -} +type clientFilesystemReadTask struct{} func (t clientFilesystemReadTask) PreRun(_ context.Context, pctx *plancontext.Context, v *compiler.Value) error { path, err := t.parsePath(v) @@ -27,8 +26,10 @@ func (t clientFilesystemReadTask) PreRun(_ context.Context, pctx *plancontext.Co return err } - if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { + if pi, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { return fmt.Errorf("path %q does not exist", path) + } else if !pi.IsDir() && plancontext.IsFSValue(v.Lookup("contents")) { + return fmt.Errorf("path %q is not a directory", path) } if plancontext.IsFSValue(v.Lookup("contents")) { diff --git a/tests/plan.bats b/tests/plan.bats index c783daa9..78a6cc66 100644 --- a/tests/plan.bats +++ b/tests/plan.bats @@ -84,6 +84,15 @@ setup() { assert_output --partial 'path "/foobar" does not exist' } + +@test "plan/client/filesystem/read/fs/invalid" { + cd "$TESTDIR/plan/client/filesystem/read/fs/invalid" + + run "$DAGGER" "do" -p . test + assert_failure + assert_output --partial 'test.txt" is not a directory' +} + @test "plan/client/filesystem/read/fs/relative" { cd "$TESTDIR/plan/client/filesystem/read/fs/relative" diff --git a/tests/plan/client/filesystem/read/fs/invalid/test.cue b/tests/plan/client/filesystem/read/fs/invalid/test.cue new file mode 100644 index 00000000..155203eb --- /dev/null +++ b/tests/plan/client/filesystem/read/fs/invalid/test.cue @@ -0,0 +1,11 @@ +package main + +import ( + "dagger.io/dagger" +) + +dagger.#Plan & { + client: filesystem: "../rootfs/test.txt": read: contents: dagger.#FS + actions: test: { + } +}