gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/overlord/export_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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 overlord
    21  
    22  import (
    23  	"time"
    24  
    25  	"github.com/snapcore/snapd/overlord/configstate"
    26  	"github.com/snapcore/snapd/overlord/hookstate"
    27  	"github.com/snapcore/snapd/overlord/snapstate"
    28  	"github.com/snapcore/snapd/overlord/state"
    29  	"github.com/snapcore/snapd/overlord/storecontext"
    30  	"github.com/snapcore/snapd/store"
    31  )
    32  
    33  // MockEnsureInterval sets the overlord ensure interval for tests.
    34  func MockEnsureInterval(d time.Duration) (restore func()) {
    35  	old := ensureInterval
    36  	ensureInterval = d
    37  	return func() { ensureInterval = old }
    38  }
    39  
    40  // MockPruneInterval sets the overlord prune interval for tests.
    41  func MockPruneInterval(prunei, prunew, abortw time.Duration) (restore func()) {
    42  	oldPruneInterval := pruneInterval
    43  	oldPruneWait := pruneWait
    44  	oldAbortWait := abortWait
    45  	pruneInterval = prunei
    46  	pruneWait = prunew
    47  	abortWait = abortw
    48  	return func() {
    49  		pruneInterval = oldPruneInterval
    50  		pruneWait = oldPruneWait
    51  		abortWait = oldAbortWait
    52  	}
    53  }
    54  
    55  func MockPruneTicker(f func(t *time.Ticker) <-chan time.Time) (restore func()) {
    56  	old := pruneTickerC
    57  	pruneTickerC = f
    58  	return func() {
    59  		pruneTickerC = old
    60  	}
    61  }
    62  
    63  // MockEnsureNext sets o.ensureNext for tests.
    64  func MockEnsureNext(o *Overlord, t time.Time) {
    65  	o.ensureNext = t
    66  }
    67  
    68  // Engine exposes the state engine in an Overlord for tests.
    69  func (o *Overlord) Engine() *StateEngine {
    70  	return o.stateEng
    71  }
    72  
    73  // NewStore exposes newStore.
    74  func (o *Overlord) NewStore(devBE storecontext.DeviceBackend) snapstate.StoreService {
    75  	return o.newStore(devBE)
    76  }
    77  
    78  // MockStoreNew mocks store.New as called by overlord.New.
    79  func MockStoreNew(new func(*store.Config, store.DeviceAndAuthContext) *store.Store) (restore func()) {
    80  	storeNew = new
    81  	return func() {
    82  		storeNew = store.New
    83  	}
    84  }
    85  
    86  func MockConfigstateInit(new func(*state.State, *hookstate.HookManager) error) (restore func()) {
    87  	configstateInit = new
    88  	return func() {
    89  		configstateInit = configstate.Init
    90  	}
    91  }
    92  
    93  func MockPreseedExitWithError(f func(err error)) (restore func()) {
    94  	old := preseedExitWithError
    95  	preseedExitWithError = f
    96  	return func() {
    97  		preseedExitWithError = old
    98  	}
    99  }