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