github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/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  func NewAndAddRoutes() (*Daemon, error) {
    36  	d, err := New()
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	d.addRoutes()
    41  	return d, nil
    42  }
    43  
    44  func NewWithOverlord(o *overlord.Overlord) *Daemon {
    45  	d := &Daemon{overlord: o, state: o.State()}
    46  	d.addRoutes()
    47  	return d
    48  }
    49  
    50  func (d *Daemon) RouterMatch(req *http.Request, m *mux.RouteMatch) bool {
    51  	return d.router.Match(req, m)
    52  }
    53  
    54  func (d *Daemon) Overlord() *overlord.Overlord {
    55  	return d.overlord
    56  }
    57  
    58  func (d *Daemon) RequestedRestart() state.RestartType {
    59  	return d.requestedRestart
    60  }
    61  
    62  func MockUcrednetGet(mock func(remoteAddr string) (pid int32, uid uint32, socket string, err error)) (restore func()) {
    63  	oldUcrednetGet := ucrednetGet
    64  	ucrednetGet = mock
    65  	return func() {
    66  		ucrednetGet = oldUcrednetGet
    67  	}
    68  }
    69  
    70  func MockEnsureStateSoon(mock func(*state.State)) (original func(*state.State), restore func()) {
    71  	oldEnsureStateSoon := ensureStateSoon
    72  	ensureStateSoon = mock
    73  	return ensureStateSoonImpl, func() {
    74  		ensureStateSoon = oldEnsureStateSoon
    75  	}
    76  }
    77  
    78  func MockMuxVars(vars func(*http.Request) map[string]string) (restore func()) {
    79  	old := muxVars
    80  	muxVars = vars
    81  	return func() {
    82  		muxVars = old
    83  	}
    84  }
    85  
    86  func MockShutdownTimeout(tm time.Duration) (restore func()) {
    87  	old := shutdownTimeout
    88  	shutdownTimeout = tm
    89  	return func() {
    90  		shutdownTimeout = old
    91  	}
    92  }
    93  
    94  func MockUnsafeReadSnapInfo(mock func(string) (*snap.Info, error)) (restore func()) {
    95  	oldUnsafeReadSnapInfo := unsafeReadSnapInfo
    96  	unsafeReadSnapInfo = mock
    97  	return func() {
    98  		unsafeReadSnapInfo = oldUnsafeReadSnapInfo
    99  	}
   100  }
   101  
   102  func MockAssertstateRefreshSnapDeclarations(mock func(*state.State, int) error) (restore func()) {
   103  	oldAssertstateRefreshSnapDeclarations := assertstateRefreshSnapDeclarations
   104  	assertstateRefreshSnapDeclarations = mock
   105  	return func() {
   106  		assertstateRefreshSnapDeclarations = oldAssertstateRefreshSnapDeclarations
   107  	}
   108  }
   109  
   110  func MockSnapstateInstall(mock func(context.Context, *state.State, string, *snapstate.RevisionOptions, int, snapstate.Flags) (*state.TaskSet, error)) (restore func()) {
   111  	oldSnapstateInstall := snapstateInstall
   112  	snapstateInstall = mock
   113  	return func() {
   114  		snapstateInstall = oldSnapstateInstall
   115  	}
   116  }
   117  
   118  func MockSnapstateInstallPath(mock func(*state.State, *snap.SideInfo, string, string, string, snapstate.Flags) (*state.TaskSet, *snap.Info, error)) (restore func()) {
   119  	oldSnapstateInstallPath := snapstateInstallPath
   120  	snapstateInstallPath = mock
   121  	return func() {
   122  		snapstateInstallPath = oldSnapstateInstallPath
   123  	}
   124  }
   125  
   126  func MockSnapstateUpdate(mock func(*state.State, string, *snapstate.RevisionOptions, int, snapstate.Flags) (*state.TaskSet, error)) (restore func()) {
   127  	oldSnapstateUpdate := snapstateUpdate
   128  	snapstateUpdate = mock
   129  	return func() {
   130  		snapstateUpdate = oldSnapstateUpdate
   131  	}
   132  }
   133  
   134  func MockSnapstateTryPath(mock func(*state.State, string, string, snapstate.Flags) (*state.TaskSet, error)) (restore func()) {
   135  	oldSnapstateTryPath := snapstateTryPath
   136  	snapstateTryPath = mock
   137  	return func() {
   138  		snapstateTryPath = oldSnapstateTryPath
   139  	}
   140  }
   141  
   142  func MockSnapstateSwitch(mock func(*state.State, string, *snapstate.RevisionOptions) (*state.TaskSet, error)) (restore func()) {
   143  	oldSnapstateSwitch := snapstateSwitch
   144  	snapstateSwitch = mock
   145  	return func() {
   146  		snapstateSwitch = oldSnapstateSwitch
   147  	}
   148  }
   149  
   150  func MockSnapstateRevert(mock func(*state.State, string, snapstate.Flags) (*state.TaskSet, error)) (restore func()) {
   151  	oldSnapstateRevert := snapstateRevert
   152  	snapstateRevert = mock
   153  	return func() {
   154  		snapstateRevert = oldSnapstateRevert
   155  	}
   156  }
   157  
   158  func MockSnapstateRevertToRevision(mock func(*state.State, string, snap.Revision, snapstate.Flags) (*state.TaskSet, error)) (restore func()) {
   159  	oldSnapstateRevertToRevision := snapstateRevertToRevision
   160  	snapstateRevertToRevision = mock
   161  	return func() {
   162  		snapstateRevertToRevision = oldSnapstateRevertToRevision
   163  	}
   164  }
   165  
   166  func MockSnapstateInstallMany(mock func(*state.State, []string, int) ([]string, []*state.TaskSet, error)) (restore func()) {
   167  	oldSnapstateInstallMany := snapstateInstallMany
   168  	snapstateInstallMany = mock
   169  	return func() {
   170  		snapstateInstallMany = oldSnapstateInstallMany
   171  	}
   172  }
   173  
   174  func MockSnapstateUpdateMany(mock func(context.Context, *state.State, []string, int, *snapstate.Flags) ([]string, []*state.TaskSet, error)) (restore func()) {
   175  	oldSnapstateUpdateMany := snapstateUpdateMany
   176  	snapstateUpdateMany = mock
   177  	return func() {
   178  		snapstateUpdateMany = oldSnapstateUpdateMany
   179  	}
   180  }
   181  
   182  func MockSnapstateRemoveMany(mock func(*state.State, []string) ([]string, []*state.TaskSet, error)) (restore func()) {
   183  	oldSnapstateRemoveMany := snapstateRemoveMany
   184  	snapstateRemoveMany = mock
   185  	return func() {
   186  		snapstateRemoveMany = oldSnapstateRemoveMany
   187  	}
   188  }
   189  
   190  type (
   191  	Resp            = resp
   192  	RespJSON        = respJSON
   193  	FileResponse    = fileResponse
   194  	ErrorResult     = errorResult
   195  	SnapInstruction = snapInstruction
   196  )
   197  
   198  func (inst *snapInstruction) Dispatch() snapActionFunc {
   199  	return inst.dispatch()
   200  }
   201  
   202  func (inst *snapInstruction) DispatchForMany() snapManyActionFunc {
   203  	return inst.dispatchForMany()
   204  }
   205  
   206  func (inst *snapInstruction) SetUserID(userID int) {
   207  	inst.userID = userID
   208  }
   209  
   210  func (inst *snapInstruction) ModeFlags() (snapstate.Flags, error) {
   211  	return inst.modeFlags()
   212  }
   213  
   214  func (inst *snapInstruction) ErrToResponse(err error) Response {
   215  	return inst.errToResponse(err)
   216  }
   217  
   218  var (
   219  	MakeErrorResponder = makeErrorResponder
   220  	ErrToResponse      = errToResponse
   221  )