github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/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 PreloadGadget(m *DeviceManager) (*gadget.Info, error) { 124 return m.preloadGadget() 125 } 126 127 func MockRepeatRequestSerial(label string) (restore func()) { 128 old := repeatRequestSerial 129 repeatRequestSerial = label 130 return func() { 131 repeatRequestSerial = old 132 } 133 } 134 135 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()) { 136 old := snapstateInstallWithDeviceContext 137 snapstateInstallWithDeviceContext = f 138 return func() { 139 snapstateInstallWithDeviceContext = old 140 } 141 } 142 143 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()) { 144 old := snapstateUpdateWithDeviceContext 145 snapstateUpdateWithDeviceContext = f 146 return func() { 147 snapstateUpdateWithDeviceContext = old 148 } 149 } 150 151 func EnsureSeeded(m *DeviceManager) error { 152 return m.ensureSeeded() 153 } 154 155 func EnsureCloudInitRestricted(m *DeviceManager) error { 156 return m.ensureCloudInitRestricted() 157 } 158 159 var PopulateStateFromSeedImpl = populateStateFromSeedImpl 160 161 type PopulateStateFromSeedOptions = populateStateFromSeedOptions 162 163 func MockPopulateStateFromSeed(f func(*state.State, *PopulateStateFromSeedOptions, timings.Measurer) ([]*state.TaskSet, error)) (restore func()) { 164 old := populateStateFromSeed 165 populateStateFromSeed = f 166 return func() { 167 populateStateFromSeed = old 168 } 169 } 170 171 func EnsureBootOk(m *DeviceManager) error { 172 return m.ensureBootOk() 173 } 174 175 func SetBootOkRan(m *DeviceManager, b bool) { 176 m.bootOkRan = b 177 } 178 179 func StartTime() time.Time { 180 return startTime 181 } 182 183 type ( 184 RegistrationContext = registrationContext 185 RemodelContext = remodelContext 186 SeededSystem = seededSystem 187 ) 188 189 func RegistrationCtx(m *DeviceManager, t *state.Task) (registrationContext, error) { 190 return m.registrationCtx(t) 191 } 192 193 func RemodelDeviceBackend(remodCtx remodelContext) storecontext.DeviceBackend { 194 return remodCtx.(interface { 195 deviceBackend() storecontext.DeviceBackend 196 }).deviceBackend() 197 } 198 199 var ( 200 LoadDeviceSeed = loadDeviceSeed 201 UnloadDeviceSeed = unloadDeviceSeed 202 ImportAssertionsFromSeed = importAssertionsFromSeed 203 CheckGadgetOrKernel = checkGadgetOrKernel 204 CheckGadgetValid = checkGadgetValid 205 CheckGadgetRemodelCompatible = checkGadgetRemodelCompatible 206 CanAutoRefresh = canAutoRefresh 207 NewEnoughProxy = newEnoughProxy 208 209 IncEnsureOperationalAttempts = incEnsureOperationalAttempts 210 EnsureOperationalAttempts = ensureOperationalAttempts 211 212 RemodelTasks = remodelTasks 213 214 RemodelCtx = remodelCtx 215 CleanupRemodelCtx = cleanupRemodelCtx 216 CachedRemodelCtx = cachedRemodelCtx 217 218 GadgetUpdateBlocked = gadgetUpdateBlocked 219 CurrentGadgetInfo = currentGadgetInfo 220 PendingGadgetInfo = pendingGadgetInfo 221 222 CriticalTaskEdges = criticalTaskEdges 223 ) 224 225 func MockGadgetUpdate(mock func(current, update gadget.GadgetData, path string, policy gadget.UpdatePolicyFunc, observer gadget.ContentUpdateObserver) error) (restore func()) { 226 old := gadgetUpdate 227 gadgetUpdate = mock 228 return func() { 229 gadgetUpdate = old 230 } 231 } 232 233 func MockGadgetIsCompatible(mock func(current, update *gadget.Info) error) (restore func()) { 234 old := gadgetIsCompatible 235 gadgetIsCompatible = mock 236 return func() { 237 gadgetIsCompatible = old 238 } 239 } 240 241 func MockBootMakeBootable(f func(model *asserts.Model, rootdir string, bootWith *boot.BootableSet, seal *boot.TrustedAssetsInstallObserver) error) (restore func()) { 242 old := bootMakeBootable 243 bootMakeBootable = f 244 return func() { 245 bootMakeBootable = old 246 } 247 } 248 249 func MockSecbootCheckKeySealingSupported(f func() error) (restore func()) { 250 old := secbootCheckKeySealingSupported 251 secbootCheckKeySealingSupported = f 252 return func() { 253 secbootCheckKeySealingSupported = old 254 } 255 } 256 257 func MockHttputilNewHTTPClient(f func(opts *httputil.ClientOptions) *http.Client) (restore func()) { 258 old := httputilNewHTTPClient 259 httputilNewHTTPClient = f 260 return func() { 261 httputilNewHTTPClient = old 262 } 263 } 264 265 func MockSysconfigConfigureTargetSystem(f func(opts *sysconfig.Options) error) (restore func()) { 266 old := sysconfigConfigureTargetSystem 267 sysconfigConfigureTargetSystem = f 268 return func() { 269 sysconfigConfigureTargetSystem = old 270 } 271 } 272 273 func MockInstallRun(f func(model gadget.Model, gadgetRoot, kernelRoot, device string, options install.Options, observer gadget.ContentObserver) (*install.InstalledSystemSideData, error)) (restore func()) { 274 old := installRun 275 installRun = f 276 return func() { 277 installRun = old 278 } 279 } 280 281 func MockCloudInitStatus(f func() (sysconfig.CloudInitState, error)) (restore func()) { 282 old := cloudInitStatus 283 cloudInitStatus = f 284 return func() { 285 cloudInitStatus = old 286 } 287 } 288 289 func MockRestrictCloudInit(f func(sysconfig.CloudInitState, *sysconfig.CloudInitRestrictOptions) (sysconfig.CloudInitRestrictionResult, error)) (restore func()) { 290 old := restrictCloudInit 291 restrictCloudInit = f 292 return func() { 293 restrictCloudInit = old 294 } 295 } 296 297 func DeviceManagerHasFDESetupHook(mgr *DeviceManager) (bool, error) { 298 return mgr.hasFDESetupHook() 299 } 300 301 func DeviceManagerRunFDESetupHook(mgr *DeviceManager, op string, params *boot.FDESetupHookParams) ([]byte, error) { 302 return mgr.runFDESetupHook(op, params) 303 } 304 305 func DeviceManagerCheckEncryption(mgr *DeviceManager, st *state.State, deviceCtx snapstate.DeviceContext) (bool, error) { 306 return mgr.checkEncryption(st, deviceCtx) 307 } 308 309 func DeviceManagerCheckFDEFeatures(mgr *DeviceManager, st *state.State) error { 310 return mgr.checkFDEFeatures(st) 311 }