github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/overlord/snapshotstate/export_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 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 snapshotstate
    21  
    22  import (
    23  	"context"
    24  	"encoding/json"
    25  	"io"
    26  	"time"
    27  
    28  	"github.com/snapcore/snapd/client"
    29  	"github.com/snapcore/snapd/overlord/snapshotstate/backend"
    30  	"github.com/snapcore/snapd/overlord/snapstate"
    31  	"github.com/snapcore/snapd/overlord/state"
    32  	"github.com/snapcore/snapd/snap"
    33  )
    34  
    35  var (
    36  	NewSnapshotSetID           = newSnapshotSetID
    37  	AllActiveSnapNames         = allActiveSnapNames
    38  	SnapSummariesInSnapshotSet = snapSummariesInSnapshotSet
    39  	CheckSnapshotTaskConflict  = checkSnapshotTaskConflict
    40  	Filename                   = filename
    41  	DoSave                     = doSave
    42  	DoRestore                  = doRestore
    43  	UndoRestore                = undoRestore
    44  	CleanupRestore             = cleanupRestore
    45  	DoCheck                    = doCheck
    46  	DoForget                   = doForget
    47  	SaveExpiration             = saveExpiration
    48  	ExpiredSnapshotSets        = expiredSnapshotSets
    49  	RemoveSnapshotState        = removeSnapshotState
    50  
    51  	DefaultAutomaticSnapshotExpiration = defaultAutomaticSnapshotExpiration
    52  )
    53  
    54  func (summaries snapshotSnapSummaries) AsMaps() []map[string]string {
    55  	out := make([]map[string]string, len(summaries))
    56  	for i, summary := range summaries {
    57  		out[i] = map[string]string{
    58  			"snap":     summary.snap,
    59  			"snapID":   summary.snapID,
    60  			"filename": summary.filename,
    61  			"epoch":    summary.epoch.String(),
    62  		}
    63  	}
    64  	return out
    65  }
    66  
    67  func MockOsRemove(f func(string) error) (restore func()) {
    68  	old := osRemove
    69  	osRemove = f
    70  	return func() {
    71  		osRemove = old
    72  	}
    73  }
    74  
    75  func MockSnapstateAll(f func(*state.State) (map[string]*snapstate.SnapState, error)) (restore func()) {
    76  	old := snapstateAll
    77  	snapstateAll = f
    78  	return func() {
    79  		snapstateAll = old
    80  	}
    81  }
    82  
    83  func MockSnapstateCurrentInfo(f func(*state.State, string) (*snap.Info, error)) (restore func()) {
    84  	old := snapstateCurrentInfo
    85  	snapstateCurrentInfo = f
    86  	return func() {
    87  		snapstateCurrentInfo = old
    88  	}
    89  }
    90  
    91  func MockSnapstateCheckChangeConflictMany(f func(*state.State, []string, string) error) (restore func()) {
    92  	old := snapstateCheckChangeConflictMany
    93  	snapstateCheckChangeConflictMany = f
    94  	return func() {
    95  		snapstateCheckChangeConflictMany = old
    96  	}
    97  }
    98  
    99  func MockBackendIter(f func(context.Context, func(*backend.Reader) error) error) (restore func()) {
   100  	old := backendIter
   101  	backendIter = f
   102  	return func() {
   103  		backendIter = old
   104  	}
   105  }
   106  
   107  func MockBackendOpen(f func(string, uint64) (*backend.Reader, error)) (restore func()) {
   108  	old := backendOpen
   109  	backendOpen = f
   110  	return func() {
   111  		backendOpen = old
   112  	}
   113  }
   114  
   115  func MockBackendList(f func(ctx context.Context, setID uint64, snapNames []string) ([]client.SnapshotSet, error)) (restore func()) {
   116  	old := backendList
   117  	backendList = f
   118  	return func() {
   119  		backendList = old
   120  	}
   121  }
   122  
   123  func MockBackendRestore(f func(*backend.Reader, context.Context, snap.Revision, []string, backend.Logf) (*backend.RestoreState, error)) (restore func()) {
   124  	old := backendRestore
   125  	backendRestore = f
   126  	return func() {
   127  		backendRestore = old
   128  	}
   129  }
   130  
   131  func MockBackendCheck(f func(*backend.Reader, context.Context, []string) error) (restore func()) {
   132  	old := backendCheck
   133  	backendCheck = f
   134  	return func() {
   135  		backendCheck = old
   136  	}
   137  }
   138  
   139  func MockBackendRevert(f func(*backend.RestoreState)) (restore func()) {
   140  	old := backendRevert
   141  	backendRevert = f
   142  	return func() {
   143  		backendRevert = old
   144  	}
   145  }
   146  
   147  func MockBackendCleanup(f func(*backend.RestoreState)) (restore func()) {
   148  	old := backendCleanup
   149  	backendCleanup = f
   150  	return func() {
   151  		backendCleanup = old
   152  	}
   153  }
   154  
   155  func MockBackendImport(f func(context.Context, uint64, io.Reader) ([]string, error)) (restore func()) {
   156  	old := backendImport
   157  	backendImport = f
   158  	return func() {
   159  		backendImport = old
   160  	}
   161  }
   162  
   163  func MockBackenCleanupAbandondedImports(f func() (int, error)) (restore func()) {
   164  	old := backendCleanupAbandondedImports
   165  	backendCleanupAbandondedImports = f
   166  	return func() {
   167  		backendCleanupAbandondedImports = old
   168  	}
   169  }
   170  
   171  func MockBackendEstimateSnapshotSize(f func(*snap.Info, []string) (uint64, error)) (restore func()) {
   172  	old := backendEstimateSnapshotSize
   173  	backendEstimateSnapshotSize = f
   174  	return func() {
   175  		backendEstimateSnapshotSize = old
   176  	}
   177  }
   178  
   179  func MockConfigGetSnapConfig(f func(*state.State, string) (*json.RawMessage, error)) (restore func()) {
   180  	old := configGetSnapConfig
   181  	configGetSnapConfig = f
   182  	return func() {
   183  		configGetSnapConfig = old
   184  	}
   185  }
   186  
   187  func MockConfigSetSnapConfig(f func(*state.State, string, *json.RawMessage) error) (restore func()) {
   188  	old := configSetSnapConfig
   189  	configSetSnapConfig = f
   190  	return func() {
   191  		configSetSnapConfig = old
   192  	}
   193  }
   194  
   195  // For testing only
   196  func SetLastForgetExpiredSnapshotTime(mgr *SnapshotManager, t time.Time) {
   197  	mgr.lastForgetExpiredSnapshotTime = t
   198  }