Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #209 from sboeuf/fix_unit_tests
Browse files Browse the repository at this point in the history
virtcontainers: Fix unit tests
  • Loading branch information
Eric Ernst authored Apr 16, 2018
2 parents ad5669f + 92577c6 commit 8088a62
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
3 changes: 2 additions & 1 deletion virtcontainers/cc_shim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,9 @@ func TestCCShimStartDetachSuccessful(t *testing.T) {

testCCShimStart(t, sandbox, params, false)

readCh := make(chan error)
readCh := make(chan error, 1)
go func() {
defer close(readCh)
bufStdout := make([]byte, 1024)
n, err := rStdout.Read(bufStdout)
if err != nil && err != io.EOF {
Expand Down
13 changes: 10 additions & 3 deletions virtcontainers/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"os"
"os/exec"
"syscall"
"time"

specs "github.com/opencontainers/runtime-spec/specs-go"
Expand Down Expand Up @@ -82,16 +83,22 @@ func (h *Hook) runHook() error {
return fmt.Errorf("%s: stdout: %s, stderr: %s", err, stdout.String(), stderr.String())
}
} else {
done := make(chan error)

go func() { done <- cmd.Wait() }()
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
close(done)
}()

select {
case err := <-done:
if err != nil {
return fmt.Errorf("%s: stdout: %s, stderr: %s", err, stdout.String(), stderr.String())
}
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")
}
}
Expand Down
3 changes: 2 additions & 1 deletion virtcontainers/kata_shim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,9 @@ func TestKataShimStartDetachSuccessful(t *testing.T) {

testKataShimStart(t, sandbox, params, false)

readCh := make(chan error)
readCh := make(chan error, 1)
go func() {
defer close(readCh)
bufStdout := make([]byte, 1024)
n, err := rStdout.Read(bufStdout)
if err != nil && err != io.EOF {
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/noop_shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ type noopShim struct{}
// start is the noopShim start implementation for testing purpose.
// It does nothing.
func (s *noopShim) start(sandbox Sandbox, params ShimParams) (int, error) {
return 1000, nil
return 0, nil
}
2 changes: 1 addition & 1 deletion virtcontainers/noop_shim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestNoopShimStart(t *testing.T) {
s := &noopShim{}
sandbox := Sandbox{}
params := ShimParams{}
expected := 1000
expected := 0

pid, err := s.start(sandbox, params)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions virtcontainers/pkg/mock/cc_proxy_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const testToken = "pF56IaDpuax6hihJ5PneB8JypqmOvjkqY-wKGVYqgIM="

// CCProxyMock is an object mocking clearcontainers Proxy
type CCProxyMock struct {
sync.Mutex

t *testing.T
wg sync.WaitGroup
connectionPath string
Expand All @@ -50,6 +52,8 @@ type CCProxyMock struct {
Signal chan ShimSignal
ShimDisconnected chan bool
StdinReceived chan bool

stopped bool
}

// NewCCProxyMock creates a hyperstart instance
Expand Down Expand Up @@ -296,17 +300,30 @@ func (proxy *CCProxyMock) serve() {

// Start invokes mock proxy instance to start listening.
func (proxy *CCProxyMock) Start() {
proxy.stopped = false
proxy.startListening()
go func() {
for {
proxy.serve()

proxy.Lock()
stopped := proxy.stopped
proxy.Unlock()

if stopped {
break
}
}
}()
}

// Stop causes mock proxy instance to stop listening,
// close connection to client and close all channels
func (proxy *CCProxyMock) Stop() {
proxy.Lock()
proxy.stopped = true
proxy.Unlock()

proxy.listener.Close()

if proxy.cl != nil {
Expand Down

0 comments on commit 8088a62

Please sign in to comment.