github.com/ethanhsieh/snapd@v0.0.0-20210615102523-3db9b8e4edc5/daemon/export_api_snapshots_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 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 daemon
    21  
    22  import (
    23  	"context"
    24  	"encoding/json"
    25  	"io"
    26  
    27  	"gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/client"
    30  	"github.com/snapcore/snapd/overlord/snapshotstate"
    31  	"github.com/snapcore/snapd/overlord/state"
    32  )
    33  
    34  func MockSnapshotSave(newSave func(*state.State, []string, []string) (uint64, []string, *state.TaskSet, error)) (restore func()) {
    35  	oldSave := snapshotSave
    36  	snapshotSave = newSave
    37  	return func() {
    38  		snapshotSave = oldSave
    39  	}
    40  }
    41  
    42  func MockSnapshotList(newList func(context.Context, *state.State, uint64, []string) ([]client.SnapshotSet, error)) (restore func()) {
    43  	oldList := snapshotList
    44  	snapshotList = newList
    45  	return func() {
    46  		snapshotList = oldList
    47  	}
    48  }
    49  
    50  func MockSnapshotExport(newExport func(context.Context, *state.State, uint64) (*snapshotstate.SnapshotExport, error)) (restore func()) {
    51  	oldExport := snapshotExport
    52  	snapshotExport = newExport
    53  	return func() {
    54  		snapshotExport = oldExport
    55  	}
    56  }
    57  
    58  func MockSnapshotCheck(newCheck func(*state.State, uint64, []string, []string) ([]string, *state.TaskSet, error)) (restore func()) {
    59  	oldCheck := snapshotCheck
    60  	snapshotCheck = newCheck
    61  	return func() {
    62  		snapshotCheck = oldCheck
    63  	}
    64  }
    65  
    66  func MockSnapshotRestore(newRestore func(*state.State, uint64, []string, []string) ([]string, *state.TaskSet, error)) (restore func()) {
    67  	oldRestore := snapshotRestore
    68  	snapshotRestore = newRestore
    69  	return func() {
    70  		snapshotRestore = oldRestore
    71  	}
    72  }
    73  
    74  func MockSnapshotForget(newForget func(*state.State, uint64, []string) ([]string, *state.TaskSet, error)) (restore func()) {
    75  	oldForget := snapshotForget
    76  	snapshotForget = newForget
    77  	return func() {
    78  		snapshotForget = oldForget
    79  	}
    80  }
    81  
    82  func MockSnapshotImport(newImport func(context.Context, *state.State, io.Reader) (uint64, []string, error)) (restore func()) {
    83  	oldImport := snapshotImport
    84  	snapshotImport = newImport
    85  	return func() {
    86  		snapshotImport = oldImport
    87  	}
    88  }
    89  
    90  func MustUnmarshalSnapInstruction(c *check.C, jinst string) *snapInstruction {
    91  	var inst snapInstruction
    92  	if err := json.Unmarshal([]byte(jinst), &inst); err != nil {
    93  		c.Fatalf("cannot unmarshal %q into snapInstruction: %v", jinst, err)
    94  	}
    95  	return &inst
    96  }
    97  
    98  func MustUnmarshalSnapshotAction(c *check.C, jact string) *snapshotAction {
    99  	var act snapshotAction
   100  	if err := json.Unmarshal([]byte(jact), &act); err != nil {
   101  		c.Fatalf("cannot unmarshal %q into snapshotAction: %v", jact, err)
   102  	}
   103  	return &act
   104  }
   105  
   106  type SnapshotExportResponse = snapshotExportResponse