github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/integration/container/exec_test.go (about) 1 package container 2 3 import ( 4 "context" 5 "io/ioutil" 6 "testing" 7 8 "github.com/docker/docker/api/types" 9 "github.com/docker/docker/api/types/container" 10 "github.com/docker/docker/api/types/network" 11 "github.com/docker/docker/api/types/strslice" 12 "github.com/docker/docker/integration/util/request" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestExec(t *testing.T) { 17 defer setupTest(t)() 18 ctx := context.Background() 19 client := request.NewAPIClient(t) 20 21 container, err := client.ContainerCreate(ctx, 22 &container.Config{ 23 Image: "busybox", 24 Tty: true, 25 WorkingDir: "/root", 26 Cmd: strslice.StrSlice([]string{"top"}), 27 }, 28 &container.HostConfig{}, 29 &network.NetworkingConfig{}, 30 "foo", 31 ) 32 require.NoError(t, err) 33 err = client.ContainerStart(ctx, container.ID, types.ContainerStartOptions{}) 34 require.NoError(t, err) 35 36 id, err := client.ContainerExecCreate(ctx, container.ID, 37 types.ExecConfig{ 38 WorkingDir: "/tmp", 39 Env: strslice.StrSlice([]string{"FOO=BAR"}), 40 AttachStdout: true, 41 Cmd: strslice.StrSlice([]string{"sh", "-c", "env"}), 42 }, 43 ) 44 require.NoError(t, err) 45 46 resp, err := client.ContainerExecAttach(ctx, id.ID, 47 types.ExecStartCheck{ 48 Detach: false, 49 Tty: false, 50 }, 51 ) 52 require.NoError(t, err) 53 defer resp.Close() 54 r, err := ioutil.ReadAll(resp.Reader) 55 require.NoError(t, err) 56 out := string(r) 57 require.NoError(t, err) 58 require.Contains(t, out, "PWD=/tmp", "exec command not running in expected /tmp working directory") 59 require.Contains(t, out, "FOO=BAR", "exec command not running with expected environment variable FOO") 60 }