mirror of https://github.com/docker/docs.git
Fix TestBuildCancelationKillsSleep to not fail on Windows
Signed-off-by: Antonio Murdaca <me@runcom.ninja>
This commit is contained in:
parent
6fb8bd0450
commit
d38c90140b
|
@ -1963,8 +1963,8 @@ func TestBuildForceRm(t *testing.T) {
|
||||||
// * When docker events sees container start, close the "docker build" command
|
// * When docker events sees container start, close the "docker build" command
|
||||||
// * Wait for docker events to emit a dying event.
|
// * Wait for docker events to emit a dying event.
|
||||||
func TestBuildCancelationKillsSleep(t *testing.T) {
|
func TestBuildCancelationKillsSleep(t *testing.T) {
|
||||||
// TODO(jfrazelle): Make this work on Windows.
|
var wg sync.WaitGroup
|
||||||
testRequires(t, SameHostDaemon)
|
defer wg.Wait()
|
||||||
|
|
||||||
name := "testbuildcancelation"
|
name := "testbuildcancelation"
|
||||||
defer deleteImages(name)
|
defer deleteImages(name)
|
||||||
|
@ -1977,26 +1977,23 @@ func TestBuildCancelationKillsSleep(t *testing.T) {
|
||||||
}
|
}
|
||||||
defer ctx.Close()
|
defer ctx.Close()
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
defer wg.Wait()
|
|
||||||
|
|
||||||
finish := make(chan struct{})
|
finish := make(chan struct{})
|
||||||
defer close(finish)
|
defer close(finish)
|
||||||
|
|
||||||
eventStart := make(chan struct{})
|
eventStart := make(chan struct{})
|
||||||
eventDie := make(chan struct{})
|
eventDie := make(chan struct{})
|
||||||
|
containerID := make(chan string)
|
||||||
|
|
||||||
// Start one second ago, to avoid rounding problems
|
startEpoch := daemonTime(t).Unix()
|
||||||
startEpoch := time.Now().Add(-1 * time.Second)
|
|
||||||
|
|
||||||
// Goroutine responsible for watching start/die events from `docker events`
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
// Goroutine responsible for watching start/die events from `docker events`
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
// Watch for events since epoch.
|
// Watch for events since epoch.
|
||||||
eventsCmd := exec.Command(dockerBinary, "events",
|
eventsCmd := exec.Command(
|
||||||
"-since", fmt.Sprint(startEpoch.Unix()))
|
dockerBinary, "events",
|
||||||
|
"--since", strconv.FormatInt(startEpoch, 10))
|
||||||
stdout, err := eventsCmd.StdoutPipe()
|
stdout, err := eventsCmd.StdoutPipe()
|
||||||
err = eventsCmd.Start()
|
err = eventsCmd.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -2008,36 +2005,21 @@ func TestBuildCancelationKillsSleep(t *testing.T) {
|
||||||
eventsCmd.Process.Kill()
|
eventsCmd.Process.Kill()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var started, died bool
|
cid := <-containerID
|
||||||
var imageID string
|
|
||||||
|
|
||||||
if out, err := exec.Command(dockerBinary, "inspect", "-f", "{{.Id}}", "busybox").CombinedOutput(); err != nil {
|
matchStart := regexp.MustCompile(cid + `(.*) start$`)
|
||||||
t.Fatalf("failed to get the image ID of busybox: %s, %v", out, err)
|
matchDie := regexp.MustCompile(cid + `(.*) die$`)
|
||||||
} else {
|
|
||||||
imageID = strings.TrimSpace(string(out))
|
|
||||||
}
|
|
||||||
|
|
||||||
matchStart := regexp.MustCompile(" \\(from " + imageID + "\\) start$")
|
|
||||||
matchDie := regexp.MustCompile(" \\(from " + imageID + "\\) die$")
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Read lines of `docker events` looking for container start and stop.
|
// Read lines of `docker events` looking for container start and stop.
|
||||||
//
|
//
|
||||||
scanner := bufio.NewScanner(stdout)
|
scanner := bufio.NewScanner(stdout)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
if ok := matchStart.MatchString(scanner.Text()); ok {
|
switch {
|
||||||
if started {
|
case matchStart.MatchString(scanner.Text()):
|
||||||
t.Fatal("assertion fail: more than one container started")
|
|
||||||
}
|
|
||||||
close(eventStart)
|
close(eventStart)
|
||||||
started = true
|
case matchDie.MatchString(scanner.Text()):
|
||||||
}
|
|
||||||
if ok := matchDie.MatchString(scanner.Text()); ok {
|
|
||||||
if died {
|
|
||||||
t.Fatal("assertion fail: more than one container died")
|
|
||||||
}
|
|
||||||
close(eventDie)
|
close(eventDie)
|
||||||
died = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2050,13 +2032,24 @@ func TestBuildCancelationKillsSleep(t *testing.T) {
|
||||||
buildCmd := exec.Command(dockerBinary, "build", "-t", name, ".")
|
buildCmd := exec.Command(dockerBinary, "build", "-t", name, ".")
|
||||||
buildCmd.Dir = ctx.Dir
|
buildCmd.Dir = ctx.Dir
|
||||||
|
|
||||||
|
stdoutBuild, err := buildCmd.StdoutPipe()
|
||||||
err = buildCmd.Start()
|
err = buildCmd.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to run build: %s", err)
|
t.Fatalf("failed to run build: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
matchCID := regexp.MustCompile("Running in ")
|
||||||
|
scanner := bufio.NewScanner(stdoutBuild)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if ok := matchCID.MatchString(line); ok {
|
||||||
|
containerID <- line[len(line)-12:]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-time.After(30 * time.Second):
|
case <-time.After(5 * time.Second):
|
||||||
t.Fatal("failed to observe build container start in timely fashion")
|
t.Fatal("failed to observe build container start in timely fashion")
|
||||||
case <-eventStart:
|
case <-eventStart:
|
||||||
// Proceeds from here when we see the container fly past in the
|
// Proceeds from here when we see the container fly past in the
|
||||||
|
@ -2078,7 +2071,7 @@ func TestBuildCancelationKillsSleep(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-time.After(30 * time.Second):
|
case <-time.After(5 * time.Second):
|
||||||
// If we don't get here in a timely fashion, it wasn't killed.
|
// If we don't get here in a timely fashion, it wasn't killed.
|
||||||
t.Fatal("container cancel did not succeed")
|
t.Fatal("container cancel did not succeed")
|
||||||
case <-eventDie:
|
case <-eventDie:
|
||||||
|
|
|
@ -44,12 +44,14 @@ func processExitCode(err error) (exitCode int) {
|
||||||
|
|
||||||
func IsKilled(err error) bool {
|
func IsKilled(err error) bool {
|
||||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||||
sys := exitErr.ProcessState.Sys()
|
status, ok := exitErr.Sys().(syscall.WaitStatus)
|
||||||
status, ok := sys.(syscall.WaitStatus)
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return status.Signaled() && status.Signal() == os.Kill
|
// status.ExitStatus() is required on Windows because it does not
|
||||||
|
// implement Signal() nor Signaled(). Just check it had a bad exit
|
||||||
|
// status could mean it was killed (and in tests we do kill)
|
||||||
|
return (status.Signaled() && status.Signal() == os.Kill) || status.ExitStatus() != 0
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue