github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/apiserver/facades/client/storage/mock_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage_test
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/charm/v12"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/names/v5"
    12  
    13  	"github.com/juju/juju/apiserver/facades/client/storage"
    14  	"github.com/juju/juju/core/status"
    15  	"github.com/juju/juju/state"
    16  	jujustorage "github.com/juju/juju/storage"
    17  	"github.com/juju/juju/testing"
    18  )
    19  
    20  type mockPoolManager struct {
    21  	getPool     func(name string) (*jujustorage.Config, error)
    22  	createPool  func(name string, providerType jujustorage.ProviderType, attrs map[string]interface{}) (*jujustorage.Config, error)
    23  	removePool  func(name string) error
    24  	listPools   func() ([]*jujustorage.Config, error)
    25  	replacePool func(name, provider string, attrs map[string]interface{}) error
    26  }
    27  
    28  func (m *mockPoolManager) Get(name string) (*jujustorage.Config, error) {
    29  	return m.getPool(name)
    30  }
    31  
    32  func (m *mockPoolManager) Create(name string, providerType jujustorage.ProviderType, attrs map[string]interface{}) (*jujustorage.Config, error) {
    33  	return m.createPool(name, providerType, attrs)
    34  }
    35  
    36  func (m *mockPoolManager) Delete(name string) error {
    37  	return m.removePool(name)
    38  }
    39  
    40  func (m *mockPoolManager) List() ([]*jujustorage.Config, error) {
    41  	return m.listPools()
    42  }
    43  
    44  func (m *mockPoolManager) Replace(name, provider string, attrs map[string]interface{}) error {
    45  	return m.replacePool(name, provider, attrs)
    46  }
    47  
    48  type mockStorageAccessor struct {
    49  	storageInstance                     func(names.StorageTag) (state.StorageInstance, error)
    50  	removeStoragePool                   func(string) error
    51  	allStorageInstances                 func() ([]state.StorageInstance, error)
    52  	storageInstanceAttachments          func(names.StorageTag) ([]state.StorageAttachment, error)
    53  	storageInstanceVolume               func(names.StorageTag) (state.Volume, error)
    54  	volumeAttachment                    func(names.Tag, names.VolumeTag) (state.VolumeAttachment, error)
    55  	storageInstanceFilesystem           func(names.StorageTag) (state.Filesystem, error)
    56  	storageInstanceFilesystemAttachment func(m names.Tag, f names.FilesystemTag) (state.FilesystemAttachment, error)
    57  	volume                              func(tag names.VolumeTag) (state.Volume, error)
    58  	machineVolumeAttachments            func(machine names.MachineTag) ([]state.VolumeAttachment, error)
    59  	volumeAttachments                   func(volume names.VolumeTag) ([]state.VolumeAttachment, error)
    60  	volumeAttachmentPlan                func(names.Tag, names.VolumeTag) (state.VolumeAttachmentPlan, error)
    61  	volumeAttachmentPlans               func(names.VolumeTag) ([]state.VolumeAttachmentPlan, error)
    62  	allVolumes                          func() ([]state.Volume, error)
    63  	filesystem                          func(tag names.FilesystemTag) (state.Filesystem, error)
    64  	machineFilesystemAttachments        func(machine names.MachineTag) ([]state.FilesystemAttachment, error)
    65  	filesystemAttachments               func(filesystem names.FilesystemTag) ([]state.FilesystemAttachment, error)
    66  	allFilesystems                      func() ([]state.Filesystem, error)
    67  	addStorageForUnit                   func(u names.UnitTag, name string, cons state.StorageConstraints) ([]names.StorageTag, error)
    68  	blockDevices                        func(names.MachineTag) ([]state.BlockDeviceInfo, error)
    69  	destroyStorageInstance              func(names.StorageTag, bool, bool) error
    70  	releaseStorageInstance              func(names.StorageTag, bool, bool) error
    71  	attachStorage                       func(names.StorageTag, names.UnitTag) error
    72  	detachStorage                       func(names.StorageTag, names.UnitTag, bool) error
    73  	addExistingFilesystem               func(state.FilesystemInfo, *state.VolumeInfo, string) (names.StorageTag, error)
    74  }
    75  
    76  func (st *mockStorageAccessor) VolumeAccess() storage.StorageVolume {
    77  	return st
    78  }
    79  
    80  func (st *mockStorageAccessor) FilesystemAccess() storage.StorageFile {
    81  	return st
    82  }
    83  
    84  func (st *mockStorageAccessor) StorageInstance(s names.StorageTag) (state.StorageInstance, error) {
    85  	return st.storageInstance(s)
    86  }
    87  
    88  func (st *mockStorageAccessor) RemoveStoragePool(pool string) error {
    89  	return st.removeStoragePool(pool)
    90  }
    91  
    92  func (st *mockStorageAccessor) AllStorageInstances() ([]state.StorageInstance, error) {
    93  	return st.allStorageInstances()
    94  }
    95  
    96  func (st *mockStorageAccessor) StorageAttachments(tag names.StorageTag) ([]state.StorageAttachment, error) {
    97  	return st.storageInstanceAttachments(tag)
    98  }
    99  
   100  func (st *mockStorageAccessor) FilesystemAttachment(m names.Tag, f names.FilesystemTag) (state.FilesystemAttachment, error) {
   101  	return st.storageInstanceFilesystemAttachment(m, f)
   102  }
   103  
   104  func (st *mockStorageAccessor) StorageInstanceFilesystem(s names.StorageTag) (state.Filesystem, error) {
   105  	return st.storageInstanceFilesystem(s)
   106  }
   107  
   108  func (st *mockStorageAccessor) StorageInstanceVolume(s names.StorageTag) (state.Volume, error) {
   109  	return st.storageInstanceVolume(s)
   110  }
   111  
   112  func (st *mockStorageAccessor) VolumeAttachment(m names.Tag, v names.VolumeTag) (state.VolumeAttachment, error) {
   113  	return st.volumeAttachment(m, v)
   114  }
   115  
   116  func (st *mockStorageAccessor) VolumeAttachmentPlan(host names.Tag, volume names.VolumeTag) (state.VolumeAttachmentPlan, error) {
   117  	return st.volumeAttachmentPlan(host, volume)
   118  }
   119  
   120  func (st *mockStorageAccessor) VolumeAttachmentPlans(volume names.VolumeTag) ([]state.VolumeAttachmentPlan, error) {
   121  	// st.MethodCall(st, "VolumeAttachmentPlans", volume)
   122  	return st.volumeAttachmentPlans(volume)
   123  }
   124  
   125  func (st *mockStorageAccessor) AllVolumes() ([]state.Volume, error) {
   126  	return st.allVolumes()
   127  }
   128  
   129  func (st *mockStorageAccessor) VolumeAttachments(volume names.VolumeTag) ([]state.VolumeAttachment, error) {
   130  	return st.volumeAttachments(volume)
   131  }
   132  
   133  func (st *mockStorageAccessor) MachineVolumeAttachments(machine names.MachineTag) ([]state.VolumeAttachment, error) {
   134  	return st.machineVolumeAttachments(machine)
   135  }
   136  
   137  func (st *mockStorageAccessor) Volume(tag names.VolumeTag) (state.Volume, error) {
   138  	return st.volume(tag)
   139  }
   140  
   141  func (st *mockStorageAccessor) AllFilesystems() ([]state.Filesystem, error) {
   142  	return st.allFilesystems()
   143  }
   144  
   145  func (st *mockStorageAccessor) FilesystemAttachments(filesystem names.FilesystemTag) ([]state.FilesystemAttachment, error) {
   146  	return st.filesystemAttachments(filesystem)
   147  }
   148  
   149  func (st *mockStorageAccessor) MachineFilesystemAttachments(machine names.MachineTag) ([]state.FilesystemAttachment, error) {
   150  	return st.machineFilesystemAttachments(machine)
   151  }
   152  
   153  func (st *mockStorageAccessor) Filesystem(tag names.FilesystemTag) (state.Filesystem, error) {
   154  	return st.filesystem(tag)
   155  }
   156  
   157  func (st *mockStorageAccessor) AddStorageForUnit(u names.UnitTag, name string, cons state.StorageConstraints) ([]names.StorageTag, error) {
   158  	return st.addStorageForUnit(u, name, cons)
   159  }
   160  
   161  func (st *mockStorageAccessor) BlockDevices(m names.MachineTag) ([]state.BlockDeviceInfo, error) {
   162  	if st.blockDevices != nil {
   163  		return st.blockDevices(m)
   164  	}
   165  	return []state.BlockDeviceInfo{}, nil
   166  }
   167  
   168  func (st *mockStorageAccessor) AttachStorage(storage names.StorageTag, unit names.UnitTag) error {
   169  	return st.attachStorage(storage, unit)
   170  }
   171  
   172  func (st *mockStorageAccessor) DetachStorage(storage names.StorageTag, unit names.UnitTag, force bool, maxWait time.Duration) error {
   173  	return st.detachStorage(storage, unit, force)
   174  }
   175  
   176  func (st *mockStorageAccessor) DestroyStorageInstance(tag names.StorageTag, destroyAttached bool, force bool, maxWait time.Duration) error {
   177  	return st.destroyStorageInstance(tag, destroyAttached, force)
   178  }
   179  
   180  func (st *mockStorageAccessor) ReleaseStorageInstance(tag names.StorageTag, destroyAttached bool, force bool, maxWait time.Duration) error {
   181  	return st.releaseStorageInstance(tag, destroyAttached, force)
   182  }
   183  
   184  func (st *mockStorageAccessor) UnitStorageAttachments(tag names.UnitTag) ([]state.StorageAttachment, error) {
   185  	panic("should not be called")
   186  }
   187  
   188  func (st *mockStorageAccessor) AddExistingFilesystem(f state.FilesystemInfo, v *state.VolumeInfo, s string) (names.StorageTag, error) {
   189  	return st.addExistingFilesystem(f, v, s)
   190  }
   191  
   192  type mockVolume struct {
   193  	state.Volume
   194  	tag     names.VolumeTag
   195  	storage *names.StorageTag
   196  	info    *state.VolumeInfo
   197  	life    state.Life
   198  }
   199  
   200  func (m *mockVolume) StorageInstance() (names.StorageTag, error) {
   201  	if m.storage != nil {
   202  		return *m.storage, nil
   203  	}
   204  	return names.StorageTag{}, errors.NewNotAssigned(nil, "error from mock")
   205  }
   206  
   207  func (m *mockVolume) VolumeTag() names.VolumeTag {
   208  	return m.tag
   209  }
   210  
   211  func (m *mockVolume) Params() (state.VolumeParams, bool) {
   212  	return state.VolumeParams{
   213  		Pool: "loop",
   214  		Size: 1024,
   215  	}, true
   216  }
   217  
   218  func (m *mockVolume) Info() (state.VolumeInfo, error) {
   219  	if m.info != nil {
   220  		return *m.info, nil
   221  	}
   222  	return state.VolumeInfo{}, errors.NotProvisionedf("%v", m.tag)
   223  }
   224  
   225  func (m *mockVolume) Life() state.Life {
   226  	return m.life
   227  }
   228  
   229  func (m *mockVolume) Status() (status.StatusInfo, error) {
   230  	return status.StatusInfo{Status: status.Attached}, nil
   231  }
   232  
   233  type mockFilesystem struct {
   234  	state.Filesystem
   235  	tag     names.FilesystemTag
   236  	storage *names.StorageTag
   237  	volume  *names.VolumeTag
   238  	info    *state.FilesystemInfo
   239  	life    state.Life
   240  }
   241  
   242  func (m *mockFilesystem) Storage() (names.StorageTag, error) {
   243  	if m.storage != nil {
   244  		return *m.storage, nil
   245  	}
   246  	return names.StorageTag{}, errors.NewNotAssigned(nil, "error from mock")
   247  }
   248  
   249  func (m *mockFilesystem) FilesystemTag() names.FilesystemTag {
   250  	return m.tag
   251  }
   252  
   253  func (m *mockFilesystem) Volume() (names.VolumeTag, error) {
   254  	if m.volume != nil {
   255  		return *m.volume, nil
   256  	}
   257  	return names.VolumeTag{}, state.ErrNoBackingVolume
   258  }
   259  
   260  func (m *mockFilesystem) Info() (state.FilesystemInfo, error) {
   261  	if m.info != nil {
   262  		return *m.info, nil
   263  	}
   264  	return state.FilesystemInfo{}, errors.NotProvisionedf("filesystem")
   265  }
   266  
   267  func (m *mockFilesystem) Life() state.Life {
   268  	return m.life
   269  }
   270  
   271  func (m *mockFilesystem) Status() (status.StatusInfo, error) {
   272  	return status.StatusInfo{Status: status.Attached}, nil
   273  }
   274  
   275  type mockFilesystemAttachment struct {
   276  	state.FilesystemAttachment
   277  	filesystem names.FilesystemTag
   278  	machine    names.MachineTag
   279  	info       *state.FilesystemAttachmentInfo
   280  	life       state.Life
   281  }
   282  
   283  func (m *mockFilesystemAttachment) Filesystem() names.FilesystemTag {
   284  	return m.filesystem
   285  }
   286  
   287  func (m *mockFilesystemAttachment) Host() names.Tag {
   288  	return m.machine
   289  }
   290  
   291  func (m *mockFilesystemAttachment) Info() (state.FilesystemAttachmentInfo, error) {
   292  	if m.info != nil {
   293  		return *m.info, nil
   294  	}
   295  	return state.FilesystemAttachmentInfo{}, errors.NotProvisionedf("filesystem attachment")
   296  }
   297  
   298  func (m *mockFilesystemAttachment) Life() state.Life {
   299  	return m.life
   300  }
   301  
   302  type mockStorageInstance struct {
   303  	state.StorageInstance
   304  	kind       state.StorageKind
   305  	owner      names.Tag
   306  	storageTag names.Tag
   307  	life       state.Life
   308  }
   309  
   310  func (m *mockStorageInstance) Kind() state.StorageKind {
   311  	return m.kind
   312  }
   313  
   314  func (m *mockStorageInstance) Owner() (names.Tag, bool) {
   315  	return m.owner, m.owner != nil
   316  }
   317  
   318  func (m *mockStorageInstance) Tag() names.Tag {
   319  	return m.storageTag
   320  }
   321  
   322  func (m *mockStorageInstance) StorageTag() names.StorageTag {
   323  	return m.storageTag.(names.StorageTag)
   324  }
   325  
   326  func (m *mockStorageInstance) CharmURL() *charm.URL {
   327  	panic("not implemented for test")
   328  }
   329  
   330  func (m *mockStorageInstance) Life() state.Life {
   331  	return m.life
   332  }
   333  
   334  type mockStorageAttachment struct {
   335  	state.StorageAttachment
   336  	storage *mockStorageInstance
   337  	life    state.Life
   338  }
   339  
   340  func (m *mockStorageAttachment) StorageInstance() names.StorageTag {
   341  	return m.storage.Tag().(names.StorageTag)
   342  }
   343  
   344  func (m *mockStorageAttachment) Unit() names.UnitTag {
   345  	return m.storage.owner.(names.UnitTag)
   346  }
   347  
   348  func (m *mockStorageAttachment) Life() state.Life {
   349  	return m.life
   350  }
   351  
   352  type mockVolumeAttachmentPlan struct {
   353  	VolumeTag names.VolumeTag
   354  	HostTag   names.MachineTag
   355  	info      *state.VolumeAttachmentPlanInfo
   356  	life      state.Life
   357  	blk       *state.BlockDeviceInfo
   358  }
   359  
   360  func (v *mockVolumeAttachmentPlan) Volume() names.VolumeTag {
   361  	return v.VolumeTag
   362  }
   363  
   364  func (v *mockVolumeAttachmentPlan) Machine() names.MachineTag {
   365  	return v.HostTag
   366  }
   367  
   368  func (v *mockVolumeAttachmentPlan) PlanInfo() (state.VolumeAttachmentPlanInfo, error) {
   369  	if v.info != nil {
   370  		return *v.info, nil
   371  	}
   372  	return state.VolumeAttachmentPlanInfo{}, nil
   373  }
   374  
   375  func (v *mockVolumeAttachmentPlan) BlockDeviceInfo() (state.BlockDeviceInfo, error) {
   376  	if v.blk != nil {
   377  		return *v.blk, nil
   378  	}
   379  	return state.BlockDeviceInfo{}, nil
   380  }
   381  
   382  func (v *mockVolumeAttachmentPlan) Life() state.Life {
   383  	return v.life
   384  }
   385  
   386  type mockVolumeAttachment struct {
   387  	VolumeTag names.VolumeTag
   388  	HostTag   names.Tag
   389  	info      *state.VolumeAttachmentInfo
   390  	life      state.Life
   391  }
   392  
   393  func (va *mockVolumeAttachment) Volume() names.VolumeTag {
   394  	return va.VolumeTag
   395  }
   396  
   397  func (va *mockVolumeAttachment) Host() names.Tag {
   398  	return va.HostTag
   399  }
   400  
   401  func (va *mockVolumeAttachment) Life() state.Life {
   402  	return va.life
   403  }
   404  
   405  func (va *mockVolumeAttachment) Info() (state.VolumeAttachmentInfo, error) {
   406  	if va.info != nil {
   407  		return *va.info, nil
   408  	}
   409  	return state.VolumeAttachmentInfo{}, errors.NotProvisionedf("volume attachment")
   410  }
   411  
   412  func (va *mockVolumeAttachment) Params() (state.VolumeAttachmentParams, bool) {
   413  	panic("not implemented for test")
   414  }
   415  
   416  type mockBlock struct {
   417  	state.Block
   418  	t   state.BlockType
   419  	msg string
   420  }
   421  
   422  func (b mockBlock) Type() state.BlockType {
   423  	return b.t
   424  }
   425  
   426  func (b mockBlock) Message() string {
   427  	return b.msg
   428  }
   429  
   430  type mockUnit struct {
   431  	assignedMachine string
   432  }
   433  
   434  func (u *mockUnit) AssignedMachineId() (string, error) {
   435  	return u.assignedMachine, nil
   436  }
   437  
   438  type mockState struct {
   439  	modelTag        names.ModelTag
   440  	getBlockForType func(t state.BlockType) (state.Block, bool, error)
   441  	unitName        string
   442  	unitErr         string
   443  	assignedMachine string
   444  }
   445  
   446  func (st *mockState) ControllerTag() names.ControllerTag {
   447  	return testing.ControllerTag
   448  }
   449  
   450  func (st *mockState) ModelTag() names.ModelTag {
   451  	return st.modelTag
   452  }
   453  
   454  func (st *mockState) GetBlockForType(t state.BlockType) (state.Block, bool, error) {
   455  	return st.getBlockForType(t)
   456  }
   457  
   458  func (st *mockState) Unit(unitName string) (storage.Unit, error) {
   459  	if st.unitErr != "" {
   460  		return nil, errors.New(st.unitErr)
   461  	}
   462  	if unitName == st.unitName {
   463  		return &mockUnit{assignedMachine: st.assignedMachine}, nil
   464  	}
   465  	return nil, errors.NotFoundf(unitName)
   466  }