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