github.com/rigado/snapd@v2.42.5-go-mod+incompatible/overlord/devicestate/export_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016-2018 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 devicestate
    21  
    22  import (
    23  	"context"
    24  	"time"
    25  
    26  	"github.com/snapcore/snapd/asserts"
    27  	"github.com/snapcore/snapd/gadget"
    28  	"github.com/snapcore/snapd/overlord/snapstate"
    29  	"github.com/snapcore/snapd/overlord/state"
    30  	"github.com/snapcore/snapd/overlord/storecontext"
    31  	"github.com/snapcore/snapd/timings"
    32  )
    33  
    34  func MockKeyLength(n int) (restore func()) {
    35  	if n < 1024 {
    36  		panic("key length must be >= 1024")
    37  	}
    38  
    39  	oldKeyLength := keyLength
    40  	keyLength = n
    41  	return func() {
    42  		keyLength = oldKeyLength
    43  	}
    44  }
    45  
    46  func MockBaseStoreURL(url string) (restore func()) {
    47  	oldURL := baseStoreURL
    48  	baseStoreURL = mustParse(url).ResolveReference(authRef)
    49  	return func() {
    50  		baseStoreURL = oldURL
    51  	}
    52  }
    53  
    54  func MockRetryInterval(interval time.Duration) (restore func()) {
    55  	old := retryInterval
    56  	retryInterval = interval
    57  	return func() {
    58  		retryInterval = old
    59  	}
    60  }
    61  
    62  func MockMaxTentatives(max int) (restore func()) {
    63  	old := maxTentatives
    64  	maxTentatives = max
    65  	return func() {
    66  		maxTentatives = old
    67  	}
    68  }
    69  
    70  func KeypairManager(m *DeviceManager) asserts.KeypairManager {
    71  	return m.keypairMgr
    72  }
    73  
    74  func EnsureOperationalShouldBackoff(m *DeviceManager, now time.Time) bool {
    75  	return m.ensureOperationalShouldBackoff(now)
    76  }
    77  
    78  func BecomeOperationalBackoff(m *DeviceManager) time.Duration {
    79  	return m.becomeOperationalBackoff
    80  }
    81  
    82  func SetLastBecomeOperationalAttempt(m *DeviceManager, t time.Time) {
    83  	m.lastBecomeOperationalAttempt = t
    84  }
    85  
    86  func MockRepeatRequestSerial(label string) (restore func()) {
    87  	old := repeatRequestSerial
    88  	repeatRequestSerial = label
    89  	return func() {
    90  		repeatRequestSerial = old
    91  	}
    92  }
    93  
    94  func MockSnapstateInstallWithDeviceContext(f func(ctx context.Context, st *state.State, name string, opts *snapstate.RevisionOptions, userID int, flags snapstate.Flags, deviceCtx snapstate.DeviceContext, fromChange string) (*state.TaskSet, error)) (restore func()) {
    95  	old := snapstateInstallWithDeviceContext
    96  	snapstateInstallWithDeviceContext = f
    97  	return func() {
    98  		snapstateInstallWithDeviceContext = old
    99  	}
   100  }
   101  
   102  func MockSnapstateUpdateWithDeviceContext(f func(st *state.State, name string, opts *snapstate.RevisionOptions, userID int, flags snapstate.Flags, deviceCtx snapstate.DeviceContext, fromChange string) (*state.TaskSet, error)) (restore func()) {
   103  	old := snapstateUpdateWithDeviceContext
   104  	snapstateUpdateWithDeviceContext = f
   105  	return func() {
   106  		snapstateUpdateWithDeviceContext = old
   107  	}
   108  }
   109  
   110  func EnsureSeedYaml(m *DeviceManager) error {
   111  	return m.ensureSeedYaml()
   112  }
   113  
   114  var PopulateStateFromSeedImpl = populateStateFromSeedImpl
   115  
   116  func MockPopulateStateFromSeed(f func(*state.State, timings.Measurer) ([]*state.TaskSet, error)) (restore func()) {
   117  	old := populateStateFromSeed
   118  	populateStateFromSeed = f
   119  	return func() {
   120  		populateStateFromSeed = old
   121  	}
   122  }
   123  
   124  func EnsureBootOk(m *DeviceManager) error {
   125  	return m.ensureBootOk()
   126  }
   127  
   128  func SetBootOkRan(m *DeviceManager, b bool) {
   129  	m.bootOkRan = b
   130  }
   131  
   132  type (
   133  	RegistrationContext = registrationContext
   134  	RemodelContext      = remodelContext
   135  )
   136  
   137  func RegistrationCtx(m *DeviceManager, t *state.Task) (registrationContext, error) {
   138  	return m.registrationCtx(t)
   139  }
   140  
   141  func RemodelDeviceBackend(remodCtx remodelContext) storecontext.DeviceBackend {
   142  	return remodCtx.(interface {
   143  		deviceBackend() storecontext.DeviceBackend
   144  	}).deviceBackend()
   145  }
   146  
   147  var (
   148  	ImportAssertionsFromSeed = importAssertionsFromSeed
   149  	CheckGadgetOrKernel      = checkGadgetOrKernel
   150  	CanAutoRefresh           = canAutoRefresh
   151  	NewEnoughProxy           = newEnoughProxy
   152  
   153  	IncEnsureOperationalAttempts = incEnsureOperationalAttempts
   154  	EnsureOperationalAttempts    = ensureOperationalAttempts
   155  
   156  	RemodelTasks = remodelTasks
   157  
   158  	RemodelCtx        = remodelCtx
   159  	CleanupRemodelCtx = cleanupRemodelCtx
   160  	CachedRemodelCtx  = cachedRemodelCtx
   161  
   162  	GadgetUpdateBlocked    = gadgetUpdateBlocked
   163  	GadgetCurrentAndUpdate = gadgetCurrentAndUpdate
   164  )
   165  
   166  func MockGadgetUpdate(mock func(current, update gadget.GadgetData, path string) error) (restore func()) {
   167  	old := gadgetUpdate
   168  	gadgetUpdate = mock
   169  	return func() {
   170  		gadgetUpdate = old
   171  	}
   172  }