github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/worker/uniter/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/names"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  	"launchpad.net/tomb"
    13  
    14  	"github.com/juju/juju/api/watcher"
    15  	"github.com/juju/juju/apiserver/common"
    16  	"github.com/juju/juju/apiserver/params"
    17  	"github.com/juju/juju/testing"
    18  	"github.com/juju/juju/worker/uniter/hook"
    19  )
    20  
    21  type mockStorageAccessor struct {
    22  	watchStorageAttachment        func(names.StorageTag, names.UnitTag) (watcher.NotifyWatcher, error)
    23  	storageAttachment             func(names.StorageTag, names.UnitTag) (params.StorageAttachment, error)
    24  	storageAttachmentLife         func([]params.StorageAttachmentId) ([]params.LifeResult, error)
    25  	unitStorageAttachments        func(names.UnitTag) ([]params.StorageAttachmentId, error)
    26  	destroyUnitStorageAttachments func(names.UnitTag) error
    27  	remove                        func(names.StorageTag, names.UnitTag) error
    28  }
    29  
    30  func (m *mockStorageAccessor) WatchStorageAttachment(s names.StorageTag, u names.UnitTag) (watcher.NotifyWatcher, error) {
    31  	return m.watchStorageAttachment(s, u)
    32  }
    33  
    34  func (m *mockStorageAccessor) StorageAttachment(s names.StorageTag, u names.UnitTag) (params.StorageAttachment, error) {
    35  	return m.storageAttachment(s, u)
    36  }
    37  
    38  func (m *mockStorageAccessor) StorageAttachmentLife(ids []params.StorageAttachmentId) ([]params.LifeResult, error) {
    39  	if m.storageAttachmentLife != nil {
    40  		return m.storageAttachmentLife(ids)
    41  	}
    42  	results := make([]params.LifeResult, len(ids))
    43  	for i, id := range ids {
    44  		storageTag, err := names.ParseStorageTag(id.StorageTag)
    45  		if err != nil {
    46  			results[i].Error = common.ServerError(err)
    47  			continue
    48  		}
    49  		unitTag, err := names.ParseUnitTag(id.UnitTag)
    50  		if err != nil {
    51  			results[i].Error = common.ServerError(err)
    52  			continue
    53  		}
    54  		att, err := m.storageAttachment(storageTag, unitTag)
    55  		if err != nil {
    56  			results[i].Error = common.ServerError(err)
    57  			continue
    58  		}
    59  		results[i].Life = att.Life
    60  	}
    61  	return results, nil
    62  }
    63  
    64  func (m *mockStorageAccessor) UnitStorageAttachments(u names.UnitTag) ([]params.StorageAttachmentId, error) {
    65  	return m.unitStorageAttachments(u)
    66  }
    67  
    68  func (m *mockStorageAccessor) DestroyUnitStorageAttachments(u names.UnitTag) error {
    69  	return m.destroyUnitStorageAttachments(u)
    70  }
    71  
    72  func (m *mockStorageAccessor) RemoveStorageAttachment(s names.StorageTag, u names.UnitTag) error {
    73  	return m.remove(s, u)
    74  }
    75  
    76  type mockNotifyWatcher struct {
    77  	tomb    tomb.Tomb
    78  	changes chan struct{}
    79  }
    80  
    81  func newMockNotifyWatcher() *mockNotifyWatcher {
    82  	m := &mockNotifyWatcher{
    83  		changes: make(chan struct{}, 1),
    84  	}
    85  	go func() {
    86  		<-m.tomb.Dying()
    87  		close(m.changes)
    88  		m.tomb.Kill(tomb.ErrDying)
    89  		m.tomb.Done()
    90  	}()
    91  	return m
    92  }
    93  
    94  func (m *mockNotifyWatcher) Changes() <-chan struct{} {
    95  	return m.changes
    96  }
    97  
    98  func (m *mockNotifyWatcher) Stop() error {
    99  	m.tomb.Kill(nil)
   100  	return m.tomb.Wait()
   101  }
   102  
   103  func (m *mockNotifyWatcher) Err() error {
   104  	return m.tomb.Err()
   105  }
   106  
   107  func assertNoHooks(c *gc.C, hooks <-chan hook.Info) {
   108  	select {
   109  	case <-hooks:
   110  		c.Fatal("unexpected hook")
   111  	case <-time.After(testing.ShortWait):
   112  	}
   113  }
   114  
   115  func waitOneHook(c *gc.C, hooks <-chan hook.Info) hook.Info {
   116  	var hi hook.Info
   117  	var ok bool
   118  	select {
   119  	case hi, ok = <-hooks:
   120  		c.Assert(ok, jc.IsTrue)
   121  	case <-time.After(testing.LongWait):
   122  		c.Fatal("timed out waiting for hook")
   123  	}
   124  	assertNoHooks(c, hooks)
   125  	return hi
   126  }