github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/apiserver/facades/controller/caasfirewaller/mock_test.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package caasfirewaller_test 5 6 import ( 7 "github.com/juju/charm/v12" 8 "github.com/juju/names/v5" 9 "github.com/juju/testing" 10 11 charmscommon "github.com/juju/juju/apiserver/common/charms" 12 "github.com/juju/juju/apiserver/facades/controller/caasfirewaller" 13 "github.com/juju/juju/core/config" 14 "github.com/juju/juju/core/network" 15 "github.com/juju/juju/state" 16 statetesting "github.com/juju/juju/state/testing" 17 ) 18 19 type mockState struct { 20 testing.Stub 21 application mockApplication 22 applicationsWatcher *statetesting.MockStringsWatcher 23 openPortsWatcher *statetesting.MockStringsWatcher 24 appExposedWatcher *statetesting.MockNotifyWatcher 25 } 26 27 func (st *mockState) WatchApplications() state.StringsWatcher { 28 st.MethodCall(st, "WatchApplications") 29 return st.applicationsWatcher 30 } 31 32 func (st *mockState) WatchOpenedPorts() state.StringsWatcher { 33 st.MethodCall(st, "WatchOpenedPorts") 34 return st.openPortsWatcher 35 } 36 37 func (st *mockState) Application(name string) (caasfirewaller.Application, error) { 38 st.MethodCall(st, "Application", name) 39 if err := st.NextErr(); err != nil { 40 return nil, err 41 } 42 return &st.application, nil 43 } 44 45 func (st *mockState) FindEntity(tag names.Tag) (state.Entity, error) { 46 st.MethodCall(st, "FindEntity", tag) 47 if err := st.NextErr(); err != nil { 48 return nil, err 49 } 50 return &st.application, nil 51 } 52 53 func (st *mockState) Charm(curl string) (*state.Charm, error) { 54 st.MethodCall(st, "Charm", curl) 55 if err := st.NextErr(); err != nil { 56 return nil, err 57 } 58 return nil, nil 59 } 60 61 func (st *mockState) Model() (*state.Model, error) { 62 st.MethodCall(st, "Model") 63 if err := st.NextErr(); err != nil { 64 return nil, err 65 } 66 return nil, nil 67 } 68 69 type mockApplication struct { 70 testing.Stub 71 state.Entity // Pull in Tag method (which tests don't use) 72 life state.Life 73 exposed bool 74 watcher state.NotifyWatcher 75 76 charm mockCharm 77 appPortRanges network.GroupedPortRanges 78 } 79 80 func (a *mockApplication) Life() state.Life { 81 a.MethodCall(a, "Life") 82 return a.life 83 } 84 85 func (a *mockApplication) IsExposed() bool { 86 a.MethodCall(a, "IsExposed") 87 return a.exposed 88 } 89 90 func (a *mockApplication) ApplicationConfig() (config.ConfigAttributes, error) { 91 a.MethodCall(a, "ApplicationConfig") 92 return config.ConfigAttributes{"foo": "bar"}, a.NextErr() 93 } 94 95 func (a *mockApplication) Watch() state.NotifyWatcher { 96 a.MethodCall(a, "Watch") 97 return a.watcher 98 } 99 100 func (a *mockApplication) OpenedPortRanges() (network.GroupedPortRanges, error) { 101 a.MethodCall(a, "OpenedPortRanges") 102 return a.appPortRanges, nil 103 } 104 105 func (a *mockApplication) Charm() (charmscommon.Charm, bool, error) { 106 a.MethodCall(a, "Charm") 107 return &a.charm, false, nil 108 } 109 110 type mockCharm struct { 111 testing.Stub 112 charmscommon.Charm // Override only the methods the tests use 113 meta *charm.Meta 114 manifest *charm.Manifest 115 url string 116 } 117 118 func (s *mockCharm) Meta() *charm.Meta { 119 s.MethodCall(s, "Meta") 120 return s.meta 121 } 122 123 func (s *mockCharm) Manifest() *charm.Manifest { 124 s.MethodCall(s, "Manifest") 125 return s.manifest 126 } 127 128 func (s *mockCharm) URL() string { 129 s.MethodCall(s, "URL") 130 return s.url 131 } 132 133 type mockCommonStateShim struct { 134 *mockState 135 } 136 137 func (s *mockCommonStateShim) Model() (charmscommon.Model, error) { 138 return s.mockState.Model() 139 } 140 141 func (s *mockCommonStateShim) Charm(curl string) (charmscommon.Charm, error) { 142 return s.mockState.Charm(curl) 143 } 144 145 func (s *mockCommonStateShim) Application(id string) (charmscommon.Application, error) { 146 app, err := s.mockState.Application(id) 147 if err != nil { 148 return nil, err 149 } 150 return &mockCommonApplicationShim{app}, nil 151 } 152 153 type mockCommonApplicationShim struct { 154 caasfirewaller.Application 155 } 156 157 func (a *mockCommonApplicationShim) Charm() (st charmscommon.Charm, force bool, err error) { 158 return a.Application.Charm() 159 }