github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/controller/caasoperatorprovisioner/mock_test.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caasoperatorprovisioner_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/testing"
     9  	"gopkg.in/juju/names.v2"
    10  	"gopkg.in/tomb.v2"
    11  
    12  	"github.com/juju/juju/apiserver/common"
    13  	"github.com/juju/juju/apiserver/facades/controller/caasoperatorprovisioner"
    14  	"github.com/juju/juju/caas/kubernetes/provider"
    15  	"github.com/juju/juju/controller"
    16  	"github.com/juju/juju/environs/config"
    17  	"github.com/juju/juju/network"
    18  	"github.com/juju/juju/state"
    19  	"github.com/juju/juju/storage"
    20  	"github.com/juju/juju/storage/poolmanager"
    21  	coretesting "github.com/juju/juju/testing"
    22  )
    23  
    24  type mockState struct {
    25  	testing.Stub
    26  	common.AddressAndCertGetter
    27  	model              *mockModel
    28  	applicationWatcher *mockStringsWatcher
    29  	app                *mockApplication
    30  	operatorImage      string
    31  }
    32  
    33  func newMockState() *mockState {
    34  	return &mockState{
    35  		applicationWatcher: newMockStringsWatcher(),
    36  		model:              &mockModel{},
    37  	}
    38  }
    39  
    40  func (st *mockState) WatchApplications() state.StringsWatcher {
    41  	st.MethodCall(st, "WatchApplications")
    42  	return st.applicationWatcher
    43  }
    44  
    45  func (st *mockState) FindEntity(tag names.Tag) (state.Entity, error) {
    46  	if st.app.tag == tag {
    47  		return st.app, nil
    48  	}
    49  	return nil, errors.NotFoundf("entity %v", tag)
    50  }
    51  
    52  func (st *mockState) ControllerConfig() (controller.Config, error) {
    53  	cfg := coretesting.FakeControllerConfig()
    54  	cfg[controller.CAASOperatorImagePath] = st.operatorImage
    55  	return cfg, nil
    56  }
    57  
    58  func (st *mockState) APIHostPortsForAgents() ([][]network.HostPort, error) {
    59  	st.MethodCall(st, "APIHostPortsForAgents")
    60  	return [][]network.HostPort{
    61  		network.NewHostPorts(1, "10.0.0.1"),
    62  	}, nil
    63  }
    64  
    65  func (st *mockState) Model() (caasoperatorprovisioner.Model, error) {
    66  	st.MethodCall(st, "Model")
    67  	if err := st.NextErr(); err != nil {
    68  		return nil, err
    69  	}
    70  	return st.model, nil
    71  }
    72  
    73  type mockStorageProviderRegistry struct {
    74  	testing.Stub
    75  	storage.ProviderRegistry
    76  }
    77  
    78  func (m *mockStorageProviderRegistry) StorageProvider(providerType storage.ProviderType) (storage.Provider, error) {
    79  	m.MethodCall(m, "StorageProvider", providerType)
    80  	return nil, errors.NotSupportedf("StorageProvider")
    81  }
    82  
    83  type mockStoragePoolManager struct {
    84  	testing.Stub
    85  	poolmanager.PoolManager
    86  }
    87  
    88  func (m *mockStoragePoolManager) Get(name string) (*storage.Config, error) {
    89  	m.MethodCall(m, "Get", name)
    90  	if err := m.NextErr(); err != nil {
    91  		return nil, err
    92  	}
    93  	return storage.NewConfig(name, provider.K8s_ProviderType, map[string]interface{}{"foo": "bar"})
    94  }
    95  
    96  type mockModel struct {
    97  	testing.Stub
    98  }
    99  
   100  func (m *mockModel) UUID() string {
   101  	m.MethodCall(m, "UUID")
   102  	return coretesting.ModelTag.Id()
   103  }
   104  
   105  func (m *mockModel) ModelConfig() (*config.Config, error) {
   106  	m.MethodCall(m, "ModelConfig")
   107  	return config.New(config.UseDefaults, coretesting.FakeConfig())
   108  }
   109  
   110  type mockApplication struct {
   111  	state.Authenticator
   112  	tag      names.Tag
   113  	password string
   114  }
   115  
   116  func (m *mockApplication) Tag() names.Tag {
   117  	return m.tag
   118  }
   119  
   120  func (m *mockApplication) SetPassword(password string) error {
   121  	m.password = password
   122  	return nil
   123  }
   124  
   125  func (a *mockApplication) Life() state.Life {
   126  	return state.Alive
   127  }
   128  
   129  type mockWatcher struct {
   130  	testing.Stub
   131  	tomb.Tomb
   132  }
   133  
   134  func (w *mockWatcher) Kill() {
   135  	w.MethodCall(w, "Kill")
   136  	w.Tomb.Kill(nil)
   137  }
   138  
   139  func (w *mockWatcher) Stop() error {
   140  	w.MethodCall(w, "Stop")
   141  	if err := w.NextErr(); err != nil {
   142  		return err
   143  	}
   144  	w.Tomb.Kill(nil)
   145  	return w.Tomb.Wait()
   146  }
   147  
   148  type mockStringsWatcher struct {
   149  	mockWatcher
   150  	changes chan []string
   151  }
   152  
   153  func newMockStringsWatcher() *mockStringsWatcher {
   154  	w := &mockStringsWatcher{changes: make(chan []string, 1)}
   155  	w.Tomb.Go(func() error {
   156  		<-w.Tomb.Dying()
   157  		return nil
   158  	})
   159  	return w
   160  }
   161  
   162  func (w *mockStringsWatcher) Changes() <-chan []string {
   163  	w.MethodCall(w, "Changes")
   164  	return w.changes
   165  }