github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/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  	"github.com/juju/names"
     9  	"gopkg.in/juju/charm.v6-unstable"
    10  
    11  	"github.com/juju/juju/state"
    12  	"github.com/juju/juju/status"
    13  	jujustorage "github.com/juju/juju/storage"
    14  )
    15  
    16  type mockPoolManager struct {
    17  	getPool    func(name string) (*jujustorage.Config, error)
    18  	createPool func(name string, providerType jujustorage.ProviderType, attrs map[string]interface{}) (*jujustorage.Config, error)
    19  	deletePool func(name string) error
    20  	listPools  func() ([]*jujustorage.Config, error)
    21  }
    22  
    23  func (m *mockPoolManager) Get(name string) (*jujustorage.Config, error) {
    24  	return m.getPool(name)
    25  }
    26  
    27  func (m *mockPoolManager) Create(name string, providerType jujustorage.ProviderType, attrs map[string]interface{}) (*jujustorage.Config, error) {
    28  	return m.createPool(name, providerType, attrs)
    29  }
    30  
    31  func (m *mockPoolManager) Delete(name string) error {
    32  	return m.deletePool(name)
    33  }
    34  
    35  func (m *mockPoolManager) List() ([]*jujustorage.Config, error) {
    36  	return m.listPools()
    37  }
    38  
    39  type mockState struct {
    40  	storageInstance                     func(names.StorageTag) (state.StorageInstance, error)
    41  	allStorageInstances                 func() ([]state.StorageInstance, error)
    42  	storageInstanceAttachments          func(names.StorageTag) ([]state.StorageAttachment, error)
    43  	unitAssignedMachine                 func(u names.UnitTag) (names.MachineTag, error)
    44  	storageInstanceVolume               func(names.StorageTag) (state.Volume, error)
    45  	volumeAttachment                    func(names.MachineTag, names.VolumeTag) (state.VolumeAttachment, error)
    46  	storageInstanceFilesystem           func(names.StorageTag) (state.Filesystem, error)
    47  	storageInstanceFilesystemAttachment func(m names.MachineTag, f names.FilesystemTag) (state.FilesystemAttachment, error)
    48  	watchStorageAttachment              func(names.StorageTag, names.UnitTag) state.NotifyWatcher
    49  	watchFilesystemAttachment           func(names.MachineTag, names.FilesystemTag) state.NotifyWatcher
    50  	watchVolumeAttachment               func(names.MachineTag, names.VolumeTag) state.NotifyWatcher
    51  	watchBlockDevices                   func(names.MachineTag) state.NotifyWatcher
    52  	modelName                           string
    53  	volume                              func(tag names.VolumeTag) (state.Volume, error)
    54  	machineVolumeAttachments            func(machine names.MachineTag) ([]state.VolumeAttachment, error)
    55  	volumeAttachments                   func(volume names.VolumeTag) ([]state.VolumeAttachment, error)
    56  	allVolumes                          func() ([]state.Volume, error)
    57  	filesystem                          func(tag names.FilesystemTag) (state.Filesystem, error)
    58  	machineFilesystemAttachments        func(machine names.MachineTag) ([]state.FilesystemAttachment, error)
    59  	filesystemAttachments               func(filesystem names.FilesystemTag) ([]state.FilesystemAttachment, error)
    60  	allFilesystems                      func() ([]state.Filesystem, error)
    61  	addStorageForUnit                   func(u names.UnitTag, name string, cons state.StorageConstraints) error
    62  	getBlockForType                     func(t state.BlockType) (state.Block, bool, error)
    63  	blockDevices                        func(names.MachineTag) ([]state.BlockDeviceInfo, error)
    64  }
    65  
    66  func (st *mockState) StorageInstance(s names.StorageTag) (state.StorageInstance, error) {
    67  	return st.storageInstance(s)
    68  }
    69  
    70  func (st *mockState) AllStorageInstances() ([]state.StorageInstance, error) {
    71  	return st.allStorageInstances()
    72  }
    73  
    74  func (st *mockState) StorageAttachments(tag names.StorageTag) ([]state.StorageAttachment, error) {
    75  	return st.storageInstanceAttachments(tag)
    76  }
    77  
    78  func (st *mockState) UnitAssignedMachine(unit names.UnitTag) (names.MachineTag, error) {
    79  	return st.unitAssignedMachine(unit)
    80  }
    81  
    82  func (st *mockState) FilesystemAttachment(m names.MachineTag, f names.FilesystemTag) (state.FilesystemAttachment, error) {
    83  	return st.storageInstanceFilesystemAttachment(m, f)
    84  }
    85  
    86  func (st *mockState) StorageInstanceFilesystem(s names.StorageTag) (state.Filesystem, error) {
    87  	return st.storageInstanceFilesystem(s)
    88  }
    89  
    90  func (st *mockState) StorageInstanceVolume(s names.StorageTag) (state.Volume, error) {
    91  	return st.storageInstanceVolume(s)
    92  }
    93  
    94  func (st *mockState) VolumeAttachment(m names.MachineTag, v names.VolumeTag) (state.VolumeAttachment, error) {
    95  	return st.volumeAttachment(m, v)
    96  }
    97  
    98  func (st *mockState) WatchStorageAttachment(s names.StorageTag, u names.UnitTag) state.NotifyWatcher {
    99  	return st.watchStorageAttachment(s, u)
   100  }
   101  
   102  func (st *mockState) WatchFilesystemAttachment(mtag names.MachineTag, f names.FilesystemTag) state.NotifyWatcher {
   103  	return st.watchFilesystemAttachment(mtag, f)
   104  }
   105  
   106  func (st *mockState) WatchVolumeAttachment(mtag names.MachineTag, v names.VolumeTag) state.NotifyWatcher {
   107  	return st.watchVolumeAttachment(mtag, v)
   108  }
   109  
   110  func (st *mockState) WatchBlockDevices(mtag names.MachineTag) state.NotifyWatcher {
   111  	return st.watchBlockDevices(mtag)
   112  }
   113  
   114  func (st *mockState) ModelName() (string, error) {
   115  	return st.modelName, nil
   116  }
   117  
   118  func (st *mockState) AllVolumes() ([]state.Volume, error) {
   119  	return st.allVolumes()
   120  }
   121  
   122  func (st *mockState) VolumeAttachments(volume names.VolumeTag) ([]state.VolumeAttachment, error) {
   123  	return st.volumeAttachments(volume)
   124  }
   125  
   126  func (st *mockState) MachineVolumeAttachments(machine names.MachineTag) ([]state.VolumeAttachment, error) {
   127  	return st.machineVolumeAttachments(machine)
   128  }
   129  
   130  func (st *mockState) Volume(tag names.VolumeTag) (state.Volume, error) {
   131  	return st.volume(tag)
   132  }
   133  
   134  func (st *mockState) AllFilesystems() ([]state.Filesystem, error) {
   135  	return st.allFilesystems()
   136  }
   137  
   138  func (st *mockState) FilesystemAttachments(filesystem names.FilesystemTag) ([]state.FilesystemAttachment, error) {
   139  	return st.filesystemAttachments(filesystem)
   140  }
   141  
   142  func (st *mockState) MachineFilesystemAttachments(machine names.MachineTag) ([]state.FilesystemAttachment, error) {
   143  	return st.machineFilesystemAttachments(machine)
   144  }
   145  
   146  func (st *mockState) Filesystem(tag names.FilesystemTag) (state.Filesystem, error) {
   147  	return st.filesystem(tag)
   148  }
   149  
   150  func (st *mockState) AddStorageForUnit(u names.UnitTag, name string, cons state.StorageConstraints) error {
   151  	return st.addStorageForUnit(u, name, cons)
   152  }
   153  
   154  func (st *mockState) GetBlockForType(t state.BlockType) (state.Block, bool, error) {
   155  	return st.getBlockForType(t)
   156  }
   157  
   158  func (st *mockState) BlockDevices(m names.MachineTag) ([]state.BlockDeviceInfo, error) {
   159  	if st.blockDevices != nil {
   160  		return st.blockDevices(m)
   161  	}
   162  	return []state.BlockDeviceInfo{}, nil
   163  }
   164  
   165  type mockNotifyWatcher struct {
   166  	state.NotifyWatcher
   167  	changes chan struct{}
   168  }
   169  
   170  func (m *mockNotifyWatcher) Changes() <-chan struct{} {
   171  	return m.changes
   172  }
   173  
   174  type mockVolume struct {
   175  	state.Volume
   176  	tag     names.VolumeTag
   177  	storage *names.StorageTag
   178  	info    *state.VolumeInfo
   179  }
   180  
   181  func (m *mockVolume) StorageInstance() (names.StorageTag, error) {
   182  	if m.storage != nil {
   183  		return *m.storage, nil
   184  	}
   185  	return names.StorageTag{}, errors.NewNotAssigned(nil, "error from mock")
   186  }
   187  
   188  func (m *mockVolume) VolumeTag() names.VolumeTag {
   189  	return m.tag
   190  }
   191  
   192  func (m *mockVolume) Params() (state.VolumeParams, bool) {
   193  	return state.VolumeParams{
   194  		Pool: "loop",
   195  		Size: 1024,
   196  	}, true
   197  }
   198  
   199  func (m *mockVolume) Info() (state.VolumeInfo, error) {
   200  	if m.info != nil {
   201  		return *m.info, nil
   202  	}
   203  	return state.VolumeInfo{}, errors.NotProvisionedf("%v", m.tag)
   204  }
   205  
   206  func (m *mockVolume) Status() (status.StatusInfo, error) {
   207  	return status.StatusInfo{Status: status.StatusAttached}, nil
   208  }
   209  
   210  type mockFilesystem struct {
   211  	state.Filesystem
   212  	tag     names.FilesystemTag
   213  	storage *names.StorageTag
   214  	volume  *names.VolumeTag
   215  	info    *state.FilesystemInfo
   216  }
   217  
   218  func (m *mockFilesystem) Storage() (names.StorageTag, error) {
   219  	if m.storage != nil {
   220  		return *m.storage, nil
   221  	}
   222  	return names.StorageTag{}, errors.NewNotAssigned(nil, "error from mock")
   223  }
   224  
   225  func (m *mockFilesystem) FilesystemTag() names.FilesystemTag {
   226  	return m.tag
   227  }
   228  
   229  func (m *mockFilesystem) Volume() (names.VolumeTag, error) {
   230  	if m.volume != nil {
   231  		return *m.volume, nil
   232  	}
   233  	return names.VolumeTag{}, state.ErrNoBackingVolume
   234  }
   235  
   236  func (m *mockFilesystem) Info() (state.FilesystemInfo, error) {
   237  	if m.info != nil {
   238  		return *m.info, nil
   239  	}
   240  	return state.FilesystemInfo{}, errors.NotProvisionedf("filesystem")
   241  }
   242  
   243  func (m *mockFilesystem) Status() (status.StatusInfo, error) {
   244  	return status.StatusInfo{Status: status.StatusAttached}, nil
   245  }
   246  
   247  type mockFilesystemAttachment struct {
   248  	state.FilesystemAttachment
   249  	filesystem names.FilesystemTag
   250  	machine    names.MachineTag
   251  	info       *state.FilesystemAttachmentInfo
   252  }
   253  
   254  func (m *mockFilesystemAttachment) Filesystem() names.FilesystemTag {
   255  	return m.filesystem
   256  }
   257  
   258  func (m *mockFilesystemAttachment) Machine() names.MachineTag {
   259  	return m.machine
   260  }
   261  
   262  func (m *mockFilesystemAttachment) Info() (state.FilesystemAttachmentInfo, error) {
   263  	if m.info != nil {
   264  		return *m.info, nil
   265  	}
   266  	return state.FilesystemAttachmentInfo{}, errors.NotProvisionedf("filesystem attachment")
   267  }
   268  
   269  type mockStorageInstance struct {
   270  	state.StorageInstance
   271  	kind       state.StorageKind
   272  	owner      names.Tag
   273  	storageTag names.Tag
   274  }
   275  
   276  func (m *mockStorageInstance) Kind() state.StorageKind {
   277  	return m.kind
   278  }
   279  
   280  func (m *mockStorageInstance) Owner() names.Tag {
   281  	return m.owner
   282  }
   283  
   284  func (m *mockStorageInstance) Tag() names.Tag {
   285  	return m.storageTag
   286  }
   287  
   288  func (m *mockStorageInstance) StorageTag() names.StorageTag {
   289  	return m.storageTag.(names.StorageTag)
   290  }
   291  
   292  func (m *mockStorageInstance) CharmURL() *charm.URL {
   293  	panic("not implemented for test")
   294  }
   295  
   296  type mockStorageAttachment struct {
   297  	state.StorageAttachment
   298  	storage *mockStorageInstance
   299  }
   300  
   301  func (m *mockStorageAttachment) StorageInstance() names.StorageTag {
   302  	return m.storage.Tag().(names.StorageTag)
   303  }
   304  
   305  func (m *mockStorageAttachment) Unit() names.UnitTag {
   306  	return m.storage.Owner().(names.UnitTag)
   307  }
   308  
   309  type mockVolumeAttachment struct {
   310  	VolumeTag  names.VolumeTag
   311  	MachineTag names.MachineTag
   312  	info       *state.VolumeAttachmentInfo
   313  }
   314  
   315  func (va *mockVolumeAttachment) Volume() names.VolumeTag {
   316  	return va.VolumeTag
   317  }
   318  
   319  func (va *mockVolumeAttachment) Machine() names.MachineTag {
   320  	return va.MachineTag
   321  }
   322  
   323  func (va *mockVolumeAttachment) Life() state.Life {
   324  	panic("not implemented for test")
   325  }
   326  
   327  func (va *mockVolumeAttachment) Info() (state.VolumeAttachmentInfo, error) {
   328  	if va.info != nil {
   329  		return *va.info, nil
   330  	}
   331  	return state.VolumeAttachmentInfo{}, errors.NotProvisionedf("volume attachment")
   332  }
   333  
   334  func (va *mockVolumeAttachment) Params() (state.VolumeAttachmentParams, bool) {
   335  	panic("not implemented for test")
   336  }
   337  
   338  type mockBlock struct {
   339  	state.Block
   340  	t   state.BlockType
   341  	msg string
   342  }
   343  
   344  func (b mockBlock) Type() state.BlockType {
   345  	return b.t
   346  }
   347  
   348  func (b mockBlock) Message() string {
   349  	return b.msg
   350  }