Fix bugs in BinaryIO creator
Signed-off-by: Maksym Pavlenko <makpav@amazon.com>
This commit is contained in:
parent
3e7c6f6a6b
commit
5e0d793801
13
cio/io.go
13
cio/io.go
@ -18,10 +18,13 @@ package cio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/containerd/containerd/defaults"
|
||||
@ -242,13 +245,19 @@ func LogURI(uri *url.URL) Creator {
|
||||
// BinaryIO forwards container STDOUT|STDERR directly to a logging binary
|
||||
func BinaryIO(binary string, args map[string]string) Creator {
|
||||
return func(_ string) (IO, error) {
|
||||
binary = filepath.Clean(binary)
|
||||
if !strings.HasPrefix(binary, "/") {
|
||||
return nil, errors.New("absolute path needed")
|
||||
}
|
||||
uri := &url.URL{
|
||||
Scheme: "binary",
|
||||
Host: binary,
|
||||
Path: binary,
|
||||
}
|
||||
q := uri.Query()
|
||||
for k, v := range args {
|
||||
uri.Query().Set(k, v)
|
||||
q.Set(k, v)
|
||||
}
|
||||
uri.RawQuery = q.Encode()
|
||||
return &logURI{
|
||||
config: Config{
|
||||
Stdout: uri.String(),
|
||||
|
@ -23,6 +23,7 @@ import (
|
||||
"context"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
@ -153,3 +154,26 @@ func initProducers(t *testing.T, producers producers, stdout, stderr string) {
|
||||
assert.NilError(t, err)
|
||||
assert.NilError(t, producers.Stderr.Close())
|
||||
}
|
||||
|
||||
func TestBinaryIOArgs(t *testing.T) {
|
||||
res, err := BinaryIO("/file.bin", map[string]string{"id": "1"})("")
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, "binary:///file.bin?id=1", res.Config().Stdout)
|
||||
assert.Equal(t, "binary:///file.bin?id=1", res.Config().Stderr)
|
||||
}
|
||||
|
||||
func TestBinaryIOAbsolutePath(t *testing.T) {
|
||||
res, err := BinaryIO("/full/path/bin", nil)("!")
|
||||
assert.NilError(t, err)
|
||||
|
||||
// Test parse back
|
||||
parsed, err := url.Parse(res.Config().Stdout)
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, "binary", parsed.Scheme)
|
||||
assert.Equal(t, "/full/path/bin", parsed.Path)
|
||||
}
|
||||
|
||||
func TestBinaryIOFailOnRelativePath(t *testing.T) {
|
||||
_, err := BinaryIO("./bin", nil)("!")
|
||||
assert.Error(t, err, "absolute path needed")
|
||||
}
|
||||
|
@ -265,7 +265,7 @@ func NewBinaryIO(ctx context.Context, id string, uri *url.URL) (runc.IO, error)
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
cmd := exec.CommandContext(ctx, uri.Host, args...)
|
||||
cmd := exec.CommandContext(ctx, uri.Path, args...)
|
||||
cmd.Env = append(cmd.Env,
|
||||
"CONTAINER_ID="+id,
|
||||
"CONTAINER_NAMESPACE="+ns,
|
||||
|
Loading…
Reference in New Issue
Block a user