github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/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  	GetKeypairManager = getKeypairManager
    96  	GenerateKey       = generateKey
    97  )
    98  
    99  func HiddenCmd(descr string, completeHidden bool) *cmdInfo {
   100  	return &cmdInfo{
   101  		shortHelp:      descr,
   102  		hidden:         true,
   103  		completeHidden: completeHidden,
   104  	}
   105  }
   106  
   107  type ChangeTimings = changeTimings
   108  
   109  func NewInfoWriter(w writeflusher) *infoWriter {
   110  	return &infoWriter{
   111  		writeflusher: w,
   112  		termWidth:    20,
   113  		esc:          &escapes{dash: "--", tick: "*"},
   114  		fmtTime:      func(t time.Time) string { return t.Format(time.Kitchen) },
   115  	}
   116  }
   117  
   118  func SetVerbose(iw *infoWriter, verbose bool) {
   119  	iw.verbose = verbose
   120  }
   121  
   122  var (
   123  	ClientSnapFromPath          = clientSnapFromPath
   124  	SetupDiskSnap               = (*infoWriter).setupDiskSnap
   125  	SetupSnap                   = (*infoWriter).setupSnap
   126  	MaybePrintServices          = (*infoWriter).maybePrintServices
   127  	MaybePrintCommands          = (*infoWriter).maybePrintCommands
   128  	MaybePrintType              = (*infoWriter).maybePrintType
   129  	PrintSummary                = (*infoWriter).printSummary
   130  	MaybePrintPublisher         = (*infoWriter).maybePrintPublisher
   131  	MaybePrintNotes             = (*infoWriter).maybePrintNotes
   132  	MaybePrintStandaloneVersion = (*infoWriter).maybePrintStandaloneVersion
   133  	MaybePrintBuildDate         = (*infoWriter).maybePrintBuildDate
   134  	MaybePrintContact           = (*infoWriter).maybePrintContact
   135  	MaybePrintBase              = (*infoWriter).maybePrintBase
   136  	MaybePrintPath              = (*infoWriter).maybePrintPath
   137  	MaybePrintSum               = (*infoWriter).maybePrintSum
   138  	MaybePrintCohortKey         = (*infoWriter).maybePrintCohortKey
   139  	MaybePrintHealth            = (*infoWriter).maybePrintHealth
   140  )
   141  
   142  func MockPollTime(d time.Duration) (restore func()) {
   143  	d0 := pollTime
   144  	pollTime = d
   145  	return func() {
   146  		pollTime = d0
   147  	}
   148  }
   149  
   150  func MockMaxGoneTime(d time.Duration) (restore func()) {
   151  	d0 := maxGoneTime
   152  	maxGoneTime = d
   153  	return func() {
   154  		maxGoneTime = d0
   155  	}
   156  }
   157  
   158  func MockSyscallExec(f func(string, []string, []string) error) (restore func()) {
   159  	syscallExecOrig := syscallExec
   160  	syscallExec = f
   161  	return func() {
   162  		syscallExec = syscallExecOrig
   163  	}
   164  }
   165  
   166  func MockUserCurrent(f func() (*user.User, error)) (restore func()) {
   167  	userCurrentOrig := userCurrent
   168  	userCurrent = f
   169  	return func() {
   170  		userCurrent = userCurrentOrig
   171  	}
   172  }
   173  
   174  func MockStoreNew(f func(*store.Config, store.DeviceAndAuthContext) *store.Store) (restore func()) {
   175  	storeNewOrig := storeNew
   176  	storeNew = f
   177  	return func() {
   178  		storeNew = storeNewOrig
   179  	}
   180  }
   181  
   182  func MockGetEnv(f func(name string) string) (restore func()) {
   183  	osGetenvOrig := osGetenv
   184  	osGetenv = f
   185  	return func() {
   186  		osGetenv = osGetenvOrig
   187  	}
   188  }
   189  
   190  func MockMountInfoPath(newMountInfoPath string) (restore func()) {
   191  	mountInfoPathOrig := mountInfoPath
   192  	mountInfoPath = newMountInfoPath
   193  	return func() {
   194  		mountInfoPath = mountInfoPathOrig
   195  	}
   196  }
   197  
   198  func MockOsReadlink(f func(string) (string, error)) (restore func()) {
   199  	osReadlinkOrig := osReadlink
   200  	osReadlink = f
   201  	return func() {
   202  		osReadlink = osReadlinkOrig
   203  	}
   204  }
   205  
   206  var AutoImportCandidates = autoImportCandidates
   207  
   208  func AliasInfoLess(snapName1, alias1, cmd1, snapName2, alias2, cmd2 string) bool {
   209  	x := aliasInfos{
   210  		&aliasInfo{
   211  			Snap:    snapName1,
   212  			Alias:   alias1,
   213  			Command: cmd1,
   214  		},
   215  		&aliasInfo{
   216  			Snap:    snapName2,
   217  			Alias:   alias2,
   218  			Command: cmd2,
   219  		},
   220  	}
   221  	return x.Less(0, 1)
   222  }
   223  
   224  func AssertTypeNameCompletion(match string) []flags.Completion {
   225  	return assertTypeName("").Complete(match)
   226  }
   227  
   228  func MockIsStdoutTTY(t bool) (restore func()) {
   229  	oldIsStdoutTTY := isStdoutTTY
   230  	isStdoutTTY = t
   231  	return func() {
   232  		isStdoutTTY = oldIsStdoutTTY
   233  	}
   234  }
   235  
   236  func MockIsStdinTTY(t bool) (restore func()) {
   237  	oldIsStdinTTY := isStdinTTY
   238  	isStdinTTY = t
   239  	return func() {
   240  		isStdinTTY = oldIsStdinTTY
   241  	}
   242  }
   243  
   244  func MockTimeNow(newTimeNow func() time.Time) (restore func()) {
   245  	oldTimeNow := timeNow
   246  	timeNow = newTimeNow
   247  	return func() {
   248  		timeNow = oldTimeNow
   249  	}
   250  }
   251  
   252  func MockTimeutilHuman(h func(time.Time) string) (restore func()) {
   253  	oldH := timeutilHuman
   254  	timeutilHuman = h
   255  	return func() {
   256  		timeutilHuman = oldH
   257  	}
   258  }
   259  
   260  func MockWaitConfTimeout(d time.Duration) (restore func()) {
   261  	oldWaitConfTimeout := d
   262  	waitConfTimeout = d
   263  	return func() {
   264  		waitConfTimeout = oldWaitConfTimeout
   265  	}
   266  }
   267  
   268  func Wait(cli *client.Client, id string) (*client.Change, error) {
   269  	wmx := waitMixin{}
   270  	wmx.client = cli
   271  	return wmx.wait(id)
   272  }
   273  
   274  func ColorMixin(cmode, umode string) colorMixin {
   275  	return colorMixin{
   276  		Color:        cmode,
   277  		unicodeMixin: unicodeMixin{Unicode: umode},
   278  	}
   279  }
   280  
   281  func CmdAdviseSnap() *cmdAdviseSnap {
   282  	return &cmdAdviseSnap{}
   283  }
   284  
   285  func MockSELinuxIsEnabled(isEnabled func() (bool, error)) (restore func()) {
   286  	old := selinuxIsEnabled
   287  	selinuxIsEnabled = isEnabled
   288  	return func() {
   289  		selinuxIsEnabled = old
   290  	}
   291  }
   292  
   293  func MockSELinuxVerifyPathContext(verifypathcon func(string) (bool, error)) (restore func()) {
   294  	old := selinuxVerifyPathContext
   295  	selinuxVerifyPathContext = verifypathcon
   296  	return func() {
   297  		selinuxVerifyPathContext = old
   298  	}
   299  }
   300  
   301  func MockSELinuxRestoreContext(restorecon func(string, selinux.RestoreMode) error) (restore func()) {
   302  	old := selinuxRestoreContext
   303  	selinuxRestoreContext = restorecon
   304  	return func() {
   305  		selinuxRestoreContext = old
   306  	}
   307  }
   308  
   309  func MockTermSize(newTermSize func() (int, int)) (restore func()) {
   310  	old := termSize
   311  	termSize = newTermSize
   312  	return func() {
   313  		termSize = old
   314  	}
   315  }
   316  
   317  func MockImagePrepare(newImagePrepare func(*image.Options) error) (restore func()) {
   318  	old := imagePrepare
   319  	imagePrepare = newImagePrepare
   320  	return func() {
   321  		imagePrepare = old
   322  	}
   323  }
   324  
   325  func MockSignalNotify(newSignalNotify func(sig ...os.Signal) (chan os.Signal, func())) (restore func()) {
   326  	old := signalNotify
   327  	signalNotify = newSignalNotify
   328  	return func() {
   329  		signalNotify = old
   330  	}
   331  }
   332  
   333  type ServiceName = serviceName
   334  
   335  func MockCreateTransientScopeForTracking(fn func(securityTag string, opts *cgroup.TrackingOptions) error) (restore func()) {
   336  	old := cgroupCreateTransientScopeForTracking
   337  	cgroupCreateTransientScopeForTracking = fn
   338  	return func() {
   339  		cgroupCreateTransientScopeForTracking = old
   340  	}
   341  }
   342  
   343  func MockConfirmSystemdServiceTracking(fn func(securityTag string) error) (restore func()) {
   344  	old := cgroupConfirmSystemdServiceTracking
   345  	cgroupConfirmSystemdServiceTracking = fn
   346  	return func() {
   347  		cgroupConfirmSystemdServiceTracking = old
   348  	}
   349  }
   350  
   351  func MockApparmorSnapAppFromPid(f func(pid int) (string, string, string, error)) (restore func()) {
   352  	old := apparmorSnapAppFromPid
   353  	apparmorSnapAppFromPid = f
   354  	return func() {
   355  		apparmorSnapAppFromPid = old
   356  	}
   357  }
   358  
   359  func MockCgroupSnapNameFromPid(f func(pid int) (string, error)) (restore func()) {
   360  	old := cgroupSnapNameFromPid
   361  	cgroupSnapNameFromPid = f
   362  	return func() {
   363  		cgroupSnapNameFromPid = old
   364  	}
   365  }
   366  
   367  func MockSyscallUmount(f func(string, int) error) (restore func()) {
   368  	old := syscallUnmount
   369  	syscallUnmount = f
   370  	return func() {
   371  		syscallUnmount = old
   372  	}
   373  }
   374  
   375  func MockIoutilTempDir(f func(string, string) (string, error)) (restore func()) {
   376  	old := ioutilTempDir
   377  	ioutilTempDir = f
   378  	return func() {
   379  		ioutilTempDir = old
   380  	}
   381  }
   382  
   383  func MockDownloadDirect(f func(snapName string, revision snap.Revision, dlOpts image.DownloadOptions) error) (restore func()) {
   384  	old := downloadDirect
   385  	downloadDirect = f
   386  	return func() {
   387  		downloadDirect = old
   388  	}
   389  }
   390  
   391  func MockSnapdAPIInterval(t time.Duration) (restore func()) {
   392  	old := snapdAPIInterval
   393  	snapdAPIInterval = t
   394  	return func() {
   395  		snapdAPIInterval = old
   396  	}
   397  }
   398  
   399  func MockSnapdWaitForFullSystemReboot(t time.Duration) (restore func()) {
   400  	old := snapdWaitForFullSystemReboot
   401  	snapdWaitForFullSystemReboot = t
   402  	return func() {
   403  		snapdWaitForFullSystemReboot = old
   404  	}
   405  }
   406  
   407  func MockOsChmod(f func(string, os.FileMode) error) (restore func()) {
   408  	old := osChmod
   409  	osChmod = f
   410  	return func() {
   411  		osChmod = old
   412  	}
   413  }