github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/ifacestate/export_test.go (about) 1 /* 2 * Copyright (C) 2016-2018 Canonical Ltd 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 3 as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 * 16 */ 17 18 package ifacestate 19 20 import ( 21 "time" 22 23 "github.com/snapcore/snapd/interfaces" 24 "github.com/snapcore/snapd/overlord/ifacestate/udevmonitor" 25 "github.com/snapcore/snapd/overlord/state" 26 "github.com/snapcore/snapd/snap" 27 "github.com/snapcore/snapd/timings" 28 ) 29 30 var ( 31 AddImplicitSlots = addImplicitSlots 32 SnapsWithSecurityProfiles = snapsWithSecurityProfiles 33 CheckAutoconnectConflicts = checkAutoconnectConflicts 34 FindSymmetricAutoconnectTask = findSymmetricAutoconnectTask 35 ConnectPriv = connect 36 DisconnectPriv = disconnectTasks 37 GetConns = getConns 38 SetConns = setConns 39 DefaultDeviceKey = defaultDeviceKey 40 RemoveDevice = removeDevice 41 MakeSlotName = makeSlotName 42 EnsureUniqueName = ensureUniqueName 43 SuggestedSlotName = suggestedSlotName 44 HotplugSlotName = hotplugSlotName 45 InSameChangeWaitChain = inSameChangeWaitChain 46 GetHotplugAttrs = getHotplugAttrs 47 SetHotplugAttrs = setHotplugAttrs 48 GetHotplugSlots = getHotplugSlots 49 SetHotplugSlots = setHotplugSlots 50 UpdateDevice = updateDevice 51 FindConnsForHotplugKey = findConnsForHotplugKey 52 CheckSystemSnapIsPresent = checkSystemSnapIsPresent 53 SystemSnapInfo = systemSnapInfo 54 IsHotplugChange = isHotplugChange 55 GetHotplugChangeAttrs = getHotplugChangeAttrs 56 SetHotplugChangeAttrs = setHotplugChangeAttrs 57 AllocHotplugSeq = allocHotplugSeq 58 AddHotplugSeqWaitTask = addHotplugSeqWaitTask 59 AddHotplugSlot = addHotplugSlot 60 61 BatchConnectTasks = batchConnectTasks 62 ) 63 64 type ConnectOpts = connectOpts 65 66 func NewConnectOptsWithAutoSet() connectOpts { 67 return connectOpts{AutoConnect: true, ByGadget: false} 68 } 69 70 func NewDisconnectOptsWithByHotplugSet() disconnectOpts { 71 return disconnectOpts{ByHotplug: true} 72 } 73 74 func NewConnectOptsWithDelayProfilesSet() connectOpts { 75 return connectOpts{AutoConnect: true, ByGadget: false, DelayedSetupProfiles: true} 76 } 77 78 func MockRemoveStaleConnections(f func(st *state.State) error) (restore func()) { 79 old := removeStaleConnections 80 removeStaleConnections = f 81 return func() { removeStaleConnections = old } 82 } 83 84 func MockContentLinkRetryTimeout(d time.Duration) (restore func()) { 85 old := contentLinkRetryTimeout 86 contentLinkRetryTimeout = d 87 return func() { contentLinkRetryTimeout = old } 88 } 89 90 func MockHotplugRetryTimeout(d time.Duration) (restore func()) { 91 old := hotplugRetryTimeout 92 hotplugRetryTimeout = d 93 return func() { hotplugRetryTimeout = old } 94 } 95 96 func MockCreateUDevMonitor(new func(udevmonitor.DeviceAddedFunc, udevmonitor.DeviceRemovedFunc, udevmonitor.EnumerationDoneFunc) udevmonitor.Interface) (restore func()) { 97 old := createUDevMonitor 98 createUDevMonitor = new 99 return func() { 100 createUDevMonitor = old 101 } 102 } 103 104 func MockUDevInitRetryTimeout(t time.Duration) (restore func()) { 105 old := udevInitRetryTimeout 106 udevInitRetryTimeout = t 107 return func() { 108 udevInitRetryTimeout = old 109 } 110 } 111 112 // UpperCaseConnState returns a canned connection state map. 113 // This allows us to keep connState private and still write some tests for it. 114 func UpperCaseConnState() map[string]*connState { 115 return map[string]*connState{ 116 "APP:network CORE:network": {Auto: true, Interface: "network"}, 117 } 118 } 119 120 func UpdateConnectionInConnState(conns map[string]*connState, conn *interfaces.Connection, autoConnect, byGadget, undesired, hotplugGone bool) { 121 connRef := &interfaces.ConnRef{ 122 PlugRef: *conn.Plug.Ref(), 123 SlotRef: *conn.Slot.Ref(), 124 } 125 126 conns[connRef.ID()] = &connState{ 127 Interface: conn.Interface(), 128 StaticPlugAttrs: conn.Plug.StaticAttrs(), 129 DynamicPlugAttrs: conn.Plug.DynamicAttrs(), 130 StaticSlotAttrs: conn.Slot.StaticAttrs(), 131 DynamicSlotAttrs: conn.Slot.DynamicAttrs(), 132 Auto: autoConnect, 133 ByGadget: byGadget, 134 Undesired: undesired, 135 HotplugGone: hotplugGone, 136 } 137 } 138 139 func GetConnStateAttrs(conns map[string]*connState, connID string) (plugStatic, plugDynamic, slotStatic, SlotDynamic map[string]interface{}, ok bool) { 140 conn, ok := conns[connID] 141 if !ok { 142 return nil, nil, nil, nil, false 143 } 144 return conn.StaticPlugAttrs, conn.DynamicPlugAttrs, conn.StaticSlotAttrs, conn.DynamicSlotAttrs, true 145 } 146 147 // SystemSnapName returns actual name of the system snap - reimplemented by concrete mapper. 148 func (m *IdentityMapper) SystemSnapName() string { 149 return "unknown" 150 } 151 152 // MockProfilesNeedRegeneration mocks the function checking if profiles need regeneration. 153 func MockProfilesNeedRegeneration(fn func() bool) func() { 154 old := profilesNeedRegeneration 155 profilesNeedRegeneration = fn 156 return func() { profilesNeedRegeneration = old } 157 } 158 159 // MockWriteSystemKey mocks the function responsible for writing the system key. 160 func MockWriteSystemKey(fn func() error) func() { 161 old := writeSystemKey 162 writeSystemKey = fn 163 return func() { writeSystemKey = old } 164 } 165 166 func (m *InterfaceManager) TransitionConnectionsCoreMigration(st *state.State, oldName, newName string) error { 167 return m.transitionConnectionsCoreMigration(st, oldName, newName) 168 } 169 170 func (m *InterfaceManager) SetupSecurityByBackend(task *state.Task, snaps []*snap.Info, opts []interfaces.ConfinementOptions, tm timings.Measurer) error { 171 return m.setupSecurityByBackend(task, snaps, opts, tm) 172 }