github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/worker/uniter/remotestate/mock_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package remotestate_test
     5  
     6  import (
     7  	"github.com/juju/names"
     8  	"gopkg.in/juju/charm.v6-unstable"
     9  
    10  	"github.com/juju/juju/api/watcher"
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/juju/state/multiwatcher"
    13  	"github.com/juju/juju/worker/leadership"
    14  	"github.com/juju/juju/worker/uniter/remotestate"
    15  )
    16  
    17  type mockNotifyWatcher struct {
    18  	changes chan struct{}
    19  	stopped bool
    20  }
    21  
    22  func (w *mockNotifyWatcher) Stop() error {
    23  	w.stopped = true
    24  	return nil
    25  }
    26  
    27  func (*mockNotifyWatcher) Err() error {
    28  	return nil
    29  }
    30  
    31  func (w *mockNotifyWatcher) Changes() <-chan struct{} {
    32  	return w.changes
    33  }
    34  
    35  type mockStringsWatcher struct {
    36  	changes chan []string
    37  	stopped bool
    38  }
    39  
    40  func (w *mockStringsWatcher) Stop() error {
    41  	w.stopped = true
    42  	return nil
    43  }
    44  
    45  func (*mockStringsWatcher) Err() error {
    46  	return nil
    47  }
    48  
    49  func (w *mockStringsWatcher) Changes() <-chan []string {
    50  	return w.changes
    51  }
    52  
    53  type mockRelationUnitsWatcher struct {
    54  	changes chan multiwatcher.RelationUnitsChange
    55  	stopped bool
    56  }
    57  
    58  func (w *mockRelationUnitsWatcher) Stop() error {
    59  	w.stopped = true
    60  	return nil
    61  }
    62  
    63  func (*mockRelationUnitsWatcher) Err() error {
    64  	return nil
    65  }
    66  
    67  func (w *mockRelationUnitsWatcher) Changes() <-chan multiwatcher.RelationUnitsChange {
    68  	return w.changes
    69  }
    70  
    71  type mockStorageAttachmentWatcher struct {
    72  	changes chan struct{}
    73  	stopped bool
    74  }
    75  
    76  func (w *mockStorageAttachmentWatcher) Stop() error {
    77  	w.stopped = true
    78  	return nil
    79  }
    80  
    81  func (*mockStorageAttachmentWatcher) Err() error {
    82  	return nil
    83  }
    84  
    85  func (w *mockStorageAttachmentWatcher) Changes() <-chan struct{} {
    86  	return w.changes
    87  }
    88  
    89  type mockState struct {
    90  	unit                      mockUnit
    91  	relations                 map[names.RelationTag]*mockRelation
    92  	storageAttachment         map[params.StorageAttachmentId]params.StorageAttachment
    93  	relationUnitsWatchers     map[names.RelationTag]*mockRelationUnitsWatcher
    94  	storageAttachmentWatchers map[names.StorageTag]*mockStorageAttachmentWatcher
    95  }
    96  
    97  func (st *mockState) Relation(tag names.RelationTag) (remotestate.Relation, error) {
    98  	r, ok := st.relations[tag]
    99  	if !ok {
   100  		return nil, &params.Error{Code: params.CodeNotFound}
   101  	}
   102  	return r, nil
   103  }
   104  
   105  func (st *mockState) StorageAttachment(
   106  	storageTag names.StorageTag, unitTag names.UnitTag,
   107  ) (params.StorageAttachment, error) {
   108  	if unitTag != st.unit.tag {
   109  		return params.StorageAttachment{}, &params.Error{Code: params.CodeNotFound}
   110  	}
   111  	attachment, ok := st.storageAttachment[params.StorageAttachmentId{
   112  		UnitTag:    unitTag.String(),
   113  		StorageTag: storageTag.String(),
   114  	}]
   115  	if !ok {
   116  		return params.StorageAttachment{}, &params.Error{Code: params.CodeNotFound}
   117  	}
   118  	if attachment.Kind == params.StorageKindUnknown {
   119  		return params.StorageAttachment{}, &params.Error{Code: params.CodeNotProvisioned}
   120  	}
   121  	return attachment, nil
   122  }
   123  
   124  func (st *mockState) StorageAttachmentLife(
   125  	ids []params.StorageAttachmentId,
   126  ) ([]params.LifeResult, error) {
   127  	results := make([]params.LifeResult, len(ids))
   128  	for i, id := range ids {
   129  		attachment, ok := st.storageAttachment[id]
   130  		if !ok {
   131  			results[i] = params.LifeResult{
   132  				Error: &params.Error{Code: params.CodeNotFound},
   133  			}
   134  			continue
   135  		}
   136  		results[i] = params.LifeResult{Life: attachment.Life}
   137  	}
   138  	return results, nil
   139  }
   140  
   141  func (st *mockState) Unit(tag names.UnitTag) (remotestate.Unit, error) {
   142  	if tag != st.unit.tag {
   143  		return nil, &params.Error{Code: params.CodeNotFound}
   144  	}
   145  	return &st.unit, nil
   146  }
   147  
   148  func (st *mockState) WatchRelationUnits(
   149  	relationTag names.RelationTag, unitTag names.UnitTag,
   150  ) (watcher.RelationUnitsWatcher, error) {
   151  	if unitTag != st.unit.tag {
   152  		return nil, &params.Error{Code: params.CodeNotFound}
   153  	}
   154  	watcher, ok := st.relationUnitsWatchers[relationTag]
   155  	if !ok {
   156  		return nil, &params.Error{Code: params.CodeNotFound}
   157  	}
   158  	return watcher, nil
   159  }
   160  
   161  func (st *mockState) WatchStorageAttachment(
   162  	storageTag names.StorageTag, unitTag names.UnitTag,
   163  ) (watcher.NotifyWatcher, error) {
   164  	if unitTag != st.unit.tag {
   165  		return nil, &params.Error{Code: params.CodeNotFound}
   166  	}
   167  	watcher, ok := st.storageAttachmentWatchers[storageTag]
   168  	if !ok {
   169  		return nil, &params.Error{Code: params.CodeNotFound}
   170  	}
   171  	return watcher, nil
   172  }
   173  
   174  type mockUnit struct {
   175  	tag                   names.UnitTag
   176  	life                  params.Life
   177  	resolved              params.ResolvedMode
   178  	service               mockService
   179  	unitWatcher           mockNotifyWatcher
   180  	addressesWatcher      mockNotifyWatcher
   181  	configSettingsWatcher mockNotifyWatcher
   182  	storageWatcher        mockStringsWatcher
   183  	actionWatcher         mockStringsWatcher
   184  }
   185  
   186  func (u *mockUnit) Life() params.Life {
   187  	return u.life
   188  }
   189  
   190  func (u *mockUnit) Refresh() error {
   191  	return nil
   192  }
   193  
   194  func (u *mockUnit) Resolved() (params.ResolvedMode, error) {
   195  	return u.resolved, nil
   196  }
   197  
   198  func (u *mockUnit) Service() (remotestate.Service, error) {
   199  	return &u.service, nil
   200  }
   201  
   202  func (u *mockUnit) Tag() names.UnitTag {
   203  	return u.tag
   204  }
   205  
   206  func (u *mockUnit) Watch() (watcher.NotifyWatcher, error) {
   207  	return &u.unitWatcher, nil
   208  }
   209  
   210  func (u *mockUnit) WatchAddresses() (watcher.NotifyWatcher, error) {
   211  	return &u.addressesWatcher, nil
   212  }
   213  
   214  func (u *mockUnit) WatchConfigSettings() (watcher.NotifyWatcher, error) {
   215  	return &u.configSettingsWatcher, nil
   216  }
   217  
   218  func (u *mockUnit) WatchStorage() (watcher.StringsWatcher, error) {
   219  	return &u.storageWatcher, nil
   220  }
   221  
   222  func (u *mockUnit) WatchActionNotifications() (watcher.StringsWatcher, error) {
   223  	return &u.actionWatcher, nil
   224  }
   225  
   226  type mockService struct {
   227  	tag                   names.ServiceTag
   228  	life                  params.Life
   229  	curl                  *charm.URL
   230  	forceUpgrade          bool
   231  	serviceWatcher        mockNotifyWatcher
   232  	leaderSettingsWatcher mockNotifyWatcher
   233  	relationsWatcher      mockStringsWatcher
   234  }
   235  
   236  func (s *mockService) CharmURL() (*charm.URL, bool, error) {
   237  	return s.curl, s.forceUpgrade, nil
   238  }
   239  
   240  func (s *mockService) Life() params.Life {
   241  	return s.life
   242  }
   243  
   244  func (s *mockService) Refresh() error {
   245  	return nil
   246  }
   247  
   248  func (s *mockService) Tag() names.ServiceTag {
   249  	return s.tag
   250  }
   251  
   252  func (s *mockService) Watch() (watcher.NotifyWatcher, error) {
   253  	return &s.serviceWatcher, nil
   254  }
   255  
   256  func (s *mockService) WatchLeadershipSettings() (watcher.NotifyWatcher, error) {
   257  	return &s.leaderSettingsWatcher, nil
   258  }
   259  
   260  func (s *mockService) WatchRelations() (watcher.StringsWatcher, error) {
   261  	return &s.relationsWatcher, nil
   262  }
   263  
   264  type mockRelation struct {
   265  	id   int
   266  	life params.Life
   267  }
   268  
   269  func (r *mockRelation) Id() int {
   270  	return r.id
   271  }
   272  
   273  func (r *mockRelation) Life() params.Life {
   274  	return r.life
   275  }
   276  
   277  type mockLeadershipTracker struct {
   278  	leadership.Tracker
   279  	claimTicket  mockTicket
   280  	leaderTicket mockTicket
   281  	minionTicket mockTicket
   282  }
   283  
   284  func (mock *mockLeadershipTracker) ClaimLeader() leadership.Ticket {
   285  	return &mock.claimTicket
   286  }
   287  
   288  func (mock *mockLeadershipTracker) WaitLeader() leadership.Ticket {
   289  	return &mock.leaderTicket
   290  }
   291  
   292  func (mock *mockLeadershipTracker) WaitMinion() leadership.Ticket {
   293  	return &mock.minionTicket
   294  }
   295  
   296  type mockTicket struct {
   297  	ch     chan struct{}
   298  	result bool
   299  }
   300  
   301  func (t *mockTicket) Ready() <-chan struct{} {
   302  	return t.ch
   303  }
   304  
   305  func (t *mockTicket) Wait() bool {
   306  	return t.result
   307  }