Temporarily remove unit test relying on fake containerd services.
Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
@@ -26,6 +26,14 @@ import (
|
||||
"github.com/kubernetes-incubator/cri-o/pkg/ocicni"
|
||||
)
|
||||
|
||||
// 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{}
|
||||
}
|
||||
|
||||
// CNIPluginArgument is arguments used to call CNI related functions.
|
||||
type CNIPluginArgument struct {
|
||||
NetnsPath string
|
||||
|
||||
@@ -1,181 +0,0 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package testing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/containerd/containerd/api/services/containers"
|
||||
googleprotobuf "github.com/golang/protobuf/ptypes/empty"
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
)
|
||||
|
||||
// ContainerNotExistError is the fake error returned when container does not exist.
|
||||
var ContainerNotExistError = grpc.Errorf(codes.NotFound, "container does not exist")
|
||||
|
||||
// FakeContainersClient is a simple fake containers client, so that cri-containerd
|
||||
// can be run for testing without requiring a real containerd setup.
|
||||
type FakeContainersClient struct {
|
||||
sync.Mutex
|
||||
called []CalledDetail
|
||||
errors map[string]error
|
||||
ContainerList map[string]containers.Container
|
||||
}
|
||||
|
||||
var _ containers.ContainersClient = &FakeContainersClient{}
|
||||
|
||||
// NewFakeContainersClient creates a FakeContainersClient
|
||||
func NewFakeContainersClient() *FakeContainersClient {
|
||||
return &FakeContainersClient{
|
||||
errors: make(map[string]error),
|
||||
ContainerList: make(map[string]containers.Container),
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FakeContainersClient) getError(op string) error {
|
||||
err, ok := f.errors[op]
|
||||
if ok {
|
||||
delete(f.errors, op)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InjectError inject error for call
|
||||
func (f *FakeContainersClient) InjectError(fn string, err error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.errors[fn] = err
|
||||
}
|
||||
|
||||
// InjectErrors inject errors for calls
|
||||
func (f *FakeContainersClient) InjectErrors(errs map[string]error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
for fn, err := range errs {
|
||||
f.errors[fn] = err
|
||||
}
|
||||
}
|
||||
|
||||
// ClearErrors clear errors for call
|
||||
func (f *FakeContainersClient) ClearErrors() {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.errors = make(map[string]error)
|
||||
}
|
||||
|
||||
func (f *FakeContainersClient) appendCalled(name string, argument interface{}) {
|
||||
call := CalledDetail{Name: name, Argument: argument}
|
||||
f.called = append(f.called, call)
|
||||
}
|
||||
|
||||
// GetCalledNames get names of call
|
||||
func (f *FakeContainersClient) GetCalledNames() []string {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
names := []string{}
|
||||
for _, detail := range f.called {
|
||||
names = append(names, detail.Name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// ClearCalls clear all call detail.
|
||||
func (f *FakeContainersClient) ClearCalls() {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.called = []CalledDetail{}
|
||||
}
|
||||
|
||||
// GetCalledDetails get detail of each call.
|
||||
func (f *FakeContainersClient) GetCalledDetails() []CalledDetail {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
// Copy the list and return.
|
||||
return append([]CalledDetail{}, f.called...)
|
||||
}
|
||||
|
||||
// Create is a test implementation of containers.Create.
|
||||
func (f *FakeContainersClient) Create(ctx context.Context, createOpts *containers.CreateContainerRequest, opts ...grpc.CallOption) (*containers.CreateContainerResponse, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("create", createOpts)
|
||||
if err := f.getError("create"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, ok := f.ContainerList[createOpts.Container.ID]
|
||||
if ok {
|
||||
return nil, fmt.Errorf("container already exists")
|
||||
}
|
||||
f.ContainerList[createOpts.Container.ID] = createOpts.Container
|
||||
return &containers.CreateContainerResponse{Container: createOpts.Container}, nil
|
||||
}
|
||||
|
||||
// Delete is a test implementation of containers.Delete
|
||||
func (f *FakeContainersClient) Delete(ctx context.Context, deleteOpts *containers.DeleteContainerRequest, opts ...grpc.CallOption) (*googleprotobuf.Empty, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("delete", deleteOpts)
|
||||
if err := f.getError("delete"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, ok := f.ContainerList[deleteOpts.ID]
|
||||
if !ok {
|
||||
return nil, ContainerNotExistError
|
||||
}
|
||||
delete(f.ContainerList, deleteOpts.ID)
|
||||
return &googleprotobuf.Empty{}, nil
|
||||
}
|
||||
|
||||
// Get is a test implementation of containers.Get
|
||||
func (f *FakeContainersClient) Get(ctx context.Context, getOpts *containers.GetContainerRequest, opts ...grpc.CallOption) (*containers.GetContainerResponse, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("get", getOpts)
|
||||
if err := f.getError("get"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c, ok := f.ContainerList[getOpts.ID]
|
||||
if !ok {
|
||||
return nil, ContainerNotExistError
|
||||
}
|
||||
return &containers.GetContainerResponse{Container: c}, nil
|
||||
}
|
||||
|
||||
// List is a test implementation of containers.List
|
||||
func (f *FakeContainersClient) List(ctx context.Context, listOpts *containers.ListContainersRequest, opts ...grpc.CallOption) (*containers.ListContainersResponse, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("list", listOpts)
|
||||
if err := f.getError("list"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var cs []containers.Container
|
||||
for _, c := range f.ContainerList {
|
||||
cs = append(cs, c)
|
||||
}
|
||||
return &containers.ListContainersResponse{Containers: cs}, nil
|
||||
}
|
||||
|
||||
// Update is a test implementation of containers.Update
|
||||
func (f *FakeContainersClient) Update(ctx context.Context, updateOpts *containers.UpdateContainerRequest, opts ...grpc.CallOption) (*containers.UpdateContainerResponse, error) {
|
||||
// TODO: implement Update()
|
||||
return nil, nil
|
||||
}
|
||||
@@ -1,392 +0,0 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package testing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/containerd/containerd/api/services/execution"
|
||||
"github.com/containerd/containerd/api/types/task"
|
||||
googleprotobuf "github.com/golang/protobuf/ptypes/empty"
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
)
|
||||
|
||||
// TaskNotExistError is the fake error returned when task does not exist.
|
||||
var TaskNotExistError = grpc.Errorf(codes.NotFound, "task does not exist")
|
||||
|
||||
// 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.Tasks_EventsClient = &EventClient{}
|
||||
|
||||
// EventClient is a test implementation of execution.Tasks_EventsClient
|
||||
type EventClient struct {
|
||||
Events chan *task.Event
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
// Recv is a test implementation of Recv
|
||||
func (cli *EventClient) Recv() (*task.Event, error) {
|
||||
event, ok := <-cli.Events
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("event channel closed")
|
||||
}
|
||||
return event, nil
|
||||
}
|
||||
|
||||
// FakeExecutionClient is a simple fake execution client, so that cri-containerd
|
||||
// can be run for testing without requiring a real containerd setup.
|
||||
type FakeExecutionClient struct {
|
||||
sync.Mutex
|
||||
called []CalledDetail
|
||||
errors map[string]error
|
||||
TaskList map[string]task.Task
|
||||
eventsQueue chan *task.Event
|
||||
eventClients []*EventClient
|
||||
}
|
||||
|
||||
var _ execution.TasksClient = &FakeExecutionClient{}
|
||||
|
||||
// NewFakeExecutionClient creates a FakeExecutionClient
|
||||
func NewFakeExecutionClient() *FakeExecutionClient {
|
||||
return &FakeExecutionClient{
|
||||
errors: make(map[string]error),
|
||||
TaskList: make(map[string]task.Task),
|
||||
}
|
||||
}
|
||||
|
||||
// Stop the fake execution service. Needed when event is enabled.
|
||||
func (f *FakeExecutionClient) Stop() {
|
||||
if f.eventsQueue != nil {
|
||||
close(f.eventsQueue)
|
||||
}
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
for _, client := range f.eventClients {
|
||||
close(client.Events)
|
||||
}
|
||||
f.eventClients = nil
|
||||
}
|
||||
|
||||
// WithEvents setup events publisher for FakeExecutionClient
|
||||
func (f *FakeExecutionClient) WithEvents() *FakeExecutionClient {
|
||||
f.eventsQueue = make(chan *task.Event, 1024)
|
||||
go func() {
|
||||
for e := range f.eventsQueue {
|
||||
f.Lock()
|
||||
for _, client := range f.eventClients {
|
||||
client.Events <- e
|
||||
}
|
||||
f.Unlock()
|
||||
}
|
||||
}()
|
||||
return f
|
||||
}
|
||||
|
||||
func (f *FakeExecutionClient) getError(op string) error {
|
||||
err, ok := f.errors[op]
|
||||
if ok {
|
||||
delete(f.errors, op)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InjectError inject error for call
|
||||
func (f *FakeExecutionClient) InjectError(fn string, err error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.errors[fn] = err
|
||||
}
|
||||
|
||||
// InjectErrors inject errors for calls
|
||||
func (f *FakeExecutionClient) InjectErrors(errs map[string]error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
for fn, err := range errs {
|
||||
f.errors[fn] = err
|
||||
}
|
||||
}
|
||||
|
||||
// ClearErrors clear errors for call
|
||||
func (f *FakeExecutionClient) ClearErrors() {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.errors = make(map[string]error)
|
||||
}
|
||||
|
||||
func generatePid() uint32 {
|
||||
rand.Seed(time.Now().Unix())
|
||||
randPid := uint32(rand.Intn(1000))
|
||||
return randPid
|
||||
}
|
||||
|
||||
func (f *FakeExecutionClient) sendEvent(event *task.Event) {
|
||||
if f.eventsQueue != nil {
|
||||
f.eventsQueue <- event
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FakeExecutionClient) appendCalled(name string, argument interface{}) {
|
||||
call := CalledDetail{Name: name, Argument: argument}
|
||||
f.called = append(f.called, call)
|
||||
}
|
||||
|
||||
// GetCalledNames get names of call
|
||||
func (f *FakeExecutionClient) GetCalledNames() []string {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
names := []string{}
|
||||
for _, detail := range f.called {
|
||||
names = append(names, detail.Name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// ClearCalls clear all call detail.
|
||||
func (f *FakeExecutionClient) ClearCalls() {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.called = []CalledDetail{}
|
||||
}
|
||||
|
||||
// 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...)
|
||||
}
|
||||
|
||||
// SetFakeTasks injects fake tasks.
|
||||
func (f *FakeExecutionClient) SetFakeTasks(tasks []task.Task) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
for _, t := range tasks {
|
||||
f.TaskList[t.ID] = t
|
||||
}
|
||||
}
|
||||
|
||||
// Create is a test implementation of execution.Create.
|
||||
func (f *FakeExecutionClient) Create(ctx context.Context, createOpts *execution.CreateRequest, opts ...grpc.CallOption) (*execution.CreateResponse, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("create", createOpts)
|
||||
if err := f.getError("create"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, ok := f.TaskList[createOpts.ContainerID]
|
||||
if ok {
|
||||
return nil, fmt.Errorf("task already exists")
|
||||
}
|
||||
pid := generatePid()
|
||||
f.TaskList[createOpts.ContainerID] = task.Task{
|
||||
ContainerID: createOpts.ContainerID,
|
||||
Pid: pid,
|
||||
Status: task.StatusCreated,
|
||||
}
|
||||
f.sendEvent(&task.Event{
|
||||
ID: createOpts.ContainerID,
|
||||
Type: task.Event_CREATE,
|
||||
Pid: pid,
|
||||
})
|
||||
return &execution.CreateResponse{
|
||||
ContainerID: createOpts.ContainerID,
|
||||
Pid: pid,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Start is a test implementation of execution.Start
|
||||
func (f *FakeExecutionClient) Start(ctx context.Context, startOpts *execution.StartRequest, opts ...grpc.CallOption) (*googleprotobuf.Empty, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("start", startOpts)
|
||||
if err := f.getError("start"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c, ok := f.TaskList[startOpts.ContainerID]
|
||||
if !ok {
|
||||
return nil, TaskNotExistError
|
||||
}
|
||||
f.sendEvent(&task.Event{
|
||||
ID: c.ID,
|
||||
Type: task.Event_START,
|
||||
Pid: c.Pid,
|
||||
})
|
||||
switch c.Status {
|
||||
case task.StatusCreated:
|
||||
c.Status = task.StatusRunning
|
||||
f.TaskList[startOpts.ContainerID] = c
|
||||
return &googleprotobuf.Empty{}, nil
|
||||
case task.StatusStopped:
|
||||
return &googleprotobuf.Empty{}, fmt.Errorf("cannot start a container that has stopped")
|
||||
case task.StatusRunning:
|
||||
return &googleprotobuf.Empty{}, fmt.Errorf("cannot start an already running container")
|
||||
default:
|
||||
return &googleprotobuf.Empty{}, fmt.Errorf("cannot start a container in the %s state", c.Status)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete is a test implementation of execution.Delete
|
||||
func (f *FakeExecutionClient) Delete(ctx context.Context, deleteOpts *execution.DeleteRequest, opts ...grpc.CallOption) (*execution.DeleteResponse, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("delete", deleteOpts)
|
||||
if err := f.getError("delete"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c, ok := f.TaskList[deleteOpts.ContainerID]
|
||||
if !ok {
|
||||
return nil, TaskNotExistError
|
||||
}
|
||||
delete(f.TaskList, deleteOpts.ContainerID)
|
||||
f.sendEvent(&task.Event{
|
||||
ID: c.ID,
|
||||
Type: task.Event_EXIT,
|
||||
Pid: c.Pid,
|
||||
})
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Info is a test implementation of execution.Info
|
||||
func (f *FakeExecutionClient) Info(ctx context.Context, infoOpts *execution.InfoRequest, opts ...grpc.CallOption) (*execution.InfoResponse, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("info", infoOpts)
|
||||
if err := f.getError("info"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c, ok := f.TaskList[infoOpts.ContainerID]
|
||||
if !ok {
|
||||
return nil, TaskNotExistError
|
||||
}
|
||||
return &execution.InfoResponse{Task: &c}, nil
|
||||
}
|
||||
|
||||
// List is a test implementation of execution.List
|
||||
func (f *FakeExecutionClient) List(ctx context.Context, listOpts *execution.ListRequest, opts ...grpc.CallOption) (*execution.ListResponse, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("list", listOpts)
|
||||
if err := f.getError("list"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp := &execution.ListResponse{}
|
||||
for _, c := range f.TaskList {
|
||||
resp.Tasks = append(resp.Tasks, &task.Task{
|
||||
ID: c.ID,
|
||||
Pid: c.Pid,
|
||||
Status: c.Status,
|
||||
})
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Kill is a test implementation of execution.Kill
|
||||
func (f *FakeExecutionClient) Kill(ctx context.Context, killOpts *execution.KillRequest, opts ...grpc.CallOption) (*googleprotobuf.Empty, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("kill", killOpts)
|
||||
if err := f.getError("kill"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c, ok := f.TaskList[killOpts.ContainerID]
|
||||
if !ok {
|
||||
return nil, TaskNotExistError
|
||||
}
|
||||
c.Status = task.StatusStopped
|
||||
f.TaskList[killOpts.ContainerID] = c
|
||||
f.sendEvent(&task.Event{
|
||||
ID: c.ID,
|
||||
Type: task.Event_EXIT,
|
||||
Pid: c.Pid,
|
||||
})
|
||||
return &googleprotobuf.Empty{}, nil
|
||||
}
|
||||
|
||||
// Events is a test implementation of execution.Events
|
||||
func (f *FakeExecutionClient) Events(ctx context.Context, eventsOpts *execution.EventsRequest, opts ...grpc.CallOption) (execution.Tasks_EventsClient, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("events", eventsOpts)
|
||||
if err := f.getError("events"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var client = &EventClient{
|
||||
Events: make(chan *task.Event, 100),
|
||||
}
|
||||
f.eventClients = append(f.eventClients, client)
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// Exec is a test implementation of execution.Exec
|
||||
func (f *FakeExecutionClient) Exec(ctx context.Context, execOpts *execution.ExecRequest, opts ...grpc.CallOption) (*execution.ExecResponse, error) {
|
||||
// TODO: implement Exec()
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Pty is a test implementation of execution.Pty
|
||||
func (f *FakeExecutionClient) Pty(ctx context.Context, ptyOpts *execution.PtyRequest, opts ...grpc.CallOption) (*googleprotobuf.Empty, error) {
|
||||
// TODO: implement Pty()
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// CloseStdin is a test implementation of execution.CloseStdin
|
||||
func (f *FakeExecutionClient) CloseStdin(ctx context.Context, closeStdinOpts *execution.CloseStdinRequest, opts ...grpc.CallOption) (*googleprotobuf.Empty, error) {
|
||||
// TODO: implement CloseStdin()
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Pause is a test implementation of execution.Pause
|
||||
func (f *FakeExecutionClient) Pause(ctx context.Context, in *execution.PauseRequest, opts ...grpc.CallOption) (*googleprotobuf.Empty, error) {
|
||||
// TODO: implement Pause()
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Resume is a test implementation of execution.Resume
|
||||
func (f *FakeExecutionClient) Resume(ctx context.Context, in *execution.ResumeRequest, opts ...grpc.CallOption) (*googleprotobuf.Empty, error) {
|
||||
// TODO: implement Resume()
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Checkpoint is a test implementation of execution.Checkpoint
|
||||
func (f *FakeExecutionClient) Checkpoint(ctx context.Context, in *execution.CheckpointRequest, opts ...grpc.CallOption) (*execution.CheckpointResponse, error) {
|
||||
// TODO: implement Checkpoint()
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Processes is a test implementation of execution.Processes
|
||||
func (f *FakeExecutionClient) Processes(ctx context.Context, in *execution.ProcessesRequest, opts ...grpc.CallOption) (*execution.ProcessesResponse, error) {
|
||||
// TODO: implement Processes()
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// DeleteProcess is a test implementation of execution.DeleteProcess
|
||||
func (f *FakeExecutionClient) DeleteProcess(ctx context.Context, in *execution.DeleteProcessRequest, opts ...grpc.CallOption) (*execution.DeleteResponse, error) {
|
||||
// TODO: implement DeleteProcess()
|
||||
return nil, nil
|
||||
}
|
||||
@@ -1,164 +0,0 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package testing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/containerd/containerd/api/services/images"
|
||||
googleprotobuf "github.com/golang/protobuf/ptypes/empty"
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// FakeImagesClient is a simple fake images client, so that cri-containerd
|
||||
// can be run for testing without requiring a real containerd setup.
|
||||
type FakeImagesClient struct {
|
||||
sync.Mutex
|
||||
called []CalledDetail
|
||||
errors map[string]error
|
||||
ImageList map[string]images.Image
|
||||
}
|
||||
|
||||
var _ images.ImagesClient = &FakeImagesClient{}
|
||||
|
||||
// NewFakeImagesClient creates a FakeImagesClient
|
||||
func NewFakeImagesClient() *FakeImagesClient {
|
||||
return &FakeImagesClient{
|
||||
errors: make(map[string]error),
|
||||
ImageList: make(map[string]images.Image),
|
||||
}
|
||||
}
|
||||
|
||||
// getError get error for call
|
||||
func (f *FakeImagesClient) getError(op string) error {
|
||||
err, ok := f.errors[op]
|
||||
if ok {
|
||||
delete(f.errors, op)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InjectError inject error for call
|
||||
func (f *FakeImagesClient) InjectError(fn string, err error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.errors[fn] = err
|
||||
}
|
||||
|
||||
// InjectErrors inject errors for calls
|
||||
func (f *FakeImagesClient) InjectErrors(errs map[string]error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
for fn, err := range errs {
|
||||
f.errors[fn] = err
|
||||
}
|
||||
}
|
||||
|
||||
// ClearErrors clear errors for call
|
||||
func (f *FakeImagesClient) ClearErrors() {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.errors = make(map[string]error)
|
||||
}
|
||||
|
||||
func (f *FakeImagesClient) appendCalled(name string, argument interface{}) {
|
||||
call := CalledDetail{Name: name, Argument: argument}
|
||||
f.called = append(f.called, call)
|
||||
}
|
||||
|
||||
// GetCalledNames get names of call
|
||||
func (f *FakeImagesClient) GetCalledNames() []string {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
names := []string{}
|
||||
for _, detail := range f.called {
|
||||
names = append(names, detail.Name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// SetFakeImages injects fake images.
|
||||
func (f *FakeImagesClient) SetFakeImages(images []images.Image) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
for _, image := range images {
|
||||
f.ImageList[image.Name] = image
|
||||
}
|
||||
}
|
||||
|
||||
// Get is a test implementation of images.Get
|
||||
func (f *FakeImagesClient) Get(ctx context.Context, getOpts *images.GetRequest, opts ...grpc.CallOption) (*images.GetResponse, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("get", getOpts)
|
||||
if err := f.getError("get"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
image, ok := f.ImageList[getOpts.Name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("image does not exist")
|
||||
}
|
||||
return &images.GetResponse{
|
||||
Image: &image,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Put is a test implementation of images.Put
|
||||
func (f *FakeImagesClient) Put(ctx context.Context, putOpts *images.PutRequest, opts ...grpc.CallOption) (*googleprotobuf.Empty, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("put", putOpts)
|
||||
if err := f.getError("put"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f.ImageList[putOpts.Image.Name] = putOpts.Image
|
||||
return &googleprotobuf.Empty{}, nil
|
||||
}
|
||||
|
||||
// List is a test implementation of images.List
|
||||
func (f *FakeImagesClient) List(ctx context.Context, listOpts *images.ListRequest, opts ...grpc.CallOption) (*images.ListResponse, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("list", listOpts)
|
||||
if err := f.getError("list"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp := &images.ListResponse{}
|
||||
for _, image := range f.ImageList {
|
||||
resp.Images = append(resp.Images, image)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Delete is a test implementation of images.Delete
|
||||
func (f *FakeImagesClient) Delete(ctx context.Context, deleteOpts *images.DeleteRequest, opts ...grpc.CallOption) (*googleprotobuf.Empty, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("delete", deleteOpts)
|
||||
if err := f.getError("delete"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, ok := f.ImageList[deleteOpts.Name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("image does not exist")
|
||||
}
|
||||
delete(f.ImageList, deleteOpts.Name)
|
||||
return &googleprotobuf.Empty{}, nil
|
||||
}
|
||||
@@ -1,221 +0,0 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package testing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/containerd/containerd/api/services/snapshot"
|
||||
"github.com/containerd/containerd/api/types/mount"
|
||||
google_protobuf1 "github.com/golang/protobuf/ptypes/empty"
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
)
|
||||
|
||||
// SnapshotNotExistError is the fake error returned when snapshot does not exist.
|
||||
var SnapshotNotExistError = grpc.Errorf(codes.NotFound, "snapshot does not exist")
|
||||
|
||||
// FakeSnapshotClient is a simple fake snapshot client, so that cri-containerd
|
||||
// can be run for testing without requiring a real containerd setup.
|
||||
type FakeSnapshotClient struct {
|
||||
sync.Mutex
|
||||
called []CalledDetail
|
||||
errors map[string]error
|
||||
MountList map[string][]*mount.Mount
|
||||
}
|
||||
|
||||
var _ snapshot.SnapshotClient = &FakeSnapshotClient{}
|
||||
|
||||
// NewFakeSnapshotClient creates a FakeSnapshotClient
|
||||
func NewFakeSnapshotClient() *FakeSnapshotClient {
|
||||
return &FakeSnapshotClient{
|
||||
errors: make(map[string]error),
|
||||
MountList: make(map[string][]*mount.Mount),
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FakeSnapshotClient) getError(op string) error {
|
||||
err, ok := f.errors[op]
|
||||
if ok {
|
||||
delete(f.errors, op)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InjectError inject error for call
|
||||
func (f *FakeSnapshotClient) InjectError(fn string, err error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.errors[fn] = err
|
||||
}
|
||||
|
||||
// InjectErrors inject errors for calls
|
||||
func (f *FakeSnapshotClient) InjectErrors(errs map[string]error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
for fn, err := range errs {
|
||||
f.errors[fn] = err
|
||||
}
|
||||
}
|
||||
|
||||
// ClearErrors clear errors for call
|
||||
func (f *FakeSnapshotClient) ClearErrors() {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.errors = make(map[string]error)
|
||||
}
|
||||
|
||||
func (f *FakeSnapshotClient) appendCalled(name string, argument interface{}) {
|
||||
call := CalledDetail{Name: name, Argument: argument}
|
||||
f.called = append(f.called, call)
|
||||
}
|
||||
|
||||
// GetCalledNames get names of call
|
||||
func (f *FakeSnapshotClient) GetCalledNames() []string {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
names := []string{}
|
||||
for _, detail := range f.called {
|
||||
names = append(names, detail.Name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// GetCalledDetails get detail of each call.
|
||||
func (f *FakeSnapshotClient) GetCalledDetails() []CalledDetail {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
// Copy the list and return.
|
||||
return append([]CalledDetail{}, f.called...)
|
||||
}
|
||||
|
||||
// SetFakeMounts injects fake mounts.
|
||||
func (f *FakeSnapshotClient) SetFakeMounts(name string, mounts []*mount.Mount) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.MountList[name] = mounts
|
||||
}
|
||||
|
||||
// ListMounts lists all the fake mounts.
|
||||
func (f *FakeSnapshotClient) ListMounts() [][]*mount.Mount {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
var ms [][]*mount.Mount
|
||||
for _, m := range f.MountList {
|
||||
ms = append(ms, m)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
|
||||
// Prepare is a test implementation of snapshot.Prepare
|
||||
func (f *FakeSnapshotClient) Prepare(ctx context.Context, prepareOpts *snapshot.PrepareRequest, opts ...grpc.CallOption) (*snapshot.MountsResponse, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("prepare", prepareOpts)
|
||||
if err := f.getError("prepare"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, ok := f.MountList[prepareOpts.Key]
|
||||
if ok {
|
||||
return nil, fmt.Errorf("mounts already exist")
|
||||
}
|
||||
f.MountList[prepareOpts.Key] = []*mount.Mount{{
|
||||
Type: "bind",
|
||||
Source: prepareOpts.Key,
|
||||
// TODO(random-liu): Fake options based on Readonly option.
|
||||
}}
|
||||
return &snapshot.MountsResponse{
|
||||
Mounts: f.MountList[prepareOpts.Key],
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Mounts is a test implementation of snapshot.Mounts
|
||||
func (f *FakeSnapshotClient) Mounts(ctx context.Context, mountsOpts *snapshot.MountsRequest, opts ...grpc.CallOption) (*snapshot.MountsResponse, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("mounts", mountsOpts)
|
||||
if err := f.getError("mounts"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mounts, ok := f.MountList[mountsOpts.Key]
|
||||
if !ok {
|
||||
return nil, SnapshotNotExistError
|
||||
}
|
||||
return &snapshot.MountsResponse{
|
||||
Mounts: mounts,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Commit is a test implementation of snapshot.Commit
|
||||
func (f *FakeSnapshotClient) Commit(ctx context.Context, in *snapshot.CommitRequest, opts ...grpc.CallOption) (*google_protobuf1.Empty, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// View is a test implementation of snapshot.View
|
||||
func (f *FakeSnapshotClient) View(ctx context.Context, viewOpts *snapshot.PrepareRequest, opts ...grpc.CallOption) (*snapshot.MountsResponse, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("view", viewOpts)
|
||||
if err := f.getError("view"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, ok := f.MountList[viewOpts.Key]
|
||||
if ok {
|
||||
return nil, fmt.Errorf("mounts already exist")
|
||||
}
|
||||
f.MountList[viewOpts.Key] = []*mount.Mount{{
|
||||
Type: "bind",
|
||||
Source: viewOpts.Key,
|
||||
// TODO(random-liu): Fake options based on Readonly option.
|
||||
}}
|
||||
return &snapshot.MountsResponse{
|
||||
Mounts: f.MountList[viewOpts.Key],
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Remove is a test implementation of snapshot.Remove
|
||||
func (f *FakeSnapshotClient) Remove(ctx context.Context, removeOpts *snapshot.RemoveRequest, opts ...grpc.CallOption) (*google_protobuf1.Empty, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
f.appendCalled("remove", removeOpts)
|
||||
if err := f.getError("remove"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, ok := f.MountList[removeOpts.Key]; !ok {
|
||||
return nil, SnapshotNotExistError
|
||||
}
|
||||
delete(f.MountList, removeOpts.Key)
|
||||
return &google_protobuf1.Empty{}, nil
|
||||
}
|
||||
|
||||
// Stat is a test implementation of snapshot.Stat
|
||||
func (f *FakeSnapshotClient) Stat(ctx context.Context, in *snapshot.StatRequest, opts ...grpc.CallOption) (*snapshot.StatResponse, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// List is a test implementation of snapshot.List
|
||||
func (f *FakeSnapshotClient) List(ctx context.Context, in *snapshot.ListRequest, opts ...grpc.CallOption) (snapshot.Snapshot_ListClient, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Usage is a test implementation of snapshot.Usage
|
||||
func (f *FakeSnapshotClient) Usage(ctx context.Context, in *snapshot.UsageRequest, opts ...grpc.CallOption) (*snapshot.UsageResponse, error) {
|
||||
return nil, nil
|
||||
}
|
||||
Reference in New Issue
Block a user