File: export_test.go

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (493 lines) | stat: -rw-r--r-- 13,069 bytes parent folder | download | duplicates (2)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2016-2019 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 main

import (
	"context"
	"os"
	"time"

	"github.com/jessevdk/go-flags"

	"github.com/snapcore/snapd/client"
	"github.com/snapcore/snapd/cmd/snaplock/runinhibit"
	"github.com/snapcore/snapd/image"
	"github.com/snapcore/snapd/osutil"
	"github.com/snapcore/snapd/osutil/user"
	"github.com/snapcore/snapd/sandbox/cgroup"
	"github.com/snapcore/snapd/sandbox/selinux"
	"github.com/snapcore/snapd/seed/seedwriter"
	"github.com/snapcore/snapd/snap"
	"github.com/snapcore/snapd/store"
	"github.com/snapcore/snapd/store/tooling"
	"github.com/snapcore/snapd/testutil"
)

var RunMain = run

var (
	Client = mkClient

	FirstNonOptionIsRun = firstNonOptionIsRun

	CreateUserDataDirs  = createUserDataDirs
	ResolveApp          = resolveApp
	SnapdHelperPath     = snapdHelperPath
	SortByPath          = sortByPath
	AdviseCommand       = adviseCommand
	Antialias           = antialias
	FormatChannel       = fmtChannel
	PrintDescr          = printDescr
	TrueishJSON         = trueishJSON
	CompletionHandler   = completionHandler
	MarkForNoCompletion = markForNoCompletion

	CanUnicode           = canUnicode
	ColorTable           = colorTable
	MonoColorTable       = mono
	ColorColorTable      = color
	NoEscColorTable      = noesc
	ColorMixinGetEscapes = (colorMixin).getEscapes
	FillerPublisher      = fillerPublisher
	LongPublisher        = longPublisher
	ShortPublisher       = shortPublisher

	ReadRpc = readRpc

	WriteWarningTimestamp = writeWarningTimestamp
	MaybePresentWarnings  = maybePresentWarnings

	LongSnapDescription     = longSnapDescription
	SnapUsage               = snapUsage
	SnapHelpCategoriesIntro = snapHelpCategoriesIntro
	SnapHelpAllIntro        = snapHelpAllIntro
	SnapHelpAllFooter       = snapHelpAllFooter
	SnapHelpFooter          = snapHelpFooter
	HelpCategories          = helpCategories

	LintArg  = lintArg
	LintDesc = lintDesc

	FixupArg = fixupArg

	InterfacesDeprecationNotice = interfacesDeprecationNotice

	SignalNotify = signalNotify

	SortTimingsTasks = sortTimingsTasks

	PrintInstallHint = printInstallHint

	IsStopping = isStopping

	GetSnapDirOptions                   = getSnapDirOptions
	SnapInstancesAndComponentsFromNames = snapInstancesAndComponentsFromNames

	GetSystemKeyRetryCount = getSystemKeyRetryCount

	ComposeSubCmd = composeSubCmd
)

func HiddenCmd(descr string, completeHidden bool) *cmdInfo {
	return &cmdInfo{
		shortHelp:      descr,
		hidden:         true,
		completeHidden: completeHidden,
	}
}

type ChangeTimings = changeTimings

func NewInfoWriter(w writeflusher) *infoWriter {
	return NewInfoWriterWithFmtTime(w, nil)
}

func NewInfoWriterWithFmtTime(w writeflusher, fmtTime func(time.Time) string) *infoWriter {
	if fmtTime == nil {
		fmtTime = func(t time.Time) string { return t.Format(time.Kitchen) }
	}

	return &infoWriter{
		writeflusher: w,
		termWidth:    20,
		esc:          &escapes{dash: "--", tick: "*"},
		fmtTime:      fmtTime,
	}
}

func SetVerbose(iw *infoWriter, verbose bool) {
	iw.verbose = verbose
}

var (
	ClientSnapFromPath          = clientSnapFromPath
	SetupDiskSnap               = (*infoWriter).setupDiskSnap
	SetupSnap                   = (*infoWriter).setupSnap
	MaybePrintServices          = (*infoWriter).maybePrintServices
	MaybePrintCommands          = (*infoWriter).maybePrintCommands
	MaybePrintType              = (*infoWriter).maybePrintType
	PrintSummary                = (*infoWriter).printSummary
	MaybePrintPublisher         = (*infoWriter).maybePrintPublisher
	MaybePrintNotes             = (*infoWriter).maybePrintNotes
	MaybePrintStandaloneVersion = (*infoWriter).maybePrintStandaloneVersion
	MaybePrintBuildDate         = (*infoWriter).maybePrintBuildDate
	MaybePrintLinks             = (*infoWriter).maybePrintLinks
	MaybePrintBase              = (*infoWriter).maybePrintBase
	MaybePrintPath              = (*infoWriter).maybePrintPath
	MaybePrintSum               = (*infoWriter).maybePrintSum
	MaybePrintCohortKey         = (*infoWriter).maybePrintCohortKey
	MaybePrintHealth            = (*infoWriter).maybePrintHealth
	MaybePrintRefreshInfo       = (*infoWriter).maybePrintRefreshInfo
	WaitWhileInhibited          = waitWhileInhibited
	NewInhibitionFlow           = newInhibitionFlow
	ErrSnapRefreshConflict      = errSnapRefreshConflict
)

func MockPollTime(d time.Duration) (restore func()) {
	d0 := pollTime
	pollTime = d
	return func() {
		pollTime = d0
	}
}

func MockMaxGoneTime(d time.Duration) (restore func()) {
	d0 := maxGoneTime
	maxGoneTime = d
	return func() {
		maxGoneTime = d0
	}
}

func MockSyscallExec(f func(string, []string, []string) error) (restore func()) {
	syscallExecOrig := syscallExec
	syscallExec = f
	return func() {
		syscallExec = syscallExecOrig
	}
}

func MockUserCurrent(f func() (*user.User, error)) (restore func()) {
	userCurrentOrig := userCurrent
	userCurrent = f
	return func() {
		userCurrent = userCurrentOrig
	}
}

func MockStoreNew(f func(*store.Config, store.DeviceAndAuthContext) *store.Store) (restore func()) {
	storeNewOrig := storeNew
	storeNew = f
	return func() {
		storeNew = storeNewOrig
	}
}

func MockGetEnv(f func(name string) string) (restore func()) {
	osGetenvOrig := osGetenv
	osGetenv = f
	return func() {
		osGetenv = osGetenvOrig
	}
}

func MockOsReadlink(f func(string) (string, error)) (restore func()) {
	osReadlinkOrig := osReadlink
	osReadlink = f
	return func() {
		osReadlink = osReadlinkOrig
	}
}

var AutoImportCandidates = autoImportCandidates

func AliasInfoLess(snapName1, alias1, cmd1, snapName2, alias2, cmd2 string) bool {
	x := aliasInfos{
		&aliasInfo{
			Snap:    snapName1,
			Alias:   alias1,
			Command: cmd1,
		},
		&aliasInfo{
			Snap:    snapName2,
			Alias:   alias2,
			Command: cmd2,
		},
	}
	return x.Less(0, 1)
}

func AssertTypeNameCompletion(match string) []flags.Completion {
	return assertTypeName("").Complete(match)
}

func MockIsStdoutTTY(t bool) (restore func()) {
	oldIsStdoutTTY := isStdoutTTY
	isStdoutTTY = t
	return func() {
		isStdoutTTY = oldIsStdoutTTY
	}
}

func MockIsStdinTTY(t bool) (restore func()) {
	oldIsStdinTTY := isStdinTTY
	isStdinTTY = t
	return func() {
		isStdinTTY = oldIsStdinTTY
	}
}

func MockTimeNow(newTimeNow func() time.Time) (restore func()) {
	oldTimeNow := timeNow
	timeNow = newTimeNow
	return func() {
		timeNow = oldTimeNow
	}
}

func MockTimeutilHuman(h func(time.Time) string) (restore func()) {
	oldH := timeutilHuman
	timeutilHuman = h
	return func() {
		timeutilHuman = oldH
	}
}

func MockWaitConfTimeout(d time.Duration) (restore func()) {
	oldWaitConfTimeout := d
	waitConfTimeout = d
	return func() {
		waitConfTimeout = oldWaitConfTimeout
	}
}

func Wait(cli *client.Client, id string) (*client.Change, error) {
	wmx := waitMixin{}
	wmx.client = cli
	return wmx.wait(id)
}

func ColorMixin(cmode, umode string) colorMixin {
	return colorMixin{
		Color:        cmode,
		unicodeMixin: unicodeMixin{Unicode: umode},
	}
}

func CmdAdviseSnap() *cmdAdviseSnap {
	return &cmdAdviseSnap{}
}

func MockSELinuxIsEnabled(isEnabled func() (bool, error)) (restore func()) {
	old := selinuxIsEnabled
	selinuxIsEnabled = isEnabled
	return func() {
		selinuxIsEnabled = old
	}
}

func MockSELinuxVerifyPathContext(verifypathcon func(string) (bool, error)) (restore func()) {
	old := selinuxVerifyPathContext
	selinuxVerifyPathContext = verifypathcon
	return func() {
		selinuxVerifyPathContext = old
	}
}

func MockSELinuxRestoreContext(restorecon func(string, selinux.RestoreMode) error) (restore func()) {
	old := selinuxRestoreContext
	selinuxRestoreContext = restorecon
	return func() {
		selinuxRestoreContext = old
	}
}

func MockTermSize(newTermSize func() (int, int)) (restore func()) {
	old := termSize
	termSize = newTermSize
	return func() {
		termSize = old
	}
}

func MockImagePrepare(newImagePrepare func(*image.Options) error) (restore func()) {
	old := imagePrepare
	imagePrepare = newImagePrepare
	return func() {
		imagePrepare = old
	}
}

func MockSignalNotify(newSignalNotify func(sig ...os.Signal) (chan os.Signal, func())) (restore func()) {
	old := signalNotify
	signalNotify = newSignalNotify
	return func() {
		signalNotify = old
	}
}

type ServiceName = serviceName

func MockCreateTransientScopeForTracking(fn func(securityTag string, opts *cgroup.TrackingOptions) error) (restore func()) {
	old := cgroupCreateTransientScopeForTracking
	cgroupCreateTransientScopeForTracking = fn
	return func() {
		cgroupCreateTransientScopeForTracking = old
	}
}

func MockConfirmSystemdServiceTracking(fn func(securityTag string) error) (restore func()) {
	old := cgroupConfirmSystemdServiceTracking
	cgroupConfirmSystemdServiceTracking = fn
	return func() {
		cgroupConfirmSystemdServiceTracking = old
	}
}

func MockConfirmSystemdAppTracking(fn func(securityTag string) error) (restore func()) {
	old := cgroupConfirmSystemdAppTracking
	cgroupConfirmSystemdAppTracking = fn
	return func() {
		cgroupConfirmSystemdAppTracking = old
	}
}

func MockApparmorSnapAppFromPid(f func(pid int) (string, string, string, error)) (restore func()) {
	old := apparmorSnapAppFromPid
	apparmorSnapAppFromPid = f
	return func() {
		apparmorSnapAppFromPid = old
	}
}

func MockCgroupSnapNameFromPid(f func(pid int) (string, error)) (restore func()) {
	old := cgroupSnapNameFromPid
	cgroupSnapNameFromPid = f
	return func() {
		cgroupSnapNameFromPid = old
	}
}

func MockSyscallUmount(f func(string, int) error) (restore func()) {
	old := syscallUnmount
	syscallUnmount = f
	return func() {
		syscallUnmount = old
	}
}

func MockIoutilTempDir(f func(string, string) (string, error)) (restore func()) {
	old := osMkdirTemp
	osMkdirTemp = f
	return func() {
		osMkdirTemp = old
	}
}

func MockDownloadContainers(f func(snapName string, components []string, tsto *tooling.ToolingStore, opts tooling.DownloadSnapOptions) (*tooling.DownloadedSnap, error)) (restore func()) {
	old := downloadContainers
	downloadContainers = f
	return func() {
		downloadContainers = old
	}
}

func MockDownloadAssertions(f func(info *snap.Info, snapPath string, components map[string]*snap.ComponentInfo, tsto *tooling.ToolingStore, opts tooling.DownloadSnapOptions) (string, error)) (restore func()) {
	old := downloadAssertions
	downloadAssertions = f
	return func() {
		downloadAssertions = old
	}
}

func MockSnapdAPIInterval(t time.Duration) (restore func()) {
	old := snapdAPIInterval
	snapdAPIInterval = t
	return func() {
		snapdAPIInterval = old
	}
}

func MockSnapdWaitForFullSystemReboot(t time.Duration) (restore func()) {
	old := snapdWaitForFullSystemReboot
	snapdWaitForFullSystemReboot = t
	return func() {
		snapdWaitForFullSystemReboot = old
	}
}

func MockOsChmod(f func(string, os.FileMode) error) (restore func()) {
	old := osChmod
	osChmod = f
	return func() {
		osChmod = old
	}
}

func MockWaitWhileInhibited(f func(ctx context.Context, snapName string, notInhibited func(ctx context.Context) error, inhibited func(ctx context.Context, hint runinhibit.Hint, inhibitInfo *runinhibit.InhibitInfo) (cont bool, err error), interval time.Duration) (flock *osutil.FileLock, retErr error)) (restore func()) {
	restore = testutil.Backup(&runinhibitWaitWhileInhibited)
	runinhibitWaitWhileInhibited = f
	return restore
}

func MockInhibitionFlow(flow inhibitionFlow) (restore func()) {
	old := newInhibitionFlow
	newInhibitionFlow = func(cli *client.Client, name string) inhibitionFlow {
		return flow
	}
	return func() {
		newInhibitionFlow = old
	}
}

func MockAutostartSessionApps(f func(string) error) func() {
	old := autostartSessionApps
	autostartSessionApps = f
	return func() {
		autostartSessionApps = old
	}
}

func ParseQuotaValues(maxMemory, cpuMax, cpuSet, threadsMax, journalSizeMax, journalRateLimit string) (*client.QuotaValues, error) {
	var quotas cmdSetQuota

	quotas.MemoryMax = maxMemory
	quotas.CPUMax = cpuMax
	quotas.CPUSet = cpuSet
	quotas.ThreadsMax = threadsMax
	quotas.JournalSizeMax = journalSizeMax
	quotas.JournalRateLimit = journalRateLimit

	return quotas.parseQuotas()
}

func MockSeedWriterReadManifest(f func(manifestFile string) (*seedwriter.Manifest, error)) (restore func()) {
	restore = testutil.Backup(&seedwriterReadManifest)
	seedwriterReadManifest = f
	return restore
}

func MockGetSystemKeyRetryCount(f func() int) (restore func()) {
	return testutil.Mock(&getSystemKeyRetryCount, f)
}

func MockTimeAfter(f func(d time.Duration) <-chan time.Time) (restore func()) {
	return testutil.Mock(&timeAfter, f)
}