github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/engine/mocks/fakeengine/mock.go (about)

     1  package fakeengine
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"context"
     7  	"fmt"
     8  	"io"
     9  
    10  	"github.com/docker/go-units"
    11  	"github.com/mitchellh/mapstructure"
    12  	"github.com/sanity-io/litter"
    13  	mock "github.com/stretchr/testify/mock"
    14  
    15  	"github.com/projecteru2/core/engine"
    16  	enginemocks "github.com/projecteru2/core/engine/mocks"
    17  	enginetypes "github.com/projecteru2/core/engine/types"
    18  	resourcetypes "github.com/projecteru2/core/resource/types"
    19  	coretypes "github.com/projecteru2/core/types"
    20  	"github.com/projecteru2/core/utils"
    21  )
    22  
    23  const (
    24  	// PrefixKey indicate key prefix
    25  	PrefixKey = "mock://"
    26  )
    27  
    28  type writeCloser struct {
    29  	*bufio.Writer
    30  }
    31  
    32  // Close close
    33  func (wc *writeCloser) Close() error {
    34  	// Noop
    35  	return nil
    36  }
    37  
    38  // MakeClient make a mock client
    39  func MakeClient(_ context.Context, _ coretypes.Config, _, _, _, _, _ string) (engine.API, error) {
    40  	e := &enginemocks.API{}
    41  	// info
    42  	e.On("Info", mock.Anything).Return(&enginetypes.Info{NCPU: 100, MemTotal: units.GiB * 100, StorageTotal: units.GiB * 100}, nil)
    43  	e.On("Ping", mock.Anything).Return(nil)
    44  	// exec
    45  	var execID string
    46  	e.On("Execute", mock.Anything, mock.Anything, mock.Anything).Return(
    47  		func(context.Context, string, *enginetypes.ExecConfig) string {
    48  			return utils.RandomString(64)
    49  		},
    50  		func(context.Context, string, *enginetypes.ExecConfig) io.ReadCloser {
    51  			return io.NopCloser(bytes.NewBufferString(utils.RandomString(128)))
    52  		},
    53  		func(context.Context, string, *enginetypes.ExecConfig) io.ReadCloser {
    54  			return io.NopCloser(bytes.NewBufferString(utils.RandomString(128)))
    55  		},
    56  		func(context.Context, string, *enginetypes.ExecConfig) io.WriteCloser {
    57  			return &writeCloser{bufio.NewWriter(bytes.NewBuffer([]byte{}))}
    58  		},
    59  		nil,
    60  	)
    61  	e.On("ExecResize", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
    62  	e.On("ExecExitCode", mock.Anything, execID).Return(0, nil)
    63  	// network
    64  	e.On("NetworkConnect", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]string{}, nil)
    65  	e.On("NetworkDisconnect", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
    66  	e.On("NetworkList", mock.Anything, mock.Anything).Return([]*enginetypes.Network{{
    67  		Name: "mock-network", Subnets: []string{"1.1.1.1/8", "2.2.2.2/8"},
    68  	}}, nil)
    69  	// image
    70  	e.On("ImageList", mock.Anything, mock.Anything).Return(
    71  		[]*enginetypes.Image{{ID: "mock-image", Tags: []string{"latest"}}}, nil)
    72  	e.On("ImageRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(
    73  		[]string{"mock-image1", "mock-image2"}, nil)
    74  	e.On("ImagesPrune", mock.Anything).Return(nil)
    75  	pullImageData := io.NopCloser(bytes.NewBufferString("pull image layer1 ...\npull image layer2...\n"))
    76  	e.On("ImagePull", mock.Anything, mock.Anything, mock.Anything).Return(pullImageData, nil)
    77  	pushImageData := io.NopCloser(bytes.NewBufferString("{\"stream\":\"push something...\"}\n"))
    78  	e.On("ImagePush", mock.Anything, mock.Anything).Return(pushImageData, nil)
    79  	buildImageData := io.NopCloser(bytes.NewBufferString("{\"stream\":\"build something...\"}\n"))
    80  	e.On("ImageBuild", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(buildImageData, nil)
    81  	e.On("ImageBuildCachePrune", mock.Anything, mock.Anything).Return(uint64(0), nil)
    82  	imageDigest := utils.RandomString(64)
    83  	e.On("ImageLocalDigests", mock.Anything, mock.Anything).Return([]string{imageDigest}, nil)
    84  	e.On("ImageRemoteDigest", mock.Anything, mock.Anything).Return(imageDigest, nil)
    85  	e.On("ImageBuildFromExist", mock.Anything, mock.Anything, mock.Anything).Return("ImageBuildFromExist", nil)
    86  	// build
    87  	e.On("BuildRefs", mock.Anything, mock.Anything, mock.Anything).Return([]string{"ref1", "ref2"})
    88  	buildContent := io.NopCloser(bytes.NewBufferString("this is content"))
    89  	e.On("BuildContent", mock.Anything, mock.Anything, mock.Anything).Return("BuildContent", buildContent, nil)
    90  	// virtualization
    91  	var ID string
    92  	e.On("VirtualizationCreate", mock.Anything, mock.Anything).Return(func(_ context.Context, opts *enginetypes.VirtualizationCreateOptions) *enginetypes.VirtualizationCreated {
    93  		type virtualizationResource struct {
    94  			CPU           map[string]int64            `json:"cpu_map" mapstructure:"cpu_map"` // for cpu binding
    95  			Quota         float64                     `json:"cpu" mapstructure:"cpu"`         // for cpu quota
    96  			Memory        int64                       `json:"memory" mapstructure:"memory"`   // for memory binding
    97  			Storage       int64                       `json:"storage" mapstructure:"storage"`
    98  			NUMANode      string                      `json:"numa_node" mapstructure:"numa_node"` // numa node
    99  			Volumes       []string                    `json:"volumes" mapstructure:"volumes"`
   100  			VolumePlan    map[string]map[string]int64 `json:"volume_plan" mapstructure:"volume_plan"`       // literal VolumePlan
   101  			VolumeChanged bool                        `json:"volume_changed" mapstructure:"volume_changed"` // indicate whether new volumes contained in realloc request
   102  			IOPSOptions   map[string]string           `json:"iops_options" mapstructure:"IOPS_options"`     // format: {device_name: "read-IOPS:write-IOPS:read-bps:write-bps"}
   103  			Remap         bool                        `json:"remap" mapstructure:"remap"`
   104  		}
   105  
   106  		// parse engine args to resource options
   107  		resourceOpts := &virtualizationResource{}
   108  		_ = engine.MakeVirtualizationResource(opts.EngineParams, resourceOpts, func(p resourcetypes.Resources, d *virtualizationResource) error {
   109  			for _, v := range p {
   110  				if err := mapstructure.Decode(v, d); err != nil {
   111  					return err
   112  				}
   113  			}
   114  			return nil
   115  		})
   116  		litter.Dump(resourceOpts)
   117  		ID = utils.RandomString(64)
   118  		return &enginetypes.VirtualizationCreated{ID: ID, Name: "mock-test-cvm" + utils.RandomString(6)}
   119  	}, nil)
   120  	e.On("VirtualizationCopyChunkTo", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
   121  	e.On("VirtualizationStart", mock.Anything, mock.Anything).Return(nil)
   122  	e.On("VirtualizationStop", mock.Anything, mock.Anything, mock.Anything).Return(nil)
   123  	e.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
   124  	e.On("VirtualizationSuspend", mock.Anything, mock.Anything).Return(nil)
   125  	e.On("VirtualizationResume", mock.Anything, mock.Anything).Return(nil)
   126  	vcJSON := &enginetypes.VirtualizationInfo{ID: ID, Image: "mock-image", Running: true, Networks: map[string]string{"mock-network": "1.1.1.1"}}
   127  	e.On("VirtualizationInspect", mock.Anything, mock.Anything).Return(vcJSON, nil)
   128  	logs := io.NopCloser(bytes.NewBufferString("logs1...\nlogs2...\n"))
   129  	e.On("VirtualizationLogs", mock.Anything, mock.Anything).Return(logs, logs, nil)
   130  	attachData := io.NopCloser(bytes.NewBufferString("logs1...\nlogs2...\n"))
   131  	writeBuffer := &writeCloser{bufio.NewWriter(bytes.NewBuffer([]byte{}))}
   132  	e.On("VirtualizationAttach", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(attachData, attachData, writeBuffer, nil)
   133  	e.On("VirtualizationResize", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
   134  	e.On("VirtualizationWait", mock.Anything, mock.Anything, mock.Anything).Return(&enginetypes.VirtualizationWaitResult{Message: "", Code: 0}, nil)
   135  	e.On("VirtualizationUpdateResource", mock.Anything, mock.Anything, mock.Anything).Return(
   136  		func(_ context.Context, ID string, params resourcetypes.Resources) error {
   137  			fmt.Println(ID)
   138  			litter.Dump(params)
   139  			return nil
   140  		},
   141  	)
   142  	e.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return([]byte("d1...\nd2...\n"), 0, 0, int64(0), nil)
   143  	return e, nil
   144  }