2014-10-17 08:00:59 +08:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2015-02-07 13:12:27 +08:00
|
|
|
"strings"
|
2014-12-18 05:10:41 +08:00
|
|
|
"syscall"
|
2014-10-17 08:00:59 +08:00
|
|
|
|
2014-12-18 05:10:41 +08:00
|
|
|
"github.com/docker/libcontainer"
|
2014-12-17 17:12:23 +08:00
|
|
|
"github.com/docker/libcontainer/configs"
|
2014-10-17 08:00:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func newStdBuffers() *stdBuffers {
|
|
|
|
return &stdBuffers{
|
|
|
|
Stdin: bytes.NewBuffer(nil),
|
|
|
|
Stdout: bytes.NewBuffer(nil),
|
|
|
|
Stderr: bytes.NewBuffer(nil),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stdBuffers struct {
|
|
|
|
Stdin *bytes.Buffer
|
|
|
|
Stdout *bytes.Buffer
|
|
|
|
Stderr *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
2015-02-07 13:12:27 +08:00
|
|
|
func (b *stdBuffers) String() string {
|
|
|
|
s := []string{}
|
|
|
|
if b.Stderr != nil {
|
|
|
|
s = append(s, b.Stderr.String())
|
2014-10-17 08:00:59 +08:00
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
if b.Stdout != nil {
|
|
|
|
s = append(s, b.Stdout.String())
|
2014-10-17 08:00:59 +08:00
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
return strings.Join(s, "|")
|
2014-10-17 08:00:59 +08:00
|
|
|
}
|
|
|
|
|
2015-02-04 09:44:58 +08:00
|
|
|
// newRootfs creates a new tmp directory and copies the busybox root filesystem
|
|
|
|
func newRootfs() (string, error) {
|
2014-10-17 08:00:59 +08:00
|
|
|
dir, err := ioutil.TempDir("", "")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := copyBusybox(dir); err != nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func remove(dir string) {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyBusybox copies the rootfs for a busybox container created for the test image
|
|
|
|
// into the new directory for the specific test
|
|
|
|
func copyBusybox(dest string) error {
|
|
|
|
out, err := exec.Command("sh", "-c", fmt.Sprintf("cp -R /busybox/* %s/", dest)).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("copy error %q: %q", err, out)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-17 05:09:00 +08:00
|
|
|
func newContainer(config *configs.Config) (libcontainer.Container, error) {
|
|
|
|
factory, err := libcontainer.New(".",
|
|
|
|
libcontainer.InitArgs(os.Args[0], "init", "--"),
|
|
|
|
libcontainer.Cgroupfs,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return factory.Create("testCT", config)
|
|
|
|
}
|
|
|
|
|
2014-10-17 08:00:59 +08:00
|
|
|
// runContainer runs the container with the specific config and arguments
|
|
|
|
//
|
|
|
|
// buffers are returned containing the STDOUT and STDERR output for the run
|
|
|
|
// along with the exit code and any go error
|
2014-12-17 17:12:23 +08:00
|
|
|
func runContainer(config *configs.Config, console string, args ...string) (buffers *stdBuffers, exitCode int, err error) {
|
2015-02-17 05:09:00 +08:00
|
|
|
container, err := newContainer(config)
|
|
|
|
if err != nil {
|
2014-10-17 08:00:59 +08:00
|
|
|
return nil, -1, err
|
|
|
|
}
|
2015-02-17 05:09:00 +08:00
|
|
|
defer container.Destroy()
|
2014-10-17 08:00:59 +08:00
|
|
|
buffers = newStdBuffers()
|
2015-02-03 18:53:31 +08:00
|
|
|
process := &libcontainer.Process{
|
2014-12-18 05:10:41 +08:00
|
|
|
Args: args,
|
2015-02-07 11:16:11 +08:00
|
|
|
Env: standardEnvironment,
|
2014-12-18 05:10:41 +08:00
|
|
|
Stdin: buffers.Stdin,
|
|
|
|
Stdout: buffers.Stdout,
|
|
|
|
Stderr: buffers.Stderr,
|
|
|
|
}
|
|
|
|
|
2015-02-03 18:53:31 +08:00
|
|
|
pid, err := container.Start(process)
|
2014-12-18 05:10:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
|
|
|
p, err := os.FindProcess(pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
|
|
|
ps, err := p.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
|
|
|
status := ps.Sys().(syscall.WaitStatus)
|
|
|
|
if status.Exited() {
|
|
|
|
exitCode = status.ExitStatus()
|
|
|
|
} else if status.Signaled() {
|
|
|
|
exitCode = -int(status.Signal())
|
|
|
|
} else {
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
2014-10-17 08:00:59 +08:00
|
|
|
return
|
|
|
|
}
|