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
|
// -*- 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 builtin
import (
"fmt"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/sandbox/apparmor"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/testutil"
)
var (
RegisterIface = registerIface
ResolveSpecialVariable = resolveSpecialVariable
ImplicitSystemPermanentSlot = implicitSystemPermanentSlot
ImplicitSystemConnectedSlot = implicitSystemConnectedSlot
StringListAttribute = stringListAttribute
)
type GbmDriverLibsInterface gbmDriverLibsInterface
func SymlinksUserIfaceFromGbmIface(iface interfaces.Interface) interfaces.SymlinksUser {
gbmIface := iface.(*gbmDriverLibsInterface)
return interfaces.SymlinksUser(gbmIface)
}
func SymlinksUserIfaceFromEglIface(iface interfaces.Interface) interfaces.SymlinksUser {
eglIface := iface.(*eglDriverLibsInterface)
return interfaces.SymlinksUser(eglIface)
}
func MprisGetName(iface interfaces.Interface, attribs map[string]any) (string, error) {
return iface.(*mprisInterface).getName(attribs)
}
// MockInterfaces replaces the set of known interfaces and returns a restore function.
func MockInterfaces(ifaces map[string]interfaces.Interface) (restore func()) {
old := allInterfaces
allInterfaces = ifaces
return func() { allInterfaces = old }
}
// Interface returns the interface with the given name (or nil).
func Interface(name string) interfaces.Interface {
return allInterfaces[name]
}
// MustInterface returns the interface with the given name or panics.
func MustInterface(name string) interfaces.Interface {
if iface, ok := allInterfaces[name]; ok {
return iface
}
panic(fmt.Errorf("cannot find interface with name %q", name))
}
func MockPlug(c *C, yaml string, si *snap.SideInfo, plugName string) *snap.PlugInfo {
info := snaptest.MockInfo(c, yaml, si)
if plugInfo, ok := info.Plugs[plugName]; ok {
return plugInfo
}
panic(fmt.Sprintf("cannot find plug %q in snap %q", plugName, info.InstanceName()))
}
func MockSlot(c *C, yaml string, si *snap.SideInfo, slotName string) *snap.SlotInfo {
info := snaptest.MockInfo(c, yaml, si)
if slotInfo, ok := info.Slots[slotName]; ok {
return slotInfo
}
panic(fmt.Sprintf("cannot find slot %q in snap %q", slotName, info.InstanceName()))
}
func MockConnectedPlug(c *C, yaml string, si *snap.SideInfo, plugName string) (*interfaces.ConnectedPlug, *snap.PlugInfo) {
info := snaptest.MockInfo(c, yaml, si)
set, err := interfaces.NewSnapAppSet(info, nil)
c.Assert(err, IsNil)
if plugInfo, ok := info.Plugs[plugName]; ok {
return interfaces.NewConnectedPlug(plugInfo, set, nil, nil), plugInfo
}
panic(fmt.Sprintf("cannot find plug %q in snap %q", plugName, info.InstanceName()))
}
func MockConnectedSlot(c *C, yaml string, si *snap.SideInfo, slotName string) (*interfaces.ConnectedSlot, *snap.SlotInfo) {
info := snaptest.MockInfo(c, yaml, si)
set, err := interfaces.NewSnapAppSet(info, nil)
c.Assert(err, IsNil)
if slotInfo, ok := info.Slots[slotName]; ok {
return interfaces.NewConnectedSlot(slotInfo, set, nil, nil), slotInfo
}
panic(fmt.Sprintf("cannot find slot %q in snap %q", slotName, info.InstanceName()))
}
func MockOsGetenv(mock func(string) string) (restore func()) {
old := osGetenv
restore = func() {
osGetenv = old
}
osGetenv = mock
return restore
}
func MockProcCpuinfo(filename string) (restore func()) {
old := procCpuinfo
restore = func() {
procCpuinfo = old
}
procCpuinfo = filename
return restore
}
func MockDirsToEnsure(fn func(paths []string) ([]*interfaces.EnsureDirSpec, error)) (restore func()) {
old := dirsToEnsure
restore = func() {
dirsToEnsure = old
}
dirsToEnsure = fn
return restore
}
func MockPolkitDaemonPaths(path1, path2 string) (restore func()) {
oldDaemonPath1 := polkitDaemonPath1
oldDaemonPath2 := polkitDaemonPath2
polkitDaemonPath1 = path1
polkitDaemonPath2 = path2
return func() {
polkitDaemonPath1 = oldDaemonPath1
polkitDaemonPath2 = oldDaemonPath2
}
}
func MockApparmorGenerateAAREExclusionPatterns(fn func(excludePatterns []string, opts *apparmor.AAREExclusionPatternsOptions) (string, error)) (restore func()) {
return testutil.Mock(&apparmorGenerateAAREExclusionPatterns, fn)
}
func MockDesktopFilesFromInstalledSnap(fn func(s *snap.Info) ([]string, error)) (restore func()) {
return testutil.Mock(&desktopFilesFromInstalledSnap, fn)
}
func MockGpioCheckConfigfsSupport(fn func() error) (restore func()) {
return testutil.Mock(&gpioCheckConfigfsSupport, fn)
}
func AllowedKernelMountOptions() []string {
return allowedKernelMountOptions
}
|