1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package osutil
import (
"fmt"
"io"
"os"
"os/exec"
"syscall"
"time"
"github.com/snapcore/snapd/osutil/sys"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/strutil"
"github.com/snapcore/snapd/testutil"
)
var (
StreamsEqualChunked = streamsEqualChunked
FilesAreEqualChunked = filesAreEqualChunked
SudoersFile = sudoersFile
DoCopyFile = doCopyFile
)
type Fileish = fileish
func MockMaxCp(new int64) (restore func()) {
old := maxcp
maxcp = new
return func() {
maxcp = old
}
}
func MockCopyFile(new func(fileish, fileish, os.FileInfo) error) (restore func()) {
old := copyfile
copyfile = new
return func() {
copyfile = old
}
}
func MockOpenFile(new func(string, int, os.FileMode) (fileish, error)) (restore func()) {
old := openfile
openfile = new
return func() {
openfile = old
}
}
func MockSyscallSettimeofday(f func(*syscall.Timeval) error) (restore func()) {
old := syscallSettimeofday
syscallSettimeofday = f
return func() {
syscallSettimeofday = old
}
}
func MockUserCurrent(mock func() (*user.User, error)) func() {
realUserCurrent := userCurrent
userCurrent = mock
return func() { userCurrent = realUserCurrent }
}
func MockSudoersDotD(mockDir string) func() {
realSudoersD := sudoersDotD
sudoersDotD = mockDir
return func() { sudoersDotD = realSudoersD }
}
func MockSyscallKill(f func(int, syscall.Signal) error) func() {
oldSyscallKill := syscallKill
syscallKill = f
return func() {
syscallKill = oldSyscallKill
}
}
func MockSyscallStatfs(f func(string, *syscall.Statfs_t) error) func() {
oldSyscallStatfs := syscallStatfs
syscallStatfs = f
return func() {
syscallStatfs = oldSyscallStatfs
}
}
func MockSyscallGetpgid(f func(int) (int, error)) func() {
oldSyscallGetpgid := syscallGetpgid
syscallGetpgid = f
return func() {
syscallGetpgid = oldSyscallGetpgid
}
}
func MockCmdWaitTimeout(timeout time.Duration) func() {
oldCmdWaitTimeout := cmdWaitTimeout
cmdWaitTimeout = timeout
return func() {
cmdWaitTimeout = oldCmdWaitTimeout
}
}
func WaitingReaderGuts(r io.Reader) (io.Reader, *exec.Cmd) {
wr := r.(*waitingReader)
return wr.reader, wr.cmd
}
func MockChown(f func(*os.File, sys.UserID, sys.GroupID) error) func() {
oldChown := chown
chown = f
return func() {
chown = oldChown
}
}
func MockLookPath(new func(string) (string, error)) (restore func()) {
old := lookPath
lookPath = new
return func() {
lookPath = old
}
}
func MockhasAddUserExecutable(new func() bool) (restore func()) {
old := hasAddUserExecutable
hasAddUserExecutable = new
return func() {
hasAddUserExecutable = old
}
}
func SetAtomicFileRenamed(aw *AtomicFile, renamed bool) {
aw.renamed = renamed
}
func SetUnsafeIO(b bool) func() {
oldSnapdUnsafeIO := snapdUnsafeIO
snapdUnsafeIO = b
return func() {
snapdUnsafeIO = oldSnapdUnsafeIO
}
}
func GetUnsafeIO() bool {
// a getter so that tests do not attempt to modify that directly
return snapdUnsafeIO
}
func MockOsReadlink(f func(string) (string, error)) func() {
realOsReadlink := osReadlink
osReadlink = f
return func() { osReadlink = realOsReadlink }
}
// MockEtcFstab mocks content of /etc/fstab read by IsHomeUsingNFS
func MockEtcFstab(text string) (restore func()) {
old := etcFstab
f, err := os.CreateTemp("", "fstab")
if err != nil {
panic(fmt.Errorf("cannot open temporary file: %s", err))
}
if err := os.WriteFile(f.Name(), []byte(text), 0644); err != nil {
panic(fmt.Errorf("cannot write mock fstab file: %s", err))
}
etcFstab = f.Name()
return func() {
if etcFstab == "/etc/fstab" {
panic("respectfully refusing to remove /etc/fstab")
}
os.Remove(etcFstab)
etcFstab = old
}
}
// MockUname mocks syscall.Uname as used by MachineName and KernelVersion
func MockUname(f func(*syscall.Utsname) error) (restore func()) {
r := testutil.Backup(&syscallUname)
syscallUname = f
return r
}
var (
FindUidNoGetentFallback = findUidNoGetentFallback
FindGidNoGetentFallback = findGidNoGetentFallback
FindUidWithGetentFallback = findUidWithGetentFallback
FindGidWithGetentFallback = findGidWithGetentFallback
)
func MockFindUidNoFallback(mock func(name string) (uint64, error)) (restore func()) {
old := findUidNoGetentFallback
findUidNoGetentFallback = mock
return func() { findUidNoGetentFallback = old }
}
func MockFindGidNoFallback(mock func(name string) (uint64, error)) (restore func()) {
old := findGidNoGetentFallback
findGidNoGetentFallback = mock
return func() { findGidNoGetentFallback = old }
}
const MaxLinkTries = maxLinkTries
var ParseRawEnvironment = parseRawEnvironment
// ParseRawExpandableEnv returns a new expandable environment parsed from key=value strings.
func ParseRawExpandableEnv(entries []string) (ExpandableEnv, error) {
om := strutil.NewOrderedMap()
for _, entry := range entries {
key, value, err := parseEnvEntry(entry)
if err != nil {
return ExpandableEnv{}, err
}
if om.Get(key) != "" {
return ExpandableEnv{}, fmt.Errorf("cannot overwrite earlier value of %q", key)
}
om.Set(key, value)
}
return ExpandableEnv{OrderedMap: om}, nil
}
func ReadGoBuildID(fname string) (string, error) {
return readGenericBuildID(fname, goElfNote, goHdrType)
}
|