Browse Source

docker_api_attach_test: fix WS test to use DOCKER_TEST_HOST if specified

TestGetContainersAttachWebsocket is currently broken on Windows CI tests
b/c it has hardcoded unix://var/run/docker.sock.  This change makes use
of @icecrime's code in docker_utils and generalizes it with sockConn()
to provide a net.Conn by making use of DOCKER_TEST_HOST. Also fixes
the test.

Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>
Ahmet Alp Balkan 10 năm trước cách đây
mục cha
commit
ac6cb41d52
2 tập tin đã thay đổi với 20 bổ sung16 xóa
  1. 2 2
      integration-cli/docker_api_attach_test.go
  2. 18 14
      integration-cli/docker_utils.go

+ 2 - 2
integration-cli/docker_api_attach_test.go

@@ -2,9 +2,9 @@ package main
 
 import (
 	"bytes"
-	"net"
 	"os/exec"
 	"testing"
+	"time"
 
 	"code.google.com/p/go.net/websocket"
 )
@@ -17,7 +17,7 @@ func TestGetContainersAttachWebsocket(t *testing.T) {
 	}
 	defer deleteAllContainers()
 
-	rwc, err := net.Dial("unix", "/var/run/docker.sock")
+	rwc, err := sockConn(time.Duration(10 * time.Second))
 	if err != nil {
 		t.Fatal(err)
 	}

+ 18 - 14
integration-cli/docker_utils.go

@@ -273,16 +273,7 @@ func daemonHost() string {
 	return daemonUrlStr
 }
 
-func sockRequest(method, endpoint string, data interface{}) ([]byte, error) {
-	jsonData := bytes.NewBuffer(nil)
-	if err := json.NewEncoder(jsonData).Encode(data); err != nil {
-		return nil, err
-	}
-
-	return sockRequestRaw(method, endpoint, jsonData, "application/json")
-}
-
-func sockRequestRaw(method, endpoint string, data io.Reader, ct string) ([]byte, error) {
+func sockConn(timeout time.Duration) (net.Conn, error) {
 	daemon := daemonHost()
 	daemonUrl, err := url.Parse(daemon)
 	if err != nil {
@@ -292,14 +283,27 @@ func sockRequestRaw(method, endpoint string, data io.Reader, ct string) ([]byte,
 	var c net.Conn
 	switch daemonUrl.Scheme {
 	case "unix":
-		c, err = net.DialTimeout(daemonUrl.Scheme, daemonUrl.Path, time.Duration(10*time.Second))
+		return net.DialTimeout(daemonUrl.Scheme, daemonUrl.Path, timeout)
 	case "tcp":
-		c, err = net.DialTimeout(daemonUrl.Scheme, daemonUrl.Host, time.Duration(10*time.Second))
+		return net.DialTimeout(daemonUrl.Scheme, daemonUrl.Host, timeout)
 	default:
-		err = fmt.Errorf("unknown scheme %v", daemonUrl.Scheme)
+		return c, fmt.Errorf("unknown scheme %v (%s)", daemonUrl.Scheme, daemon)
+	}
+}
+
+func sockRequest(method, endpoint string, data interface{}) ([]byte, error) {
+	jsonData := bytes.NewBuffer(nil)
+	if err := json.NewEncoder(jsonData).Encode(data); err != nil {
+		return nil, err
 	}
+
+	return sockRequestRaw(method, endpoint, jsonData, "application/json")
+}
+
+func sockRequestRaw(method, endpoint string, data io.Reader, ct string) ([]byte, error) {
+	c, err := sockConn(time.Duration(10 * time.Second))
 	if err != nil {
-		return nil, fmt.Errorf("could not dial docker daemon at %s: %v", daemon, err)
+		return nil, fmt.Errorf("could not dial docker daemon: %v", err)
 	}
 
 	client := httputil.NewClientConn(c, nil)