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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2018-2022 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 daemon
import (
"context"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/asserts/snapasserts"
"github.com/snapcore/snapd/boot"
"github.com/snapcore/snapd/client/clientutil"
"github.com/snapcore/snapd/confdb"
"github.com/snapcore/snapd/features"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/overlord"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/confdbstate"
"github.com/snapcore/snapd/overlord/devicestate"
"github.com/snapcore/snapd/overlord/hookstate"
"github.com/snapcore/snapd/overlord/restart"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/testutil"
)
var (
CreateQuotaValues = createQuotaValues
ParseOptionalTime = parseOptionalTime
)
func APICommands() []*Command {
return api
}
func NewAndAddRoutes() (*Daemon, error) {
d, err := New()
if err != nil {
return nil, err
}
d.addRoutes()
return d, nil
}
func NewWithOverlord(o *overlord.Overlord) *Daemon {
d := &Daemon{overlord: o, state: o.State()}
d.addRoutes()
return d
}
func (d *Daemon) RouterMatch(req *http.Request, m *mux.RouteMatch) bool {
return d.router.Match(req, m)
}
func (d *Daemon) Overlord() *overlord.Overlord {
return d.overlord
}
func (d *Daemon) RequestedRestart() restart.RestartType {
return d.requestedRestart
}
type Ucrednet = ucrednet
func BeforeNewChange(beforeNewChange func(st *state.State, kind, summary string, tsets []*state.TaskSet, snapNames []string)) (restore func()) {
oldNewChange := newChange
newChange = func(st *state.State, kind, summary string, tsets []*state.TaskSet, snapNames []string) *state.Change {
beforeNewChange(st, kind, summary, tsets, snapNames)
return newChangeImpl(st, kind, summary, tsets, snapNames)
}
return func() {
newChange = oldNewChange
}
}
func MockUcrednetGet(mock func(remoteAddr string) (ucred *Ucrednet, err error)) (restore func()) {
oldUcrednetGet := ucrednetGet
ucrednetGet = mock
return func() {
ucrednetGet = oldUcrednetGet
}
}
func MockEnsureStateSoon(mock func(*state.State)) (original func(*state.State), restore func()) {
oldEnsureStateSoon := ensureStateSoon
ensureStateSoon = mock
return ensureStateSoonImpl, func() {
ensureStateSoon = oldEnsureStateSoon
}
}
func MockMuxVars(vars func(*http.Request) map[string]string) (restore func()) {
old := muxVars
muxVars = vars
return func() {
muxVars = old
}
}
func MockShutdownTimeout(tm time.Duration) (restore func()) {
old := shutdownTimeout
shutdownTimeout = tm
return func() {
shutdownTimeout = old
}
}
func MockUnsafeReadSnapInfo(mock func(string) (*snap.Info, error)) (restore func()) {
oldUnsafeReadSnapInfo := unsafeReadSnapInfo
unsafeReadSnapInfo = mock
return func() {
unsafeReadSnapInfo = oldUnsafeReadSnapInfo
}
}
func MockReadComponentInfoFromCont(mock func(tempPath string, csi *snap.ComponentSideInfo) (*snap.ComponentInfo, error)) (restore func()) {
oldUnsafeReadSnapInfo := readComponentInfoFromCont
readComponentInfoFromCont = mock
return func() {
readComponentInfoFromCont = oldUnsafeReadSnapInfo
}
}
func MockAssertstateRefreshSnapAssertions(mock func(*state.State, int, *assertstate.RefreshAssertionsOptions) error) (restore func()) {
oldAssertstateRefreshSnapAssertions := assertstateRefreshSnapAssertions
assertstateRefreshSnapAssertions = mock
return func() {
assertstateRefreshSnapAssertions = oldAssertstateRefreshSnapAssertions
}
}
func MockAssertstateTryEnforceValidationSets(f func(st *state.State, validationSets []string, userID int, snaps []*snapasserts.InstalledSnap, ignoreValidation map[string]bool) error) (restore func()) {
r := testutil.Backup(&assertstateTryEnforcedValidationSets)
assertstateTryEnforcedValidationSets = f
return r
}
func MockSnapstateInstallWithGoal(mock func(ctx context.Context, st *state.State, goal snapstate.InstallGoal, opts snapstate.Options) ([]*snap.Info, []*state.TaskSet, error)) (restore func()) {
old := snapstateInstallWithGoal
snapstateInstallWithGoal = mock
return func() {
snapstateInstallWithGoal = old
}
}
func MockSnapstateUpdateWithGoal(mock func(ctx context.Context, st *state.State, goal snapstate.UpdateGoal, filter func(*snap.Info, *snapstate.SnapState) bool, opts snapstate.Options) ([]string, *snapstate.UpdateTaskSets, error)) (restore func()) {
return testutil.Mock(&snapstateUpdateWithGoal, mock)
}
func MockSnapstatePathUpdateGoal(mock func(snaps ...snapstate.PathSnap) snapstate.UpdateGoal) (restore func()) {
return testutil.Mock(&snapstatePathUpdateGoal, mock)
}
func MockSnapstateUpdateOne(mock func(ctx context.Context, st *state.State, goal snapstate.UpdateGoal, filter func(*snap.Info, *snapstate.SnapState) bool, opts snapstate.Options) (*state.TaskSet, error)) (restore func()) {
old := snapstateUpdateOne
snapstateUpdateOne = mock
return func() {
snapstateUpdateOne = old
}
}
func MockSnapstateInstallComponents(mock 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 = mock
return func() {
snapstateInstallComponents = old
}
}
func MockSnapstateStoreInstallGoal(mock func(snaps ...snapstate.StoreSnap) snapstate.InstallGoal) (restore func()) {
old := snapstateStoreInstallGoal
snapstateStoreInstallGoal = mock
return func() {
snapstateStoreInstallGoal = old
}
}
func MockSnapstateStoreUpdateGoal(mock func(snaps ...snapstate.StoreUpdate) snapstate.UpdateGoal) (restore func()) {
old := snapstateStoreUpdateGoal
snapstateStoreUpdateGoal = mock
return func() {
snapstateStoreUpdateGoal = old
}
}
func MockSnapstateInstallPath(mock func(*state.State, *snap.SideInfo, string, string, string, snapstate.Flags, snapstate.PrereqTracker) (*state.TaskSet, *snap.Info, error)) (restore func()) {
oldSnapstateInstallPath := snapstateInstallPath
snapstateInstallPath = mock
return func() {
snapstateInstallPath = oldSnapstateInstallPath
}
}
func MockSnapstateTryPath(mock func(*state.State, string, string, snapstate.Flags) (*state.TaskSet, error)) (restore func()) {
oldSnapstateTryPath := snapstateTryPath
snapstateTryPath = mock
return func() {
snapstateTryPath = oldSnapstateTryPath
}
}
func MockSnapstateSwitch(mock func(*state.State, string, *snapstate.RevisionOptions, snapstate.PrereqTracker) (*state.TaskSet, error)) (restore func()) {
oldSnapstateSwitch := snapstateSwitch
snapstateSwitch = mock
return func() {
snapstateSwitch = oldSnapstateSwitch
}
}
func MockSnapstateRevert(mock func(*state.State, string, snapstate.Flags, string) (*state.TaskSet, error)) (restore func()) {
oldSnapstateRevert := snapstateRevert
snapstateRevert = mock
return func() {
snapstateRevert = oldSnapstateRevert
}
}
func MockSnapstateRevertToRevision(mock func(*state.State, string, snap.Revision, snapstate.Flags, string) (*state.TaskSet, error)) (restore func()) {
oldSnapstateRevertToRevision := snapstateRevertToRevision
snapstateRevertToRevision = mock
return func() {
snapstateRevertToRevision = oldSnapstateRevertToRevision
}
}
func MockSnapstateRemove(mock func(st *state.State, name string, revision snap.Revision, flags *snapstate.RemoveFlags) (*state.TaskSet, error)) (restore func()) {
oldSnapstateRemove := snapstateRemove
snapstateRemove = mock
return func() {
snapstateRemove = oldSnapstateRemove
}
}
func MockSnapstateRemoveMany(mock func(*state.State, []string, *snapstate.RemoveFlags) ([]string, []*state.TaskSet, error)) (restore func()) {
oldSnapstateRemoveMany := snapstateRemoveMany
snapstateRemoveMany = mock
return func() {
snapstateRemoveMany = oldSnapstateRemoveMany
}
}
func MockSnapstateInstallPathMany(f func(context.Context, *state.State, []*snap.SideInfo, []string, int, *snapstate.Flags) ([]*state.TaskSet, error)) func() {
old := snapstateInstallPathMany
snapstateInstallPathMany = f
return func() {
snapstateInstallPathMany = old
}
}
func MockSnapstateInstallComponentPath(f func(st *state.State, csi *snap.ComponentSideInfo, info *snap.Info, path string, opts snapstate.Options) (*state.TaskSet, error)) func() {
old := snapstateInstallComponentPath
snapstateInstallComponentPath = f
return func() {
snapstateInstallComponentPath = old
}
}
func MockSnapstateResolveValSetEnforcementError(f func(context.Context, *state.State, *snapasserts.ValidationSetsValidationError, map[string]int, int) ([]*state.TaskSet, []string, error)) func() {
old := snapstateResolveValSetsEnforcementError
snapstateResolveValSetsEnforcementError = f
return func() {
snapstateResolveValSetsEnforcementError = old
}
}
func MockSnapstateMigrate(mock func(*state.State, []string) ([]*state.TaskSet, error)) (restore func()) {
oldSnapstateMigrate := snapstateMigrateHome
snapstateMigrateHome = mock
return func() {
snapstateMigrateHome = oldSnapstateMigrate
}
}
func MockSnapstateProceedWithRefresh(f func(st *state.State, gatingSnap string, snaps []string) error) (restore func()) {
old := snapstateProceedWithRefresh
snapstateProceedWithRefresh = f
return func() {
snapstateProceedWithRefresh = old
}
}
func MockSnapstateHoldRefreshesBySystem(f func(st *state.State, level snapstate.HoldLevel, time string, snaps []string) error) (restore func()) {
old := snapstateHoldRefreshesBySystem
snapstateHoldRefreshesBySystem = f
return func() {
snapstateHoldRefreshesBySystem = old
}
}
func MockSnapstateRemoveComponents(mock func(st *state.State, snapName string, compName []string, opts snapstate.RemoveComponentsOpts) ([]*state.TaskSet, error)) (restore func()) {
oldSnapstateRemoveComponents := snapstateRemoveComponents
snapstateRemoveComponents = mock
return func() {
snapstateRemoveComponents = oldSnapstateRemoveComponents
}
}
func MockConfigstateConfigureInstalled(f func(st *state.State, name string, patchValues map[string]any, flags int) (*state.TaskSet, error)) (restore func()) {
old := configstateConfigureInstalled
configstateConfigureInstalled = f
return func() {
configstateConfigureInstalled = old
}
}
func MockSystemHold(f func(st *state.State, name string) (time.Time, error)) (restore func()) {
old := snapstateSystemHold
snapstateSystemHold = f
return func() {
snapstateSystemHold = old
}
}
func MockLongestGatingHold(f func(st *state.State, name string) (time.Time, error)) (restore func()) {
old := snapstateLongestGatingHold
snapstateLongestGatingHold = f
return func() {
snapstateLongestGatingHold = old
}
}
func MockReboot(f func(boot.RebootAction, time.Duration, *boot.RebootInfo) error) func() {
reboot = f
return func() { reboot = boot.Reboot }
}
func MockSideloadSnapsInfo(infos []*snap.Info) (restore func()) {
r := testutil.Backup(&sideloadSnapsInfo)
sideloadSnapsInfo = func(st *state.State, snapFiles []*uploadedContainer,
flags sideloadFlags) (*sideloadedInfo, *apiError) {
var snaps []sideloadSnapInfo
for i, snapFile := range snapFiles {
snaps = append(snaps, sideloadSnapInfo{
info: infos[i],
origPath: snapFile.filename,
tmpPath: snapFile.tmpPath,
})
}
return &sideloadedInfo{snaps: snaps}, nil
}
return r
}
type (
RespJSON = respJSON
FileResponse = fileResponse
APIError = apiError
ErrorResult = errorResult
SnapInstruction = snapInstruction
)
func (inst *snapInstruction) Dispatch() snapActionFunc {
return inst.dispatch()
}
func (inst *snapInstruction) DispatchForMany() snapManyActionFunc {
return inst.dispatchForMany()
}
func (inst *snapInstruction) SetUserID(userID int) {
inst.userID = userID
}
func (inst *snapInstruction) ModeFlags() (snapstate.Flags, error) {
return inst.modeFlags()
}
func (inst *snapInstruction) ErrToResponse(err error) *APIError {
return inst.errToResponse(err)
}
var (
UserFromRequest = userFromRequest
IsTrue = isTrue
MakeErrorResponder = makeErrorResponder
ErrToResponse = errToResponse
MaxReadBuflen = maxReadBuflen
)
func MockConfdbstateGetTransaction(f func(*hookstate.Context, *state.State, *confdb.View) (*confdbstate.Transaction, confdbstate.CommitTxFunc, error)) (restore func()) {
return testutil.Mock(&confdbstateGetTransactionToSet, f)
}
func MockRebootNoticeWait(d time.Duration) (restore func()) {
restore = testutil.Backup(&rebootNoticeWait)
rebootNoticeWait = d
return restore
}
func MockSystemUserFromRequest(f func(r *http.Request) (*user.User, error)) (restore func()) {
restore = testutil.Backup(&systemUserFromRequest)
systemUserFromRequest = f
return restore
}
func MockOsReadlink(f func(string) (string, error)) func() {
old := osReadlink
osReadlink = f
return func() {
osReadlink = 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 MockConfdbstateGetView(f func(_ *state.State, _, _, _ string) (*confdb.View, error)) (restore func()) {
return testutil.Mock(&confdbstateGetView, f)
}
func MockConfdbstateSetViaView(f func(confdb.Databag, *confdb.View, map[string]any) error) (restore func()) {
return testutil.Mock(&confdbstateSetViaView, f)
}
func MockAssertstateFetchAllValidationSets(f func(*state.State, int, *assertstate.RefreshAssertionsOptions) error) (restore func()) {
return testutil.Mock(&assertstateFetchAllValidationSets, f)
}
func MockConfdbstateLoadConfdbAsync(f func(_ *state.State, _ *confdb.View, _ []string) (string, error)) (restore func()) {
return testutil.Mock(&confdbstateLoadConfdbAsync, f)
}
func ValidateFeatureFlag(st *state.State, feature features.SnapdFeature) *apiError {
return validateFeatureFlag(st, feature)
}
func MockDeviceStateSignConfdbControl(f func(m *devicestate.DeviceManager, groups []any, revision int) (*asserts.ConfdbControl, error)) (restore func()) {
return testutil.Mock(&devicestateSignConfdbControl, f)
}
|