Skip to content

Commit

Permalink
Fix path in LogFile creator
Browse files Browse the repository at this point in the history
Signed-off-by: Maksym Pavlenko <makpav@amazon.com>
  • Loading branch information
mxpv committed Jun 19, 2019
1 parent 5e0d793 commit fbf96d3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
16 changes: 11 additions & 5 deletions cio/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,11 @@ func BinaryIO(binary string, args map[string]string) Creator {
q.Set(k, v)
}
uri.RawQuery = q.Encode()
res := uri.String()
return &logURI{
config: Config{
Stdout: uri.String(),
Stderr: uri.String(),
Stdout: res,
Stderr: res,
},
}, nil
}
Expand All @@ -271,14 +272,19 @@ func BinaryIO(binary string, args map[string]string) Creator {
// If the log file already exists, the logs will be appended to the file.
func LogFile(path string) Creator {
return func(_ string) (IO, error) {
path = filepath.Clean(path)
if !strings.HasPrefix(path, "/") {
return nil, errors.New("absolute path needed")
}
uri := &url.URL{
Scheme: "file",
Host: path,
Path: path,
}
res := uri.String()
return &logURI{
config: Config{
Stdout: uri.String(),
Stderr: uri.String(),
Stdout: res,
Stderr: res,
},
}, nil
}
Expand Down
18 changes: 18 additions & 0 deletions cio/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,21 @@ func TestBinaryIOFailOnRelativePath(t *testing.T) {
_, err := BinaryIO("./bin", nil)("!")
assert.Error(t, err, "absolute path needed")
}

func TestLogFileAbsolutePath(t *testing.T) {
res, err := LogFile("/full/path/file.txt")("!")
assert.NilError(t, err)
assert.Equal(t, "file:///full/path/file.txt", res.Config().Stdout)
assert.Equal(t, "file:///full/path/file.txt", res.Config().Stderr)

// Test parse back
parsed, err := url.Parse(res.Config().Stdout)
assert.NilError(t, err)
assert.Equal(t, "file", parsed.Scheme)
assert.Equal(t, "/full/path/file.txt", parsed.Path)
}

func TestLogFileFailOnRelativePath(t *testing.T) {
_, err := LogFile("./file.txt")("!")
assert.Error(t, err, "absolute path needed")
}
4 changes: 2 additions & 2 deletions runtime/v1/linux/proc/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ func createIO(ctx context.Context, id string, ioUID, ioGID int, stdio proc.Stdio
case "binary":
pio.io, err = NewBinaryIO(ctx, id, u)
case "file":
if err := os.MkdirAll(filepath.Dir(u.Host), 0755); err != nil {
if err := os.MkdirAll(filepath.Dir(u.Path), 0755); err != nil {
return nil, err
}
var f *os.File
f, err = os.OpenFile(u.Host, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
f, err = os.OpenFile(u.Path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit fbf96d3

Please sign in to comment.