github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/overlord/devicestate/export_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016-2018 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 devicestate 21 22 import ( 23 "context" 24 "net/http" 25 "time" 26 27 "github.com/snapcore/snapd/asserts" 28 "github.com/snapcore/snapd/boot" 29 "github.com/snapcore/snapd/gadget" 30 "github.com/snapcore/snapd/gadget/install" 31 "github.com/snapcore/snapd/httputil" 32 "github.com/snapcore/snapd/overlord/snapstate" 33 "github.com/snapcore/snapd/overlord/state" 34 "github.com/snapcore/snapd/overlord/storecontext" 35 "github.com/snapcore/snapd/sysconfig" 36 "github.com/snapcore/snapd/timings" 37 ) 38 39 func MockKeyLength(n int) (restore func()) { 40 if n < 1024 { 41 panic("key length must be >= 1024") 42 } 43 44 oldKeyLength := keyLength 45 keyLength = n 46 return func() { 47 keyLength = oldKeyLength 48 } 49 } 50 51 func MockBaseStoreURL(url string) (restore func()) { 52 oldURL := baseStoreURL 53 baseStoreURL = mustParse(url).ResolveReference(authRef) 54 return func() { 55 baseStoreURL = oldURL 56 } 57 } 58 59 func MockRetryInterval(interval time.Duration) (restore func()) { 60 old := retryInterval 61 retryInterval = interval 62 return func() { 63 retryInterval = old 64 } 65 } 66 67 func MockMaxTentatives(max int) (restore func()) { 68 old := maxTentatives 69 maxTentatives = max 70 return func() { 71 maxTentatives = old 72 } 73 } 74 75 func MockTimeNow(f func() time.Time) (restore func()) { 76 old := timeNow 77 timeNow = f 78 return func() { 79 timeNow = old 80 } 81 } 82 83 func KeypairManager(m *DeviceManager) (keypairMgr asserts.KeypairManager) { 84 // XXX expose the with... method at some point 85 err := m.withKeypairMgr(func(km asserts.KeypairManager) error { 86 keypairMgr = km 87 return nil 88 }) 89 if err != nil { 90 panic(err) 91 } 92 return keypairMgr 93 } 94 95 func SaveAvailable(m *DeviceManager) bool { 96 return m.saveAvailable 97 } 98 99 func SetSaveAvailable(m *DeviceManager, avail bool) { 100 m.saveAvailable = avail 101 } 102 103 func EnsureOperationalShouldBackoff(m *DeviceManager, now time.Time) bool { 104 return m.ensureOperationalShouldBackoff(now) 105 } 106 107 func BecomeOperationalBackoff(m *DeviceManager) time.Duration { 108 return m.becomeOperationalBackoff 109 } 110 111 func SetLastBecomeOperationalAttempt(m *DeviceManager, t time.Time) { 112 m.lastBecomeOperationalAttempt = t 113 } 114 115 func SetSystemMode(m *DeviceManager, mode string) { 116 m.systemMode = mode 117 } 118 119 func SetTimeOnce(m *DeviceManager, name string, t time.Time) error { 120 return m.setTimeOnce(name, t) 121 } 122 123 func MockRepeatRequestSerial(label string) (restore func()) { 124 old := repeatRequestSerial 125 repeatRequestSerial = label 126 return func() { 127 repeatRequestSerial = old 128 } 129 } 130 131 func MockSnapstateInstallWithDeviceContext(f func(ctx context.Context, st *state.State, name string, opts *snapstate.RevisionOptions, userID int, flags snapstate.Flags, deviceCtx snapstate.DeviceContext, fromChange string) (*state.TaskSet, error)) (restore func()) { 132 old := snapstateInstallWithDeviceContext 133 snapstateInstallWithDeviceContext = f 134 return func() { 135 snapstateInstallWithDeviceContext = old 136 } 137 } 138 139 func MockSnapstateUpdateWithDeviceContext(f func(st *state.State, name string, opts *snapstate.RevisionOptions, userID int, flags snapstate.Flags, deviceCtx snapstate.DeviceContext, fromChange string) (*state.TaskSet, error)) (restore func()) { 140 old := snapstateUpdateWithDeviceContext 141 snapstateUpdateWithDeviceContext = f 142 return func() { 143 snapstateUpdateWithDeviceContext = old 144 } 145 } 146 147 func EnsureSeeded(m *DeviceManager) error { 148 return m.ensureSeeded() 149 } 150 151 func EnsureCloudInitRestricted(m *DeviceManager) error { 152 return m.ensureCloudInitRestricted() 153 } 154 155 var PopulateStateFromSeedImpl = populateStateFromSeedImpl 156 157 type PopulateStateFromSeedOptions = populateStateFromSeedOptions 158 159 func MockPopulateStateFromSeed(f func(*state.State, *PopulateStateFromSeedOptions, timings.Measurer) ([]*state.TaskSet, error)) (restore func()) { 160 old := populateStateFromSeed 161 populateStateFromSeed = f 162 return func() { 163 populateStateFromSeed = old 164 } 165 } 166 167 func EnsureBootOk(m *DeviceManager) error { 168 return m.ensureBootOk() 169 } 170 171 func SetBootOkRan(m *DeviceManager, b bool) { 172 m.bootOkRan = b 173 } 174 175 func StartTime() time.Time { 176 return startTime 177 } 178 179 type ( 180 RegistrationContext = registrationContext 181 RemodelContext = remodelContext 182 SeededSystem = seededSystem 183 ) 184 185 func RegistrationCtx(m *DeviceManager, t *state.Task) (registrationContext, error) { 186 return m.registrationCtx(t) 187 } 188 189 func RemodelDeviceBackend(remodCtx remodelContext) storecontext.DeviceBackend { 190 return remodCtx.(interface { 191 deviceBackend() storecontext.DeviceBackend 192 }).deviceBackend() 193 } 194 195 var ( 196 ImportAssertionsFromSeed = importAssertionsFromSeed 197 CheckGadgetOrKernel = checkGadgetOrKernel 198 CheckGadgetValid = checkGadgetValid 199 CheckGadgetRemodelCompatible = checkGadgetRemodelCompatible 200 CanAutoRefresh = canAutoRefresh 201 NewEnoughProxy = newEnoughProxy 202 203 IncEnsureOperationalAttempts = incEnsureOperationalAttempts 204 EnsureOperationalAttempts = ensureOperationalAttempts 205 206 RemodelTasks = remodelTasks 207 208 RemodelCtx = remodelCtx 209 CleanupRemodelCtx = cleanupRemodelCtx 210 CachedRemodelCtx = cachedRemodelCtx 211 212 GadgetUpdateBlocked = gadgetUpdateBlocked 213 CurrentGadgetInfo = currentGadgetInfo 214 PendingGadgetInfo = pendingGadgetInfo 215 216 CriticalTaskEdges = criticalTaskEdges 217 ) 218 219 func MockGadgetUpdate(mock func(current, update gadget.GadgetData, path string, policy gadget.UpdatePolicyFunc, observer gadget.ContentUpdateObserver) error) (restore func()) { 220 old := gadgetUpdate 221 gadgetUpdate = mock 222 return func() { 223 gadgetUpdate = old 224 } 225 } 226 227 func MockGadgetIsCompatible(mock func(current, update *gadget.Info) error) (restore func()) { 228 old := gadgetIsCompatible 229 gadgetIsCompatible = mock 230 return func() { 231 gadgetIsCompatible = old 232 } 233 } 234 235 func MockBootMakeBootable(f func(model *asserts.Model, rootdir string, bootWith *boot.BootableSet, seal *boot.TrustedAssetsInstallObserver) error) (restore func()) { 236 old := bootMakeBootable 237 bootMakeBootable = f 238 return func() { 239 bootMakeBootable = old 240 } 241 } 242 243 func MockSecbootCheckKeySealingSupported(f func() error) (restore func()) { 244 old := secbootCheckKeySealingSupported 245 secbootCheckKeySealingSupported = f 246 return func() { 247 secbootCheckKeySealingSupported = old 248 } 249 } 250 251 func MockHttputilNewHTTPClient(f func(opts *httputil.ClientOptions) *http.Client) (restore func()) { 252 old := httputilNewHTTPClient 253 httputilNewHTTPClient = f 254 return func() { 255 httputilNewHTTPClient = old 256 } 257 } 258 259 func MockSysconfigConfigureTargetSystem(f func(opts *sysconfig.Options) error) (restore func()) { 260 old := sysconfigConfigureTargetSystem 261 sysconfigConfigureTargetSystem = f 262 return func() { 263 sysconfigConfigureTargetSystem = old 264 } 265 } 266 267 func MockInstallRun(f func(model gadget.Model, gadgetRoot, device string, options install.Options, observer gadget.ContentObserver) (*install.InstalledSystemSideData, error)) (restore func()) { 268 old := installRun 269 installRun = f 270 return func() { 271 installRun = old 272 } 273 } 274 275 func MockCloudInitStatus(f func() (sysconfig.CloudInitState, error)) (restore func()) { 276 old := cloudInitStatus 277 cloudInitStatus = f 278 return func() { 279 cloudInitStatus = old 280 } 281 } 282 283 func MockRestrictCloudInit(f func(sysconfig.CloudInitState, *sysconfig.CloudInitRestrictOptions) (sysconfig.CloudInitRestrictionResult, error)) (restore func()) { 284 old := restrictCloudInit 285 restrictCloudInit = f 286 return func() { 287 restrictCloudInit = old 288 } 289 } 290 291 func DeviceManagerHasFDESetupHook(mgr *DeviceManager) (bool, error) { 292 return mgr.hasFDESetupHook() 293 } 294 295 func DeviceManagerRunFDESetupHook(mgr *DeviceManager, op string, params *boot.FDESetupHookParams) ([]byte, error) { 296 return mgr.runFDESetupHook(op, params) 297 } 298 299 func DeviceManagerCheckEncryption(mgr *DeviceManager, st *state.State, deviceCtx snapstate.DeviceContext) (bool, error) { 300 return mgr.checkEncryption(st, deviceCtx) 301 } 302 303 func DeviceManagerCheckFDEFeatures(mgr *DeviceManager, st *state.State) error { 304 return mgr.checkFDEFeatures(st) 305 }