github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/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/charm/v12" 8 "github.com/juju/errors" 9 "github.com/juju/names/v5" 10 "github.com/juju/proxy" 11 "github.com/juju/testing" 12 "github.com/juju/version/v2" 13 "k8s.io/client-go/kubernetes" 14 15 "github.com/juju/juju/agent" 16 caasoperatorapi "github.com/juju/juju/api/agent/caasoperator" 17 "github.com/juju/juju/caas" 18 "github.com/juju/juju/caas/kubernetes/provider/exec" 19 "github.com/juju/juju/core/life" 20 "github.com/juju/juju/core/model" 21 "github.com/juju/juju/core/status" 22 "github.com/juju/juju/core/watcher" 23 "github.com/juju/juju/core/watcher/watchertest" 24 "github.com/juju/juju/downloader" 25 coretesting "github.com/juju/juju/testing" 26 "github.com/juju/juju/worker/caasoperator" 27 "github.com/juju/juju/worker/fortress" 28 ) 29 30 var ( 31 gitlabCharmURL = charm.MustParseURL("ch:gitlab-1") 32 gitlabSettings = charm.Settings{"k": 123} 33 34 fakeCharmContent = []byte("abc") 35 fakeCharmSHA256 = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" 36 fakeModifiedVersion = 666 37 ) 38 39 type fakeAgent struct { 40 agent.Agent 41 config fakeAgentConfig 42 } 43 44 func (a *fakeAgent) CurrentConfig() agent.Config { 45 return &a.config 46 } 47 48 type fakeAgentConfig struct { 49 agent.Config 50 dataDir string 51 tag names.Tag 52 } 53 54 func (c *fakeAgentConfig) Tag() names.Tag { 55 return c.tag 56 } 57 58 func (c *fakeAgentConfig) Model() names.ModelTag { 59 return coretesting.ModelTag 60 } 61 62 func (c *fakeAgentConfig) DataDir() string { 63 return c.dataDir 64 } 65 66 type fakeClient struct { 67 testing.Stub 68 caasoperator.Client 69 unitsWatcher *watchertest.MockStringsWatcher 70 containerWatcher *watchertest.MockStringsWatcher 71 watcher *watchertest.MockNotifyWatcher 72 applicationWatched chan struct{} 73 unitRemoved chan struct{} 74 life life.Value 75 mode caas.DeploymentMode 76 } 77 78 func (c *fakeClient) SetStatus(application string, status status.Status, message string, data map[string]interface{}) error { 79 c.MethodCall(c, "SetStatus", application, status, message, data) 80 return c.NextErr() 81 } 82 83 func (c *fakeClient) Charm(application string) (*caasoperatorapi.CharmInfo, error) { 84 c.MethodCall(c, "Charm", application) 85 if err := c.NextErr(); err != nil { 86 return nil, err 87 } 88 return &caasoperatorapi.CharmInfo{ 89 URL: gitlabCharmURL, 90 ForceUpgrade: true, 91 SHA256: fakeCharmSHA256, 92 CharmModifiedVersion: fakeModifiedVersion, 93 DeploymentMode: c.mode, 94 }, nil 95 } 96 97 func (c *fakeClient) CharmConfig(application string) (charm.Settings, error) { 98 c.MethodCall(c, "CharmConfig", application) 99 if err := c.NextErr(); err != nil { 100 return nil, err 101 } 102 return gitlabSettings, nil 103 } 104 105 func (c *fakeClient) WatchCharmConfig(application string) (watcher.NotifyWatcher, error) { 106 return nil, errors.NotSupportedf("watch charm config") 107 } 108 109 func (c *fakeClient) WatchContainerStart(application, container string) (watcher.StringsWatcher, error) { 110 c.MethodCall(c, "WatchContainerStart", application, container) 111 if err := c.NextErr(); err != nil { 112 return nil, err 113 } 114 return c.containerWatcher, nil 115 } 116 117 func (c *fakeClient) WatchUnits(application string) (watcher.StringsWatcher, error) { 118 c.MethodCall(c, "WatchUnits", application) 119 if err := c.NextErr(); err != nil { 120 return nil, err 121 } 122 return c.unitsWatcher, nil 123 } 124 125 func (c *fakeClient) Watch(application string) (watcher.NotifyWatcher, error) { 126 c.MethodCall(c, "Watch", application) 127 if err := c.NextErr(); err != nil { 128 return nil, err 129 } 130 c.applicationWatched <- struct{}{} 131 return c.watcher, nil 132 } 133 134 func (c *fakeClient) RemoveUnit(unit string) error { 135 c.MethodCall(c, "RemoveUnit", unit) 136 c.unitRemoved <- struct{}{} 137 return c.NextErr() 138 } 139 140 func (c *fakeClient) SetVersion(appName string, v version.Binary) error { 141 c.MethodCall(c, "SetVersion", appName, v) 142 return c.NextErr() 143 } 144 145 func (c *fakeClient) Life(entity string) (life.Value, error) { 146 c.MethodCall(c, "Life", entity) 147 if err := c.NextErr(); err != nil { 148 return life.Dead, err 149 } 150 return c.life, nil 151 } 152 153 func (c *fakeClient) APIAddresses() ([]string, error) { 154 c.MethodCall(c, "APIAddresses", nil) 155 if err := c.NextErr(); err != nil { 156 return nil, err 157 } 158 return []string{"10.0.0.1:10000"}, nil 159 } 160 161 func (c *fakeClient) ProxySettings() (proxy.Settings, error) { 162 c.MethodCall(c, "ProxySettings", nil) 163 if err := c.NextErr(); err != nil { 164 return proxy.Settings{}, err 165 } 166 return proxy.Settings{Http: "http.proxy"}, nil 167 } 168 169 func (c *fakeClient) Model() (*model.Model, error) { 170 return &model.Model{ 171 Name: "gitlab-model", 172 }, nil 173 } 174 175 type fakeDownloader struct { 176 testing.Stub 177 path string 178 } 179 180 func (d *fakeDownloader) Download(req downloader.Request) (string, error) { 181 d.MethodCall(d, "Download", req) 182 if err := d.NextErr(); err != nil { 183 return "", err 184 } 185 return d.path, nil 186 } 187 188 type mockCharmDirGuard struct { 189 fortress.Guard 190 testing.Stub 191 } 192 193 func (l *mockCharmDirGuard) Unlock() error { 194 l.MethodCall(l, "Unlock") 195 return l.NextErr() 196 } 197 198 func (l *mockCharmDirGuard) Lockdown(abort fortress.Abort) error { 199 l.MethodCall(l, "Lockdown", abort) 200 return l.NextErr() 201 } 202 203 type mockHookLogger struct { 204 stopped bool 205 } 206 207 func (m *mockHookLogger) Stop() { 208 m.stopped = true 209 } 210 211 type mockExecutor struct { 212 testing.Stub 213 214 status exec.Status 215 } 216 217 func (m *mockExecutor) Copy(params exec.CopyParams, cancel <-chan struct{}) error { 218 m.MethodCall(m, "Copy", params, cancel) 219 return m.NextErr() 220 } 221 222 func (m *mockExecutor) Exec(params exec.ExecParams, cancel <-chan struct{}) error { 223 m.MethodCall(m, "Exec", params, cancel) 224 return m.NextErr() 225 } 226 227 func (m *mockExecutor) Status(params exec.StatusParams) (*exec.Status, error) { 228 m.MethodCall(m, "Status", params) 229 return &m.status, m.NextErr() 230 } 231 232 func (m *mockExecutor) NameSpace() string { 233 m.MethodCall(m, "NameSpace") 234 return "test" 235 } 236 237 func (m *mockExecutor) RawClient() kubernetes.Interface { 238 m.MethodCall(m, "RawClient") 239 return nil 240 }