github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/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 MockOsutilIsMounted(f func(string) (bool, error)) (restore func()) { 60 old := osutilIsMounted 61 osutilIsMounted = f 62 return func() { 63 osutilIsMounted = old 64 } 65 } 66 67 func MockSystemdMount(f func(_, _ string, opts *SystemdMountOptions) error) (restore func()) { 68 old := doSystemdMount 69 doSystemdMount = f 70 return func() { 71 doSystemdMount = old 72 } 73 } 74 75 func MockTriggerwatchWait(f func(_ time.Duration) error) (restore func()) { 76 oldTriggerwatchWait := triggerwatchWait 77 triggerwatchWait = f 78 return func() { 79 triggerwatchWait = oldTriggerwatchWait 80 } 81 } 82 83 var DefaultTimeout = defaultTimeout 84 85 func MockDefaultMarkerFile(p string) (restore func()) { 86 old := defaultMarkerFile 87 defaultMarkerFile = p 88 return func() { 89 defaultMarkerFile = old 90 } 91 } 92 93 func MockSecbootUnlockVolumeUsingSealedKeyIfEncrypted(f func(disk disks.Disk, name string, sealedEncryptionKeyFile string, opts *secboot.UnlockVolumeUsingSealedKeyOptions) (secboot.UnlockResult, error)) (restore func()) { 94 old := secbootUnlockVolumeUsingSealedKeyIfEncrypted 95 secbootUnlockVolumeUsingSealedKeyIfEncrypted = f 96 return func() { 97 secbootUnlockVolumeUsingSealedKeyIfEncrypted = old 98 } 99 } 100 101 func MockSecbootUnlockEncryptedVolumeUsingKey(f func(disk disks.Disk, name string, key []byte) (secboot.UnlockResult, error)) (restore func()) { 102 old := secbootUnlockEncryptedVolumeUsingKey 103 secbootUnlockEncryptedVolumeUsingKey = f 104 return func() { 105 secbootUnlockEncryptedVolumeUsingKey = old 106 } 107 } 108 109 func MockSecbootMeasureSnapSystemEpochWhenPossible(f func() error) (restore func()) { 110 old := secbootMeasureSnapSystemEpochWhenPossible 111 secbootMeasureSnapSystemEpochWhenPossible = f 112 return func() { 113 secbootMeasureSnapSystemEpochWhenPossible = old 114 } 115 } 116 117 func MockSecbootMeasureSnapModelWhenPossible(f func(findModel func() (*asserts.Model, error)) error) (restore func()) { 118 old := secbootMeasureSnapModelWhenPossible 119 secbootMeasureSnapModelWhenPossible = f 120 return func() { 121 secbootMeasureSnapModelWhenPossible = old 122 } 123 } 124 125 func MockSecbootLockSealedKeys(f func() error) (restore func()) { 126 old := secbootLockSealedKeys 127 secbootLockSealedKeys = f 128 return func() { 129 secbootLockSealedKeys = old 130 } 131 } 132 133 func MockPartitionUUIDForBootedKernelDisk(uuid string) (restore func()) { 134 old := bootFindPartitionUUIDForBootedKernelDisk 135 bootFindPartitionUUIDForBootedKernelDisk = func() (string, error) { 136 if uuid == "" { 137 // mock error 138 return "", fmt.Errorf("mocked error") 139 } 140 return uuid, nil 141 } 142 143 return func() { 144 bootFindPartitionUUIDForBootedKernelDisk = old 145 } 146 }