github.com/rigado/snapd@v2.42.5-go-mod+incompatible/cmd/snap-update-ns/export_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2017 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 "os" 24 "syscall" 25 26 . "gopkg.in/check.v1" 27 28 "github.com/snapcore/snapd/osutil" 29 "github.com/snapcore/snapd/osutil/sys" 30 ) 31 32 var ( 33 // change 34 ValidateInstanceName = validateInstanceName 35 ProcessArguments = processArguments 36 37 // freezer 38 FreezeSnapProcesses = freezeSnapProcesses 39 ThawSnapProcesses = thawSnapProcesses 40 41 // utils 42 PlanWritableMimic = planWritableMimic 43 ExecWritableMimic = execWritableMimic 44 45 // bootstrap 46 ClearBootstrapError = clearBootstrapError 47 48 // trespassing 49 IsReadOnly = isReadOnly 50 IsPrivateTmpfsCreatedBySnapd = isPrivateTmpfsCreatedBySnapd 51 52 // system 53 DesiredSystemProfilePath = desiredSystemProfilePath 54 CurrentSystemProfilePath = currentSystemProfilePath 55 56 // user 57 DesiredUserProfilePath = desiredUserProfilePath 58 CurrentUserProfilePath = currentUserProfilePath 59 60 // xdg 61 XdgRuntimeDir = xdgRuntimeDir 62 ExpandPrefixVariable = expandPrefixVariable 63 ExpandXdgRuntimeDir = expandXdgRuntimeDir 64 65 // update 66 ExecuteMountProfileUpdate = executeMountProfileUpdate 67 ) 68 69 // SystemCalls encapsulates various system interactions performed by this module. 70 type SystemCalls interface { 71 OsLstat(name string) (os.FileInfo, error) 72 SysLstat(name string, buf *syscall.Stat_t) error 73 ReadDir(dirname string) ([]os.FileInfo, error) 74 Symlinkat(oldname string, dirfd int, newname string) error 75 Readlinkat(dirfd int, path string, buf []byte) (int, error) 76 Remove(name string) error 77 78 Close(fd int) error 79 Fchdir(fd int) error 80 Fchown(fd int, uid sys.UserID, gid sys.GroupID) error 81 Mkdirat(dirfd int, path string, mode uint32) error 82 Mount(source string, target string, fstype string, flags uintptr, data string) (err error) 83 Open(path string, flags int, mode uint32) (fd int, err error) 84 Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) 85 Unmount(target string, flags int) error 86 Fstat(fd int, buf *syscall.Stat_t) error 87 Fstatfs(fd int, buf *syscall.Statfs_t) error 88 } 89 90 // MockSystemCalls replaces real system calls with those of the argument. 91 func MockSystemCalls(sc SystemCalls) (restore func()) { 92 // save 93 oldOsLstat := osLstat 94 oldRemove := osRemove 95 oldIoutilReadDir := ioutilReadDir 96 97 oldSysClose := sysClose 98 oldSysFchown := sysFchown 99 oldSysMkdirat := sysMkdirat 100 oldSysMount := sysMount 101 oldSysOpen := sysOpen 102 oldSysOpenat := sysOpenat 103 oldSysUnmount := sysUnmount 104 oldSysSymlinkat := sysSymlinkat 105 oldReadlinkat := sysReadlinkat 106 oldFstat := sysFstat 107 oldFstatfs := sysFstatfs 108 oldSysFchdir := sysFchdir 109 oldSysLstat := sysLstat 110 111 // override 112 osLstat = sc.OsLstat 113 osRemove = sc.Remove 114 ioutilReadDir = sc.ReadDir 115 116 sysClose = sc.Close 117 sysFchown = sc.Fchown 118 sysMkdirat = sc.Mkdirat 119 sysMount = sc.Mount 120 sysOpen = sc.Open 121 sysOpenat = sc.Openat 122 sysUnmount = sc.Unmount 123 sysSymlinkat = sc.Symlinkat 124 sysReadlinkat = sc.Readlinkat 125 sysFstat = sc.Fstat 126 sysFstatfs = sc.Fstatfs 127 sysFchdir = sc.Fchdir 128 sysLstat = sc.SysLstat 129 130 return func() { 131 // restore 132 osLstat = oldOsLstat 133 osRemove = oldRemove 134 ioutilReadDir = oldIoutilReadDir 135 136 sysClose = oldSysClose 137 sysFchown = oldSysFchown 138 sysMkdirat = oldSysMkdirat 139 sysMount = oldSysMount 140 sysOpen = oldSysOpen 141 sysOpenat = oldSysOpenat 142 sysUnmount = oldSysUnmount 143 sysSymlinkat = oldSysSymlinkat 144 sysReadlinkat = oldReadlinkat 145 sysFstat = oldFstat 146 sysFstatfs = oldFstatfs 147 sysFchdir = oldSysFchdir 148 sysLstat = oldSysLstat 149 } 150 } 151 152 func MockFreezerCgroupDir(c *C) (restore func()) { 153 old := freezerCgroupDir 154 freezerCgroupDir = c.MkDir() 155 return func() { 156 freezerCgroupDir = old 157 } 158 } 159 160 func FreezerCgroupDir() string { 161 return freezerCgroupDir 162 } 163 164 func MockFreezing(freeze, thaw func(snapName string) error) (restore func()) { 165 oldFreeze := freezeSnapProcesses 166 oldThaw := thawSnapProcesses 167 168 freezeSnapProcesses = freeze 169 thawSnapProcesses = thaw 170 171 return func() { 172 freezeSnapProcesses = oldFreeze 173 thawSnapProcesses = oldThaw 174 } 175 } 176 177 func MockChangePerform(f func(chg *Change, as *Assumptions) ([]*Change, error)) func() { 178 origChangePerform := changePerform 179 changePerform = f 180 return func() { 181 changePerform = origChangePerform 182 } 183 } 184 185 func MockNeededChanges(f func(old, new *osutil.MountProfile) []*Change) (restore func()) { 186 origNeededChanges := NeededChanges 187 NeededChanges = f 188 return func() { 189 NeededChanges = origNeededChanges 190 } 191 } 192 193 func MockReadDir(fn func(string) ([]os.FileInfo, error)) (restore func()) { 194 old := ioutilReadDir 195 ioutilReadDir = fn 196 return func() { 197 ioutilReadDir = old 198 } 199 } 200 201 func MockReadlink(fn func(string) (string, error)) (restore func()) { 202 old := osReadlink 203 osReadlink = fn 204 return func() { 205 osReadlink = old 206 } 207 } 208 209 func (as *Assumptions) IsRestricted(path string) bool { 210 return as.isRestricted(path) 211 } 212 213 func (as *Assumptions) PastChanges() []*Change { 214 return as.pastChanges 215 } 216 217 func (as *Assumptions) CanWriteToDirectory(dirFd int, dirName string) (bool, error) { 218 return as.canWriteToDirectory(dirFd, dirName) 219 } 220 221 func (as *Assumptions) UnrestrictedPaths() []string { 222 return as.unrestrictedPaths 223 } 224 225 func (upCtx *CommonProfileUpdateContext) CurrentProfilePath() string { 226 return upCtx.currentProfilePath 227 } 228 229 func (upCtx *CommonProfileUpdateContext) DesiredProfilePath() string { 230 return upCtx.desiredProfilePath 231 } 232 233 func (upCtx *CommonProfileUpdateContext) FromSnapConfine() bool { 234 return upCtx.fromSnapConfine 235 } 236 237 func (upCtx *CommonProfileUpdateContext) SetFromSnapConfine(v bool) { 238 upCtx.fromSnapConfine = v 239 } 240 241 func NewCommonProfileUpdateContext(instanceName string, fromSnapConfine bool, currentProfilePath, desiredProfilePath string) *CommonProfileUpdateContext { 242 return &CommonProfileUpdateContext{ 243 instanceName: instanceName, 244 fromSnapConfine: fromSnapConfine, 245 currentProfilePath: currentProfilePath, 246 desiredProfilePath: desiredProfilePath, 247 } 248 }