Skip to content

Instantly share code, notes, and snippets.

@dqminh
Created April 10, 2015 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dqminh/a5c4855a0c773ff72231 to your computer and use it in GitHub Desktop.
Save dqminh/a5c4855a0c773ff72231 to your computer and use it in GitHub Desktop.
+
+func TestExecinPassExtraFiles(t *testing.T) {
+ if testing.Short() {
+ return
+ }
+ rootfs, err := newRootfs()
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer remove(rootfs)
+ config := newTemplateConfig(rootfs)
+ container, err := newContainer(config)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer container.Destroy()
+
+ // Execute a first process in the container
+ stdinR, stdinW, err := os.Pipe()
+ if err != nil {
+ t.Fatal(err)
+ }
+ process := &libcontainer.Process{
+ Args: []string{"cat"},
+ Env: standardEnvironment,
+ Stdin: stdinR,
+ }
+ err = container.Start(process)
+ stdinR.Close()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var stdout bytes.Buffer
+ pipeout1, pipein1, err := os.Pipe()
+ pipeout2, pipein2, err := os.Pipe()
+ inprocess := &libcontainer.Process{
+ Args: []string{"sh", "-c", "cd /proc/$$/fd; echo -n *; echo -n 1 >3; echo -n 2 >4"},
+ Env: []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"},
+ ExtraFiles: []*os.File{pipein1, pipein2},
+ Stdin: nil,
+ Stdout: &stdout,
+ }
+ err = container.Start(inprocess)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ waitProcess(inprocess, t)
+ stdinW.Close()
+ waitProcess(process, t)
+
+ out := string(stdout.Bytes())
+ // fd 5 is the directory handle for /proc/$$/fd
+ if out != "0 1 2 3 4 5" {
+ t.Fatalf("expected to have the file descriptors '0 1 2 3 4 5' passed to exec, got '%s'", out)
+ }
+
+ var buf = []byte{0}
+ _, err = pipeout1.Read(buf)
+ if err != nil {
+ t.Fatal(err)
+ }
+ out1 := string(buf)
+ if out1 != "1" {
+ t.Fatalf("expected first pipe to receive '1', got '%s'", out1)
+ }
+
+ _, err = pipeout2.Read(buf)
+ if err != nil {
+ t.Fatal(err)
+ }
+ out2 := string(buf)
+ if out2 != "2" {
+ t.Fatalf("expected second pipe to receive '2', got '%s'", out2)
+ }
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment