github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/snap/export_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016-2019 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 "os/user" 25 "time" 26 27 "github.com/jessevdk/go-flags" 28 29 "github.com/snapcore/snapd/client" 30 "github.com/snapcore/snapd/image" 31 "github.com/snapcore/snapd/sandbox/cgroup" 32 "github.com/snapcore/snapd/sandbox/selinux" 33 "github.com/snapcore/snapd/snap" 34 "github.com/snapcore/snapd/store" 35 ) 36 37 var RunMain = run 38 39 var ( 40 Client = mkClient 41 42 FirstNonOptionIsRun = firstNonOptionIsRun 43 44 CreateUserDataDirs = createUserDataDirs 45 ResolveApp = resolveApp 46 SnapdHelperPath = snapdHelperPath 47 SortByPath = sortByPath 48 AdviseCommand = adviseCommand 49 Antialias = antialias 50 FormatChannel = fmtChannel 51 PrintDescr = printDescr 52 WrapFlow = wrapFlow 53 TrueishJSON = trueishJSON 54 CompletionHandler = completionHandler 55 MarkForNoCompletion = markForNoCompletion 56 57 CanUnicode = canUnicode 58 ColorTable = colorTable 59 MonoColorTable = mono 60 ColorColorTable = color 61 NoEscColorTable = noesc 62 ColorMixinGetEscapes = (colorMixin).getEscapes 63 FillerPublisher = fillerPublisher 64 LongPublisher = longPublisher 65 ShortPublisher = shortPublisher 66 67 ReadRpc = readRpc 68 69 WriteWarningTimestamp = writeWarningTimestamp 70 MaybePresentWarnings = maybePresentWarnings 71 72 LongSnapDescription = longSnapDescription 73 SnapUsage = snapUsage 74 SnapHelpCategoriesIntro = snapHelpCategoriesIntro 75 SnapHelpAllIntro = snapHelpAllIntro 76 SnapHelpAllFooter = snapHelpAllFooter 77 SnapHelpFooter = snapHelpFooter 78 HelpCategories = helpCategories 79 80 LintArg = lintArg 81 LintDesc = lintDesc 82 83 FixupArg = fixupArg 84 85 InterfacesDeprecationNotice = interfacesDeprecationNotice 86 87 SignalNotify = signalNotify 88 89 SortTimingsTasks = sortTimingsTasks 90 91 PrintInstallHint = printInstallHint 92 93 IsStopping = isStopping 94 ) 95 96 func HiddenCmd(descr string, completeHidden bool) *cmdInfo { 97 return &cmdInfo{ 98 shortHelp: descr, 99 hidden: true, 100 completeHidden: completeHidden, 101 } 102 } 103 104 type ChangeTimings = changeTimings 105 106 func NewInfoWriter(w writeflusher) *infoWriter { 107 return &infoWriter{ 108 writeflusher: w, 109 termWidth: 20, 110 esc: &escapes{dash: "--", tick: "*"}, 111 fmtTime: func(t time.Time) string { return t.Format(time.Kitchen) }, 112 } 113 } 114 115 func SetVerbose(iw *infoWriter, verbose bool) { 116 iw.verbose = verbose 117 } 118 119 var ( 120 ClientSnapFromPath = clientSnapFromPath 121 SetupDiskSnap = (*infoWriter).setupDiskSnap 122 SetupSnap = (*infoWriter).setupSnap 123 MaybePrintServices = (*infoWriter).maybePrintServices 124 MaybePrintCommands = (*infoWriter).maybePrintCommands 125 MaybePrintType = (*infoWriter).maybePrintType 126 PrintSummary = (*infoWriter).printSummary 127 MaybePrintPublisher = (*infoWriter).maybePrintPublisher 128 MaybePrintNotes = (*infoWriter).maybePrintNotes 129 MaybePrintStandaloneVersion = (*infoWriter).maybePrintStandaloneVersion 130 MaybePrintBuildDate = (*infoWriter).maybePrintBuildDate 131 MaybePrintContact = (*infoWriter).maybePrintContact 132 MaybePrintBase = (*infoWriter).maybePrintBase 133 MaybePrintPath = (*infoWriter).maybePrintPath 134 MaybePrintSum = (*infoWriter).maybePrintSum 135 MaybePrintCohortKey = (*infoWriter).maybePrintCohortKey 136 MaybePrintHealth = (*infoWriter).maybePrintHealth 137 ) 138 139 func MockPollTime(d time.Duration) (restore func()) { 140 d0 := pollTime 141 pollTime = d 142 return func() { 143 pollTime = d0 144 } 145 } 146 147 func MockMaxGoneTime(d time.Duration) (restore func()) { 148 d0 := maxGoneTime 149 maxGoneTime = d 150 return func() { 151 maxGoneTime = d0 152 } 153 } 154 155 func MockSyscallExec(f func(string, []string, []string) error) (restore func()) { 156 syscallExecOrig := syscallExec 157 syscallExec = f 158 return func() { 159 syscallExec = syscallExecOrig 160 } 161 } 162 163 func MockUserCurrent(f func() (*user.User, error)) (restore func()) { 164 userCurrentOrig := userCurrent 165 userCurrent = f 166 return func() { 167 userCurrent = userCurrentOrig 168 } 169 } 170 171 func MockStoreNew(f func(*store.Config, store.DeviceAndAuthContext) *store.Store) (restore func()) { 172 storeNewOrig := storeNew 173 storeNew = f 174 return func() { 175 storeNew = storeNewOrig 176 } 177 } 178 179 func MockGetEnv(f func(name string) string) (restore func()) { 180 osGetenvOrig := osGetenv 181 osGetenv = f 182 return func() { 183 osGetenv = osGetenvOrig 184 } 185 } 186 187 func MockMountInfoPath(newMountInfoPath string) (restore func()) { 188 mountInfoPathOrig := mountInfoPath 189 mountInfoPath = newMountInfoPath 190 return func() { 191 mountInfoPath = mountInfoPathOrig 192 } 193 } 194 195 func MockOsReadlink(f func(string) (string, error)) (restore func()) { 196 osReadlinkOrig := osReadlink 197 osReadlink = f 198 return func() { 199 osReadlink = osReadlinkOrig 200 } 201 } 202 203 var AutoImportCandidates = autoImportCandidates 204 205 func AliasInfoLess(snapName1, alias1, cmd1, snapName2, alias2, cmd2 string) bool { 206 x := aliasInfos{ 207 &aliasInfo{ 208 Snap: snapName1, 209 Alias: alias1, 210 Command: cmd1, 211 }, 212 &aliasInfo{ 213 Snap: snapName2, 214 Alias: alias2, 215 Command: cmd2, 216 }, 217 } 218 return x.Less(0, 1) 219 } 220 221 func AssertTypeNameCompletion(match string) []flags.Completion { 222 return assertTypeName("").Complete(match) 223 } 224 225 func MockIsStdoutTTY(t bool) (restore func()) { 226 oldIsStdoutTTY := isStdoutTTY 227 isStdoutTTY = t 228 return func() { 229 isStdoutTTY = oldIsStdoutTTY 230 } 231 } 232 233 func MockIsStdinTTY(t bool) (restore func()) { 234 oldIsStdinTTY := isStdinTTY 235 isStdinTTY = t 236 return func() { 237 isStdinTTY = oldIsStdinTTY 238 } 239 } 240 241 func MockTimeNow(newTimeNow func() time.Time) (restore func()) { 242 oldTimeNow := timeNow 243 timeNow = newTimeNow 244 return func() { 245 timeNow = oldTimeNow 246 } 247 } 248 249 func MockTimeutilHuman(h func(time.Time) string) (restore func()) { 250 oldH := timeutilHuman 251 timeutilHuman = h 252 return func() { 253 timeutilHuman = oldH 254 } 255 } 256 257 func MockWaitConfTimeout(d time.Duration) (restore func()) { 258 oldWaitConfTimeout := d 259 waitConfTimeout = d 260 return func() { 261 waitConfTimeout = oldWaitConfTimeout 262 } 263 } 264 265 func Wait(cli *client.Client, id string) (*client.Change, error) { 266 wmx := waitMixin{} 267 wmx.client = cli 268 return wmx.wait(id) 269 } 270 271 func ColorMixin(cmode, umode string) colorMixin { 272 return colorMixin{ 273 Color: cmode, 274 unicodeMixin: unicodeMixin{Unicode: umode}, 275 } 276 } 277 278 func CmdAdviseSnap() *cmdAdviseSnap { 279 return &cmdAdviseSnap{} 280 } 281 282 func MockSELinuxIsEnabled(isEnabled func() (bool, error)) (restore func()) { 283 old := selinuxIsEnabled 284 selinuxIsEnabled = isEnabled 285 return func() { 286 selinuxIsEnabled = old 287 } 288 } 289 290 func MockSELinuxVerifyPathContext(verifypathcon func(string) (bool, error)) (restore func()) { 291 old := selinuxVerifyPathContext 292 selinuxVerifyPathContext = verifypathcon 293 return func() { 294 selinuxVerifyPathContext = old 295 } 296 } 297 298 func MockSELinuxRestoreContext(restorecon func(string, selinux.RestoreMode) error) (restore func()) { 299 old := selinuxRestoreContext 300 selinuxRestoreContext = restorecon 301 return func() { 302 selinuxRestoreContext = old 303 } 304 } 305 306 func MockTermSize(newTermSize func() (int, int)) (restore func()) { 307 old := termSize 308 termSize = newTermSize 309 return func() { 310 termSize = old 311 } 312 } 313 314 func MockImagePrepare(newImagePrepare func(*image.Options) error) (restore func()) { 315 old := imagePrepare 316 imagePrepare = newImagePrepare 317 return func() { 318 imagePrepare = old 319 } 320 } 321 322 func MockSignalNotify(newSignalNotify func(sig ...os.Signal) (chan os.Signal, func())) (restore func()) { 323 old := signalNotify 324 signalNotify = newSignalNotify 325 return func() { 326 signalNotify = old 327 } 328 } 329 330 type ServiceName = serviceName 331 332 func MockCreateTransientScopeForTracking(fn func(securityTag string, opts *cgroup.TrackingOptions) error) (restore func()) { 333 old := cgroupCreateTransientScopeForTracking 334 cgroupCreateTransientScopeForTracking = fn 335 return func() { 336 cgroupCreateTransientScopeForTracking = old 337 } 338 } 339 340 func MockConfirmSystemdServiceTracking(fn func(securityTag string) error) (restore func()) { 341 old := cgroupConfirmSystemdServiceTracking 342 cgroupConfirmSystemdServiceTracking = fn 343 return func() { 344 cgroupConfirmSystemdServiceTracking = old 345 } 346 } 347 348 func MockApparmorSnapAppFromPid(f func(pid int) (string, string, string, error)) (restore func()) { 349 old := apparmorSnapAppFromPid 350 apparmorSnapAppFromPid = f 351 return func() { 352 apparmorSnapAppFromPid = old 353 } 354 } 355 356 func MockCgroupSnapNameFromPid(f func(pid int) (string, error)) (restore func()) { 357 old := cgroupSnapNameFromPid 358 cgroupSnapNameFromPid = f 359 return func() { 360 cgroupSnapNameFromPid = old 361 } 362 } 363 364 func MockSyscallUmount(f func(string, int) error) (restore func()) { 365 old := syscallUnmount 366 syscallUnmount = f 367 return func() { 368 syscallUnmount = old 369 } 370 } 371 372 func MockIoutilTempDir(f func(string, string) (string, error)) (restore func()) { 373 old := ioutilTempDir 374 ioutilTempDir = f 375 return func() { 376 ioutilTempDir = old 377 } 378 } 379 380 func MockDownloadDirect(f func(snapName string, revision snap.Revision, dlOpts image.DownloadOptions) error) (restore func()) { 381 old := downloadDirect 382 downloadDirect = f 383 return func() { 384 downloadDirect = old 385 } 386 }