github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/daemon/export_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018-2020 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 "net/http" 25 "time" 26 27 "github.com/gorilla/mux" 28 29 "github.com/snapcore/snapd/overlord" 30 "github.com/snapcore/snapd/overlord/snapstate" 31 "github.com/snapcore/snapd/overlord/state" 32 "github.com/snapcore/snapd/snap" 33 ) 34 35 var MinLane = minLane 36 37 func NewAndAddRoutes() (*Daemon, error) { 38 d, err := New() 39 if err != nil { 40 return nil, err 41 } 42 d.addRoutes() 43 return d, nil 44 } 45 46 func NewWithOverlord(o *overlord.Overlord) *Daemon { 47 d := &Daemon{overlord: o, state: o.State()} 48 d.addRoutes() 49 return d 50 } 51 52 func (d *Daemon) RouterMatch(req *http.Request, m *mux.RouteMatch) bool { 53 return d.router.Match(req, m) 54 } 55 56 func (d *Daemon) Overlord() *overlord.Overlord { 57 return d.overlord 58 } 59 60 func (d *Daemon) RequestedRestart() state.RestartType { 61 return d.requestedRestart 62 } 63 64 func MockUcrednetGet(mock func(remoteAddr string) (pid int32, uid uint32, socket string, err error)) (restore func()) { 65 oldUcrednetGet := ucrednetGet 66 ucrednetGet = mock 67 return func() { 68 ucrednetGet = oldUcrednetGet 69 } 70 } 71 72 func MockEnsureStateSoon(mock func(*state.State)) (original func(*state.State), restore func()) { 73 oldEnsureStateSoon := ensureStateSoon 74 ensureStateSoon = mock 75 return ensureStateSoonImpl, func() { 76 ensureStateSoon = oldEnsureStateSoon 77 } 78 } 79 80 func MockMuxVars(vars func(*http.Request) map[string]string) (restore func()) { 81 old := muxVars 82 muxVars = vars 83 return func() { 84 muxVars = old 85 } 86 } 87 88 func MockShutdownTimeout(tm time.Duration) (restore func()) { 89 old := shutdownTimeout 90 shutdownTimeout = tm 91 return func() { 92 shutdownTimeout = old 93 } 94 } 95 96 func MockUnsafeReadSnapInfo(mock func(string) (*snap.Info, error)) (restore func()) { 97 oldUnsafeReadSnapInfo := unsafeReadSnapInfo 98 unsafeReadSnapInfo = mock 99 return func() { 100 unsafeReadSnapInfo = oldUnsafeReadSnapInfo 101 } 102 } 103 104 func MockAssertstateRefreshSnapDeclarations(mock func(*state.State, int) error) (restore func()) { 105 oldAssertstateRefreshSnapDeclarations := assertstateRefreshSnapDeclarations 106 assertstateRefreshSnapDeclarations = mock 107 return func() { 108 assertstateRefreshSnapDeclarations = oldAssertstateRefreshSnapDeclarations 109 } 110 } 111 112 func MockSnapstateInstall(mock func(context.Context, *state.State, string, *snapstate.RevisionOptions, int, snapstate.Flags) (*state.TaskSet, error)) (restore func()) { 113 oldSnapstateInstall := snapstateInstall 114 snapstateInstall = mock 115 return func() { 116 snapstateInstall = oldSnapstateInstall 117 } 118 } 119 120 func MockSnapstateInstallPath(mock func(*state.State, *snap.SideInfo, string, string, string, snapstate.Flags) (*state.TaskSet, *snap.Info, error)) (restore func()) { 121 oldSnapstateInstallPath := snapstateInstallPath 122 snapstateInstallPath = mock 123 return func() { 124 snapstateInstallPath = oldSnapstateInstallPath 125 } 126 } 127 128 func MockSnapstateTryPath(mock func(*state.State, string, string, snapstate.Flags) (*state.TaskSet, error)) (restore func()) { 129 oldSnapstateTryPath := snapstateTryPath 130 snapstateTryPath = mock 131 return func() { 132 snapstateTryPath = oldSnapstateTryPath 133 } 134 } 135 136 func MockSnapstateInstallMany(mock func(*state.State, []string, int) ([]string, []*state.TaskSet, error)) (restore func()) { 137 oldSnapstateInstallMany := snapstateInstallMany 138 snapstateInstallMany = mock 139 return func() { 140 snapstateInstallMany = oldSnapstateInstallMany 141 } 142 } 143 144 func MockSnapstateUpdateMany(mock func(context.Context, *state.State, []string, int, *snapstate.Flags) ([]string, []*state.TaskSet, error)) (restore func()) { 145 oldSnapstateUpdateMany := snapstateUpdateMany 146 snapstateUpdateMany = mock 147 return func() { 148 snapstateUpdateMany = oldSnapstateUpdateMany 149 } 150 } 151 152 func MockSnapstateRemoveMany(mock func(*state.State, []string) ([]string, []*state.TaskSet, error)) (restore func()) { 153 oldSnapstateRemoveMany := snapstateRemoveMany 154 snapstateRemoveMany = mock 155 return func() { 156 snapstateRemoveMany = oldSnapstateRemoveMany 157 } 158 } 159 160 type ( 161 Resp = resp 162 ErrorResult = errorResult 163 SnapInstruction = snapInstruction 164 ) 165 166 func (inst *snapInstruction) Dispatch() snapActionFunc { 167 return inst.dispatch() 168 } 169 170 func (inst *snapInstruction) DispatchForMany() snapsActionFunc { 171 return inst.dispatchForMany() 172 }