Merge pull request #1979 from marcosnils/fix/fs_read_file

Fixes hang errors when incorrectly using files as  dagger.#FS on client filesystem
This commit is contained in:
Marcos Nils 2022-04-01 15:17:50 -03:00 committed by GitHub
commit d00fbff9d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 4 deletions

View File

@ -18,8 +18,7 @@ func init() {
Register("ClientFilesystemRead", func() Task { return &clientFilesystemReadTask{} }) 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 { func (t clientFilesystemReadTask) PreRun(_ context.Context, pctx *plancontext.Context, v *compiler.Value) error {
path, err := t.parsePath(v) path, err := t.parsePath(v)
@ -27,8 +26,15 @@ func (t clientFilesystemReadTask) PreRun(_ context.Context, pctx *plancontext.Co
return err return err
} }
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { isFS := plancontext.IsFSValue(v.Lookup("contents"))
switch pi, err := os.Stat(path); {
case errors.Is(err, os.ErrNotExist):
return fmt.Errorf("path %q does not exist", path) return fmt.Errorf("path %q does not exist", path)
case !pi.IsDir() && isFS:
return fmt.Errorf("path %q is not a directory", path)
case pi.IsDir() && !isFS:
return fmt.Errorf("path %q cannot be a directory", path)
} }
if plancontext.IsFSValue(v.Lookup("contents")) { if plancontext.IsFSValue(v.Lookup("contents")) {

View File

@ -84,6 +84,24 @@ setup() {
assert_output --partial 'path "/foobar" does not exist' assert_output --partial 'path "/foobar" does not exist'
} }
@test "plan/client/filesystem/read/fs/invalid_fs_input" {
cd "$TESTDIR/plan/client/filesystem/read/fs/invalid_fs_input"
run "$DAGGER" "do" -p . test
assert_failure
assert_output --partial 'test.txt" is not a directory'
}
@test "plan/client/filesystem/read/fs/invalid_fs_type" {
cd "$TESTDIR/plan/client/filesystem/read/fs/invalid_fs_type"
run "$DAGGER" "do" -p . test
assert_failure
assert_output --partial 'rootfs" cannot be a directory'
}
@test "plan/client/filesystem/read/fs/relative" { @test "plan/client/filesystem/read/fs/relative" {
cd "$TESTDIR/plan/client/filesystem/read/fs/relative" cd "$TESTDIR/plan/client/filesystem/read/fs/relative"
@ -221,4 +239,4 @@ setup() {
# Run with invalid platform # Run with invalid platform
run "$DAGGER" "do" -p./plan/platform/config_platform_failure_invalid_platform.cue verify run "$DAGGER" "do" -p./plan/platform/config_platform_failure_invalid_platform.cue verify
assert_failure assert_failure
} }

View File

@ -0,0 +1,12 @@
package main
import (
"dagger.io/dagger"
)
dagger.#Plan & {
// Reading a file into a dagger.#FS should not be possbile
client: filesystem: "../rootfs/test.txt": read: contents: dagger.#FS
actions: test: {
}
}

View File

@ -0,0 +1,21 @@
package main
import (
"dagger.io/dagger"
"dagger.io/dagger/core"
)
dagger.#Plan & {
// Reading a directory into a non-fs should fail
client: filesystem: "../rootfs": read: contents: string
actions: {
image: core.#Pull & {
source: "alpine:3.15.0@sha256:e7d88de73db3d3fd9b2d63aa7f447a10fd0220b7cbf39803c803f2af9ba256b3"
}
test: core.#Exec & {
input: image.output
args: ["test", client.filesystem."../rootfs".read.contents]
}
}
}