Merge pull request #39 from mozhuli/enhance-fake-services

Enhance fake services
This commit is contained in:
Lantao Liu 2017-05-17 09:03:48 -07:00 committed by GitHub
commit dea7ed3fcc
2 changed files with 22 additions and 28 deletions

View File

@ -25,7 +25,7 @@ import (
"github.com/containerd/containerd" "github.com/containerd/containerd"
"github.com/containerd/containerd/api/services/execution" "github.com/containerd/containerd/api/services/execution"
"github.com/containerd/containerd/api/types/container" "github.com/containerd/containerd/api/types/container"
google_protobuf "github.com/golang/protobuf/ptypes/empty" googleprotobuf "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" "google.golang.org/grpc/codes"
@ -91,10 +91,7 @@ func (f *FakeExecutionClient) WithEvents() *FakeExecutionClient {
return f return f
} }
func (f *FakeExecutionClient) popError(op string) error { func (f *FakeExecutionClient) getError(op string) error {
if f.errors == nil {
return nil
}
err, ok := f.errors[op] err, ok := f.errors[op]
if ok { if ok {
delete(f.errors, op) delete(f.errors, op)
@ -176,7 +173,7 @@ func (f *FakeExecutionClient) Create(ctx context.Context, createOpts *execution.
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
f.appendCalled("create", createOpts) f.appendCalled("create", createOpts)
if err := f.popError("create"); err != nil { if err := f.getError("create"); err != nil {
return nil, err return nil, err
} }
_, ok := f.ContainerList[createOpts.ID] _, ok := f.ContainerList[createOpts.ID]
@ -201,11 +198,11 @@ func (f *FakeExecutionClient) Create(ctx context.Context, createOpts *execution.
} }
// Start is a test implementation of execution.Start // Start is a test implementation of execution.Start
func (f *FakeExecutionClient) Start(ctx context.Context, startOpts *execution.StartRequest, opts ...grpc.CallOption) (*google_protobuf.Empty, error) { func (f *FakeExecutionClient) Start(ctx context.Context, startOpts *execution.StartRequest, opts ...grpc.CallOption) (*googleprotobuf.Empty, error) {
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
f.appendCalled("start", startOpts) f.appendCalled("start", startOpts)
if err := f.popError("start"); err != nil { if err := f.getError("start"); err != nil {
return nil, err return nil, err
} }
c, ok := f.ContainerList[startOpts.ID] c, ok := f.ContainerList[startOpts.ID]
@ -221,13 +218,13 @@ func (f *FakeExecutionClient) Start(ctx context.Context, startOpts *execution.St
case container.Status_CREATED: case container.Status_CREATED:
c.Status = container.Status_RUNNING c.Status = container.Status_RUNNING
f.ContainerList[startOpts.ID] = c f.ContainerList[startOpts.ID] = c
return &google_protobuf.Empty{}, nil return &googleprotobuf.Empty{}, nil
case container.Status_STOPPED: case container.Status_STOPPED:
return &google_protobuf.Empty{}, fmt.Errorf("cannot start a container that has stopped") return &googleprotobuf.Empty{}, fmt.Errorf("cannot start a container that has stopped")
case container.Status_RUNNING: case container.Status_RUNNING:
return &google_protobuf.Empty{}, fmt.Errorf("cannot start an already running container") return &googleprotobuf.Empty{}, fmt.Errorf("cannot start an already running container")
default: default:
return &google_protobuf.Empty{}, fmt.Errorf("cannot start a container in the %s state", c.Status) return &googleprotobuf.Empty{}, fmt.Errorf("cannot start a container in the %s state", c.Status)
} }
} }
@ -236,7 +233,7 @@ func (f *FakeExecutionClient) Delete(ctx context.Context, deleteOpts *execution.
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
f.appendCalled("delete", deleteOpts) f.appendCalled("delete", deleteOpts)
if err := f.popError("delete"); err != nil { if err := f.getError("delete"); err != nil {
return nil, err return nil, err
} }
c, ok := f.ContainerList[deleteOpts.ID] c, ok := f.ContainerList[deleteOpts.ID]
@ -257,7 +254,7 @@ func (f *FakeExecutionClient) Info(ctx context.Context, infoOpts *execution.Info
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
f.appendCalled("info", infoOpts) f.appendCalled("info", infoOpts)
if err := f.popError("info"); err != nil { if err := f.getError("info"); err != nil {
return nil, err return nil, err
} }
c, ok := f.ContainerList[infoOpts.ID] c, ok := f.ContainerList[infoOpts.ID]
@ -272,7 +269,7 @@ func (f *FakeExecutionClient) List(ctx context.Context, listOpts *execution.List
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
f.appendCalled("list", listOpts) f.appendCalled("list", listOpts)
if err := f.popError("list"); err != nil { if err := f.getError("list"); err != nil {
return nil, err return nil, err
} }
resp := &execution.ListResponse{} resp := &execution.ListResponse{}
@ -287,11 +284,11 @@ func (f *FakeExecutionClient) List(ctx context.Context, listOpts *execution.List
} }
// Kill is a test implementation of execution.Kill // Kill is a test implementation of execution.Kill
func (f *FakeExecutionClient) Kill(ctx context.Context, killOpts *execution.KillRequest, opts ...grpc.CallOption) (*google_protobuf.Empty, error) { func (f *FakeExecutionClient) Kill(ctx context.Context, killOpts *execution.KillRequest, opts ...grpc.CallOption) (*googleprotobuf.Empty, error) {
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
f.appendCalled("kill", killOpts) f.appendCalled("kill", killOpts)
if err := f.popError("kill"); err != nil { if err := f.getError("kill"); err != nil {
return nil, err return nil, err
} }
c, ok := f.ContainerList[killOpts.ID] c, ok := f.ContainerList[killOpts.ID]
@ -305,7 +302,7 @@ func (f *FakeExecutionClient) Kill(ctx context.Context, killOpts *execution.Kill
Type: container.Event_EXIT, Type: container.Event_EXIT,
Pid: c.Pid, Pid: c.Pid,
}) })
return &google_protobuf.Empty{}, nil return &googleprotobuf.Empty{}, nil
} }
// Events is a test implementation of execution.Events // Events is a test implementation of execution.Events
@ -313,7 +310,7 @@ func (f *FakeExecutionClient) Events(ctx context.Context, eventsOpts *execution.
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
f.appendCalled("events", eventsOpts) f.appendCalled("events", eventsOpts)
if err := f.popError("events"); err != nil { if err := f.getError("events"); err != nil {
return nil, err return nil, err
} }
var client = &EventClient{ var client = &EventClient{
@ -330,13 +327,13 @@ func (f *FakeExecutionClient) Exec(ctx context.Context, execOpts *execution.Exec
} }
// Pty is a test implementation of execution.Pty // Pty is a test implementation of execution.Pty
func (f *FakeExecutionClient) Pty(ctx context.Context, ptyOpts *execution.PtyRequest, opts ...grpc.CallOption) (*google_protobuf.Empty, error) { func (f *FakeExecutionClient) Pty(ctx context.Context, ptyOpts *execution.PtyRequest, opts ...grpc.CallOption) (*googleprotobuf.Empty, error) {
// TODO: implement Pty() // TODO: implement Pty()
return nil, nil return nil, nil
} }
// CloseStdin is a test implementation of execution.CloseStdin // CloseStdin is a test implementation of execution.CloseStdin
func (f *FakeExecutionClient) CloseStdin(ctx context.Context, closeStdinOpts *execution.CloseStdinRequest, opts ...grpc.CallOption) (*google_protobuf.Empty, error) { func (f *FakeExecutionClient) CloseStdin(ctx context.Context, closeStdinOpts *execution.CloseStdinRequest, opts ...grpc.CallOption) (*googleprotobuf.Empty, error) {
// TODO: implement CloseStdin() // TODO: implement CloseStdin()
return nil, nil return nil, nil
} }

View File

@ -50,10 +50,7 @@ func NewFakeRootfsClient() *FakeRootfsClient {
} }
} }
func (f *FakeRootfsClient) popError(op string) error { func (f *FakeRootfsClient) getError(op string) error {
if f.errors == nil {
return nil
}
err, ok := f.errors[op] err, ok := f.errors[op]
if ok { if ok {
delete(f.errors, op) delete(f.errors, op)
@ -142,7 +139,7 @@ func (f *FakeRootfsClient) Unpack(ctx context.Context, unpackOpts *rootfs.Unpack
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
f.appendCalled("unpack", unpackOpts) f.appendCalled("unpack", unpackOpts)
if err := f.popError("unpack"); err != nil { if err := f.getError("unpack"); err != nil {
return nil, err return nil, err
} }
chainID := generateChainID(unpackOpts.Layers) chainID := generateChainID(unpackOpts.Layers)
@ -161,7 +158,7 @@ func (f *FakeRootfsClient) Prepare(ctx context.Context, prepareOpts *rootfs.Prep
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
f.appendCalled("prepare", prepareOpts) f.appendCalled("prepare", prepareOpts)
if err := f.popError("prepare"); err != nil { if err := f.getError("prepare"); err != nil {
return nil, err return nil, err
} }
_, ok := f.ChainIDList[prepareOpts.ChainID] _, ok := f.ChainIDList[prepareOpts.ChainID]
@ -183,7 +180,7 @@ func (f *FakeRootfsClient) Mounts(ctx context.Context, mountsOpts *rootfs.Mounts
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
f.appendCalled("mounts", mountsOpts) f.appendCalled("mounts", mountsOpts)
if err := f.popError("mounts"); err != nil { if err := f.getError("mounts"); err != nil {
return nil, err return nil, err
} }
mounts, ok := f.MountList[mountsOpts.Name] mounts, ok := f.MountList[mountsOpts.Name]