github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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/charm/v12"
     8  	"github.com/juju/errors"
     9  	"github.com/juju/names/v5"
    10  	"github.com/juju/testing"
    11  	"gopkg.in/tomb.v2"
    12  
    13  	"github.com/juju/juju/apiserver/common"
    14  	charmscommon "github.com/juju/juju/apiserver/common/charms"
    15  	"github.com/juju/juju/apiserver/facades/controller/caasoperatorprovisioner"
    16  	k8sconstants "github.com/juju/juju/caas/kubernetes/provider/constants"
    17  	"github.com/juju/juju/controller"
    18  	"github.com/juju/juju/core/network"
    19  	"github.com/juju/juju/environs/config"
    20  	"github.com/juju/juju/state"
    21  	"github.com/juju/juju/storage"
    22  	"github.com/juju/juju/storage/poolmanager"
    23  	coretesting "github.com/juju/juju/testing"
    24  )
    25  
    26  type mockState struct {
    27  	testing.Stub
    28  	common.APIAddressAccessor
    29  	model              *mockModel
    30  	applicationWatcher *mockStringsWatcher
    31  	app                *mockApplication
    32  	operatorRepo       string
    33  }
    34  
    35  func newMockState() *mockState {
    36  	return &mockState{
    37  		applicationWatcher: newMockStringsWatcher(),
    38  		model:              &mockModel{},
    39  	}
    40  }
    41  
    42  func (st *mockState) WatchApplications() state.StringsWatcher {
    43  	st.MethodCall(st, "WatchApplications")
    44  	return st.applicationWatcher
    45  }
    46  
    47  func (st *mockState) FindEntity(tag names.Tag) (state.Entity, error) {
    48  	if st.app.tag == tag {
    49  		return st.app, nil
    50  	}
    51  	return nil, errors.NotFoundf("entity %v", tag)
    52  }
    53  
    54  func (st *mockState) ControllerConfig() (controller.Config, error) {
    55  	cfg := coretesting.FakeControllerConfig()
    56  	cfg[controller.CAASImageRepo] = st.operatorRepo
    57  	return cfg, nil
    58  }
    59  
    60  func (st *mockState) APIHostPortsForAgents() ([]network.SpaceHostPorts, error) {
    61  	st.MethodCall(st, "APIHostPortsForAgents")
    62  	return []network.SpaceHostPorts{
    63  		network.NewSpaceHostPorts(1, "10.0.0.1"),
    64  	}, nil
    65  }
    66  
    67  func (st *mockState) Application(appName string) (caasoperatorprovisioner.Application, error) {
    68  	st.MethodCall(st, "Application", appName)
    69  	if appName != "gitlab" {
    70  		return nil, errors.NotFoundf("app %v", appName)
    71  	}
    72  	return st.app, nil
    73  }
    74  
    75  func (st *mockState) Model() (caasoperatorprovisioner.Model, error) {
    76  	st.MethodCall(st, "Model")
    77  	if err := st.NextErr(); err != nil {
    78  		return nil, err
    79  	}
    80  	return st.model, nil
    81  }
    82  
    83  func (st *mockState) StateServingInfo() (controller.StateServingInfo, error) {
    84  	st.MethodCall(st, "StateServingInfo")
    85  	return controller.StateServingInfo{
    86  		CAPrivateKey: coretesting.CAKey,
    87  	}, nil
    88  }
    89  
    90  type mockStorageRegistry struct {
    91  	storage.ProviderRegistry
    92  }
    93  
    94  func (m *mockStorageRegistry) StorageProvider(p storage.ProviderType) (storage.Provider, error) {
    95  	return nil, errors.NotFoundf("provider %q", p)
    96  }
    97  
    98  type mockStoragePoolManager struct {
    99  	testing.Stub
   100  	poolmanager.PoolManager
   101  }
   102  
   103  func (m *mockStoragePoolManager) Get(name string) (*storage.Config, error) {
   104  	m.MethodCall(m, "Get", name)
   105  	if err := m.NextErr(); err != nil {
   106  		return nil, err
   107  	}
   108  	return storage.NewConfig(name, k8sconstants.CAASProviderType, map[string]interface{}{"foo": "bar"})
   109  }
   110  
   111  type mockModel struct {
   112  	testing.Stub
   113  }
   114  
   115  func (m *mockModel) UUID() string {
   116  	m.MethodCall(m, "UUID")
   117  	return coretesting.ModelTag.Id()
   118  }
   119  
   120  func (m *mockModel) ModelConfig() (*config.Config, error) {
   121  	m.MethodCall(m, "ModelConfig")
   122  	attrs := coretesting.FakeConfig()
   123  	attrs["operator-storage"] = "k8s-storage"
   124  	attrs["agent-version"] = "2.6-beta3.666"
   125  	return config.New(config.UseDefaults, attrs)
   126  }
   127  
   128  type mockApplication struct {
   129  	state.Authenticator
   130  	tag      names.Tag
   131  	password string
   132  	charm    *mockCharm
   133  }
   134  
   135  func (m *mockApplication) Tag() names.Tag {
   136  	return m.tag
   137  }
   138  
   139  func (m *mockApplication) SetPassword(password string) error {
   140  	m.password = password
   141  	return nil
   142  }
   143  
   144  func (a *mockApplication) Life() state.Life {
   145  	return state.Alive
   146  }
   147  
   148  func (a *mockApplication) Charm() (charmscommon.Charm, bool, error) {
   149  	return a.charm, false, nil
   150  }
   151  
   152  type mockCharm struct {
   153  	charmscommon.Charm
   154  	meta     *charm.Meta
   155  	manifest *charm.Manifest
   156  }
   157  
   158  func (ch *mockCharm) Meta() *charm.Meta {
   159  	return ch.meta
   160  }
   161  
   162  func (ch *mockCharm) Manifest() *charm.Manifest {
   163  	return ch.manifest
   164  }
   165  
   166  type mockWatcher struct {
   167  	testing.Stub
   168  	tomb.Tomb
   169  }
   170  
   171  func (w *mockWatcher) Kill() {
   172  	w.MethodCall(w, "Kill")
   173  	w.Tomb.Kill(nil)
   174  }
   175  
   176  func (w *mockWatcher) Stop() error {
   177  	w.MethodCall(w, "Stop")
   178  	if err := w.NextErr(); err != nil {
   179  		return err
   180  	}
   181  	w.Tomb.Kill(nil)
   182  	return w.Tomb.Wait()
   183  }
   184  
   185  type mockStringsWatcher struct {
   186  	mockWatcher
   187  	changes chan []string
   188  }
   189  
   190  func newMockStringsWatcher() *mockStringsWatcher {
   191  	w := &mockStringsWatcher{changes: make(chan []string, 1)}
   192  	w.Tomb.Go(func() error {
   193  		<-w.Tomb.Dying()
   194  		return nil
   195  	})
   196  	return w
   197  }
   198  
   199  func (w *mockStringsWatcher) Changes() <-chan []string {
   200  	w.MethodCall(w, "Changes")
   201  	return w.changes
   202  }