1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016-2024 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"fmt"
"time"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/boot"
"github.com/snapcore/snapd/gadget"
gadgetInstall "github.com/snapcore/snapd/gadget/install"
"github.com/snapcore/snapd/osutil/disks"
"github.com/snapcore/snapd/secboot"
"github.com/snapcore/snapd/seed"
"github.com/snapcore/snapd/testutil"
"github.com/snapcore/snapd/timings"
)
var (
Parser = parser
DoSystemdMount = doSystemdMountImpl
MountNonDataPartitionMatchingKernelDisk = mountNonDataPartitionMatchingKernelDisk
GetNonUEFISystemDisk = getNonUEFISystemDisk
GenerateMountsFromManifest = generateMountsFromManifest
ParseImageManifest = parseImageManifest
CreateOverlayDirs = createOverlayDirs
)
type OverlayFsOptions = overlayFsOptions
type DmVerityOptions = dmVerityOptions
type SystemdMountOptions = systemdMountOptions
type DiskUnlockState = diskUnlockState
type PartitionState = partitionState
func (r *DiskUnlockState) Degraded(isEncrypted bool) bool {
m := recoverModeStateMachine{
isEncryptedDev: isEncrypted,
degradedState: r,
}
return m.degraded()
}
func MockPollWaitForLabel(newPollDur time.Duration) (restore func()) {
restore = testutil.Backup(&pollWaitForLabel)
pollWaitForLabel = newPollDur
return restore
}
func MockPollWaitForLabelIters(newNumIters int) (restore func()) {
restore = testutil.Backup(&pollWaitForLabelIters)
pollWaitForLabelIters = newNumIters
return restore
}
func MockTimeNow(f func() time.Time) (restore func()) {
old := timeNow
timeNow = f
return func() {
timeNow = old
}
}
func MockOsutilSetTime(f func(t time.Time) error) (restore func()) {
old := osutilSetTime
osutilSetTime = f
return func() {
osutilSetTime = old
}
}
func MockOsutilIsMounted(f func(string) (bool, error)) (restore func()) {
old := osutilIsMounted
osutilIsMounted = f
return func() {
osutilIsMounted = old
}
}
func MockSystemdMount(f func(_, _ string, opts *SystemdMountOptions) error) (restore func()) {
old := doSystemdMount
doSystemdMount = f
return func() {
doSystemdMount = old
}
}
func MockTriggerwatchWait(f func(_ time.Duration, _ time.Duration) error) (restore func()) {
oldTriggerwatchWait := triggerwatchWait
triggerwatchWait = f
return func() {
triggerwatchWait = oldTriggerwatchWait
}
}
var DefaultTimeout = defaultTimeout
var DefaultDeviceTimeout = defaultDeviceTimeout
func MockDefaultMarkerFile(p string) (restore func()) {
old := defaultMarkerFile
defaultMarkerFile = p
return func() {
defaultMarkerFile = old
}
}
func MockSecbootUnlockVolumeUsingSealedKeyIfEncrypted(f func(disk disks.Disk, name string, sealedEncryptionKeyFile string, opts *secboot.UnlockVolumeUsingSealedKeyOptions) (secboot.UnlockResult, error)) (restore func()) {
old := secbootUnlockVolumeUsingSealedKeyIfEncrypted
secbootUnlockVolumeUsingSealedKeyIfEncrypted = f
return func() {
secbootUnlockVolumeUsingSealedKeyIfEncrypted = old
}
}
func MockSecbootUnlockEncryptedVolumeUsingProtectorKey(f func(disk disks.Disk, name string, key []byte) (secboot.UnlockResult, error)) (restore func()) {
old := secbootUnlockEncryptedVolumeUsingProtectorKey
secbootUnlockEncryptedVolumeUsingProtectorKey = f
return func() {
secbootUnlockEncryptedVolumeUsingProtectorKey = old
}
}
func MockSecbootProvisionForCVM(f func(_ string) error) (restore func()) {
old := secbootProvisionForCVM
secbootProvisionForCVM = f
return func() {
secbootProvisionForCVM = old
}
}
func MockSecbootMeasureSnapSystemEpochWhenPossible(f func() error) (restore func()) {
old := secbootMeasureSnapSystemEpochWhenPossible
secbootMeasureSnapSystemEpochWhenPossible = f
return func() {
secbootMeasureSnapSystemEpochWhenPossible = old
}
}
func MockSecbootMeasureSnapModelWhenPossible(f func(findModel func() (*asserts.Model, error)) error) (restore func()) {
old := secbootMeasureSnapModelWhenPossible
secbootMeasureSnapModelWhenPossible = f
return func() {
secbootMeasureSnapModelWhenPossible = old
}
}
func MockSecbootLockSealedKeys(f func() error) (restore func()) {
old := secbootLockSealedKeys
secbootLockSealedKeys = f
return func() {
secbootLockSealedKeys = old
}
}
func MockPartitionUUIDForBootedKernelDisk(uuid string) (restore func()) {
old := bootFindPartitionUUIDForBootedKernelDisk
bootFindPartitionUUIDForBootedKernelDisk = func() (string, error) {
if uuid == "" {
// mock error
return "", fmt.Errorf("mocked error")
}
return uuid, nil
}
return func() {
bootFindPartitionUUIDForBootedKernelDisk = old
}
}
func MockTryRecoverySystemHealthCheck(mock func(gadget.Model) error) (restore func()) {
old := tryRecoverySystemHealthCheck
tryRecoverySystemHealthCheck = mock
return func() {
tryRecoverySystemHealthCheck = old
}
}
func MockWaitFile(f func(string, time.Duration, int) error) (restore func()) {
old := waitFile
waitFile = f
return func() {
waitFile = old
}
}
var WaitFile = waitFile
func MockGadgetInstallRun(f func(model gadget.Model, gadgetRoot string, kernelSnapInfo *gadgetInstall.KernelSnapInfo, bootDevice string, options gadgetInstall.Options, observer gadget.ContentObserver, perfTimings timings.Measurer) (*gadgetInstall.InstalledSystemSideData, error)) (restore func()) {
old := gadgetInstallRun
gadgetInstallRun = f
return func() {
gadgetInstallRun = old
}
}
func MockMakeRunnableStandaloneSystem(f func(model *asserts.Model, bootWith *boot.BootableSet, obs boot.TrustedAssetsInstallObserver) error) (restore func()) {
old := bootMakeRunnableStandaloneSystem
bootMakeRunnableStandaloneSystem = f
return func() {
bootMakeRunnableStandaloneSystem = old
}
}
func MockApplyPreseededData(f func(preseedSeed seed.PreseedCapable, writableDir string) error) (restore func()) {
old := installApplyPreseededData
installApplyPreseededData = f
return func() {
installApplyPreseededData = old
}
}
func MockEnsureNextBootToRunMode(f func(systemLabel string) error) (restore func()) {
old := bootEnsureNextBootToRunMode
bootEnsureNextBootToRunMode = f
return func() {
bootEnsureNextBootToRunMode = old
}
}
func MockBuildInstallObserver(f func(model *asserts.Model, gadgetDir string, useEncryption bool) (observer gadget.ContentObserver, trustedObserver boot.TrustedAssetsInstallObserver, err error)) (restore func()) {
old := installBuildInstallObserver
installBuildInstallObserver = f
return func() {
installBuildInstallObserver = old
}
}
func MockOsGetenv(mock func(string) string) (restore func()) {
old := osGetenv
osGetenv = mock
return func() {
osGetenv = old
}
}
|