...

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 2020 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package test
    15  
    16  import (
    17  	"context"
    18  	"syscall"
    19  
    20  	"github.com/containerd/containerd"
    21  	"github.com/containerd/containerd/cio"
    22  	"github.com/docker/docker/api/types"
    23  
    24  	"github.com/chaos-mesh/chaos-mesh/pkg/mock"
    25  )
    26  
    27  type MockClient struct{}
    28  
    29  func (m *MockClient) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
    30  	if err := mock.On("ContainerInspectError"); err != nil {
    31  		return types.ContainerJSON{}, err.(error)
    32  	}
    33  
    34  	var pid int
    35  	if p := mock.On("pid"); p != nil {
    36  		pid = p.(int)
    37  	}
    38  	return types.ContainerJSON{
    39  		ContainerJSONBase: &types.ContainerJSONBase{
    40  			State: &types.ContainerState{
    41  				Pid: pid,
    42  			},
    43  		},
    44  	}, nil
    45  }
    46  
    47  func (m *MockClient) ContainerKill(ctx context.Context, containerID, signal string) error {
    48  	if err := mock.On("ContainerKillError"); err != nil {
    49  		return err.(error)
    50  	}
    51  	return nil
    52  }
    53  
    54  func (m *MockClient) LoadContainer(ctx context.Context, id string) (containerd.Container, error) {
    55  	if err := mock.On("LoadContainerError"); err != nil {
    56  		return nil, err.(error)
    57  	}
    58  
    59  	return &MockContainer{}, nil
    60  }
    61  
    62  type MockContainer struct {
    63  	containerd.Container
    64  }
    65  
    66  func (m *MockContainer) Task(context.Context, cio.Attach) (containerd.Task, error) {
    67  	if err := mock.On("TaskError"); err != nil {
    68  		return nil, err.(error)
    69  	}
    70  
    71  	return &MockTask{}, nil
    72  }
    73  
    74  type MockTask struct {
    75  	containerd.Task
    76  }
    77  
    78  func (m *MockTask) Pid() uint32 {
    79  	var pid int
    80  	if p := mock.On("pid"); p != nil {
    81  		pid = p.(int)
    82  	}
    83  	return uint32(pid)
    84  }
    85  
    86  func (m *MockTask) Kill(context.Context, syscall.Signal, ...containerd.KillOpts) error {
    87  	if err := mock.On("KillError"); err != nil {
    88  		return err.(error)
    89  	}
    90  	return nil
    91  }
    92