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