github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/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 "net/http" 26 27 "gopkg.in/check.v1" 28 29 "github.com/snapcore/snapd/client" 30 "github.com/snapcore/snapd/overlord/auth" 31 "github.com/snapcore/snapd/overlord/snapshotstate" 32 "github.com/snapcore/snapd/overlord/state" 33 ) 34 35 func MockSnapshotSave(newSave func(*state.State, []string, []string) (uint64, []string, *state.TaskSet, error)) (restore func()) { 36 oldSave := snapshotSave 37 snapshotSave = newSave 38 return func() { 39 snapshotSave = oldSave 40 } 41 } 42 43 func MockSnapshotList(newList func(context.Context, uint64, []string) ([]client.SnapshotSet, error)) (restore func()) { 44 oldList := snapshotList 45 snapshotList = newList 46 return func() { 47 snapshotList = oldList 48 } 49 } 50 51 func MockSnapshotExport(newExport func(context.Context, uint64) (*snapshotstate.SnapshotExport, error)) (restore func()) { 52 oldExport := snapshotExport 53 snapshotExport = newExport 54 return func() { 55 snapshotExport = oldExport 56 } 57 } 58 59 func MockSnapshotCheck(newCheck func(*state.State, uint64, []string, []string) ([]string, *state.TaskSet, error)) (restore func()) { 60 oldCheck := snapshotCheck 61 snapshotCheck = newCheck 62 return func() { 63 snapshotCheck = oldCheck 64 } 65 } 66 67 func MockSnapshotRestore(newRestore func(*state.State, uint64, []string, []string) ([]string, *state.TaskSet, error)) (restore func()) { 68 oldRestore := snapshotRestore 69 snapshotRestore = newRestore 70 return func() { 71 snapshotRestore = oldRestore 72 } 73 } 74 75 func MockSnapshotForget(newForget func(*state.State, uint64, []string) ([]string, *state.TaskSet, error)) (restore func()) { 76 oldForget := snapshotForget 77 snapshotForget = newForget 78 return func() { 79 snapshotForget = oldForget 80 } 81 } 82 83 func MustUnmarshalSnapInstruction(c *check.C, jinst string) *snapInstruction { 84 var inst snapInstruction 85 if err := json.Unmarshal([]byte(jinst), &inst); err != nil { 86 c.Fatalf("cannot unmarshal %q into snapInstruction: %v", jinst, err) 87 } 88 return &inst 89 } 90 91 func MustUnmarshalSnapshotAction(c *check.C, jact string) *snapshotAction { 92 var act snapshotAction 93 if err := json.Unmarshal([]byte(jact), &act); err != nil { 94 c.Fatalf("cannot unmarshal %q into snapshotAction: %v", jact, err) 95 } 96 return &act 97 } 98 99 func (rsp *resp) ErrorResult() *errorResult { 100 return rsp.Result.(*errorResult) 101 } 102 103 func ListSnapshots(c *Command, r *http.Request, user *auth.UserState) *resp { 104 return listSnapshots(c, r, user).(*resp) 105 } 106 107 func ChangeSnapshots(c *Command, r *http.Request, user *auth.UserState) *resp { 108 return changeSnapshots(c, r, user).(*resp) 109 } 110 111 func ExportSnapshot(c *Command, r *http.Request, user *auth.UserState) interface{} { 112 return getSnapshotExport(c, r, user) 113 } 114 115 var ( 116 SnapshotMany = snapshotMany 117 SnapshotCmd = snapshotCmd 118 SnapshotExportCmd = snapshotExportCmd 119 ) 120 121 type SnapshotExportResponse = snapshotExportResponse