Fix path in LogFile creator
Signed-off-by: Maksym Pavlenko <makpav@amazon.com>
This commit is contained in:
		
							
								
								
									
										16
									
								
								cio/io.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								cio/io.go
									
									
									
									
									
								
							| @@ -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 | ||||
| 	} | ||||
| @@ -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 | ||||
| 	} | ||||
|   | ||||
| @@ -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") | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| 		} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Maksym Pavlenko
					Maksym Pavlenko