github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/agent/caasoperator/mock_test.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package caasoperator_test 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/testing" 9 "github.com/juju/version" 10 "gopkg.in/juju/charm.v6" 11 "gopkg.in/juju/names.v2" 12 13 "github.com/juju/juju/apiserver/common" 14 "github.com/juju/juju/apiserver/facades/agent/caasoperator" 15 apiservertesting "github.com/juju/juju/apiserver/testing" 16 _ "github.com/juju/juju/caas/kubernetes/provider" 17 "github.com/juju/juju/core/status" 18 "github.com/juju/juju/environs/config" 19 "github.com/juju/juju/network" 20 "github.com/juju/juju/state" 21 statetesting "github.com/juju/juju/state/testing" 22 coretesting "github.com/juju/juju/testing" 23 "github.com/juju/juju/tools" 24 ) 25 26 type mockState struct { 27 testing.Stub 28 common.AddressAndCertGetter 29 entities map[string]state.Entity 30 app mockApplication 31 unit mockUnit 32 model mockModel 33 } 34 35 func newMockState() *mockState { 36 unitsChanges := make(chan []string, 1) 37 appChanges := make(chan struct{}, 1) 38 st := &mockState{ 39 entities: make(map[string]state.Entity), 40 app: mockApplication{ 41 life: state.Alive, 42 charm: mockCharm{ 43 url: charm.MustParseURL("cs:gitlab-1"), 44 sha256: "fake-sha256", 45 }, 46 unitsChanges: unitsChanges, 47 appChanges: appChanges, 48 unitsWatcher: statetesting.NewMockStringsWatcher(unitsChanges), 49 watcher: statetesting.NewMockNotifyWatcher(appChanges), 50 }, 51 unit: mockUnit{ 52 life: state.Dying, 53 }, 54 } 55 st.entities[st.app.Tag().String()] = &st.app 56 st.entities[st.unit.Tag().String()] = &st.unit 57 return st 58 } 59 60 func (st *mockState) APIHostPortsForAgents() ([][]network.HostPort, error) { 61 st.MethodCall(st, "APIHostPortsForAgents") 62 return nil, nil 63 } 64 65 func (st *mockState) WatchAPIHostPortsForAgents() state.NotifyWatcher { 66 st.MethodCall(st, "WatchAPIHostPortsForAgents") 67 return apiservertesting.NewFakeNotifyWatcher() 68 } 69 70 func (st *mockState) Application(id string) (caasoperator.Application, error) { 71 st.MethodCall(st, "Application", id) 72 if err := st.NextErr(); err != nil { 73 return nil, err 74 } 75 return &st.app, nil 76 } 77 78 func (st *mockState) Model() (caasoperator.Model, error) { 79 st.MethodCall(st, "Model") 80 if err := st.NextErr(); err != nil { 81 return nil, err 82 } 83 return &st.model, nil 84 } 85 86 func (st *mockState) FindEntity(tag names.Tag) (state.Entity, error) { 87 st.MethodCall(st, "FindEntity", tag) 88 if err := st.NextErr(); err != nil { 89 return nil, err 90 } 91 entity, ok := st.entities[tag.String()] 92 if !ok { 93 return nil, errors.NotFoundf("%s", names.ReadableString(tag)) 94 } 95 return entity, nil 96 } 97 98 type mockModel struct { 99 testing.Stub 100 } 101 102 func (m *mockModel) SetPodSpec(tag names.ApplicationTag, spec string) error { 103 m.MethodCall(m, "SetPodSpec", tag, spec) 104 return m.NextErr() 105 } 106 107 func (st *mockModel) Name() string { 108 return "some-model" 109 } 110 111 func (st *mockModel) UUID() string { 112 return "deadbeef" 113 } 114 115 func (st *mockModel) Type() state.ModelType { 116 return state.ModelTypeIAAS 117 } 118 119 func (st *mockModel) ModelConfig() (*config.Config, error) { 120 cfg := coretesting.FakeConfig() 121 attr := cfg.Merge(coretesting.Attrs{"type": "kubernetes"}) 122 return config.New(config.NoDefaults, attr) 123 } 124 125 type mockApplication struct { 126 testing.Stub 127 life state.Life 128 charm mockCharm 129 forceUpgrade bool 130 unitsChanges chan []string 131 unitsWatcher *statetesting.MockStringsWatcher 132 appChanges chan struct{} 133 watcher *statetesting.MockNotifyWatcher 134 } 135 136 func (*mockApplication) Tag() names.Tag { 137 return names.NewApplicationTag("gitlab") 138 } 139 140 func (a *mockApplication) Life() state.Life { 141 a.MethodCall(a, "Life") 142 return a.life 143 } 144 145 func (a *mockApplication) SetOperatorStatus(info status.StatusInfo) error { 146 a.MethodCall(a, "SetOperatorStatus", info) 147 return a.NextErr() 148 } 149 150 func (a *mockApplication) Charm() (caasoperator.Charm, bool, error) { 151 a.MethodCall(a, "Charm") 152 if err := a.NextErr(); err != nil { 153 return nil, false, err 154 } 155 return &a.charm, a.forceUpgrade, nil 156 } 157 158 func (a *mockApplication) CharmModifiedVersion() int { 159 a.MethodCall(a, "CharmModifiedVersion") 160 return 666 161 } 162 163 func (a *mockApplication) WatchUnits() state.StringsWatcher { 164 a.MethodCall(a, "WatchUnits") 165 return a.unitsWatcher 166 } 167 168 func (a *mockApplication) Watch() state.NotifyWatcher { 169 a.MethodCall(a, "Watch") 170 return a.watcher 171 } 172 173 func (a *mockApplication) AllUnits() ([]caasoperator.Unit, error) { 174 a.MethodCall(a, "AllUnits") 175 if err := a.NextErr(); err != nil { 176 return nil, err 177 } 178 return []caasoperator.Unit{&mockUnit{}}, nil 179 } 180 181 func (a *mockApplication) AgentTools() (*tools.Tools, error) { 182 return nil, errors.NotImplementedf("AgentTools") 183 } 184 185 func (a *mockApplication) SetAgentVersion(vers version.Binary) error { 186 a.MethodCall(a, "SetAgentVersion", vers) 187 return nil 188 } 189 190 type mockUnit struct { 191 testing.Stub 192 life state.Life 193 } 194 195 func (*mockUnit) Tag() names.Tag { 196 return names.NewUnitTag("gitlab/0") 197 } 198 199 func (u *mockUnit) Life() state.Life { 200 u.MethodCall(u, "Life") 201 return u.life 202 } 203 204 func (u *mockUnit) Remove() error { 205 u.MethodCall(u, "Remove") 206 return nil 207 } 208 209 func (u *mockUnit) EnsureDead() error { 210 u.MethodCall(u, "EnsureDead") 211 return nil 212 } 213 214 type mockCharm struct { 215 url *charm.URL 216 sha256 string 217 } 218 219 func (ch *mockCharm) URL() *charm.URL { 220 return ch.url 221 } 222 223 func (ch *mockCharm) BundleSha256() string { 224 return ch.sha256 225 }