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