forked from fsouza/go-dockerclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_unix_test.go
108 lines (104 loc) · 2.83 KB
/
container_unix_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// +build !windows
// Copyright 2016 go-dockerclient authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package docker
import (
"bufio"
"bytes"
"io/ioutil"
"net"
"net/http"
"os"
"path/filepath"
"testing"
"time"
)
func TestExportContainerViaUnixSocket(t *testing.T) {
t.Parallel()
content := "exported container tar content"
var buf []byte
out := bytes.NewBuffer(buf)
tempSocket := tempfile("export_socket")
defer os.Remove(tempSocket)
endpoint := "unix://" + tempSocket
u, _ := parseEndpoint(endpoint, false)
client := Client{
HTTPClient: defaultClient(),
Dialer: &net.Dialer{},
endpoint: endpoint,
endpointURL: u,
SkipServerVersionCheck: true,
}
listening := make(chan string)
done := make(chan int)
containerID := "4fa6e0f0c678"
go runStreamConnServer(t, "unix", tempSocket, listening, done, containerID)
<-listening // wait for server to start
opts := ExportContainerOptions{ID: containerID, OutputStream: out}
err := client.ExportContainer(opts)
<-done // make sure server stopped
if err != nil {
t.Errorf("ExportContainer: caugh error %#v while exporting container, expected nil", err.Error())
}
if out.String() != content {
t.Errorf("ExportContainer: wrong stdout. Want %#v. Got %#v.", content, out.String())
}
}
func TestStatsTimeoutUnixSocket(t *testing.T) {
t.Parallel()
tmpdir, err := ioutil.TempDir("", "socket")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
socketPath := filepath.Join(tmpdir, "docker_test.sock")
t.Logf("socketPath=%s", socketPath)
l, err := net.Listen("unix", socketPath)
if err != nil {
t.Fatal(err)
}
received := make(chan bool)
defer l.Close()
go func() {
conn, connErr := l.Accept()
if connErr != nil {
t.Logf("Failed to accept connection: %s", connErr)
return
}
breader := bufio.NewReader(conn)
req, connErr := http.ReadRequest(breader)
if connErr != nil {
t.Logf("Failed to read request: %s", connErr)
return
}
if req.URL.Path != "/containers/c/stats" {
t.Logf("Wrong URL path for stats: %q", req.URL.Path)
return
}
received <- true
time.Sleep(2 * time.Second)
}()
client, _ := NewClient("unix://" + socketPath)
client.SkipServerVersionCheck = true
errC := make(chan error, 1)
statsC := make(chan *Stats)
done := make(chan bool)
defer close(done)
go func() {
errC <- client.Stats(StatsOptions{ID: "c", Stats: statsC, Stream: true, Done: done, Timeout: time.Millisecond})
close(errC)
}()
err = <-errC
e, ok := err.(net.Error)
if !ok || !e.Timeout() {
t.Errorf("Failed to receive timeout error, got %#v", err)
}
recvTimeout := 2 * time.Second
select {
case <-received:
return
case <-time.After(recvTimeout):
t.Fatalf("Timeout waiting to receive message after %v", recvTimeout)
}
}