github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/worker/externalcontrollerupdater/mock_test.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package externalcontrollerupdater_test
     5  
     6  import (
     7  	"github.com/juju/testing"
     8  	tomb "gopkg.in/tomb.v2"
     9  
    10  	"github.com/juju/juju/api/controller/crosscontroller"
    11  	"github.com/juju/juju/core/crossmodel"
    12  	"github.com/juju/juju/core/watcher"
    13  )
    14  
    15  type mockExternalControllerUpdaterClient struct {
    16  	testing.Stub
    17  	watcher *mockStringsWatcher
    18  	info    crossmodel.ControllerInfo
    19  }
    20  
    21  func (m *mockExternalControllerUpdaterClient) WatchExternalControllers() (watcher.StringsWatcher, error) {
    22  	m.MethodCall(m, "WatchExternalControllers")
    23  	return m.watcher, m.NextErr()
    24  }
    25  
    26  func (m *mockExternalControllerUpdaterClient) ExternalControllerInfo(controllerUUID string) (*crossmodel.ControllerInfo, error) {
    27  	m.MethodCall(m, "ExternalControllerInfo", controllerUUID)
    28  	copied := m.info
    29  	copied.Addrs = make([]string, len(m.info.Addrs))
    30  	copy(copied.Addrs, m.info.Addrs)
    31  	return &copied, m.NextErr()
    32  }
    33  
    34  func (m *mockExternalControllerUpdaterClient) SetExternalControllerInfo(info crossmodel.ControllerInfo) error {
    35  	m.MethodCall(m, "SetExternalControllerInfo", info)
    36  	return m.NextErr()
    37  }
    38  
    39  type mockExternalControllerWatcherClient struct {
    40  	testing.Stub
    41  	watcher *mockNotifyWatcher
    42  	info    crosscontroller.ControllerInfo
    43  }
    44  
    45  func (m *mockExternalControllerWatcherClient) Close() error {
    46  	m.MethodCall(m, "Close")
    47  	return m.NextErr()
    48  }
    49  
    50  func (m *mockExternalControllerWatcherClient) WatchControllerInfo() (watcher.NotifyWatcher, error) {
    51  	m.MethodCall(m, "WatchControllerInfo")
    52  	return m.watcher, m.NextErr()
    53  }
    54  
    55  func (m *mockExternalControllerWatcherClient) ControllerInfo() (*crosscontroller.ControllerInfo, error) {
    56  	m.MethodCall(m, "ControllerInfo")
    57  	copied := m.info
    58  	copied.Addrs = make([]string, len(m.info.Addrs))
    59  	copy(copied.Addrs, m.info.Addrs)
    60  	return &copied, m.NextErr()
    61  }
    62  
    63  type mockStringsWatcher struct {
    64  	mockWatcher
    65  	changes chan []string
    66  }
    67  
    68  func newMockStringsWatcher() *mockStringsWatcher {
    69  	w := &mockStringsWatcher{changes: make(chan []string, 1)}
    70  	w.tomb.Go(func() error {
    71  		w.loop()
    72  		return nil
    73  	})
    74  	return w
    75  }
    76  
    77  func (w *mockStringsWatcher) Changes() watcher.StringsChannel {
    78  	return w.changes
    79  }
    80  
    81  type mockNotifyWatcher struct {
    82  	mockWatcher
    83  	changes chan struct{}
    84  }
    85  
    86  func newMockNotifyWatcher() *mockNotifyWatcher {
    87  	w := &mockNotifyWatcher{changes: make(chan struct{}, 1)}
    88  	w.tomb.Go(func() error {
    89  		w.loop()
    90  		return nil
    91  	})
    92  	return w
    93  }
    94  
    95  func (w *mockNotifyWatcher) Changes() watcher.NotifyChannel {
    96  	return w.changes
    97  }
    98  
    99  type mockWatcher struct {
   100  	tomb tomb.Tomb
   101  }
   102  
   103  func (w *mockWatcher) loop() {
   104  	<-w.tomb.Dying()
   105  }
   106  
   107  func (w *mockWatcher) Stop() error {
   108  	w.Kill()
   109  	return w.Wait()
   110  }
   111  
   112  func (w *mockWatcher) Wait() error {
   113  	return w.tomb.Wait()
   114  }
   115  
   116  func (w *mockWatcher) Kill() {
   117  	w.tomb.Kill(nil)
   118  }
   119  
   120  func (w *mockWatcher) Err() error {
   121  	return w.tomb.Err()
   122  }