github.com/stulluk/snapd@v0.0.0-20210611110309-f6d5d5bd24b0/cmd/snap-bootstrap/export_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016-2020 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 main 21 22 import ( 23 "fmt" 24 "time" 25 26 "github.com/snapcore/snapd/asserts" 27 "github.com/snapcore/snapd/osutil/disks" 28 "github.com/snapcore/snapd/secboot" 29 ) 30 31 var ( 32 Parser = parser 33 34 DoSystemdMount = doSystemdMountImpl 35 ) 36 37 type SystemdMountOptions = systemdMountOptions 38 39 type RecoverDegradedState = recoverDegradedState 40 41 type PartitionState = partitionState 42 43 func (r *RecoverDegradedState) Degraded(isEncrypted bool) bool { 44 m := recoverModeStateMachine{ 45 isEncryptedDev: isEncrypted, 46 degradedState: r, 47 } 48 return m.degraded() 49 } 50 51 func MockTimeNow(f func() time.Time) (restore func()) { 52 old := timeNow 53 timeNow = f 54 return func() { 55 timeNow = old 56 } 57 } 58 59 func MockOsutilSetTime(f func(t time.Time) error) (restore func()) { 60 old := osutilSetTime 61 osutilSetTime = f 62 return func() { 63 osutilSetTime = old 64 } 65 } 66 67 func MockOsutilIsMounted(f func(string) (bool, error)) (restore func()) { 68 old := osutilIsMounted 69 osutilIsMounted = f 70 return func() { 71 osutilIsMounted = old 72 } 73 } 74 75 func MockSystemdMount(f func(_, _ string, opts *SystemdMountOptions) error) (restore func()) { 76 old := doSystemdMount 77 doSystemdMount = f 78 return func() { 79 doSystemdMount = old 80 } 81 } 82 83 func MockTriggerwatchWait(f func(_ time.Duration) error) (restore func()) { 84 oldTriggerwatchWait := triggerwatchWait 85 triggerwatchWait = f 86 return func() { 87 triggerwatchWait = oldTriggerwatchWait 88 } 89 } 90 91 var DefaultTimeout = defaultTimeout 92 93 func MockDefaultMarkerFile(p string) (restore func()) { 94 old := defaultMarkerFile 95 defaultMarkerFile = p 96 return func() { 97 defaultMarkerFile = old 98 } 99 } 100 101 func MockSecbootUnlockVolumeUsingSealedKeyIfEncrypted(f func(disk disks.Disk, name string, sealedEncryptionKeyFile string, opts *secboot.UnlockVolumeUsingSealedKeyOptions) (secboot.UnlockResult, error)) (restore func()) { 102 old := secbootUnlockVolumeUsingSealedKeyIfEncrypted 103 secbootUnlockVolumeUsingSealedKeyIfEncrypted = f 104 return func() { 105 secbootUnlockVolumeUsingSealedKeyIfEncrypted = old 106 } 107 } 108 109 func MockSecbootUnlockEncryptedVolumeUsingKey(f func(disk disks.Disk, name string, key []byte) (secboot.UnlockResult, error)) (restore func()) { 110 old := secbootUnlockEncryptedVolumeUsingKey 111 secbootUnlockEncryptedVolumeUsingKey = f 112 return func() { 113 secbootUnlockEncryptedVolumeUsingKey = old 114 } 115 } 116 117 func MockSecbootMeasureSnapSystemEpochWhenPossible(f func() error) (restore func()) { 118 old := secbootMeasureSnapSystemEpochWhenPossible 119 secbootMeasureSnapSystemEpochWhenPossible = f 120 return func() { 121 secbootMeasureSnapSystemEpochWhenPossible = old 122 } 123 } 124 125 func MockSecbootMeasureSnapModelWhenPossible(f func(findModel func() (*asserts.Model, error)) error) (restore func()) { 126 old := secbootMeasureSnapModelWhenPossible 127 secbootMeasureSnapModelWhenPossible = f 128 return func() { 129 secbootMeasureSnapModelWhenPossible = old 130 } 131 } 132 133 func MockSecbootLockSealedKeys(f func() error) (restore func()) { 134 old := secbootLockSealedKeys 135 secbootLockSealedKeys = f 136 return func() { 137 secbootLockSealedKeys = old 138 } 139 } 140 141 func MockPartitionUUIDForBootedKernelDisk(uuid string) (restore func()) { 142 old := bootFindPartitionUUIDForBootedKernelDisk 143 bootFindPartitionUUIDForBootedKernelDisk = func() (string, error) { 144 if uuid == "" { 145 // mock error 146 return "", fmt.Errorf("mocked error") 147 } 148 return uuid, nil 149 } 150 151 return func() { 152 bootFindPartitionUUIDForBootedKernelDisk = old 153 } 154 } 155 156 func MockTryRecoverySystemHealthCheck(mock func() error) (restore func()) { 157 old := tryRecoverySystemHealthCheck 158 tryRecoverySystemHealthCheck = mock 159 return func() { 160 tryRecoverySystemHealthCheck = old 161 } 162 }