github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/agent/storageprovisioner/internal/filesystemwatcher/mock_test.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package filesystemwatcher_test
     5  
     6  import (
     7  	"gopkg.in/juju/names.v2"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/juju/state"
    11  	"github.com/juju/juju/state/watcher/watchertest"
    12  )
    13  
    14  type mockBackend struct {
    15  	machineFilesystemsW           *watchertest.StringsWatcher
    16  	unitFilesystemsW              *watchertest.StringsWatcher
    17  	machineFilesystemAttachmentsW *watchertest.StringsWatcher
    18  	unitFilesystemAttachmentsW    *watchertest.StringsWatcher
    19  	modelFilesystemsW             *watchertest.StringsWatcher
    20  	modelFilesystemAttachmentsW   *watchertest.StringsWatcher
    21  	modelVolumeAttachmentsW       *watchertest.StringsWatcher
    22  
    23  	filesystems               map[string]*mockFilesystem
    24  	volumeAttachments         map[string]*mockVolumeAttachment
    25  	volumeAttachmentRequested chan names.VolumeTag
    26  }
    27  
    28  func (b *mockBackend) Filesystem(tag names.FilesystemTag) (state.Filesystem, error) {
    29  	if f, ok := b.filesystems[tag.Id()]; ok {
    30  		return f, nil
    31  	}
    32  	return nil, errors.NotFoundf("filesystem %s", tag.Id())
    33  }
    34  
    35  func (b *mockBackend) VolumeAttachment(host names.Tag, v names.VolumeTag) (state.VolumeAttachment, error) {
    36  	if host.Id() != "0" && host.Id() != "mariadb/0" {
    37  		// The tests all operate on host "0" or "mariadb/0", and the watchers
    38  		// should ignore attachments for other machines, so we should
    39  		// never get here.
    40  		return nil, errors.Errorf("should not get here, unexpected host %v", host)
    41  	}
    42  	// Inform the test code that the volume attachment has been requested.
    43  	// This gives the test a way of knowing when events have been handled,
    44  	// and it's safe to make modifications.
    45  	defer func() {
    46  		select {
    47  		case b.volumeAttachmentRequested <- v:
    48  		default:
    49  		}
    50  	}()
    51  	if a, ok := b.volumeAttachments[v.Id()]; ok {
    52  		return a, nil
    53  	}
    54  	return nil, errors.NotFoundf("attachment for volume %s to host %s", v.Id(), host.Id())
    55  }
    56  
    57  func (b *mockBackend) WatchMachineFilesystems(tag names.MachineTag) state.StringsWatcher {
    58  	return b.machineFilesystemsW
    59  }
    60  
    61  func (b *mockBackend) WatchUnitFilesystems(tag names.ApplicationTag) state.StringsWatcher {
    62  	return b.unitFilesystemsW
    63  }
    64  
    65  func (b *mockBackend) WatchMachineFilesystemAttachments(tag names.MachineTag) state.StringsWatcher {
    66  	return b.machineFilesystemAttachmentsW
    67  }
    68  
    69  func (b *mockBackend) WatchUnitFilesystemAttachments(tag names.ApplicationTag) state.StringsWatcher {
    70  	return b.unitFilesystemAttachmentsW
    71  }
    72  
    73  func (b *mockBackend) WatchModelFilesystems() state.StringsWatcher {
    74  	return b.modelFilesystemsW
    75  }
    76  
    77  func (b *mockBackend) WatchModelFilesystemAttachments() state.StringsWatcher {
    78  	return b.modelFilesystemAttachmentsW
    79  }
    80  
    81  func (b *mockBackend) WatchModelVolumeAttachments() state.StringsWatcher {
    82  	return b.modelVolumeAttachmentsW
    83  }
    84  
    85  func newStringsWatcher() *watchertest.StringsWatcher {
    86  	return watchertest.NewStringsWatcher(make(chan []string, 1))
    87  }
    88  
    89  type mockFilesystem struct {
    90  	state.Filesystem
    91  	volume names.VolumeTag
    92  }
    93  
    94  func (f *mockFilesystem) Volume() (names.VolumeTag, error) {
    95  	if f.volume == (names.VolumeTag{}) {
    96  		return names.VolumeTag{}, state.ErrNoBackingVolume
    97  	}
    98  	return f.volume, nil
    99  }
   100  
   101  type mockVolumeAttachment struct {
   102  	state.VolumeAttachment
   103  	life state.Life
   104  }
   105  
   106  func (a *mockVolumeAttachment) Life() state.Life {
   107  	return a.life
   108  }
   109  
   110  type nopSyncStarter struct{}
   111  
   112  func (nopSyncStarter) StartSync() {}