...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/test/mockclient.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/test

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package test
    17  
    18  import (
    19  	"context"
    20  	"syscall"
    21  
    22  	"github.com/containerd/containerd"
    23  	"github.com/containerd/containerd/cio"
    24  	"github.com/docker/docker/api/types"
    25  
    26  	"github.com/chaos-mesh/chaos-mesh/pkg/mock"
    27  )
    28  
    29  type MockClient struct{}
    30  
    31  func (m *MockClient) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
    32  	if err := mock.On("ContainerInspectError"); err != nil {
    33  		return types.ContainerJSON{}, err.(error)
    34  	}
    35  
    36  	var pid int
    37  	if p := mock.On("pid"); p != nil {
    38  		pid = p.(int)
    39  	}
    40  	return types.ContainerJSON{
    41  		ContainerJSONBase: &types.ContainerJSONBase{
    42  			State: &types.ContainerState{
    43  				Pid: pid,
    44  			},
    45  		},
    46  	}, nil
    47  }
    48  
    49  func (m *MockClient) ContainerKill(ctx context.Context, containerID, signal string) error {
    50  	if err := mock.On("ContainerKillError"); err != nil {
    51  		return err.(error)
    52  	}
    53  	return nil
    54  }
    55  
    56  func (m *MockClient) LoadContainer(ctx context.Context, id string) (containerd.Container, error) {
    57  	if err := mock.On("LoadContainerError"); err != nil {
    58  		return nil, err.(error)
    59  	}
    60  
    61  	return &MockContainer{}, nil
    62  }
    63  
    64  type MockContainer struct {
    65  	containerd.Container
    66  }
    67  
    68  func (m *MockContainer) Task(context.Context, cio.Attach) (containerd.Task, error) {
    69  	if err := mock.On("TaskError"); err != nil {
    70  		return nil, err.(error)
    71  	}
    72  
    73  	return &MockTask{}, nil
    74  }
    75  
    76  type MockTask struct {
    77  	containerd.Task
    78  }
    79  
    80  func (m *MockTask) Pid() uint32 {
    81  	var pid int
    82  	if p := mock.On("pid"); p != nil {
    83  		pid = p.(int)
    84  	}
    85  	return uint32(pid)
    86  }
    87  
    88  func (m *MockTask) Kill(context.Context, syscall.Signal, ...containerd.KillOpts) error {
    89  	if err := mock.On("KillError"); err != nil {
    90  		return err.(error)
    91  	}
    92  	return nil
    93  }
    94