Merge pull request #209 from sboeuf/fix_unit_tests

virtcontainers: Fix unit tests
This commit is contained in:
Eric Ernst 2018-04-16 07:24:40 -07:00 committed by GitHub
commit 8088a62805
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 7 deletions

View File

@ -242,8 +242,9 @@ func TestCCShimStartDetachSuccessful(t *testing.T) {
testCCShimStart(t, sandbox, params, false) testCCShimStart(t, sandbox, params, false)
readCh := make(chan error) readCh := make(chan error, 1)
go func() { go func() {
defer close(readCh)
bufStdout := make([]byte, 1024) bufStdout := make([]byte, 1024)
n, err := rStdout.Read(bufStdout) n, err := rStdout.Read(bufStdout)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {

View File

@ -22,6 +22,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"syscall"
"time" "time"
specs "github.com/opencontainers/runtime-spec/specs-go" specs "github.com/opencontainers/runtime-spec/specs-go"
@ -82,9 +83,11 @@ func (h *Hook) runHook() error {
return fmt.Errorf("%s: stdout: %s, stderr: %s", err, stdout.String(), stderr.String()) return fmt.Errorf("%s: stdout: %s, stderr: %s", err, stdout.String(), stderr.String())
} }
} else { } else {
done := make(chan error) done := make(chan error, 1)
go func() {
go func() { done <- cmd.Wait() }() done <- cmd.Wait()
close(done)
}()
select { select {
case err := <-done: case err := <-done:
@ -92,6 +95,10 @@ func (h *Hook) runHook() error {
return fmt.Errorf("%s: stdout: %s, stderr: %s", err, stdout.String(), stderr.String()) return fmt.Errorf("%s: stdout: %s, stderr: %s", err, stdout.String(), stderr.String())
} }
case <-time.After(time.Duration(h.Timeout) * time.Second): case <-time.After(time.Duration(h.Timeout) * time.Second):
if err := syscall.Kill(cmd.Process.Pid, syscall.SIGKILL); err != nil {
return err
}
return fmt.Errorf("Hook timeout") return fmt.Errorf("Hook timeout")
} }
} }

View File

@ -236,8 +236,9 @@ func TestKataShimStartDetachSuccessful(t *testing.T) {
testKataShimStart(t, sandbox, params, false) testKataShimStart(t, sandbox, params, false)
readCh := make(chan error) readCh := make(chan error, 1)
go func() { go func() {
defer close(readCh)
bufStdout := make([]byte, 1024) bufStdout := make([]byte, 1024)
n, err := rStdout.Read(bufStdout) n, err := rStdout.Read(bufStdout)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {

View File

@ -21,5 +21,5 @@ type noopShim struct{}
// start is the noopShim start implementation for testing purpose. // start is the noopShim start implementation for testing purpose.
// It does nothing. // It does nothing.
func (s *noopShim) start(sandbox Sandbox, params ShimParams) (int, error) { func (s *noopShim) start(sandbox Sandbox, params ShimParams) (int, error) {
return 1000, nil return 0, nil
} }

View File

@ -24,7 +24,7 @@ func TestNoopShimStart(t *testing.T) {
s := &noopShim{} s := &noopShim{}
sandbox := Sandbox{} sandbox := Sandbox{}
params := ShimParams{} params := ShimParams{}
expected := 1000 expected := 0
pid, err := s.start(sandbox, params) pid, err := s.start(sandbox, params)
if err != nil { if err != nil {

View File

@ -31,6 +31,8 @@ const testToken = "pF56IaDpuax6hihJ5PneB8JypqmOvjkqY-wKGVYqgIM="
// CCProxyMock is an object mocking clearcontainers Proxy // CCProxyMock is an object mocking clearcontainers Proxy
type CCProxyMock struct { type CCProxyMock struct {
sync.Mutex
t *testing.T t *testing.T
wg sync.WaitGroup wg sync.WaitGroup
connectionPath string connectionPath string
@ -50,6 +52,8 @@ type CCProxyMock struct {
Signal chan ShimSignal Signal chan ShimSignal
ShimDisconnected chan bool ShimDisconnected chan bool
StdinReceived chan bool StdinReceived chan bool
stopped bool
} }
// NewCCProxyMock creates a hyperstart instance // NewCCProxyMock creates a hyperstart instance
@ -296,10 +300,19 @@ func (proxy *CCProxyMock) serve() {
// Start invokes mock proxy instance to start listening. // Start invokes mock proxy instance to start listening.
func (proxy *CCProxyMock) Start() { func (proxy *CCProxyMock) Start() {
proxy.stopped = false
proxy.startListening() proxy.startListening()
go func() { go func() {
for { for {
proxy.serve() proxy.serve()
proxy.Lock()
stopped := proxy.stopped
proxy.Unlock()
if stopped {
break
}
} }
}() }()
} }
@ -307,6 +320,10 @@ func (proxy *CCProxyMock) Start() {
// Stop causes mock proxy instance to stop listening, // Stop causes mock proxy instance to stop listening,
// close connection to client and close all channels // close connection to client and close all channels
func (proxy *CCProxyMock) Stop() { func (proxy *CCProxyMock) Stop() {
proxy.Lock()
proxy.stopped = true
proxy.Unlock()
proxy.listener.Close() proxy.listener.Close()
if proxy.cl != nil { if proxy.cl != nil {