...

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  	"github.com/docker/docker/api/types/container"
    26  
    27  	"github.com/chaos-mesh/chaos-mesh/pkg/mock"
    28  )
    29  
    30  type MockClient struct{}
    31  
    32  func (m *MockClient) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
    33  	if err := mock.On("ContainerInspectError"); err != nil {
    34  		return types.ContainerJSON{}, err.(error)
    35  	}
    36  
    37  	containerJSON := types.ContainerJSON{}
    38  	if pid := mock.On("pid"); pid != nil {
    39  		containerJSON.ContainerJSONBase = &types.ContainerJSONBase{
    40  			State: &types.ContainerState{
    41  				Pid: pid.(int),
    42  			},
    43  		}
    44  	}
    45  
    46  	if labels := mock.On("labels"); labels != nil {
    47  		containerJSON.Config = &container.Config{
    48  			Labels: labels.(map[string]string),
    49  		}
    50  	}
    51  
    52  	return containerJSON, nil
    53  }
    54  
    55  func (m *MockClient) ContainerKill(ctx context.Context, containerID, signal string) error {
    56  	if err := mock.On("ContainerKillError"); err != nil {
    57  		return err.(error)
    58  	}
    59  	return nil
    60  }
    61  
    62  func (m *MockClient) LoadContainer(ctx context.Context, id string) (containerd.Container, error) {
    63  	if err := mock.On("LoadContainerError"); err != nil {
    64  		return nil, err.(error)
    65  	}
    66  
    67  	return &MockContainer{}, nil
    68  }
    69  
    70  func (m *MockClient) ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) {
    71  	if err := mock.On("ContainerListError"); err != nil {
    72  		return nil, err.(error)
    73  	}
    74  
    75  	c := types.Container{}
    76  	if id := mock.On("containerID"); id != nil {
    77  		c.ID = id.(string)
    78  	}
    79  
    80  	return []types.Container{c}, nil
    81  }
    82  
    83  func (m *MockClient) Containers(ctx context.Context, filters ...string) ([]containerd.Container, error) {
    84  	if err := mock.On("ContainersError"); err != nil {
    85  		return nil, err.(error)
    86  	}
    87  
    88  	return []containerd.Container{&MockContainer{}}, nil
    89  }
    90  
    91  type MockContainer struct {
    92  	containerd.Container
    93  }
    94  
    95  func (m *MockContainer) Task(context.Context, cio.Attach) (containerd.Task, error) {
    96  	if err := mock.On("TaskError"); err != nil {
    97  		return nil, err.(error)
    98  	}
    99  
   100  	return &MockTask{}, nil
   101  }
   102  
   103  func (m *MockContainer) ID() string {
   104  	if err := mock.On("IDError"); err != nil {
   105  		return ""
   106  	}
   107  
   108  	if id := mock.On("containerID"); id != nil {
   109  		return id.(string)
   110  	}
   111  	return ""
   112  }
   113  
   114  func (m *MockContainer) Labels(ctx context.Context) (map[string]string, error) {
   115  	if err := mock.On("LabelsError"); err != nil {
   116  		return nil, err.(error)
   117  	}
   118  
   119  	if labels := mock.On("labels"); labels != nil {
   120  		return labels.(map[string]string), nil
   121  	}
   122  	return nil, nil
   123  }
   124  
   125  type MockTask struct {
   126  	containerd.Task
   127  }
   128  
   129  func (m *MockTask) Pid() uint32 {
   130  	var pid int
   131  	if p := mock.On("pid"); p != nil {
   132  		pid = p.(int)
   133  	}
   134  	return uint32(pid)
   135  }
   136  
   137  func (m *MockTask) Kill(context.Context, syscall.Signal, ...containerd.KillOpts) error {
   138  	if err := mock.On("KillError"); err != nil {
   139  		return err.(error)
   140  	}
   141  	return nil
   142  }
   143