github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/controller/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  	"gopkg.in/tomb.v2"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/juju/core/crossmodel"
    11  	"github.com/juju/juju/state"
    12  )
    13  
    14  type mockExternalControllers struct {
    15  	state.ExternalControllers
    16  	controllers []*mockExternalController
    17  	watcher     *mockStringsWatcher
    18  }
    19  
    20  func (m *mockExternalControllers) Watch() state.StringsWatcher {
    21  	return m.watcher
    22  }
    23  
    24  func (m *mockExternalControllers) Controller(uuid string) (state.ExternalController, error) {
    25  	for _, c := range m.controllers {
    26  		if c.id == uuid {
    27  			return c, nil
    28  		}
    29  	}
    30  	return nil, errors.NotFoundf("external controller %q", uuid)
    31  }
    32  
    33  func (m *mockExternalControllers) Save(info crossmodel.ControllerInfo, _ ...string) (state.ExternalController, error) {
    34  	for _, c := range m.controllers {
    35  		if c.id == info.ControllerTag.Id() {
    36  			c.info = info
    37  			return c, nil
    38  		}
    39  	}
    40  	c := &mockExternalController{
    41  		id:   info.ControllerTag.Id(),
    42  		info: info,
    43  	}
    44  	m.controllers = append(m.controllers, c)
    45  	return c, nil
    46  }
    47  
    48  type mockExternalController struct {
    49  	id   string
    50  	info crossmodel.ControllerInfo
    51  }
    52  
    53  func (c *mockExternalController) Id() string {
    54  	return c.id
    55  }
    56  
    57  func (c *mockExternalController) ControllerInfo() crossmodel.ControllerInfo {
    58  	return c.info
    59  }
    60  
    61  type mockStringsWatcher struct {
    62  	tomb    tomb.Tomb
    63  	changes chan []string
    64  }
    65  
    66  func newMockStringsWatcher() *mockStringsWatcher {
    67  	w := &mockStringsWatcher{changes: make(chan []string, 1)}
    68  	w.tomb.Go(func() error {
    69  		w.loop()
    70  		return nil
    71  	})
    72  	return w
    73  }
    74  
    75  func (w *mockStringsWatcher) loop() {
    76  	<-w.tomb.Dying()
    77  }
    78  
    79  func (w *mockStringsWatcher) Stop() error {
    80  	w.Kill()
    81  	return w.Wait()
    82  }
    83  
    84  func (w *mockStringsWatcher) Wait() error {
    85  	return w.tomb.Wait()
    86  }
    87  
    88  func (w *mockStringsWatcher) Kill() {
    89  	w.tomb.Kill(nil)
    90  }
    91  
    92  func (w *mockStringsWatcher) Err() error {
    93  	return w.tomb.Err()
    94  }
    95  
    96  func (w *mockStringsWatcher) Changes() <-chan []string {
    97  	return w.changes
    98  }