github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/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  	CheckSnapshotConflict      = checkSnapshotConflict
    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  	SetSnapshotOpInProgress = setSnapshotOpInProgress
    52  
    53  	DefaultAutomaticSnapshotExpiration = defaultAutomaticSnapshotExpiration
    54  )
    55  
    56  func (summaries snapshotSnapSummaries) AsMaps() []map[string]string {
    57  	out := make([]map[string]string, len(summaries))
    58  	for i, summary := range summaries {
    59  		out[i] = map[string]string{
    60  			"snap":     summary.snap,
    61  			"snapID":   summary.snapID,
    62  			"filename": summary.filename,
    63  			"epoch":    summary.epoch.String(),
    64  		}
    65  	}
    66  	return out
    67  }
    68  
    69  func MockOsRemove(f func(string) error) (restore func()) {
    70  	old := osRemove
    71  	osRemove = f
    72  	return func() {
    73  		osRemove = old
    74  	}
    75  }
    76  
    77  func MockSnapstateAll(f func(*state.State) (map[string]*snapstate.SnapState, error)) (restore func()) {
    78  	old := snapstateAll
    79  	snapstateAll = f
    80  	return func() {
    81  		snapstateAll = old
    82  	}
    83  }
    84  
    85  func MockSnapstateCurrentInfo(f func(*state.State, string) (*snap.Info, error)) (restore func()) {
    86  	old := snapstateCurrentInfo
    87  	snapstateCurrentInfo = f
    88  	return func() {
    89  		snapstateCurrentInfo = old
    90  	}
    91  }
    92  
    93  func MockSnapstateCheckChangeConflictMany(f func(*state.State, []string, string) error) (restore func()) {
    94  	old := snapstateCheckChangeConflictMany
    95  	snapstateCheckChangeConflictMany = f
    96  	return func() {
    97  		snapstateCheckChangeConflictMany = old
    98  	}
    99  }
   100  
   101  func MockBackendIter(f func(context.Context, func(*backend.Reader) error) error) (restore func()) {
   102  	old := backendIter
   103  	backendIter = f
   104  	return func() {
   105  		backendIter = old
   106  	}
   107  }
   108  
   109  func MockBackendOpen(f func(string, uint64) (*backend.Reader, error)) (restore func()) {
   110  	old := backendOpen
   111  	backendOpen = f
   112  	return func() {
   113  		backendOpen = old
   114  	}
   115  }
   116  
   117  func MockBackendList(f func(ctx context.Context, setID uint64, snapNames []string) ([]client.SnapshotSet, error)) (restore func()) {
   118  	old := backendList
   119  	backendList = f
   120  	return func() {
   121  		backendList = old
   122  	}
   123  }
   124  
   125  func MockBackendRestore(f func(*backend.Reader, context.Context, snap.Revision, []string, backend.Logf) (*backend.RestoreState, error)) (restore func()) {
   126  	old := backendRestore
   127  	backendRestore = f
   128  	return func() {
   129  		backendRestore = old
   130  	}
   131  }
   132  
   133  func MockBackendCheck(f func(*backend.Reader, context.Context, []string) error) (restore func()) {
   134  	old := backendCheck
   135  	backendCheck = f
   136  	return func() {
   137  		backendCheck = old
   138  	}
   139  }
   140  
   141  func MockBackendRevert(f func(*backend.RestoreState)) (restore func()) {
   142  	old := backendRevert
   143  	backendRevert = f
   144  	return func() {
   145  		backendRevert = old
   146  	}
   147  }
   148  
   149  func MockBackendCleanup(f func(*backend.RestoreState)) (restore func()) {
   150  	old := backendCleanup
   151  	backendCleanup = f
   152  	return func() {
   153  		backendCleanup = old
   154  	}
   155  }
   156  
   157  func MockBackendImport(f func(context.Context, uint64, io.Reader, *backend.ImportFlags) ([]string, error)) (restore func()) {
   158  	old := backendImport
   159  	backendImport = f
   160  	return func() {
   161  		backendImport = old
   162  	}
   163  }
   164  
   165  func MockBackenCleanupAbandondedImports(f func() (int, error)) (restore func()) {
   166  	old := backendCleanupAbandondedImports
   167  	backendCleanupAbandondedImports = f
   168  	return func() {
   169  		backendCleanupAbandondedImports = old
   170  	}
   171  }
   172  
   173  func MockBackendEstimateSnapshotSize(f func(*snap.Info, []string) (uint64, error)) (restore func()) {
   174  	old := backendEstimateSnapshotSize
   175  	backendEstimateSnapshotSize = f
   176  	return func() {
   177  		backendEstimateSnapshotSize = old
   178  	}
   179  }
   180  
   181  func MockBackendNewSnapshotExport(f func(ctx context.Context, setID uint64) (se *SnapshotExport, err error)) (restore func()) {
   182  	old := backendNewSnapshotExport
   183  	backendNewSnapshotExport = f
   184  	return func() {
   185  		backendNewSnapshotExport = old
   186  	}
   187  }
   188  
   189  func MockConfigGetSnapConfig(f func(*state.State, string) (*json.RawMessage, error)) (restore func()) {
   190  	old := configGetSnapConfig
   191  	configGetSnapConfig = f
   192  	return func() {
   193  		configGetSnapConfig = old
   194  	}
   195  }
   196  
   197  func MockConfigSetSnapConfig(f func(*state.State, string, *json.RawMessage) error) (restore func()) {
   198  	old := configSetSnapConfig
   199  	configSetSnapConfig = f
   200  	return func() {
   201  		configSetSnapConfig = old
   202  	}
   203  }
   204  
   205  // For testing only
   206  func SetLastForgetExpiredSnapshotTime(mgr *SnapshotManager, t time.Time) {
   207  	mgr.lastForgetExpiredSnapshotTime = t
   208  }