2018-02-05 16:05:59 -05:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
2017-12-03 04:56:50 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/strslice"
|
2018-02-12 18:08:25 -05:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2018-02-09 13:13:26 -05:00
|
|
|
"github.com/docker/docker/integration/internal/request"
|
2018-03-13 15:28:34 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-12-03 04:56:50 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExec(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
|
2018-02-13 10:45:40 -05:00
|
|
|
cID := container.Run(t, ctx, client, container.WithTty(true), container.WithWorkingDir("/root"))
|
2017-12-03 04:56:50 -05:00
|
|
|
|
2018-02-12 18:08:25 -05:00
|
|
|
id, err := client.ContainerExecCreate(ctx, cID,
|
2017-12-03 04:56:50 -05:00
|
|
|
types.ExecConfig{
|
|
|
|
WorkingDir: "/tmp",
|
|
|
|
Env: strslice.StrSlice([]string{"FOO=BAR"}),
|
|
|
|
AttachStdout: true,
|
|
|
|
Cmd: strslice.StrSlice([]string{"sh", "-c", "env"}),
|
|
|
|
},
|
|
|
|
)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-12-03 04:56:50 -05:00
|
|
|
|
|
|
|
resp, err := client.ContainerExecAttach(ctx, id.ID,
|
|
|
|
types.ExecStartCheck{
|
|
|
|
Detach: false,
|
|
|
|
Tty: false,
|
|
|
|
},
|
|
|
|
)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-12-03 04:56:50 -05:00
|
|
|
defer resp.Close()
|
|
|
|
r, err := ioutil.ReadAll(resp.Reader)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-12-03 04:56:50 -05:00
|
|
|
out := string(r)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, is.Contains(out, "PWD=/tmp"), "exec command not running in expected /tmp working directory")
|
|
|
|
assert.Assert(t, is.Contains(out, "FOO=BAR"), "exec command not running with expected environment variable FOO")
|
2017-12-03 04:56:50 -05:00
|
|
|
}
|