Merge pull request #31 from Random-Liu/enhance-fake-execution-client
Enhance fake execution client.
This commit is contained in:
commit
fe37d7bc96
@ -28,11 +28,17 @@ import (
|
|||||||
google_protobuf "github.com/golang/protobuf/ptypes/empty"
|
google_protobuf "github.com/golang/protobuf/ptypes/empty"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
)
|
)
|
||||||
|
|
||||||
type calledDetail struct {
|
var containerNotExistError = grpc.Errorf(codes.Unknown, containerd.ErrContainerNotExist.Error())
|
||||||
name string
|
|
||||||
argument interface{}
|
// CalledDetail is the struct contains called function name and arguments.
|
||||||
|
type CalledDetail struct {
|
||||||
|
// Name of the function called.
|
||||||
|
Name string
|
||||||
|
// Argument of the function called.
|
||||||
|
Argument interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ execution.ContainerService_EventsClient = &EventClient{}
|
var _ execution.ContainerService_EventsClient = &EventClient{}
|
||||||
@ -53,7 +59,7 @@ func (cli *EventClient) Recv() (*container.Event, error) {
|
|||||||
// can be run for testing without requiring a real containerd setup.
|
// can be run for testing without requiring a real containerd setup.
|
||||||
type FakeExecutionClient struct {
|
type FakeExecutionClient struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
called []calledDetail
|
called []CalledDetail
|
||||||
errors map[string]error
|
errors map[string]error
|
||||||
ContainerList map[string]container.Container
|
ContainerList map[string]container.Container
|
||||||
eventsQueue chan *container.Event
|
eventsQueue chan *container.Event
|
||||||
@ -133,7 +139,7 @@ func (f *FakeExecutionClient) sendEvent(event *container.Event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeExecutionClient) appendCalled(name string, argument interface{}) {
|
func (f *FakeExecutionClient) appendCalled(name string, argument interface{}) {
|
||||||
call := calledDetail{name: name, argument: argument}
|
call := CalledDetail{Name: name, Argument: argument}
|
||||||
f.called = append(f.called, call)
|
f.called = append(f.called, call)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,11 +149,28 @@ func (f *FakeExecutionClient) GetCalledNames() []string {
|
|||||||
defer f.Unlock()
|
defer f.Unlock()
|
||||||
names := []string{}
|
names := []string{}
|
||||||
for _, detail := range f.called {
|
for _, detail := range f.called {
|
||||||
names = append(names, detail.name)
|
names = append(names, detail.Name)
|
||||||
}
|
}
|
||||||
return names
|
return names
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetCalledDetails get detail of each call.
|
||||||
|
func (f *FakeExecutionClient) GetCalledDetails() []CalledDetail {
|
||||||
|
f.Lock()
|
||||||
|
defer f.Unlock()
|
||||||
|
// Copy the list and return.
|
||||||
|
return append([]CalledDetail{}, f.called...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFakeContainers injects fake containers.
|
||||||
|
func (f *FakeExecutionClient) SetFakeContainers(containers []container.Container) {
|
||||||
|
f.Lock()
|
||||||
|
defer f.Unlock()
|
||||||
|
for _, c := range containers {
|
||||||
|
f.ContainerList[c.ID] = c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create is a test implementation of execution.Create.
|
// Create is a test implementation of execution.Create.
|
||||||
func (f *FakeExecutionClient) Create(ctx context.Context, createOpts *execution.CreateRequest, opts ...grpc.CallOption) (*execution.CreateResponse, error) {
|
func (f *FakeExecutionClient) Create(ctx context.Context, createOpts *execution.CreateRequest, opts ...grpc.CallOption) (*execution.CreateResponse, error) {
|
||||||
f.Lock()
|
f.Lock()
|
||||||
@ -187,7 +210,7 @@ func (f *FakeExecutionClient) Start(ctx context.Context, startOpts *execution.St
|
|||||||
}
|
}
|
||||||
c, ok := f.ContainerList[startOpts.ID]
|
c, ok := f.ContainerList[startOpts.ID]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, containerd.ErrContainerNotExist
|
return nil, containerNotExistError
|
||||||
}
|
}
|
||||||
f.sendEvent(&container.Event{
|
f.sendEvent(&container.Event{
|
||||||
ID: c.ID,
|
ID: c.ID,
|
||||||
@ -197,6 +220,7 @@ func (f *FakeExecutionClient) Start(ctx context.Context, startOpts *execution.St
|
|||||||
switch c.Status {
|
switch c.Status {
|
||||||
case container.Status_CREATED:
|
case container.Status_CREATED:
|
||||||
c.Status = container.Status_RUNNING
|
c.Status = container.Status_RUNNING
|
||||||
|
f.ContainerList[startOpts.ID] = c
|
||||||
return &google_protobuf.Empty{}, nil
|
return &google_protobuf.Empty{}, nil
|
||||||
case container.Status_STOPPED:
|
case container.Status_STOPPED:
|
||||||
return &google_protobuf.Empty{}, fmt.Errorf("cannot start a container that has stopped")
|
return &google_protobuf.Empty{}, fmt.Errorf("cannot start a container that has stopped")
|
||||||
@ -217,7 +241,7 @@ func (f *FakeExecutionClient) Delete(ctx context.Context, deleteOpts *execution.
|
|||||||
}
|
}
|
||||||
c, ok := f.ContainerList[deleteOpts.ID]
|
c, ok := f.ContainerList[deleteOpts.ID]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, containerd.ErrContainerNotExist
|
return nil, containerNotExistError
|
||||||
}
|
}
|
||||||
delete(f.ContainerList, deleteOpts.ID)
|
delete(f.ContainerList, deleteOpts.ID)
|
||||||
f.sendEvent(&container.Event{
|
f.sendEvent(&container.Event{
|
||||||
@ -238,7 +262,7 @@ func (f *FakeExecutionClient) Info(ctx context.Context, infoOpts *execution.Info
|
|||||||
}
|
}
|
||||||
c, ok := f.ContainerList[infoOpts.ID]
|
c, ok := f.ContainerList[infoOpts.ID]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, containerd.ErrContainerNotExist
|
return nil, containerNotExistError
|
||||||
}
|
}
|
||||||
return &c, nil
|
return &c, nil
|
||||||
}
|
}
|
||||||
@ -272,9 +296,10 @@ func (f *FakeExecutionClient) Kill(ctx context.Context, killOpts *execution.Kill
|
|||||||
}
|
}
|
||||||
c, ok := f.ContainerList[killOpts.ID]
|
c, ok := f.ContainerList[killOpts.ID]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, containerd.ErrContainerNotExist
|
return nil, containerNotExistError
|
||||||
}
|
}
|
||||||
c.Status = container.Status_STOPPED
|
c.Status = container.Status_STOPPED
|
||||||
|
f.ContainerList[killOpts.ID] = c
|
||||||
f.sendEvent(&container.Event{
|
f.sendEvent(&container.Event{
|
||||||
ID: c.ID,
|
ID: c.ID,
|
||||||
Type: container.Event_EXIT,
|
Type: container.Event_EXIT,
|
||||||
|
Loading…
Reference in New Issue
Block a user