mocks

package
v0.2.2-beta Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2021 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package mocks contains mock implementation of CSI methods for test purposes

Index

Constants

This section is empty.

Variables

View Source
var (
	// HDDBlockDeviceName contains name of HDD device
	HDDBlockDeviceName = "/dev/sdb"
	// LsblkAllV2 provides output for new lsblk version with children
	LsblkAllV2 = `{
   		"blockdevices": [
      		{"name":"` + HDDBlockDeviceName + `", "type":"disk", "size":480103981056, "rota":false, "serial":"PHYH937100WD480K",
"wwn":"0x55cd2e415119aed8", "vendor":"ATA     ", "model":"SSDSCKKB480G8R", "rev":"DL6N", "mountpoint":null, "fstype":null, "partuuid":null,
         		"children": [            
            		{"name":"/dev/sdc", "type":"part", "size":479563087872, "rota":false, "serial":null, "wwn":"0x55cd2e415119aed8",
"vendor":null, "model":null, "rev":null, "mountpoint":null, "fstype":"LVM2_member", "partuuid":"40b8a8aa-54fc-4725-9599-91f55a309c83",
               		"children": [
                		  {"name":"/dev/mapper/root--vg-lv_root", "type":"lvm", "size":53687091200, "rota":false, "serial":null, 
"wwn":null, "vendor":null, "model":null, "rev":null, "mountpoint":null, "fstype":"ext4", "partuuid":null}                  
               		]
            		}
        	 	]
      		},
    	  	{"name":"/dev/sdc", "type":"disk", "size":8001563222016, "rota":true, "serial":"5000cca0bbcea5c7", "wwn":"0x5000cca0bbcea5c7",
"vendor":"ATA     ", "model":"HGST_HUS728T8TAL", "rev":"RT04", "mountpoint":null, "fstype":null, "partuuid":null}
   			]
		}`
)
View Source
var (
	RunCmd             = "RunCmd"
	RunCmdWithAttempts = "RunCmdWithAttempts"
)

RunCmd is the name of CmdExecutor method name

View Source
var DiskCommands = map[string]CmdOut{
	"partprobe -d -s /dev/sda": {
		Stdout: "(no output)",
		Stderr: "",
		Err:    nil,
	},
	"partprobe -d -s /dev/sdb": {
		Stdout: "/dev/sda: msdos partitions 1",
		Stderr: "",
		Err:    nil,
	},
	"partprobe -d -s /dev/sdc": {
		Stdout: "/dev/sda: msdos partitions",
		Stderr: "",
		Err:    nil,
	},
	"partprobe -d -s /dev/sdd": {
		Stdout: "",
		Stderr: "",
		Err:    errors.New("unable to check partition existence for /dev/sdd"),
	},
	"partprobe -d -s /dev/sde": EmptyOutSuccess,
	"partprobe /dev/sde":       EmptyOutSuccess,
	"partprobe /dev/sda":       EmptyOutSuccess,
	"partprobe -d -s /dev/sdqwe": {
		Stdout: "",
		Stderr: "",
		Err:    errors.New("unable to get partition table"),
	},
	"partprobe":                      EmptyOutSuccess,
	"parted -s /dev/sda mklabel gpt": EmptyOutSuccess,
	"parted -s /dev/sdd mklabel gpt": {
		Stdout: "",
		Stderr: "",
		Err:    errors.New("unable to create partition table"),
	},
	"parted -s /dev/sdc mklabel gpt":                        EmptyOutSuccess,
	"parted -s /dev/sda rm 1":                               EmptyOutSuccess,
	"parted -s /dev/sdb rm 1":                               EmptyOutFail,
	"parted -s /dev/sde mkpart --align optimal CSI 0% 100%": EmptyOutSuccess,
	"parted -s /dev/sdf mkpart --align optimal CSI 0% 100%": EmptyOutFail,
	"sgdisk /dev/sda --partition-guid=1:64be631b-62a5-11e9-a756-00505680d67f": {
		Stdout: "The operation has completed successfully.",
		Stderr: "",
		Err:    nil,
	},
	"sgdisk /dev/sdb --partition-guid=1:64be631b-62a5-11e9-a756-00505680d67f": {
		Stdout: "The operation has completed successfully.",
		Stderr: "",
		Err:    Err,
	},
	"sgdisk /dev/sda --info=1": {
		Stdout: `Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
Partition unique GUID: 64BE631B-62A5-11E9-A756-00505680D67F
First sector: 2048 (at 1024.0 KiB)
Last sector: 1953523711 (at 931.5 GiB)
Partition size: 1953521664 sectors (931.5 GiB)
Attribute flags: 0000000000000000
Partition name: 'CSI'`,
		Stderr: "",
		Err:    nil,
	},
	"sgdisk /dev/sdb --info=1": {
		Stdout: `Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
Partition: 64BE631B-62A5-11E9-A756-00505680D67F
First sector: 2048 (at 1024.0 KiB)
Last sector: 1953523711 (at 931.5 GiB)
Partition size: 1953521664 sectors (931.5 GiB)
Attribute flags: 0000000000000000
Partition name: 'CSI'`,
		Stderr: "",
		Err:    nil,
	},
	"sgdisk /dev/sdc --info=1": EmptyOutFail,
}

DiskCommands is the map that contains Linux commands output

View Source
var DriveMgrRespDrives = []*api.Drive{
	{
		SerialNumber: "hdd1",
		Health:       apiV1.HealthGood,
		Type:         apiV1.DriveTypeHDD,
		Size:         1024 * 1024 * 1024 * 50,
	},
	{
		SerialNumber: "hdd2",
		Health:       apiV1.HealthGood,
		Type:         apiV1.DriveTypeHDD,
		Size:         1024 * 1024 * 1024 * 150,
	},
}

DriveMgrRespDrives are the drives for mock GetAllDrives call of DriveManager

View Source
var EmptyOutFail = CmdOut{
	Stdout: "",
	Stderr: "",
	Err:    Err,
}

EmptyOutFail var of type CmdOut for test purposes

View Source
var EmptyOutSuccess = CmdOut{
	Stdout: "",
	Stderr: "",
	Err:    nil,
}

EmptyOutSuccess var of type CmdOut for test purposes

View Source
var Err = errors.New("error")

Err var of type error for test purposes

View Source
var LsblkDevV2 = `` /* 317-byte string literal not displayed */

LsblkDevV2 provides output for new lsblk version

View Source
var LsblkDevWithChildren = CmdOut{
	Stdout: `{
			  "blockdevices":[{
				"name": "/dev/sdb",
				"type": "disk",
				"serial": "hdd2",
				"children": [{"name": "/dev/children1", "mountpoint":""}, 
							 {"name": "/dev/children2", "mountpoint":"/var/lib/kubelet/pods/27cc6e45-61f1-11e9-b966-001e67e6854b/volumes/kubernetes.io~csi/pvc-27cbea1b-61f1-11e9-b966-001e67e6854b/mount"}],
				"size": "213674622976"
				}]
			}`,
	Stderr: "",
	Err:    nil}

LsblkDevWithChildren imitates lsblk output with two block devices with children

View Source
var LsblkListPartitionsStr = `` /* 185-byte string literal not displayed */

LsblkListPartitionsStr imitates lsblk output with block device that has partition

View Source
var LsblkTwoDevicesStr = `` /* 175-byte string literal not displayed */

LsblkTwoDevicesStr imitates lsblk output with two block devices

View Source
var NoLsblkKeyStr = `{"anotherKey": [{"name": "/dev/sda", "type": "disk"}]}`

NoLsblkKeyStr imitates lsblk output without normal key

Functions

This section is empty.

Types

type ACOperationsMock

type ACOperationsMock struct {
	mock.Mock
}

ACOperationsMock is the mock implementation of AvailableCapacityOperations interface for test purposes

func (*ACOperationsMock) DeleteIfEmpty

func (a *ACOperationsMock) DeleteIfEmpty(ctx context.Context, acLocation string) error

DeleteIfEmpty is the mock implementation of DeleteIfEmpty method from AvailableCapacityOperations made for simulating deletion of empty AvailableCapacity which Location is acLocation. Returns error if user simulates error in tests or nil

func (*ACOperationsMock) RecreateACToLVGSC

func (a *ACOperationsMock) RecreateACToLVGSC(ctx context.Context, sc string, acs ...accrd.AvailableCapacity) *accrd.AvailableCapacity

RecreateACToLVGSC is the mock implementation of RecreateACToLVGSC method from AvailableCapacityOperations made for simulating recreation of list of ACs to LVG AC Returns error if user simulates error in tests or nil

func (*ACOperationsMock) SearchAC

func (a *ACOperationsMock) SearchAC(ctx context.Context, node string, requiredBytes int64, sc string) *accrd.AvailableCapacity

SearchAC is the mock implementation of SearchAC method from AvailableCapacityOperations made for simulating searching AvailableCapacity on a cluster. Returns a fake AvailableCapacity instance

type CmdOut

type CmdOut struct {
	Stdout string
	Stderr string
	Err    error
}

CmdOut is the struct for command output

type EmptyExecutorFail

type EmptyExecutorFail struct {
	LevelSetter
}

EmptyExecutorFail implements CmdExecutor interface for test purposes, each command will finish with error

func (EmptyExecutorFail) RunCmd

func (e EmptyExecutorFail) RunCmd(interface{}, ...command.Options) (string, string, error)

RunCmd simulates failed execution of a command Returns "error happened" as stdout, "error" as stderr and errors.New("error") as error

func (EmptyExecutorFail) RunCmdWithAttempts

func (e EmptyExecutorFail) RunCmdWithAttempts(interface{}, int, time.Duration, ...command.Options) (string, string, error)

RunCmdWithAttempts simulates failed execution of a command with attempts and given timeout between attempts Returns "error happened" as stdout, "error" as stderr and errors.New("error") as error

type EmptyExecutorSuccess

type EmptyExecutorSuccess struct {
	LevelSetter
}

EmptyExecutorSuccess implements CmdExecutor interface for test purposes, each command will finish success

func (EmptyExecutorSuccess) RunCmd

func (e EmptyExecutorSuccess) RunCmd(interface{}, ...command.Options) (string, string, error)

RunCmd simulates successful execution of a command Returns "" as stdout, "" as stderr and nil as error

func (EmptyExecutorSuccess) RunCmdWithAttempts

func (e EmptyExecutorSuccess) RunCmdWithAttempts(interface{}, int, time.Duration, ...command.Options) (string, string, error)

RunCmdWithAttempts simulates successful execution of a command with attempts and given timeout between attempts Returns "" as stdout, "" as stderr and nil as error

type GoMockExecutor

type GoMockExecutor struct {
	mock.Mock
	LevelSetter
}

GoMockExecutor implements CmdExecutor based on stretchr/testify/mock

func (*GoMockExecutor) OnCommand

func (g *GoMockExecutor) OnCommand(cmd string) *mock.Call

OnCommand is the method of mock.Mock where user can set what to return on specified command For example e.OnCommand("/sbin/lvm pvcreate --yes /dev/sda").Return("", "", errors.New("pvcreate failed")) Returns mock.Call where need to set what to return with Return() method

func (*GoMockExecutor) OnCommandWithAttempts

func (g *GoMockExecutor) OnCommandWithAttempts(cmd string, attempts int, timeout time.Duration) *mock.Call

OnCommandWithAttempts is the method of mock.Mock where user can set what to return on specified command For example e.OnCommandWithAttempts("/sbin/lvm pvcreate --yes /dev/sda", 5, time.Second).Return("", "", errors.New("pvcreate failed")) Returns mock.Call where need to set what to return with Return() method

func (*GoMockExecutor) RunCmd

func (g *GoMockExecutor) RunCmd(cmd interface{}, opts ...command.Options) (string, string, error)

RunCmd simulates execution of a command with OnCommand where user can set what the method should return

func (*GoMockExecutor) RunCmdWithAttempts

func (g *GoMockExecutor) RunCmdWithAttempts(cmd interface{}, attempts int, timeout time.Duration, opts ...command.Options) (string, string, error)

RunCmdWithAttempts simulates execution of a command with OnCommandWithAttempts where user can set what the method should return

type K8Client

type K8Client struct {
	client.Reader
	client.Writer
	client.StatusClient
	mock.Mock
}

K8Client is a mock implementation of client.Client interface from controller-runtime

func (*K8Client) Create

func (k *K8Client) Create(ctx context.Context, obj runtime.Object, opts ...client.CreateOption) error

Create is mock implementation of Create method from client.Writer interface

func (*K8Client) Delete

func (k *K8Client) Delete(ctx context.Context, obj runtime.Object, opts ...client.DeleteOption) error

Delete is mock implementation of Delete method from client.Writer interface

func (*K8Client) DeleteAllOf

func (k *K8Client) DeleteAllOf(ctx context.Context, obj runtime.Object, opts ...client.DeleteAllOfOption) error

DeleteAllOf is mock implementation of DeleteAllOf method from client.Writer interface

func (*K8Client) Get

func (k *K8Client) Get(ctx context.Context, key client.ObjectKey, obj runtime.Object) error

Get is mock implementation of Get method from client.Reader interface

func (*K8Client) List

func (k *K8Client) List(ctx context.Context, list runtime.Object, opts ...client.ListOption) error

List is mock implementation of List method from client.Reader interface

func (*K8Client) Patch

func (k *K8Client) Patch(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.PatchOption) error

Patch is mock implementation of Patch method from client.Writer interface

func (*K8Client) Update

func (k *K8Client) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOption) error

Update is mock implementation of Update method from client.Writer interface

type LevelSetter

type LevelSetter struct {
	// contains filtered or unexported fields
}

LevelSetter is the struct to fully implement CmdExecutor interface

func (LevelSetter) SetLevel

func (l LevelSetter) SetLevel(level logrus.Level)

SetLevel sets log Level to a MockExecutor Receives logrus Level

type MockDriveMgrClient

type MockDriveMgrClient struct {
	// contains filtered or unexported fields
}

MockDriveMgrClient is the implementation of DriveManager interface to imitate success state

func NewMockDriveMgrClient

func NewMockDriveMgrClient(drives []*api.Drive) *MockDriveMgrClient

NewMockDriveMgrClient returns new instance of MockDriveMgrClient Receives slice of api.Drive which would be used in imitation of GetDrivesList

func (*MockDriveMgrClient) AddDrives

func (m *MockDriveMgrClient) AddDrives(drives ...*api.Drive)

AddDrives extends drives slice

func (*MockDriveMgrClient) GetDrivesList

func (m *MockDriveMgrClient) GetDrivesList(ctx context.Context, in *api.DrivesRequest, opts ...grpc.CallOption) (*api.DrivesResponse, error)

GetDrivesList returns provided to MockDriveMgrClient drives to imitate working of DriveManager

func (*MockDriveMgrClient) Locate

Locate is a stub for Locate DriveManager's method

func (*MockDriveMgrClient) SetDrives

func (m *MockDriveMgrClient) SetDrives(drives []*api.Drive)

SetDrives set drives for current MockDriveMgrClient instance

type MockDriveMgrClientFail

type MockDriveMgrClientFail struct {
}

MockDriveMgrClientFail is the implementation of DriveManager interface to imitate failure state

func (*MockDriveMgrClientFail) GetDrivesList

GetDrivesList is the simulation of failure during DriveManager's GetDrivesList Returns nil DrivesResponse and non nil error

func (*MockDriveMgrClientFail) Locate

Locate is a stub for Locate DriveManager's method

type MockExecutor

type MockExecutor struct {
	LevelSetter
	// contains filtered or unexported fields
}

MockExecutor implements CmdExecutor interface, each command will return appropriate key from cmdMap map there is ability to return different value for same command if it runs twice, for it add this command and result (that expected on second run) in SecondRun map when cmd runs first result gets from cmdMap, when cmd runs second time and so on results is searching (at first) in SecondRun map

func NewMockExecutor

func NewMockExecutor(m map[string]CmdOut) *MockExecutor

NewMockExecutor is the constructor for MockExecutor struct Receives map which contains commands as keys and their outputs as values Returns an instance of MockExecutor

func (*MockExecutor) AddSecondRun

func (e *MockExecutor) AddSecondRun(cmd string, res CmdOut)

AddSecondRun adds command output - res for command - cmd for the second execution

func (*MockExecutor) GetMap

func (e *MockExecutor) GetMap() map[string]CmdOut

GetMap returns command map from MockExecutor

func (*MockExecutor) RunCmd

func (e *MockExecutor) RunCmd(cmd interface{}, opts ...command.Options) (string, string, error)

RunCmd simulates execution of a command. If a command is in cmdMap then return value as an output for it. If the command ran before then trying to return output from secondRun map if it set. Receives cmd as interface and cast it to a string Returns stdout, stderr, error for a given command

func (*MockExecutor) RunCmdWithAttempts

func (e *MockExecutor) RunCmdWithAttempts(cmd interface{}, attempts int, timeout time.Duration, opts ...command.Options) (string, string, error)

RunCmdWithAttempts simulates execution of a command. Execute RunCmd. Receives cmd as interface, number of attempts, timeout Returns stdout, stderr, error for a given command

func (*MockExecutor) SetMap

func (e *MockExecutor) SetMap(m map[string]CmdOut)

SetMap sets map which contains commands as keys and their outputs as values to the MockExecutor

func (*MockExecutor) SetSuccessIfNotFound

func (e *MockExecutor) SetSuccessIfNotFound(val bool)

SetSuccessIfNotFound sets MockExecutor mode when it returns success output even if a command wasn't found in map

type NoOpRecorder

type NoOpRecorder struct {
	Calls []eventRecorderCalls
	// contains filtered or unexported fields
}

NoOpRecorder is blank implementation of event recorder interface which stores calls to the interface methods

func (*NoOpRecorder) Eventf

func (n *NoOpRecorder) Eventf(object runtime.Object, eventtype, reason, messageFmt string, args ...interface{})

Eventf do nothing

type VolumeOperationsMock

type VolumeOperationsMock struct {
	mock.Mock
}

VolumeOperationsMock is the mock implementation of VolumeOperations interface for test purposes. All of the mock methods based on stretchr/testify/mock.

func (*VolumeOperationsMock) CreateVolume

func (vo *VolumeOperationsMock) CreateVolume(ctx context.Context, v api.Volume) (*api.Volume, error)

CreateVolume is the mock implementation of CreateVolume method from VolumeOperations made for simulating creating of Volume CR on a cluster. Returns a fake api.Volume instance

func (*VolumeOperationsMock) DeleteVolume

func (vo *VolumeOperationsMock) DeleteVolume(ctx context.Context, volumeID string) error

DeleteVolume is the mock implementation of DeleteVolume method from VolumeOperations made for simulating deletion of Volume CR on a cluster. Returns error if user simulates error in tests or nil

func (*VolumeOperationsMock) ExpandVolume

func (vo *VolumeOperationsMock) ExpandVolume(ctx context.Context, volume *volumecrd.Volume, requiredBytes int64) error

ExpandVolume is the mock implementation of ExpandVolume method from VolumeOperations made for simulating Receive golang context, volume CR, requiredBytes as int Return volume spec, error

func (*VolumeOperationsMock) UpdateCRsAfterVolumeDeletion

func (vo *VolumeOperationsMock) UpdateCRsAfterVolumeDeletion(ctx context.Context, volumeID string)

UpdateCRsAfterVolumeDeletion is the mock implementation of UpdateCRsAfterVolumeDeletion

func (*VolumeOperationsMock) UpdateCRsAfterVolumeExpansion

func (vo *VolumeOperationsMock) UpdateCRsAfterVolumeExpansion(ctx context.Context, volID string, requiredBytes int64)

UpdateCRsAfterVolumeExpansion is the mock implementation of UpdateCRsAfterVolumeExpansion method from VolumeOperations made for simulating Receive golang context, volume spec Return error

func (*VolumeOperationsMock) WaitStatus

func (vo *VolumeOperationsMock) WaitStatus(ctx context.Context, volumeID string, statuses ...string) error

WaitStatus is the mock implementation of WaitStatus. Simulates waiting of Volume to be reached one of provided statuses

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL