Skip to content

Commit

Permalink
add WorkingDir option for exec
Browse files Browse the repository at this point in the history
  • Loading branch information
fayep committed May 1, 2018
1 parent 3a20603 commit 2f3f8d8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var (
apiVersion119, _ = NewAPIVersion("1.19")
apiVersion124, _ = NewAPIVersion("1.24")
apiVersion125, _ = NewAPIVersion("1.25")
apiVersion135, _ = NewAPIVersion("1.35")
)

// APIVersion is an internal representation of a version of the Remote API.
Expand Down
4 changes: 4 additions & 0 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type CreateExecOptions struct {
Cmd []string `json:"Cmd,omitempty" yaml:"Cmd,omitempty" toml:"Cmd,omitempty"`
Container string `json:"Container,omitempty" yaml:"Container,omitempty" toml:"Container,omitempty"`
User string `json:"User,omitempty" yaml:"User,omitempty" toml:"User,omitempty"`
WorkingDir string `json:"WorkingDir,omitempty" yaml:"WorkingDir,omitempty" toml:"WorkingDir,omitempty"`
Context context.Context `json:"-"`
Privileged bool `json:"Privileged,omitempty" yaml:"Privileged,omitempty" toml:"Privileged,omitempty"`
}
Expand All @@ -45,6 +46,9 @@ func (c *Client) CreateExec(opts CreateExecOptions) (*Exec, error) {
if len(opts.Env) > 0 && c.serverAPIVersion.LessThan(apiVersion125) {
return nil, errors.New("exec configuration Env is only supported in API#1.25 and above")
}
if len(opts.WorkingDir) > 0 && c.serverAPIVersion.LessThan(apiVersion135) {
return nil, errors.New("exec configuration WorkingDir is only supported in API#1.35 and above")
}
path := fmt.Sprintf("/containers/%s/exec", opts.Container)
resp, err := c.do("POST", path, doOptions{data: opts, context: opts.Context})
if err != nil {
Expand Down
62 changes: 62 additions & 0 deletions exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,68 @@ func TestExecCreateWithEnv(t *testing.T) {
}
}

func TestExecCreateWithWorkingDirErr(t *testing.T) {
t.Parallel()
jsonContainer := `{"Id": "4fa6e0f0c6786287e131c3852c58a2e01cc697a68231826813597e4994f1d6e2"}`
var expected struct{ ID string }
err := json.Unmarshal([]byte(jsonContainer), &expected)
if err != nil {
t.Fatal(err)
}
fakeRT := &FakeRoundTripper{message: jsonContainer, status: http.StatusOK}
client := newTestClient(fakeRT)
config := CreateExecOptions{
Container: "test",
AttachStdin: true,
AttachStdout: true,
AttachStderr: false,
Tty: false,
WorkingDir: "/tmp",
Cmd: []string{"touch", "file"},
User: "a-user",
}
_, err = client.CreateExec(config)
if err == nil || err.Error() != "exec configuration WorkingDir is only supported in API#1.35 and above" {
t.Error("CreateExec: options contain WorkingDir for unsupported api version")
}
}

func TestExecCreateWithWorkingDir(t *testing.T) {
t.Parallel()
jsonContainer := `{"Id": "4fa6e0f0c6786287e131c3852c58a2e01cc697a68231826813597e4994f1d6e2"}`
var expected struct{ ID string }
err := json.Unmarshal([]byte(jsonContainer), &expected)
if err != nil {
t.Fatal(err)
}
fakeRT := &FakeRoundTripper{message: jsonContainer, status: http.StatusOK}
endpoint := "http://localhost:4243"
u, _ := parseEndpoint("http://localhost:4243", false)
testAPIVersion, _ := NewAPIVersion("1.35")
client := Client{
HTTPClient: &http.Client{Transport: fakeRT},
Dialer: &net.Dialer{},
endpoint: endpoint,
endpointURL: u,
SkipServerVersionCheck: true,
serverAPIVersion: testAPIVersion,
}
config := CreateExecOptions{
Container: "test",
AttachStdin: true,
AttachStdout: true,
AttachStderr: false,
Tty: false,
WorkingDir: "/tmp",
Cmd: []string{"touch", "file"},
User: "a-user",
}
_, err = client.CreateExec(config)
if err != nil {
t.Error(err)
}
}

func TestExecStartDetached(t *testing.T) {
t.Parallel()
execID := "4fa6e0f0c6786287e131c3852c58a2e01cc697a68231826813597e4994f1d6e2"
Expand Down

0 comments on commit 2f3f8d8

Please sign in to comment.