github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/overlord/snapshotstate/backend/export_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 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 backend 21 22 import ( 23 "os" 24 "os/user" 25 "time" 26 27 "github.com/snapcore/snapd/client" 28 "github.com/snapcore/snapd/osutil/sys" 29 "github.com/snapcore/snapd/snap" 30 ) 31 32 var ( 33 AddDirToZip = addDirToZip 34 TarAsUser = tarAsUser 35 PickUserWrapper = pickUserWrapper 36 37 IsSnapshotFilename = isSnapshotFilename 38 39 NewMultiError = newMultiError 40 ) 41 42 func MockIsTesting(newIsTesting bool) func() { 43 oldIsTesting := isTesting 44 isTesting = newIsTesting 45 return func() { 46 isTesting = oldIsTesting 47 } 48 } 49 50 func MockUserLookupId(newLookupId func(string) (*user.User, error)) func() { 51 oldLookupId := userLookupId 52 userLookupId = newLookupId 53 return func() { 54 userLookupId = oldLookupId 55 } 56 } 57 58 func MockOsOpen(newOsOpen func(string) (*os.File, error)) func() { 59 oldOsOpen := osOpen 60 osOpen = newOsOpen 61 return func() { 62 osOpen = oldOsOpen 63 } 64 } 65 66 func MockDirNames(newDirNames func(*os.File, int) ([]string, error)) func() { 67 oldDirNames := dirNames 68 dirNames = newDirNames 69 return func() { 70 dirNames = oldDirNames 71 } 72 } 73 74 func MockOpen(newOpen func(string, uint64) (*Reader, error)) func() { 75 oldOpen := backendOpen 76 backendOpen = newOpen 77 return func() { 78 backendOpen = oldOpen 79 } 80 } 81 82 func MockSysGeteuid(newGeteuid func() sys.UserID) (restore func()) { 83 oldGeteuid := sysGeteuid 84 sysGeteuid = newGeteuid 85 return func() { 86 sysGeteuid = oldGeteuid 87 } 88 } 89 90 func MockExecLookPath(newLookPath func(string) (string, error)) (restore func()) { 91 oldLookPath := execLookPath 92 execLookPath = newLookPath 93 return func() { 94 execLookPath = oldLookPath 95 } 96 } 97 98 func SetUserWrapper(newUserWrapper string) (restore func()) { 99 oldUserWrapper := userWrapper 100 userWrapper = newUserWrapper 101 return func() { 102 userWrapper = oldUserWrapper 103 } 104 } 105 106 func MockUsersForUsernames(f func(usernames []string) ([]*user.User, error)) (restore func()) { 107 old := usersForUsernames 108 usersForUsernames = f 109 return func() { 110 usersForUsernames = old 111 } 112 } 113 114 func MockTimeNow(f func() time.Time) (restore func()) { 115 oldTimeNow := timeNow 116 timeNow = f 117 return func() { 118 timeNow = oldTimeNow 119 } 120 } 121 122 func MockSnapshot(setID uint64, snapName string, revision snap.Revision, size int64, shaSums map[string]string) *client.Snapshot { 123 return &client.Snapshot{ 124 SetID: setID, 125 Snap: snapName, 126 SnapID: "id", 127 Revision: revision, 128 Version: "1.0", 129 Epoch: snap.Epoch{}, 130 Time: timeNow(), 131 SHA3_384: shaSums, 132 Size: size, 133 } 134 } 135 136 func MockFilepathGlob(new func(pattern string) (matches []string, err error)) (restore func()) { 137 oldFilepathGlob := filepathGlob 138 filepathGlob = new 139 return func() { 140 filepathGlob = oldFilepathGlob 141 } 142 } 143 144 func (se *SnapshotExport) ContentHash() []byte { 145 return se.contentHash 146 }