generated from sensu/check-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnet_unix_test.go
58 lines (53 loc) · 955 Bytes
/
net_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
// +build unix
package main
import (
"bytes"
"io/ioutil"
"net"
"net/url"
"os"
"testing"
)
func TestReadSocket(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
_ = tmpfile.Close()
if err := os.RemoveAll(tmpfile.Name()); err != nil {
t.Fatal(err)
}
sock, err := net.Listen("unix", tmpfile.Name())
if err != nil {
t.Fatal(err)
}
defer sock.Close()
go func() {
conn, err := sock.Accept()
if err != nil {
panic(err)
}
defer conn.Close()
buf := make([]byte, 10)
if _, err := conn.Read(buf); err != nil {
panic(err)
}
if string(buf) != "show stat\n" {
panic(string(buf))
}
if _, err := conn.Write(testDataCSV); err != nil {
panic(err)
}
}()
url, err := url.Parse(sock.Addr().String())
if err != nil {
t.Fatal(err)
}
data, err := readUnix(url)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data.data, testDataCSV) {
t.Error("unexpected data")
}
}