github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/snapstate/export_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016-2019 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package snapstate 21 22 import ( 23 "context" 24 "time" 25 26 "github.com/snapcore/snapd/overlord/state" 27 "github.com/snapcore/snapd/snap" 28 "github.com/snapcore/snapd/store" 29 ) 30 31 type ManagerBackend managerBackend 32 33 func SetSnapManagerBackend(s *SnapManager, b ManagerBackend) { 34 s.backend = b 35 } 36 37 func MockSnapReadInfo(mock func(name string, si *snap.SideInfo) (*snap.Info, error)) (restore func()) { 38 old := snapReadInfo 39 snapReadInfo = mock 40 return func() { snapReadInfo = old } 41 } 42 43 func MockMountPollInterval(intv time.Duration) (restore func()) { 44 old := mountPollInterval 45 mountPollInterval = intv 46 return func() { mountPollInterval = old } 47 } 48 49 func MockRevisionDate(mock func(info *snap.Info) time.Time) (restore func()) { 50 old := revisionDate 51 if mock == nil { 52 mock = revisionDateImpl 53 } 54 revisionDate = mock 55 return func() { revisionDate = old } 56 } 57 58 func MockOpenSnapFile(mock func(path string, si *snap.SideInfo) (*snap.Info, snap.Container, error)) (restore func()) { 59 prevOpenSnapFile := openSnapFile 60 openSnapFile = mock 61 return func() { openSnapFile = prevOpenSnapFile } 62 } 63 64 func MockErrtrackerReport(mock func(string, string, string, map[string]string) (string, error)) (restore func()) { 65 prev := errtrackerReport 66 errtrackerReport = mock 67 return func() { errtrackerReport = prev } 68 } 69 70 func MockPrerequisitesRetryTimeout(d time.Duration) (restore func()) { 71 old := prerequisitesRetryTimeout 72 prerequisitesRetryTimeout = d 73 return func() { prerequisitesRetryTimeout = old } 74 } 75 76 func MockOsutilEnsureUserGroup(mock func(name string, id uint32, extraUsers bool) error) (restore func()) { 77 old := osutilEnsureUserGroup 78 osutilEnsureUserGroup = mock 79 return func() { osutilEnsureUserGroup = old } 80 } 81 82 var ( 83 CoreInfoInternal = coreInfo 84 CheckSnap = checkSnap 85 CanRemove = canRemove 86 CanDisable = canDisable 87 CachedStore = cachedStore 88 DefaultRefreshSchedule = defaultRefreshSchedule 89 DoInstall = doInstall 90 UserFromUserID = userFromUserID 91 ValidateFeatureFlags = validateFeatureFlags 92 ResolveChannel = resolveChannel 93 94 CurrentSnaps = currentSnaps 95 96 DefaultContentPlugProviders = defaultContentPlugProviders 97 98 HasOtherInstances = hasOtherInstances 99 ) 100 101 func PreviousSideInfo(snapst *SnapState) *snap.SideInfo { 102 return snapst.previousSideInfo() 103 } 104 105 // helpers 106 var InstallSize = installSize 107 108 // aliases v2 109 var ( 110 ApplyAliasesChange = applyAliasesChange 111 AutoAliasesDelta = autoAliasesDelta 112 RefreshAliases = refreshAliases 113 CheckAliasesConflicts = checkAliasesConflicts 114 DisableAliases = disableAliases 115 SwitchSummary = switchSummary 116 ) 117 118 // dbus 119 var ( 120 CheckDBusServiceConflicts = checkDBusServiceConflicts 121 ) 122 123 // readme files 124 var ( 125 WriteSnapReadme = writeSnapReadme 126 SnapReadme = snapReadme 127 ) 128 129 // refreshes 130 var ( 131 NewAutoRefresh = newAutoRefresh 132 NewRefreshHints = newRefreshHints 133 CanRefreshOnMeteredConnection = canRefreshOnMeteredConnection 134 135 NewCatalogRefresh = newCatalogRefresh 136 CatalogRefreshDelayBase = catalogRefreshDelayBase 137 CatalogRefreshDelayWithDelta = catalogRefreshDelayWithDelta 138 ) 139 140 func MockNextRefresh(ar *autoRefresh, when time.Time) { 141 ar.nextRefresh = when 142 } 143 144 func MockLastRefreshSchedule(ar *autoRefresh, schedule string) { 145 ar.lastRefreshSchedule = schedule 146 } 147 148 func MockCatalogRefreshNextRefresh(cr *catalogRefresh, when time.Time) { 149 cr.nextCatalogRefresh = when 150 } 151 152 func NextCatalogRefresh(cr *catalogRefresh) time.Time { 153 return cr.nextCatalogRefresh 154 } 155 156 func MockRefreshRetryDelay(d time.Duration) func() { 157 origRefreshRetryDelay := refreshRetryDelay 158 refreshRetryDelay = d 159 return func() { 160 refreshRetryDelay = origRefreshRetryDelay 161 } 162 } 163 164 func MockIsOnMeteredConnection(mock func() (bool, error)) func() { 165 old := IsOnMeteredConnection 166 IsOnMeteredConnection = mock 167 return func() { 168 IsOnMeteredConnection = old 169 } 170 } 171 172 func MockLocalInstallCleanupWait(d time.Duration) (restore func()) { 173 old := localInstallCleanupWait 174 localInstallCleanupWait = d 175 return func() { 176 localInstallCleanupWait = old 177 } 178 } 179 180 func MockLocalInstallLastCleanup(t time.Time) (restore func()) { 181 old := localInstallLastCleanup 182 localInstallLastCleanup = t 183 return func() { 184 localInstallLastCleanup = old 185 } 186 } 187 188 // re-refresh related 189 var ( 190 RefreshedSnaps = refreshedSnaps 191 ReRefreshFilter = reRefreshFilter 192 ) 193 194 type UpdateFilter = updateFilter 195 196 func MockReRefreshUpdateMany(f func(context.Context, *state.State, []string, int, UpdateFilter, *Flags, string) ([]string, []*state.TaskSet, error)) (restore func()) { 197 old := reRefreshUpdateMany 198 reRefreshUpdateMany = f 199 return func() { 200 reRefreshUpdateMany = old 201 } 202 } 203 204 func MockReRefreshRetryTimeout(d time.Duration) (restore func()) { 205 old := reRefreshRetryTimeout 206 reRefreshRetryTimeout = d 207 return func() { 208 reRefreshRetryTimeout = old 209 } 210 } 211 212 // aux store info 213 var ( 214 AuxStoreInfoFilename = auxStoreInfoFilename 215 RetrieveAuxStoreInfo = retrieveAuxStoreInfo 216 KeepAuxStoreInfo = keepAuxStoreInfo 217 DiscardAuxStoreInfo = discardAuxStoreInfo 218 ) 219 220 type AuxStoreInfo = auxStoreInfo 221 222 // link, misc handlers 223 var ( 224 MissingDisabledServices = missingDisabledServices 225 ) 226 227 func (m *SnapManager) MaybeUndoRemodelBootChanges(t *state.Task) error { 228 return m.maybeUndoRemodelBootChanges(t) 229 } 230 231 func MockPidsOfSnap(f func(instanceName string) (map[string][]int, error)) func() { 232 old := pidsOfSnap 233 pidsOfSnap = f 234 return func() { 235 pidsOfSnap = old 236 } 237 } 238 239 func MockCurrentSnaps(f func(st *state.State) ([]*store.CurrentSnap, error)) func() { 240 old := currentSnaps 241 currentSnaps = f 242 return func() { 243 currentSnaps = old 244 } 245 } 246 247 func MockInstallSize(f func(st *state.State, snaps []*snap.Info, userID int) (uint64, error)) func() { 248 old := installSize 249 installSize = f 250 return func() { 251 installSize = old 252 } 253 } 254 255 // autorefresh 256 var ( 257 InhibitRefresh = inhibitRefresh 258 MaxInhibition = maxInhibition 259 )