docker/contrib/crashTest.go

127 строки
2.4 KiB
Go
Исходник Обычный вид История

2013-04-15 02:13:32 +04:00
package main
import (
2013-04-28 14:54:22 +04:00
"fmt"
2013-04-15 02:13:32 +04:00
"io"
"log"
2013-04-28 14:54:22 +04:00
"net"
2013-04-15 02:13:32 +04:00
"os"
"os/exec"
"path"
2013-04-15 02:13:32 +04:00
"time"
)
var DOCKER_PATH string = path.Join(os.Getenv("DOCKERPATH"), "docker")
2013-04-15 02:13:32 +04:00
2013-04-28 17:23:02 +04:00
// WARNING: this crashTest will 1) crash your host, 2) remove all containers
2013-04-15 02:13:32 +04:00
func runDaemon() (*exec.Cmd, error) {
2013-04-17 02:20:04 +04:00
os.Remove("/var/run/docker.pid")
2013-04-28 18:10:58 +04:00
exec.Command("rm", "-rf", "/var/lib/docker/containers").Run()
2013-04-15 02:13:32 +04:00
cmd := exec.Command(DOCKER_PATH, "-d")
outPipe, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
errPipe, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
if err := cmd.Start(); err != nil {
return nil, err
}
go func() {
io.Copy(os.Stdout, outPipe)
}()
go func() {
io.Copy(os.Stderr, errPipe)
}()
return cmd, nil
}
func crashTest() error {
if err := exec.Command("/bin/bash", "-c", "while true; do true; done").Start(); err != nil {
return err
}
2013-04-28 14:54:22 +04:00
var endpoint string
if ep := os.Getenv("TEST_ENDPOINT"); ep == "" {
endpoint = "192.168.56.1:7979"
} else {
endpoint = ep
}
2013-04-28 17:23:02 +04:00
c := make(chan bool)
var conn io.Writer
go func() {
conn, _ = net.Dial("tcp", endpoint)
c <- false
}()
go func() {
time.Sleep(2 * time.Second)
c <- true
}()
<-c
2013-04-28 14:54:22 +04:00
restartCount := 0
totalTestCount := 1
2013-04-15 02:13:32 +04:00
for {
daemon, err := runDaemon()
if err != nil {
return err
}
2013-04-28 14:54:22 +04:00
restartCount++
2013-04-17 02:20:04 +04:00
// time.Sleep(5000 * time.Millisecond)
var stop bool
2013-04-15 02:13:32 +04:00
go func() error {
2013-04-17 02:20:04 +04:00
stop = false
2013-04-28 17:23:02 +04:00
for i := 0; i < 100 && !stop; {
2013-04-17 02:20:04 +04:00
func() error {
2013-04-30 22:16:26 +04:00
cmd := exec.Command(DOCKER_PATH, "run", "base", "echo", fmt.Sprintf("%d", totalTestCount))
i++
totalTestCount++
2013-04-15 02:13:32 +04:00
outPipe, err := cmd.StdoutPipe()
if err != nil {
return err
}
2013-04-30 22:16:26 +04:00
inPipe, err := cmd.StdinPipe()
if err != nil {
return err
}
2013-04-15 02:13:32 +04:00
if err := cmd.Start(); err != nil {
return err
}
2013-04-30 22:16:26 +04:00
if conn != nil {
go io.Copy(conn, outPipe)
}
2013-04-15 02:13:32 +04:00
// Expecting error, do not check
2013-04-30 22:16:26 +04:00
inPipe.Write([]byte("hello world!!!!!\n"))
go inPipe.Write([]byte("hello world!!!!!\n"))
go inPipe.Write([]byte("hello world!!!!!\n"))
inPipe.Close()
2013-04-15 02:13:32 +04:00
if err := cmd.Wait(); err != nil {
return err
}
outPipe.Close()
return nil
}()
}
return nil
}()
time.Sleep(20 * time.Second)
2013-04-17 02:20:04 +04:00
stop = true
2013-04-15 02:13:32 +04:00
if err := daemon.Process.Kill(); err != nil {
return err
}
}
return nil
}
func main() {
if err := crashTest(); err != nil {
log.Println(err)
}
}