1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package ctlcmd
import (
"context"
"errors"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/asserts/snapasserts"
"github.com/snapcore/snapd/client/clientutil"
"github.com/snapcore/snapd/confdb"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/overlord/confdbstate"
"github.com/snapcore/snapd/overlord/devicestate"
"github.com/snapcore/snapd/overlord/hookstate"
"github.com/snapcore/snapd/overlord/servicestate"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/testutil"
)
const (
NotASnapCode = notASnapCode
ClassicSnapCode = classicSnapCode
)
var (
AttributesTask = attributesTask
KmodCheckConnection = kmodCheckConnection
KmodMatchConnection = kmodMatchConnection
)
type KmodCommand = kmodCommand
func MockKmodCheckConnection(f func(*hookstate.Context, string, []string) error) (restore func()) {
r := testutil.Backup(&kmodCheckConnection)
kmodCheckConnection = f
return r
}
func MockKmodLoadModule(f func(string, []string) error) (restore func()) {
r := testutil.Backup(&kmodLoadModule)
kmodLoadModule = f
return r
}
func MockKmodUnloadModule(f func(string) error) (restore func()) {
r := testutil.Backup(&kmodUnloadModule)
kmodUnloadModule = f
return r
}
func MockServicestateControlFunc(f func(*state.State, []*snap.AppInfo, *servicestate.Instruction, *user.User, *servicestate.Flags, *hookstate.Context) ([]*state.TaskSet, error)) (restore func()) {
old := servicestateControl
servicestateControl = f
return func() { servicestateControl = old }
}
func MockSnapstateInstallComponentsFunc(f func(ctx context.Context, st *state.State, names []string, info *snap.Info, vsets *snapasserts.ValidationSets, opts snapstate.Options) ([]*state.TaskSet, error)) (restore func()) {
old := snapstateInstallComponents
snapstateInstallComponents = f
return func() { snapstateInstallComponents = old }
}
func MockSnapstateRemoveComponentsFunc(f func(st *state.State, snapName string, compName []string, opts snapstate.RemoveComponentsOpts) ([]*state.TaskSet, error)) (restore func()) {
old := snapstateRemoveComponents
snapstateRemoveComponents = f
return func() { snapstateRemoveComponents = old }
}
func MockDevicestateSystemModeInfoFromState(f func(*state.State) (*devicestate.SystemModeInfo, error)) (restore func()) {
old := devicestateSystemModeInfoFromState
devicestateSystemModeInfoFromState = f
return func() { devicestateSystemModeInfoFromState = old }
}
func MockFdestateSystemEncryptedFromState(f func(*state.State) (bool, error)) (restore func()) {
old := fdestateSystemEncryptedFromState
fdestateSystemEncryptedFromState = f
return func() { fdestateSystemEncryptedFromState = old }
}
func AddMockCommand(name string) *MockCommand {
return addMockCmd(name, false)
}
func AddHiddenMockCommand(name string) *MockCommand {
return addMockCmd(name, true)
}
func addMockCmd(name string, hidden bool) *MockCommand {
mockCommand := NewMockCommand()
cmd := addCommand(name, "", "", func() command { return mockCommand })
cmd.hidden = hidden
return mockCommand
}
func RemoveCommand(name string) {
delete(commands, name)
}
func FormatLongPublisher(snapInfo *snap.Info, storeAccountID string) string {
var mf modelCommandFormatter
mf.snapInfo = snapInfo
return mf.LongPublisher(storeAccountID)
}
func FindSerialAssertion(st *state.State, modelAssertion *asserts.Model) (*asserts.Serial, error) {
var mc modelCommand
return mc.findSerialAssertion(st, modelAssertion)
}
type MockCommand struct {
baseCommand
ExecuteError bool
FakeStdout string
FakeStderr string
Args []string
}
func NewMockCommand() *MockCommand {
return &MockCommand{
ExecuteError: false,
}
}
func (c *MockCommand) Execute(args []string) error {
c.Args = args
if c.FakeStdout != "" {
c.print(c.FakeStdout)
}
if c.FakeStderr != "" {
c.error(c.FakeStderr)
}
if c.ExecuteError {
return errors.New("failed at user request")
}
return nil
}
func MockCgroupSnapNameFromPid(f func(int) (string, error)) (restore func()) {
old := cgroupSnapNameFromPid
cgroupSnapNameFromPid = f
return func() {
cgroupSnapNameFromPid = old
}
}
func MockAutoRefreshForGatingSnap(f func(st *state.State, gatingSnap string) error) (restore func()) {
old := autoRefreshForGatingSnap
autoRefreshForGatingSnap = f
return func() {
autoRefreshForGatingSnap = old
}
}
func MockNewStatusDecorator(f func(ctx context.Context, isGlobal bool, uid string) clientutil.StatusDecorator) (restore func()) {
restore = testutil.Backup(&newStatusDecorator)
newStatusDecorator = f
return restore
}
func MockConfdbstateTransactionForSet(f func(*hookstate.Context, *state.State, *confdb.View) (*confdbstate.Transaction, confdbstate.CommitTxFunc, error)) (restore func()) {
old := confdbstateTransactionForSet
confdbstateTransactionForSet = f
return func() {
confdbstateTransactionForSet = old
}
}
func MockConfdbstateGetView(f func(st *state.State, account, confdbName, viewName string) (*confdb.View, error)) (restore func()) {
old := confdbstateGetView
confdbstateGetView = f
return func() {
confdbstateGetView = old
}
}
func MockConfdbstateTransactionForGet(f func(*hookstate.Context, *confdb.View, []string) (*confdbstate.Transaction, error)) (restore func()) {
old := confdbstateTransactionForGet
confdbstateTransactionForGet = f
return func() {
confdbstateTransactionForGet = old
}
}
|