github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/builtin/export_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016 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 builtin 21 22 import ( 23 "fmt" 24 25 . "gopkg.in/check.v1" 26 27 "github.com/snapcore/snapd/interfaces" 28 "github.com/snapcore/snapd/snap" 29 "github.com/snapcore/snapd/snap/snaptest" 30 ) 31 32 var ( 33 RegisterIface = registerIface 34 ResolveSpecialVariable = resolveSpecialVariable 35 ImplicitSystemPermanentSlot = implicitSystemPermanentSlot 36 ImplicitSystemConnectedSlot = implicitSystemConnectedSlot 37 LabelExpr = labelExpr 38 PlugAppLabelExpr = plugAppLabelExpr 39 SlotAppLabelExpr = slotAppLabelExpr 40 AareExclusivePatterns = aareExclusivePatterns 41 GetDesktopFileRules = getDesktopFileRules 42 ) 43 44 func MprisGetName(iface interfaces.Interface, attribs map[string]interface{}) (string, error) { 45 return iface.(*mprisInterface).getName(attribs) 46 } 47 48 // MockInterfaces replaces the set of known interfaces and returns a restore function. 49 func MockInterfaces(ifaces map[string]interfaces.Interface) (restore func()) { 50 old := allInterfaces 51 allInterfaces = ifaces 52 return func() { allInterfaces = old } 53 } 54 55 // Interface returns the interface with the given name (or nil). 56 func Interface(name string) interfaces.Interface { 57 return allInterfaces[name] 58 } 59 60 // MustInterface returns the interface with the given name or panics. 61 func MustInterface(name string) interfaces.Interface { 62 if iface, ok := allInterfaces[name]; ok { 63 return iface 64 } 65 panic(fmt.Errorf("cannot find interface with name %q", name)) 66 } 67 68 func MockPlug(c *C, yaml string, si *snap.SideInfo, plugName string) *snap.PlugInfo { 69 info := snaptest.MockInfo(c, yaml, si) 70 if plugInfo, ok := info.Plugs[plugName]; ok { 71 return plugInfo 72 } 73 panic(fmt.Sprintf("cannot find plug %q in snap %q", plugName, info.InstanceName())) 74 } 75 76 func MockSlot(c *C, yaml string, si *snap.SideInfo, slotName string) *snap.SlotInfo { 77 info := snaptest.MockInfo(c, yaml, si) 78 if slotInfo, ok := info.Slots[slotName]; ok { 79 return slotInfo 80 } 81 panic(fmt.Sprintf("cannot find slot %q in snap %q", slotName, info.InstanceName())) 82 } 83 84 func MockConnectedPlug(c *C, yaml string, si *snap.SideInfo, plugName string) (*interfaces.ConnectedPlug, *snap.PlugInfo) { 85 info := snaptest.MockInfo(c, yaml, si) 86 if plugInfo, ok := info.Plugs[plugName]; ok { 87 return interfaces.NewConnectedPlug(plugInfo, nil, nil), plugInfo 88 } 89 panic(fmt.Sprintf("cannot find plug %q in snap %q", plugName, info.InstanceName())) 90 } 91 92 func MockConnectedSlot(c *C, yaml string, si *snap.SideInfo, slotName string) (*interfaces.ConnectedSlot, *snap.SlotInfo) { 93 info := snaptest.MockInfo(c, yaml, si) 94 if slotInfo, ok := info.Slots[slotName]; ok { 95 return interfaces.NewConnectedSlot(slotInfo, nil, nil), slotInfo 96 } 97 panic(fmt.Sprintf("cannot find slot %q in snap %q", slotName, info.InstanceName())) 98 } 99 100 func MockOsGetenv(mock func(string) string) (restore func()) { 101 old := osGetenv 102 restore = func() { 103 osGetenv = old 104 } 105 osGetenv = mock 106 107 return restore 108 } 109 110 func MockProcCpuinfo(filename string) (restore func()) { 111 old := procCpuinfo 112 restore = func() { 113 procCpuinfo = old 114 } 115 procCpuinfo = filename 116 117 return restore 118 }