gitee.com/mysnapcore/mysnapd@v0.1.0/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 "gitee.com/mysnapcore/mysnapd/asserts" 27 "gitee.com/mysnapcore/mysnapd/gadget" 28 "gitee.com/mysnapcore/mysnapd/osutil/disks" 29 "gitee.com/mysnapcore/mysnapd/secboot" 30 ) 31 32 var ( 33 Parser = parser 34 35 DoSystemdMount = doSystemdMountImpl 36 37 MountNonDataPartitionMatchingKernelDisk = mountNonDataPartitionMatchingKernelDisk 38 ) 39 40 type SystemdMountOptions = systemdMountOptions 41 42 type RecoverDegradedState = recoverDegradedState 43 44 type PartitionState = partitionState 45 46 func (r *RecoverDegradedState) Degraded(isEncrypted bool) bool { 47 m := recoverModeStateMachine{ 48 isEncryptedDev: isEncrypted, 49 degradedState: r, 50 } 51 return m.degraded() 52 } 53 54 func MockTimeNow(f func() time.Time) (restore func()) { 55 old := timeNow 56 timeNow = f 57 return func() { 58 timeNow = old 59 } 60 } 61 62 func MockOsutilSetTime(f func(t time.Time) error) (restore func()) { 63 old := osutilSetTime 64 osutilSetTime = f 65 return func() { 66 osutilSetTime = old 67 } 68 } 69 70 func MockOsutilIsMounted(f func(string) (bool, error)) (restore func()) { 71 old := osutilIsMounted 72 osutilIsMounted = f 73 return func() { 74 osutilIsMounted = old 75 } 76 } 77 78 func MockSystemdMount(f func(_, _ string, opts *SystemdMountOptions) error) (restore func()) { 79 old := doSystemdMount 80 doSystemdMount = f 81 return func() { 82 doSystemdMount = old 83 } 84 } 85 86 func MockTriggerwatchWait(f func(_ time.Duration, _ time.Duration) error) (restore func()) { 87 oldTriggerwatchWait := triggerwatchWait 88 triggerwatchWait = f 89 return func() { 90 triggerwatchWait = oldTriggerwatchWait 91 } 92 } 93 94 var DefaultTimeout = defaultTimeout 95 var DefaultDeviceTimeout = defaultDeviceTimeout 96 97 func MockDefaultMarkerFile(p string) (restore func()) { 98 old := defaultMarkerFile 99 defaultMarkerFile = p 100 return func() { 101 defaultMarkerFile = old 102 } 103 } 104 105 func MockSecbootUnlockVolumeUsingSealedKeyIfEncrypted(f func(disk disks.Disk, name string, sealedEncryptionKeyFile string, opts *secboot.UnlockVolumeUsingSealedKeyOptions) (secboot.UnlockResult, error)) (restore func()) { 106 old := secbootUnlockVolumeUsingSealedKeyIfEncrypted 107 secbootUnlockVolumeUsingSealedKeyIfEncrypted = f 108 return func() { 109 secbootUnlockVolumeUsingSealedKeyIfEncrypted = old 110 } 111 } 112 113 func MockSecbootUnlockEncryptedVolumeUsingKey(f func(disk disks.Disk, name string, key []byte) (secboot.UnlockResult, error)) (restore func()) { 114 old := secbootUnlockEncryptedVolumeUsingKey 115 secbootUnlockEncryptedVolumeUsingKey = f 116 return func() { 117 secbootUnlockEncryptedVolumeUsingKey = old 118 } 119 } 120 121 func MockSecbootProvisionForCVM(f func(_ string) error) (restore func()) { 122 old := secbootProvisionForCVM 123 secbootProvisionForCVM = f 124 return func() { 125 secbootProvisionForCVM = old 126 } 127 } 128 129 func MockSecbootMeasureSnapSystemEpochWhenPossible(f func() error) (restore func()) { 130 old := secbootMeasureSnapSystemEpochWhenPossible 131 secbootMeasureSnapSystemEpochWhenPossible = f 132 return func() { 133 secbootMeasureSnapSystemEpochWhenPossible = old 134 } 135 } 136 137 func MockSecbootMeasureSnapModelWhenPossible(f func(findModel func() (*asserts.Model, error)) error) (restore func()) { 138 old := secbootMeasureSnapModelWhenPossible 139 secbootMeasureSnapModelWhenPossible = f 140 return func() { 141 secbootMeasureSnapModelWhenPossible = old 142 } 143 } 144 145 func MockSecbootLockSealedKeys(f func() error) (restore func()) { 146 old := secbootLockSealedKeys 147 secbootLockSealedKeys = f 148 return func() { 149 secbootLockSealedKeys = old 150 } 151 } 152 153 func MockPartitionUUIDForBootedKernelDisk(uuid string) (restore func()) { 154 old := bootFindPartitionUUIDForBootedKernelDisk 155 bootFindPartitionUUIDForBootedKernelDisk = func() (string, error) { 156 if uuid == "" { 157 // mock error 158 return "", fmt.Errorf("mocked error") 159 } 160 return uuid, nil 161 } 162 163 return func() { 164 bootFindPartitionUUIDForBootedKernelDisk = old 165 } 166 } 167 168 func MockTryRecoverySystemHealthCheck(mock func(gadget.Model) error) (restore func()) { 169 old := tryRecoverySystemHealthCheck 170 tryRecoverySystemHealthCheck = mock 171 return func() { 172 tryRecoverySystemHealthCheck = old 173 } 174 } 175 176 func MockWaitFile(f func(string, time.Duration, int) error) (restore func()) { 177 old := waitFile 178 waitFile = f 179 return func() { 180 waitFile = old 181 } 182 } 183 184 var WaitFile = waitFile