2014-07-12 02:02:55 +08:00
|
|
|
package syncpipe
|
2014-06-27 02:50:22 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-08-29 22:17:22 +08:00
|
|
|
"syscall"
|
2014-06-27 02:50:22 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-08-07 09:00:52 +08:00
|
|
|
type testStruct struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2014-06-27 02:50:22 +08:00
|
|
|
func TestSendErrorFromChild(t *testing.T) {
|
|
|
|
pipe, err := NewSyncPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := pipe.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-08-29 22:17:22 +08:00
|
|
|
childfd, err := syscall.Dup(int(pipe.Child().Fd()))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
childPipe, _ := NewSyncPipeFromFd(0, uintptr(childfd))
|
|
|
|
|
|
|
|
pipe.CloseChild()
|
|
|
|
pipe.SendToChild(nil)
|
2014-06-27 02:50:22 +08:00
|
|
|
|
2014-08-29 22:17:22 +08:00
|
|
|
expected := "something bad happened"
|
|
|
|
childPipe.ReportChildError(fmt.Errorf(expected))
|
2014-06-27 02:50:22 +08:00
|
|
|
|
|
|
|
childError := pipe.ReadFromChild()
|
|
|
|
if childError == nil {
|
|
|
|
t.Fatal("expected an error to be returned but did not receive anything")
|
|
|
|
}
|
|
|
|
|
|
|
|
if childError.Error() != expected {
|
|
|
|
t.Fatalf("expected %q but received error message %q", expected, childError.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendPayloadToChild(t *testing.T) {
|
|
|
|
pipe, err := NewSyncPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := pipe.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
expected := "libcontainer"
|
|
|
|
|
2014-08-07 09:00:52 +08:00
|
|
|
if err := pipe.SendToChild(testStruct{Name: expected}); err != nil {
|
2014-06-27 02:50:22 +08:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-08-07 09:00:52 +08:00
|
|
|
var s *testStruct
|
|
|
|
if err := pipe.ReadFromParent(&s); err != nil {
|
2014-06-27 02:50:22 +08:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-08-07 09:00:52 +08:00
|
|
|
if s.Name != expected {
|
|
|
|
t.Fatalf("expected name %q but received %q", expected, s.Name)
|
2014-06-27 02:50:22 +08:00
|
|
|
}
|
|
|
|
}
|