Skip to content
This repository was archived by the owner on May 6, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,22 @@ func (client *Client) AttachVM(containerID string, options *AttachVMOptions) (*A
}

// Hyper wraps the Hyper payload (see payload description for more details)
func (client *Client) Hyper(hyperName string, hyperMessage interface{}) error {
func (client *Client) Hyper(hyperName string, hyperMessage interface{}) ([]byte, error) {
return client.HyperWithTokens(hyperName, nil, hyperMessage)
}

// HyperWithTokens is a Hyper variant where the users can specify a list of I/O tokens.
//
// See the api.Hyper payload description for more details.
func (client *Client) HyperWithTokens(hyperName string, tokens []string, hyperMessage interface{}) error {
func (client *Client) HyperWithTokens(hyperName string, tokens []string, hyperMessage interface{}) ([]byte, error) {
var data []byte

if hyperMessage != nil {
var err error

data, err = json.Marshal(hyperMessage)
if err != nil {
return err
return nil, err
}
}

Expand All @@ -232,10 +232,14 @@ func (client *Client) HyperWithTokens(hyperName string, tokens []string, hyperMe

resp, err := client.sendCommand(api.CmdHyper, &hyper)
if err != nil {
return err
return nil, err
}

return errorFromResponse(resp)
if err = errorFromResponse(resp); err != nil {
return nil, err
}

return resp.Payload, errorFromResponse(resp)
}

// UnregisterVM wraps the api.UnregisterVM payload.
Expand Down
34 changes: 26 additions & 8 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import (
"github.com/clearcontainers/proxy/api"
)

// XXX: could do with its own package to remove that ugly namespacing
type commandHandler func([]byte, interface{}, *handlerResponse)

// Encapsulates the different parts of what a handler can return.
type handlerResponse struct {
err error
results map[string]interface{}
data []byte
}

func (r *handlerResponse) SetError(err error) {
Expand All @@ -50,6 +50,13 @@ func (r *handlerResponse) AddResult(key string, value interface{}) {
r.results[key] = value
}

// SetData sets the data to be sent as the response payload. If both AddResult
// and SetData are called on the same handlerResponse, SetData takes precedence
// and defines what we be returned to the caller.
func (r *handlerResponse) SetData(data []byte) {
r.data = data
}

// streamHandler is the prototype of function that can be registered to be
// called when receiving a stream frame
type streamHandler func(frame *api.Frame, userData interface{}) error
Expand Down Expand Up @@ -115,14 +122,25 @@ func (proto *protocol) handleCommand(ctx *clientCtx, cmd *api.Frame) *api.Frame
return newErrorResponse(cmd.Header.Opcode, hr.err.Error())
}

var payload interface{}
if len(hr.results) > 0 {
payload = hr.results
}
frame, err := api.NewFrameJSON(api.TypeResponse, cmd.Header.Opcode, payload)
if err != nil {
return newErrorResponse(cmd.Header.Opcode, err.Error())
var frame *api.Frame

if len(hr.data) > 0 {
// We have a full payload defined.
frame = api.NewFrame(api.TypeResponse, cmd.Header.Opcode, hr.data)
} else {
// Otherwise, we'll marshal hr.results, if we have any.
var payload interface{}
var err error

if len(hr.results) > 0 {
payload = hr.results
}
frame, err = api.NewFrameJSON(api.TypeResponse, cmd.Header.Opcode, payload)
if err != nil {
return newErrorResponse(cmd.Header.Opcode, err.Error())
}
}

return frame
}

Expand Down
3 changes: 2 additions & 1 deletion proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ func hyper(data []byte, userData interface{}, response *handlerResponse) {

client.log.Infof("hyper(cmd=%s, data=%s)", hyper.HyperName, hyper.Data)

err := vm.SendMessage(&hyper)
data, err := vm.SendMessage(&hyper)
response.SetData(data)
response.SetError(err)
}

Expand Down
35 changes: 28 additions & 7 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func TestHyperPing(t *testing.T) {
// Send ping and verify we have indeed received the message on the
// hyperstart side. Ping is somewhat interesting because it's a case of
// an hyper message without data.
err = rig.Client.Hyper("ping", nil)
_, err = rig.Client.Hyper("ping", nil)
assert.Nil(t, err)

msgs := rig.Hyperstart.GetLastMessages()
Expand Down Expand Up @@ -315,7 +315,7 @@ func TestHyperStartpod(t *testing.T) {
Hostname: "testhostname",
ShareDir: "rootfs",
}
err = rig.Client.Hyper("startpod", &startpod)
_, err = rig.Client.Hyper("startpod", &startpod)
assert.Nil(t, err)

msgs := rig.Hyperstart.GetLastMessages()
Expand Down Expand Up @@ -425,7 +425,7 @@ func TestHyperSequenceNumberRelocation(t *testing.T) {
Args: []string{"/bin/sh"},
},
}
err := rig.Client.HyperWithTokens("newcontainer", []string{token}, &newcontainer)
_, err := rig.Client.HyperWithTokens("newcontainer", []string{token}, &newcontainer)
assert.Nil(t, err)

// Verify hyperstart has received the message with relocation
Expand Down Expand Up @@ -617,7 +617,7 @@ func TestShimConnectAfterExeccmd(t *testing.T) {
Args: []string{"/bin/sh"},
},
}
err := rig.Client.HyperWithTokens("execcmd", []string{token}, &execcmd)
_, err := rig.Client.HyperWithTokens("execcmd", []string{token}, &execcmd)
assert.NotNil(t, err)
assert.True(t, strings.Contains(err.Error(), "timeout"))
waitForShimTimeout = oldTimeout
Expand All @@ -635,7 +635,7 @@ func TestShimConnectAfterExeccmd(t *testing.T) {
wg.Done()
}()

err = rig.Client.HyperWithTokens("execcmd", []string{token}, &execcmd)
_, err = rig.Client.HyperWithTokens("execcmd", []string{token}, &execcmd)
assert.Nil(t, err)

wg.Wait()
Expand Down Expand Up @@ -687,7 +687,7 @@ func TestShimSendSignalAfterExeccmd(t *testing.T) {
Args: []string{"/bin/sh"},
},
}
err = rig.Client.HyperWithTokens("execcmd", []string{token}, &execcmd)
_, err = rig.Client.HyperWithTokens("execcmd", []string{token}, &execcmd)
assert.Nil(t, err)

wg.Wait()
Expand Down Expand Up @@ -752,7 +752,7 @@ func TestShimSendStdinAfterExeccmd(t *testing.T) {
Args: []string{"/bin/sh"},
},
}
err = rig.Client.HyperWithTokens("execcmd", []string{token}, &execcmd)
_, err = rig.Client.HyperWithTokens("execcmd", []string{token}, &execcmd)
assert.Nil(t, err)

wg.Wait()
Expand Down Expand Up @@ -833,3 +833,24 @@ func TestLog(t *testing.T) {

rig.Stop()
}

func TestHyperstartResponse(t *testing.T) {
rig := newTestRig(t)
rig.Start()

ctlSocketPath, ioSocketPath := rig.Hyperstart.GetSocketPaths()
_, err := rig.Client.RegisterVM(testContainerID, ctlSocketPath, ioSocketPath, nil)
assert.Nil(t, err)

// Pre-fill what hyper will return.
const testMsg = "Foo Foo!"
msgData := []byte(testMsg)
rig.Hyperstart.SendMessage(hyperstart.AckCode, msgData)

// Trigger and message + response.
data, err := rig.Client.Hyper("ping", msgData)
assert.Nil(t, err)
assert.Equal(t, msgData, data)

rig.Stop()
}
13 changes: 7 additions & 6 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,21 +430,22 @@ func (vm *vm) relocateHyperCommand(hyper *api.Hyper) (*ioSession, error) {
return session, nil
}

func (vm *vm) SendMessage(hyper *api.Hyper) (err error) {
func (vm *vm) SendMessage(hyper *api.Hyper) ([]byte, error) {
var session *ioSession
var err error

if session, err = vm.relocateHyperCommand(hyper); err != nil {
return err
return nil, err
}

_, err = vm.hyperHandler.SendCtlMessage(hyper.HyperName, hyper.Data)
response, err := vm.hyperHandler.SendCtlMessage(hyper.HyperName, hyper.Data)

if session != nil {
// We have now started the process inside the VM, let the shim send stdin
// data and signals.
close(session.processStarted)
}
return err
return response.Message, err
}

var waitForShimTimeout = 30 * time.Second
Expand Down Expand Up @@ -554,8 +555,8 @@ func (session *ioSession) SendSignal(signal syscall.Signal) error {
// FIXME: Change this when hyperstart will be capable of forwarding a
// signal to a process different from the initial container process.
if session.containerID == "" {
return fmt.Errorf("Could not send the signal %s: Sending" +
" a signal to a process different from the initial" +
return fmt.Errorf("Could not send the signal %s: Sending"+
" a signal to a process different from the initial"+
" container process is not supported",
signal.String())
}
Expand Down