gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/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 userclient "github.com/snapcore/snapd/usersession/client" 30 ) 31 32 type ManagerBackend managerBackend 33 34 type MinimalInstallInfo = minimalInstallInfo 35 type InstallSnapInfo = installSnapInfo 36 type ByType = byType 37 38 func SetSnapManagerBackend(s *SnapManager, b ManagerBackend) { 39 s.backend = b 40 } 41 42 func MockSnapReadInfo(mock func(name string, si *snap.SideInfo) (*snap.Info, error)) (restore func()) { 43 old := snapReadInfo 44 snapReadInfo = mock 45 return func() { snapReadInfo = old } 46 } 47 48 func MockMountPollInterval(intv time.Duration) (restore func()) { 49 old := mountPollInterval 50 mountPollInterval = intv 51 return func() { mountPollInterval = old } 52 } 53 54 func MockRevisionDate(mock func(info *snap.Info) time.Time) (restore func()) { 55 old := revisionDate 56 if mock == nil { 57 mock = revisionDateImpl 58 } 59 revisionDate = mock 60 return func() { revisionDate = old } 61 } 62 63 func MockOpenSnapFile(mock func(path string, si *snap.SideInfo) (*snap.Info, snap.Container, error)) (restore func()) { 64 prevOpenSnapFile := openSnapFile 65 openSnapFile = mock 66 return func() { openSnapFile = prevOpenSnapFile } 67 } 68 69 func MockErrtrackerReport(mock func(string, string, string, map[string]string) (string, error)) (restore func()) { 70 prev := errtrackerReport 71 errtrackerReport = mock 72 return func() { errtrackerReport = prev } 73 } 74 75 func MockPrerequisitesRetryTimeout(d time.Duration) (restore func()) { 76 old := prerequisitesRetryTimeout 77 prerequisitesRetryTimeout = d 78 return func() { prerequisitesRetryTimeout = old } 79 } 80 81 func MockOsutilEnsureUserGroup(mock func(name string, id uint32, extraUsers bool) error) (restore func()) { 82 old := osutilEnsureUserGroup 83 osutilEnsureUserGroup = mock 84 return func() { osutilEnsureUserGroup = old } 85 } 86 87 var ( 88 CoreInfoInternal = coreInfo 89 CheckSnap = checkSnap 90 CanRemove = canRemove 91 CanDisable = canDisable 92 CachedStore = cachedStore 93 DefaultRefreshSchedule = defaultRefreshSchedule 94 DoInstall = doInstall 95 UserFromUserID = userFromUserID 96 ValidateFeatureFlags = validateFeatureFlags 97 ResolveChannel = resolveChannel 98 99 CurrentSnaps = currentSnaps 100 101 DefaultContentPlugProviders = defaultContentPlugProviders 102 103 HasOtherInstances = hasOtherInstances 104 105 SafetyMarginDiskSpace = safetyMarginDiskSpace 106 107 AffectedByRefresh = affectedByRefresh 108 ) 109 110 func PreviousSideInfo(snapst *SnapState) *snap.SideInfo { 111 return snapst.previousSideInfo() 112 } 113 114 // helpers 115 var InstallSize = installSize 116 117 // aliases v2 118 var ( 119 ApplyAliasesChange = applyAliasesChange 120 AutoAliasesDelta = autoAliasesDelta 121 RefreshAliases = refreshAliases 122 CheckAliasesConflicts = checkAliasesConflicts 123 DisableAliases = disableAliases 124 SwitchSummary = switchSummary 125 ) 126 127 // dbus 128 var ( 129 CheckDBusServiceConflicts = checkDBusServiceConflicts 130 ) 131 132 // readme files 133 var ( 134 WriteSnapReadme = writeSnapReadme 135 SnapReadme = snapReadme 136 ) 137 138 // refreshes 139 var ( 140 NewAutoRefresh = newAutoRefresh 141 NewRefreshHints = newRefreshHints 142 CanRefreshOnMeteredConnection = canRefreshOnMeteredConnection 143 144 NewCatalogRefresh = newCatalogRefresh 145 CatalogRefreshDelayBase = catalogRefreshDelayBase 146 CatalogRefreshDelayWithDelta = catalogRefreshDelayWithDelta 147 148 SoftCheckNothingRunningForRefresh = softCheckNothingRunningForRefresh 149 HardEnsureNothingRunningDuringRefresh = hardEnsureNothingRunningDuringRefresh 150 ) 151 152 func MockNextRefresh(ar *autoRefresh, when time.Time) { 153 ar.nextRefresh = when 154 } 155 156 func MockLastRefreshSchedule(ar *autoRefresh, schedule string) { 157 ar.lastRefreshSchedule = schedule 158 } 159 160 func MockCatalogRefreshNextRefresh(cr *catalogRefresh, when time.Time) { 161 cr.nextCatalogRefresh = when 162 } 163 164 func NextCatalogRefresh(cr *catalogRefresh) time.Time { 165 return cr.nextCatalogRefresh 166 } 167 168 func MockRefreshRetryDelay(d time.Duration) func() { 169 origRefreshRetryDelay := refreshRetryDelay 170 refreshRetryDelay = d 171 return func() { 172 refreshRetryDelay = origRefreshRetryDelay 173 } 174 } 175 176 func MockIsOnMeteredConnection(mock func() (bool, error)) func() { 177 old := IsOnMeteredConnection 178 IsOnMeteredConnection = mock 179 return func() { 180 IsOnMeteredConnection = old 181 } 182 } 183 184 func MockLocalInstallCleanupWait(d time.Duration) (restore func()) { 185 old := localInstallCleanupWait 186 localInstallCleanupWait = d 187 return func() { 188 localInstallCleanupWait = old 189 } 190 } 191 192 func MockLocalInstallLastCleanup(t time.Time) (restore func()) { 193 old := localInstallLastCleanup 194 localInstallLastCleanup = t 195 return func() { 196 localInstallLastCleanup = old 197 } 198 } 199 200 func MockAsyncPendingRefreshNotification(fn func(context.Context, *userclient.Client, *userclient.PendingSnapRefreshInfo)) (restore func()) { 201 old := asyncPendingRefreshNotification 202 asyncPendingRefreshNotification = fn 203 return func() { 204 asyncPendingRefreshNotification = old 205 } 206 } 207 208 // re-refresh related 209 var ( 210 RefreshedSnaps = refreshedSnaps 211 ReRefreshFilter = reRefreshFilter 212 ) 213 214 type UpdateFilter = updateFilter 215 216 func MockReRefreshUpdateMany(f func(context.Context, *state.State, []string, int, UpdateFilter, *Flags, string) ([]string, []*state.TaskSet, error)) (restore func()) { 217 old := reRefreshUpdateMany 218 reRefreshUpdateMany = f 219 return func() { 220 reRefreshUpdateMany = old 221 } 222 } 223 224 func MockReRefreshRetryTimeout(d time.Duration) (restore func()) { 225 old := reRefreshRetryTimeout 226 reRefreshRetryTimeout = d 227 return func() { 228 reRefreshRetryTimeout = old 229 } 230 } 231 232 // aux store info 233 var ( 234 AuxStoreInfoFilename = auxStoreInfoFilename 235 RetrieveAuxStoreInfo = retrieveAuxStoreInfo 236 KeepAuxStoreInfo = keepAuxStoreInfo 237 DiscardAuxStoreInfo = discardAuxStoreInfo 238 ) 239 240 type AuxStoreInfo = auxStoreInfo 241 242 // link, misc handlers 243 var ( 244 MissingDisabledServices = missingDisabledServices 245 ) 246 247 func (m *SnapManager) MaybeUndoRemodelBootChanges(t *state.Task) error { 248 return m.maybeUndoRemodelBootChanges(t) 249 } 250 251 func MockPidsOfSnap(f func(instanceName string) (map[string][]int, error)) func() { 252 old := pidsOfSnap 253 pidsOfSnap = f 254 return func() { 255 pidsOfSnap = old 256 } 257 } 258 259 func MockCurrentSnaps(f func(st *state.State) ([]*store.CurrentSnap, error)) func() { 260 old := currentSnaps 261 currentSnaps = f 262 return func() { 263 currentSnaps = old 264 } 265 } 266 267 func MockInstallSize(f func(st *state.State, snaps []minimalInstallInfo, userID int) (uint64, error)) func() { 268 old := installSize 269 installSize = f 270 return func() { 271 installSize = old 272 } 273 } 274 275 func MockGenerateSnapdWrappers(f func(snapInfo *snap.Info) error) func() { 276 old := generateSnapdWrappers 277 generateSnapdWrappers = f 278 return func() { 279 generateSnapdWrappers = old 280 } 281 } 282 283 var ( 284 NotifyLinkParticipants = notifyLinkParticipants 285 ) 286 287 // autorefresh 288 var ( 289 InhibitRefresh = inhibitRefresh 290 MaxInhibition = maxInhibition 291 ) 292 293 type RefreshCandidate = refreshCandidate 294 295 func NewBusySnapError(info *snap.Info, pids []int, busyAppNames, busyHookNames []string) *BusySnapError { 296 return &BusySnapError{ 297 SnapInfo: info, 298 pids: pids, 299 busyAppNames: busyAppNames, 300 busyHookNames: busyHookNames, 301 } 302 } 303 304 func MockGenericRefreshCheck(fn func(info *snap.Info, canAppRunDuringRefresh func(app *snap.AppInfo) bool) error) (restore func()) { 305 old := genericRefreshCheck 306 genericRefreshCheck = fn 307 return func() { genericRefreshCheck = old } 308 } 309 310 func (m *autoRefresh) EnsureRefreshHoldAtLeast(d time.Duration) error { 311 return m.ensureRefreshHoldAtLeast(d) 312 } 313 314 func MockSecurityProfilesDiscardLate(fn func(snapName string, rev snap.Revision, typ snap.Type) error) (restore func()) { 315 old := SecurityProfilesRemoveLate 316 SecurityProfilesRemoveLate = fn 317 return func() { 318 SecurityProfilesRemoveLate = old 319 } 320 } 321 322 type HoldState = holdState 323 324 var ( 325 HoldDurationLeft = holdDurationLeft 326 LastRefreshed = lastRefreshed 327 HeldSnaps = heldSnaps 328 PruneRefreshCandidates = pruneRefreshCandidates 329 ResetGatingForRefreshed = resetGatingForRefreshed 330 PruneGating = pruneGating 331 PruneSnapsHold = pruneSnapsHold 332 CreateGateAutoRefreshHooks = createGateAutoRefreshHooks 333 AutoRefreshPhase1 = autoRefreshPhase1 334 ) 335 336 func MockTimeNow(f func() time.Time) (restore func()) { 337 old := timeNow 338 timeNow = f 339 return func() { 340 timeNow = old 341 } 342 } 343 344 func MockHoldState(firstHeld string, holdUntil string) *HoldState { 345 first, err := time.Parse(time.RFC3339, firstHeld) 346 if err != nil { 347 panic(err) 348 } 349 until, err := time.Parse(time.RFC3339, holdUntil) 350 if err != nil { 351 panic(err) 352 } 353 return &holdState{ 354 FirstHeld: first, 355 HoldUntil: until, 356 } 357 } 358 359 func MockSnapsToRefresh(f func(gatingTask *state.Task) ([]*refreshCandidate, error)) (restore func()) { 360 old := snapsToRefresh 361 snapsToRefresh = f 362 return func() { 363 snapsToRefresh = old 364 } 365 }