Skip to content
This repository was archived by the owner on May 6, 2020. It is now read-only.

Commit d0fb4bf

Browse files
author
Sebastien Boeuf
committed
mock: Extend proxy mock to be used by virtcontainers
This proxy mock is very useful and needed to be extended regarding the handlers provided. Indeed, in case of virtcontainers, we need to send commands like RegisterVM, UnregisterVM, AttachVM, or even Hyper. This patch takes care of adding appropriate handlers for those commands. Fixes #122 Signed-off-by: Sebastien Boeuf <[email protected]>
1 parent cd22939 commit d0fb4bf

File tree

2 files changed

+96
-6
lines changed

2 files changed

+96
-6
lines changed

mock/mock_proxy.go

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package mock
1616

1717
import (
1818
"encoding/json"
19+
"fmt"
1920
"net"
2021
"os"
2122
"sync"
@@ -153,6 +154,72 @@ func stdinShimHandler(data []byte, userData interface{}, response *handlerRespon
153154
proxy.StdinReceived <- true
154155
}
155156

157+
func registerVMHandler(data []byte, userData interface{}, response *handlerResponse) {
158+
client := userData.(*client)
159+
proxy := client.proxy
160+
161+
proxy.log("Register VM")
162+
163+
payload := api.RegisterVM{}
164+
if err := json.Unmarshal(data, &payload); err != nil {
165+
response.SetError(err)
166+
return
167+
}
168+
169+
// Generate fake tokens
170+
var tokens []string
171+
for i := 0; i < payload.NumIOStreams; i++ {
172+
tokens = append(tokens, fmt.Sprintf("%d", i))
173+
}
174+
175+
io := &api.IOResponse{
176+
Tokens: tokens,
177+
}
178+
179+
response.AddResult("io", io)
180+
}
181+
182+
func unregisterVMHandler(data []byte, userData interface{}, response *handlerResponse) {
183+
client := userData.(*client)
184+
proxy := client.proxy
185+
186+
proxy.log("Unregister VM")
187+
}
188+
189+
func attachVMHandler(data []byte, userData interface{}, response *handlerResponse) {
190+
client := userData.(*client)
191+
proxy := client.proxy
192+
193+
proxy.log("Attach VM")
194+
195+
payload := api.AttachVM{}
196+
if err := json.Unmarshal(data, &payload); err != nil {
197+
response.SetError(err)
198+
return
199+
}
200+
201+
// Generate fake tokens
202+
var tokens []string
203+
for i := 0; i < payload.NumIOStreams; i++ {
204+
tokens = append(tokens, fmt.Sprintf("%d", i))
205+
}
206+
207+
io := &api.IOResponse{
208+
Tokens: tokens,
209+
}
210+
211+
response.AddResult("io", io)
212+
}
213+
214+
func hyperCmdHandler(data []byte, userData interface{}, response *handlerResponse) {
215+
client := userData.(*client)
216+
proxy := client.proxy
217+
218+
proxy.log("Hyper command")
219+
220+
response.SetData([]byte{})
221+
}
222+
156223
// SendStdoutStream sends a Stdout Stream Frame to connected client
157224
func (proxy *Proxy) SendStdoutStream(payload []byte) {
158225
err := api.WriteStream(proxy.cl, api.StreamStdout, payload)
@@ -198,19 +265,34 @@ func (proxy *Proxy) serveClient(proto *protocol, newConn net.Conn) {
198265
func (proxy *Proxy) serve() {
199266
proto := newProtocol()
200267

268+
// shim handlers
201269
proto.Handle(FrameKey{api.TypeCommand, int(api.CmdConnectShim)}, connectShimHandler)
202270
proto.Handle(FrameKey{api.TypeCommand, int(api.CmdDisconnectShim)}, disconnectShimHandler)
203-
proto.Handle(FrameKey{api.TypeCommand, int(api.CmdSignal)}, signalShimHandler)
204-
proto.Handle(FrameKey{api.TypeCommand, int(api.CmdConnectShim)}, connectShimHandler)
205-
206271
proto.Handle(FrameKey{api.TypeStream, int(api.StreamStdin)}, stdinShimHandler)
207272

273+
// runtime handlers
274+
proto.Handle(FrameKey{api.TypeCommand, int(api.CmdRegisterVM)}, registerVMHandler)
275+
proto.Handle(FrameKey{api.TypeCommand, int(api.CmdUnregisterVM)}, unregisterVMHandler)
276+
proto.Handle(FrameKey{api.TypeCommand, int(api.CmdAttachVM)}, attachVMHandler)
277+
proto.Handle(FrameKey{api.TypeCommand, int(api.CmdHyper)}, hyperCmdHandler)
278+
279+
// Shared handler between shim and runtime
280+
proto.Handle(FrameKey{api.TypeCommand, int(api.CmdSignal)}, signalShimHandler)
281+
208282
//Wait for a single client connection
209283
conn, err := proxy.listener.Accept()
210-
assert.Nil(proxy.t, err)
284+
if err != nil {
285+
// Ending up into this case when the listener is closed, which
286+
// is still a valid case. We don't want to throw an error in
287+
// this case.
288+
return
289+
}
290+
211291
assert.NotNil(proxy.t, conn)
212292
proxy.log("Client connected")
213293

294+
proxy.wg.Add(1)
295+
214296
proxy.cl = conn
215297

216298
proxy.serveClient(proto, conn)
@@ -219,8 +301,11 @@ func (proxy *Proxy) serve() {
219301
// Start invokes mock proxy instance to start listening.
220302
func (proxy *Proxy) Start() {
221303
proxy.startListening()
222-
proxy.wg.Add(1)
223-
go proxy.serve()
304+
go func() {
305+
for {
306+
proxy.serve()
307+
}
308+
}()
224309
}
225310

226311
// Stop causes mock proxy instance to stop listening,

mock/protocol.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type protocolHandler func([]byte, interface{}, *handlerResponse)
2929
type handlerResponse struct {
3030
err error
3131
results map[string]interface{}
32+
data []byte
3233
}
3334

3435
// SetError indicates sets error for the response.
@@ -54,6 +55,10 @@ func (r *handlerResponse) AddResult(key string, value interface{}) {
5455
r.results[key] = value
5556
}
5657

58+
func (r *handlerResponse) SetData(data []byte) {
59+
r.data = data
60+
}
61+
5762
// FrameKey is a struct composed of the the frame type and opcode,
5863
// used as a key for retrieving the handler for handling the frame.
5964
type FrameKey struct {

0 commit comments

Comments
 (0)