github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/overlord/snapstate/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 snapstate
    21  
    22  import (
    23  	"context"
    24  	"time"
    25  
    26  	"github.com/snapcore/snapd/overlord/state"
    27  	"github.com/snapcore/snapd/snap"
    28  	"github.com/snapcore/snapd/store"
    29  	userclient "github.com/snapcore/snapd/usersession/client"
    30  )
    31  
    32  type ManagerBackend managerBackend
    33  
    34  func SetSnapManagerBackend(s *SnapManager, b ManagerBackend) {
    35  	s.backend = b
    36  }
    37  
    38  func MockSnapReadInfo(mock func(name string, si *snap.SideInfo) (*snap.Info, error)) (restore func()) {
    39  	old := snapReadInfo
    40  	snapReadInfo = mock
    41  	return func() { snapReadInfo = old }
    42  }
    43  
    44  func MockMountPollInterval(intv time.Duration) (restore func()) {
    45  	old := mountPollInterval
    46  	mountPollInterval = intv
    47  	return func() { mountPollInterval = old }
    48  }
    49  
    50  func MockRevisionDate(mock func(info *snap.Info) time.Time) (restore func()) {
    51  	old := revisionDate
    52  	if mock == nil {
    53  		mock = revisionDateImpl
    54  	}
    55  	revisionDate = mock
    56  	return func() { revisionDate = old }
    57  }
    58  
    59  func MockOpenSnapFile(mock func(path string, si *snap.SideInfo) (*snap.Info, snap.Container, error)) (restore func()) {
    60  	prevOpenSnapFile := openSnapFile
    61  	openSnapFile = mock
    62  	return func() { openSnapFile = prevOpenSnapFile }
    63  }
    64  
    65  func MockErrtrackerReport(mock func(string, string, string, map[string]string) (string, error)) (restore func()) {
    66  	prev := errtrackerReport
    67  	errtrackerReport = mock
    68  	return func() { errtrackerReport = prev }
    69  }
    70  
    71  func MockPrerequisitesRetryTimeout(d time.Duration) (restore func()) {
    72  	old := prerequisitesRetryTimeout
    73  	prerequisitesRetryTimeout = d
    74  	return func() { prerequisitesRetryTimeout = old }
    75  }
    76  
    77  func MockOsutilEnsureUserGroup(mock func(name string, id uint32, extraUsers bool) error) (restore func()) {
    78  	old := osutilEnsureUserGroup
    79  	osutilEnsureUserGroup = mock
    80  	return func() { osutilEnsureUserGroup = old }
    81  }
    82  
    83  var (
    84  	CoreInfoInternal       = coreInfo
    85  	CheckSnap              = checkSnap
    86  	CanRemove              = canRemove
    87  	CanDisable             = canDisable
    88  	CachedStore            = cachedStore
    89  	DefaultRefreshSchedule = defaultRefreshSchedule
    90  	DoInstall              = doInstall
    91  	UserFromUserID         = userFromUserID
    92  	ValidateFeatureFlags   = validateFeatureFlags
    93  	ResolveChannel         = resolveChannel
    94  
    95  	CurrentSnaps = currentSnaps
    96  
    97  	DefaultContentPlugProviders = defaultContentPlugProviders
    98  
    99  	HasOtherInstances = hasOtherInstances
   100  
   101  	SafetyMarginDiskSpace = safetyMarginDiskSpace
   102  )
   103  
   104  func PreviousSideInfo(snapst *SnapState) *snap.SideInfo {
   105  	return snapst.previousSideInfo()
   106  }
   107  
   108  // helpers
   109  var InstallSize = installSize
   110  
   111  // aliases v2
   112  var (
   113  	ApplyAliasesChange    = applyAliasesChange
   114  	AutoAliasesDelta      = autoAliasesDelta
   115  	RefreshAliases        = refreshAliases
   116  	CheckAliasesConflicts = checkAliasesConflicts
   117  	DisableAliases        = disableAliases
   118  	SwitchSummary         = switchSummary
   119  )
   120  
   121  // dbus
   122  var (
   123  	CheckDBusServiceConflicts = checkDBusServiceConflicts
   124  )
   125  
   126  // readme files
   127  var (
   128  	WriteSnapReadme = writeSnapReadme
   129  	SnapReadme      = snapReadme
   130  )
   131  
   132  // refreshes
   133  var (
   134  	NewAutoRefresh                = newAutoRefresh
   135  	NewRefreshHints               = newRefreshHints
   136  	CanRefreshOnMeteredConnection = canRefreshOnMeteredConnection
   137  
   138  	NewCatalogRefresh            = newCatalogRefresh
   139  	CatalogRefreshDelayBase      = catalogRefreshDelayBase
   140  	CatalogRefreshDelayWithDelta = catalogRefreshDelayWithDelta
   141  
   142  	SoftCheckNothingRunningForRefresh     = softCheckNothingRunningForRefresh
   143  	HardEnsureNothingRunningDuringRefresh = hardEnsureNothingRunningDuringRefresh
   144  )
   145  
   146  func MockNextRefresh(ar *autoRefresh, when time.Time) {
   147  	ar.nextRefresh = when
   148  }
   149  
   150  func MockLastRefreshSchedule(ar *autoRefresh, schedule string) {
   151  	ar.lastRefreshSchedule = schedule
   152  }
   153  
   154  func MockCatalogRefreshNextRefresh(cr *catalogRefresh, when time.Time) {
   155  	cr.nextCatalogRefresh = when
   156  }
   157  
   158  func NextCatalogRefresh(cr *catalogRefresh) time.Time {
   159  	return cr.nextCatalogRefresh
   160  }
   161  
   162  func MockRefreshRetryDelay(d time.Duration) func() {
   163  	origRefreshRetryDelay := refreshRetryDelay
   164  	refreshRetryDelay = d
   165  	return func() {
   166  		refreshRetryDelay = origRefreshRetryDelay
   167  	}
   168  }
   169  
   170  func MockIsOnMeteredConnection(mock func() (bool, error)) func() {
   171  	old := IsOnMeteredConnection
   172  	IsOnMeteredConnection = mock
   173  	return func() {
   174  		IsOnMeteredConnection = old
   175  	}
   176  }
   177  
   178  func MockLocalInstallCleanupWait(d time.Duration) (restore func()) {
   179  	old := localInstallCleanupWait
   180  	localInstallCleanupWait = d
   181  	return func() {
   182  		localInstallCleanupWait = old
   183  	}
   184  }
   185  
   186  func MockLocalInstallLastCleanup(t time.Time) (restore func()) {
   187  	old := localInstallLastCleanup
   188  	localInstallLastCleanup = t
   189  	return func() {
   190  		localInstallLastCleanup = old
   191  	}
   192  }
   193  
   194  func MockAsyncPendingRefreshNotification(fn func(context.Context, *userclient.Client, *userclient.PendingSnapRefreshInfo)) (restore func()) {
   195  	old := asyncPendingRefreshNotification
   196  	asyncPendingRefreshNotification = fn
   197  	return func() {
   198  		asyncPendingRefreshNotification = old
   199  	}
   200  }
   201  
   202  // re-refresh related
   203  var (
   204  	RefreshedSnaps  = refreshedSnaps
   205  	ReRefreshFilter = reRefreshFilter
   206  )
   207  
   208  type UpdateFilter = updateFilter
   209  
   210  func MockReRefreshUpdateMany(f func(context.Context, *state.State, []string, int, UpdateFilter, *Flags, string) ([]string, []*state.TaskSet, error)) (restore func()) {
   211  	old := reRefreshUpdateMany
   212  	reRefreshUpdateMany = f
   213  	return func() {
   214  		reRefreshUpdateMany = old
   215  	}
   216  }
   217  
   218  func MockReRefreshRetryTimeout(d time.Duration) (restore func()) {
   219  	old := reRefreshRetryTimeout
   220  	reRefreshRetryTimeout = d
   221  	return func() {
   222  		reRefreshRetryTimeout = old
   223  	}
   224  }
   225  
   226  // aux store info
   227  var (
   228  	AuxStoreInfoFilename = auxStoreInfoFilename
   229  	RetrieveAuxStoreInfo = retrieveAuxStoreInfo
   230  	KeepAuxStoreInfo     = keepAuxStoreInfo
   231  	DiscardAuxStoreInfo  = discardAuxStoreInfo
   232  )
   233  
   234  type AuxStoreInfo = auxStoreInfo
   235  
   236  // link, misc handlers
   237  var (
   238  	MissingDisabledServices = missingDisabledServices
   239  )
   240  
   241  func (m *SnapManager) MaybeUndoRemodelBootChanges(t *state.Task) error {
   242  	return m.maybeUndoRemodelBootChanges(t)
   243  }
   244  
   245  func MockPidsOfSnap(f func(instanceName string) (map[string][]int, error)) func() {
   246  	old := pidsOfSnap
   247  	pidsOfSnap = f
   248  	return func() {
   249  		pidsOfSnap = old
   250  	}
   251  }
   252  
   253  func MockCurrentSnaps(f func(st *state.State) ([]*store.CurrentSnap, error)) func() {
   254  	old := currentSnaps
   255  	currentSnaps = f
   256  	return func() {
   257  		currentSnaps = old
   258  	}
   259  }
   260  
   261  func MockInstallSize(f func(st *state.State, snaps []*snap.Info, userID int) (uint64, error)) func() {
   262  	old := installSize
   263  	installSize = f
   264  	return func() {
   265  		installSize = old
   266  	}
   267  }
   268  
   269  func MockGenerateSnapdWrappers(f func(snapInfo *snap.Info) error) func() {
   270  	old := generateSnapdWrappers
   271  	generateSnapdWrappers = f
   272  	return func() {
   273  		generateSnapdWrappers = old
   274  	}
   275  }
   276  
   277  var (
   278  	NotifyLinkParticipants = notifyLinkParticipants
   279  )
   280  
   281  // autorefresh
   282  var (
   283  	InhibitRefresh = inhibitRefresh
   284  	MaxInhibition  = maxInhibition
   285  )
   286  
   287  func NewBusySnapError(info *snap.Info, pids []int, busyAppNames, busyHookNames []string) *BusySnapError {
   288  	return &BusySnapError{
   289  		SnapInfo:      info,
   290  		pids:          pids,
   291  		busyAppNames:  busyAppNames,
   292  		busyHookNames: busyHookNames,
   293  	}
   294  }
   295  
   296  func MockGenericRefreshCheck(fn func(info *snap.Info, canAppRunDuringRefresh func(app *snap.AppInfo) bool) error) (restore func()) {
   297  	old := genericRefreshCheck
   298  	genericRefreshCheck = fn
   299  	return func() { genericRefreshCheck = old }
   300  }
   301  
   302  func (m *autoRefresh) EnsureRefreshHoldAtLeast(d time.Duration) error {
   303  	return m.ensureRefreshHoldAtLeast(d)
   304  }