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

Commit

Permalink
agent: add test to WaitProcess()
Browse files Browse the repository at this point in the history
Add the unit test to WaitProcess().

Fixes: #129
Fixes: #333

Signed-off-by: fupan <[email protected]>
  • Loading branch information
lifupan committed Sep 3, 2018
1 parent f746ed8 commit 8abc400
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ import (
"path/filepath"
"reflect"
"testing"
"time"

pb "github.com/kata-containers/agent/protocols/grpc"
"github.com/opencontainers/runc/libcontainer"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
"sync"
)

var testSharedPidNs = "testSharedPidNs"
Expand Down Expand Up @@ -533,3 +536,87 @@ func TestReseedRandomDev(t *testing.T) {
assert.NoError(err)
assert.Equal(r, emptyResp)
}

func TestSingleWaitProcess(t *testing.T) {
containerID := "1"
exitCode := 9
assert := assert.New(t)
req := &pb.WaitProcessRequest{
ContainerId: containerID,
ExecId: containerID,
}

a := &agentGRPC{
sandbox: &sandbox{
containers: make(map[string]*container),
running: true,
subreaper: &agentReaper{},
},
}

a.sandbox.containers[containerID] = &container{
id: containerID,
processes: make(map[string]*process),
}

a.sandbox.containers[containerID].processes[containerID] = &process{
id: containerID,
process: libcontainer.Process{},
exitCodeCh: make(chan int, 1),
}

go func() {
time.Sleep(time.Second)
a.sandbox.containers[containerID].processes[containerID].exitCodeCh <- exitCode
}()

resp, _ := a.WaitProcess(context.TODO(), req)
assert.Equal(resp.Status, int32(exitCode))
}

func TestMultiWaitProcess(t *testing.T) {
containerID := "1"
exitCode := 9
wg := sync.WaitGroup{}

assert := assert.New(t)
req := &pb.WaitProcessRequest{
ContainerId: containerID,
ExecId: containerID,
}

a := &agentGRPC{
sandbox: &sandbox{
containers: make(map[string]*container),
running: true,
subreaper: &agentReaper{},
},
}

a.sandbox.containers[containerID] = &container{
id: containerID,
processes: make(map[string]*process),
}

a.sandbox.containers[containerID].processes[containerID] = &process{
id: containerID,
process: libcontainer.Process{},
exitCodeCh: make(chan int, 1),
}

for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
resp, _ := a.WaitProcess(context.TODO(), req)
assert.Equal(resp.Status, int32(exitCode))
wg.Done()
}()
}

go func() {
time.Sleep(time.Second)
a.sandbox.containers[containerID].processes[containerID].exitCodeCh <- exitCode
}()

wg.Wait()
}

0 comments on commit 8abc400

Please sign in to comment.