forked from microsoft/hcsshim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge_test.go
More file actions
206 lines (186 loc) · 4.92 KB
/
bridge_test.go
File metadata and controls
206 lines (186 loc) · 4.92 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//go:build windows
package gcs
import (
"context"
"encoding/binary"
"encoding/json"
"errors"
"io"
"reflect"
"strings"
"testing"
"time"
"github.com/Microsoft/hcsshim/internal/gcs/prot"
"github.com/sirupsen/logrus"
)
type stitched struct {
io.ReadCloser
io.WriteCloser
}
func (s *stitched) Close() error {
s.ReadCloser.Close()
s.WriteCloser.Close()
return nil
}
func pipeConn() (*stitched, *stitched) {
r1, w1 := io.Pipe()
r2, w2 := io.Pipe()
return &stitched{r1, w2}, &stitched{r2, w1}
}
func sendMessage(t *testing.T, w io.Writer, typ prot.MsgType, id int64, msg []byte) {
t.Helper()
var h [16]byte
binary.LittleEndian.PutUint32(h[:], uint32(typ))
binary.LittleEndian.PutUint32(h[4:], uint32(len(msg)+16))
binary.LittleEndian.PutUint64(h[8:], uint64(id))
_, err := w.Write(h[:])
if err != nil {
t.Error(err)
return
}
_, err = w.Write(msg)
if err != nil {
t.Error(err)
return
}
}
func reflector(t *testing.T, rw io.ReadWriteCloser, delay time.Duration) {
t.Helper()
defer rw.Close()
for {
id, typ, msg, err := readMessage(rw)
if err != nil {
if !errors.Is(err, io.EOF) {
t.Error(err)
}
return
}
time.Sleep(delay) // delay is used to test timeouts (when non-zero)
typ ^= prot.MsgTypeResponse ^ prot.MsgTypeRequest
sendMessage(t, rw, typ, id, msg)
}
}
type testReq struct {
prot.RequestBase
X, Y int
}
type testResp struct {
prot.ResponseBase
X, Y int
}
func startReflectedBridge(t *testing.T, delay time.Duration) *bridge {
t.Helper()
s, c := pipeConn()
b := newBridge(s, nil, logrus.NewEntry(logrus.StandardLogger()))
b.Start()
go reflector(t, c, delay)
return b
}
func TestBridgeRPC(t *testing.T) {
b := startReflectedBridge(t, 0)
defer b.Close()
req := testReq{X: 5}
var resp testResp
err := b.RPC(context.Background(), prot.RPCCreate, &req, &resp, false)
if err != nil {
t.Fatal(err)
}
if req.X != resp.X || req.Y != resp.Y {
t.Fatalf("expected equal: %+v %+v", req, resp)
}
}
func TestBridgeRPCResponseTimeout(t *testing.T) {
b := startReflectedBridge(t, time.Minute)
defer b.Close()
b.Timeout = time.Millisecond * 100
req := testReq{X: 5}
var resp testResp
err := b.RPC(context.Background(), prot.RPCCreate, &req, &resp, false)
if err == nil || !strings.Contains(err.Error(), "bridge closed") {
t.Fatalf("expected bridge disconnection, got %s", err)
}
}
func TestBridgeRPCContextDone(t *testing.T) {
b := startReflectedBridge(t, time.Minute)
defer b.Close()
b.Timeout = time.Millisecond * 250
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
defer cancel()
req := testReq{X: 5}
var resp testResp
err := b.RPC(ctx, prot.RPCCreate, &req, &resp, true)
if err != context.DeadlineExceeded { //nolint:errorlint
t.Fatalf("expected deadline exceeded, got %s", err)
}
}
func TestBridgeRPCContextDoneNoCancel(t *testing.T) {
b := startReflectedBridge(t, time.Minute)
defer b.Close()
b.Timeout = time.Millisecond * 250
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
defer cancel()
req := testReq{X: 5}
var resp testResp
err := b.RPC(ctx, prot.RPCCreate, &req, &resp, false)
if err == nil || !strings.Contains(err.Error(), "bridge closed") {
t.Fatalf("expected bridge disconnection, got %s", err)
}
}
func TestBridgeRPCBridgeClosed(t *testing.T) {
b := startReflectedBridge(t, 0)
eerr := errors.New("forcibly terminated")
b.kill(eerr)
err := b.RPC(context.Background(), prot.RPCCreate, nil, nil, false)
if err != eerr { //nolint:errorlint
t.Fatal("unexpected: ", err)
}
}
func sendJSON(t *testing.T, w io.Writer, typ prot.MsgType, id int64, msg interface{}) error {
t.Helper()
msgb, err := json.Marshal(msg)
if err != nil {
return err
}
sendMessage(t, w, typ, id, msgb)
return nil
}
func notifyThroughBridge(t *testing.T, typ prot.MsgType, msg interface{}, fn notifyFunc) error {
t.Helper()
s, c := pipeConn()
b := newBridge(s, fn, logrus.NewEntry(logrus.StandardLogger()))
b.Start()
err := sendJSON(t, c, typ, 0, msg)
if err != nil {
b.Close()
return err
}
time.Sleep(100 * time.Millisecond)
return b.Close()
}
func TestBridgeNotify(t *testing.T) {
ntf := &prot.ContainerNotification{Operation: "testing"}
recvd := false
err := notifyThroughBridge(t, prot.MsgTypeNotify|prot.ComputeSystem|prot.NotifyContainer, ntf, func(nntf *prot.ContainerNotification) error {
if !reflect.DeepEqual(ntf, nntf) {
t.Errorf("%+v != %+v", ntf, nntf)
}
recvd = true
return nil
})
if err != nil {
t.Error("notify failed: ", err)
}
if !recvd {
t.Error("did not receive notification")
}
}
func TestBridgeNotifyFailure(t *testing.T) {
ntf := &prot.ContainerNotification{Operation: "testing"}
errMsg := "notify should have failed"
err := notifyThroughBridge(t, prot.MsgTypeNotify|prot.ComputeSystem|prot.NotifyContainer, ntf, func(nntf *prot.ContainerNotification) error {
return errors.New(errMsg)
})
if err == nil || !strings.Contains(err.Error(), errMsg) {
t.Error("unexpected result: ", err)
}
}