Bladeren bron

Merge pull request #21196 from LK4D4/detect_leaks_util

integration-cli: move goroutines info helpers to separate funcs
Sebastiaan van Stijn 9 jaren geleden
bovenliggende
commit
daabb45d0a
3 gewijzigde bestanden met toevoegingen van 51 en 84 verwijderingen
  1. 6 57
      integration-cli/docker_cli_logs_test.go
  2. 3 27
      integration-cli/docker_cli_run_test.go
  3. 42 0
      integration-cli/docker_utils.go

+ 6 - 57
integration-cli/docker_cli_logs_test.go

@@ -1,7 +1,6 @@
 package main
 
 import (
-	"encoding/json"
 	"fmt"
 	"io"
 	"os/exec"
@@ -265,20 +264,8 @@ func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
 	id := strings.TrimSpace(out)
 	c.Assert(waitRun(id), checker.IsNil)
 
-	type info struct {
-		NGoroutines int
-	}
-	getNGoroutines := func() int {
-		var i info
-		status, b, err := sockRequest("GET", "/info", nil)
-		c.Assert(err, checker.IsNil)
-		c.Assert(status, checker.Equals, 200)
-		c.Assert(json.Unmarshal(b, &i), checker.IsNil)
-		return i.NGoroutines
-	}
-
-	nroutines := getNGoroutines()
-
+	nroutines, err := getGoroutineNumber()
+	c.Assert(err, checker.IsNil)
 	cmd := exec.Command(dockerBinary, "logs", "-f", id)
 	r, w := io.Pipe()
 	cmd.Stdout = w
@@ -295,20 +282,7 @@ func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
 	c.Assert(cmd.Process.Kill(), checker.IsNil)
 
 	// NGoroutines is not updated right away, so we need to wait before failing
-	t := time.After(30 * time.Second)
-	for {
-		select {
-		case <-t:
-			n := getNGoroutines()
-			c.Assert(n <= nroutines, checker.Equals, true, check.Commentf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n))
-
-		default:
-			if n := getNGoroutines(); n <= nroutines {
-				return
-			}
-			time.Sleep(200 * time.Millisecond)
-		}
-	}
+	c.Assert(waitForGoroutines(nroutines), checker.IsNil)
 }
 
 func (s *DockerSuite) TestLogsFollowGoroutinesNoOutput(c *check.C) {
@@ -316,40 +290,15 @@ func (s *DockerSuite) TestLogsFollowGoroutinesNoOutput(c *check.C) {
 	id := strings.TrimSpace(out)
 	c.Assert(waitRun(id), checker.IsNil)
 
-	type info struct {
-		NGoroutines int
-	}
-	getNGoroutines := func() int {
-		var i info
-		status, b, err := sockRequest("GET", "/info", nil)
-		c.Assert(err, checker.IsNil)
-		c.Assert(status, checker.Equals, 200)
-		c.Assert(json.Unmarshal(b, &i), checker.IsNil)
-		return i.NGoroutines
-	}
-
-	nroutines := getNGoroutines()
-
+	nroutines, err := getGoroutineNumber()
+	c.Assert(err, checker.IsNil)
 	cmd := exec.Command(dockerBinary, "logs", "-f", id)
 	c.Assert(cmd.Start(), checker.IsNil)
 	time.Sleep(200 * time.Millisecond)
 	c.Assert(cmd.Process.Kill(), checker.IsNil)
 
 	// NGoroutines is not updated right away, so we need to wait before failing
-	t := time.After(30 * time.Second)
-	for {
-		select {
-		case <-t:
-			n := getNGoroutines()
-			c.Assert(n <= nroutines, checker.Equals, true, check.Commentf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n))
-
-		default:
-			if n := getNGoroutines(); n <= nroutines {
-				return
-			}
-			time.Sleep(200 * time.Millisecond)
-		}
-	}
+	c.Assert(waitForGoroutines(nroutines), checker.IsNil)
 }
 
 func (s *DockerSuite) TestLogsCLIContainerNotFound(c *check.C) {

+ 3 - 27
integration-cli/docker_cli_run_test.go

@@ -3,7 +3,6 @@ package main
 import (
 	"bufio"
 	"bytes"
-	"encoding/json"
 	"fmt"
 	"io/ioutil"
 	"net"
@@ -4219,18 +4218,8 @@ func (s *DockerSuite) TestRunNamedVolumesFromNotRemoved(c *check.C) {
 }
 
 func (s *DockerSuite) TestRunAttachFailedNoLeak(c *check.C) {
-	type info struct {
-		NGoroutines int
-	}
-	getNGoroutines := func() int {
-		var i info
-		status, b, err := sockRequest("GET", "/info", nil)
-		c.Assert(err, checker.IsNil)
-		c.Assert(status, checker.Equals, 200)
-		c.Assert(json.Unmarshal(b, &i), checker.IsNil)
-		return i.NGoroutines
-	}
-	nroutines := getNGoroutines()
+	nroutines, err := getGoroutineNumber()
+	c.Assert(err, checker.IsNil)
 
 	runSleepingContainer(c, "--name=test", "-p", "8000:8000")
 
@@ -4241,18 +4230,5 @@ func (s *DockerSuite) TestRunAttachFailedNoLeak(c *check.C) {
 	dockerCmd(c, "rm", "-f", "test")
 
 	// NGoroutines is not updated right away, so we need to wait before failing
-	t := time.After(30 * time.Second)
-	for {
-		select {
-		case <-t:
-			n := getNGoroutines()
-			c.Assert(n <= nroutines, checker.Equals, true, check.Commentf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n))
-
-		default:
-			if n := getNGoroutines(); n <= nroutines {
-				return
-			}
-			time.Sleep(200 * time.Millisecond)
-		}
-	}
+	c.Assert(waitForGoroutines(nroutines), checker.IsNil)
 }

+ 42 - 0
integration-cli/docker_utils.go

@@ -1435,3 +1435,45 @@ func minimalBaseImage() string {
 	}
 	return "scratch"
 }
+
+func getGoroutineNumber() (int, error) {
+	i := struct {
+		NGoroutines int
+	}{}
+	status, b, err := sockRequest("GET", "/info", nil)
+	if err != nil {
+		return 0, err
+	}
+	if status != http.StatusOK {
+		return 0, fmt.Errorf("http status code: %d", status)
+	}
+	if err := json.Unmarshal(b, &i); err != nil {
+		return 0, err
+	}
+	return i.NGoroutines, nil
+}
+
+func waitForGoroutines(expected int) error {
+	t := time.After(30 * time.Second)
+	for {
+		select {
+		case <-t:
+			n, err := getGoroutineNumber()
+			if err != nil {
+				return err
+			}
+			if n > expected {
+				return fmt.Errorf("leaked goroutines: expected less than or equal to %d, got: %d", expected, n)
+			}
+		default:
+			n, err := getGoroutineNumber()
+			if err != nil {
+				return err
+			}
+			if n <= expected {
+				return nil
+			}
+			time.Sleep(200 * time.Millisecond)
+		}
+	}
+}