github.com/rigado/snapd@v2.42.5-go-mod+incompatible/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  )
    27  
    28  var (
    29  	AddImplicitSlots             = addImplicitSlots
    30  	SnapsWithSecurityProfiles    = snapsWithSecurityProfiles
    31  	CheckAutoconnectConflicts    = checkAutoconnectConflicts
    32  	FindSymmetricAutoconnectTask = findSymmetricAutoconnectTask
    33  	ConnectPriv                  = connect
    34  	DisconnectPriv               = disconnectTasks
    35  	GetConns                     = getConns
    36  	SetConns                     = setConns
    37  	DefaultDeviceKey             = defaultDeviceKey
    38  	RemoveDevice                 = removeDevice
    39  	MakeSlotName                 = makeSlotName
    40  	EnsureUniqueName             = ensureUniqueName
    41  	SuggestedSlotName            = suggestedSlotName
    42  	HotplugSlotName              = hotplugSlotName
    43  	InSameChangeWaitChain        = inSameChangeWaitChain
    44  	GetHotplugAttrs              = getHotplugAttrs
    45  	SetHotplugAttrs              = setHotplugAttrs
    46  	GetHotplugSlots              = getHotplugSlots
    47  	SetHotplugSlots              = setHotplugSlots
    48  	UpdateDevice                 = updateDevice
    49  	FindConnsForHotplugKey       = findConnsForHotplugKey
    50  	CheckSystemSnapIsPresent     = checkSystemSnapIsPresent
    51  	SystemSnapInfo               = systemSnapInfo
    52  	IsHotplugChange              = isHotplugChange
    53  	GetHotplugChangeAttrs        = getHotplugChangeAttrs
    54  	SetHotplugChangeAttrs        = setHotplugChangeAttrs
    55  	AllocHotplugSeq              = allocHotplugSeq
    56  	AddHotplugSeqWaitTask        = addHotplugSeqWaitTask
    57  	AddHotplugSlot               = addHotplugSlot
    58  
    59  	BatchConnectTasks = batchConnectTasks
    60  )
    61  
    62  func NewConnectOptsWithAutoSet() connectOpts {
    63  	return connectOpts{AutoConnect: true, ByGadget: false}
    64  }
    65  
    66  func NewDisconnectOptsWithByHotplugSet() disconnectOpts {
    67  	return disconnectOpts{ByHotplug: true}
    68  }
    69  
    70  func NewConnectOptsWithDelayProfilesSet() connectOpts {
    71  	return connectOpts{AutoConnect: true, ByGadget: false, DelayedSetupProfiles: true}
    72  }
    73  
    74  func MockRemoveStaleConnections(f func(st *state.State) error) (restore func()) {
    75  	old := removeStaleConnections
    76  	removeStaleConnections = f
    77  	return func() { removeStaleConnections = old }
    78  }
    79  
    80  func MockContentLinkRetryTimeout(d time.Duration) (restore func()) {
    81  	old := contentLinkRetryTimeout
    82  	contentLinkRetryTimeout = d
    83  	return func() { contentLinkRetryTimeout = old }
    84  }
    85  
    86  func MockHotplugRetryTimeout(d time.Duration) (restore func()) {
    87  	old := hotplugRetryTimeout
    88  	hotplugRetryTimeout = d
    89  	return func() { hotplugRetryTimeout = old }
    90  }
    91  
    92  func MockCreateUDevMonitor(new func(udevmonitor.DeviceAddedFunc, udevmonitor.DeviceRemovedFunc, udevmonitor.EnumerationDoneFunc) udevmonitor.Interface) (restore func()) {
    93  	old := createUDevMonitor
    94  	createUDevMonitor = new
    95  	return func() {
    96  		createUDevMonitor = old
    97  	}
    98  }
    99  
   100  func MockUDevInitRetryTimeout(t time.Duration) (restore func()) {
   101  	old := udevInitRetryTimeout
   102  	udevInitRetryTimeout = t
   103  	return func() {
   104  		udevInitRetryTimeout = old
   105  	}
   106  }
   107  
   108  // UpperCaseConnState returns a canned connection state map.
   109  // This allows us to keep connState private and still write some tests for it.
   110  func UpperCaseConnState() map[string]*connState {
   111  	return map[string]*connState{
   112  		"APP:network CORE:network": {Auto: true, Interface: "network"},
   113  	}
   114  }
   115  
   116  func UpdateConnectionInConnState(conns map[string]*connState, conn *interfaces.Connection, autoConnect, byGadget, undesired, hotplugGone bool) {
   117  	connRef := &interfaces.ConnRef{
   118  		PlugRef: *conn.Plug.Ref(),
   119  		SlotRef: *conn.Slot.Ref(),
   120  	}
   121  
   122  	conns[connRef.ID()] = &connState{
   123  		Interface:        conn.Interface(),
   124  		StaticPlugAttrs:  conn.Plug.StaticAttrs(),
   125  		DynamicPlugAttrs: conn.Plug.DynamicAttrs(),
   126  		StaticSlotAttrs:  conn.Slot.StaticAttrs(),
   127  		DynamicSlotAttrs: conn.Slot.DynamicAttrs(),
   128  		Auto:             autoConnect,
   129  		ByGadget:         byGadget,
   130  		Undesired:        undesired,
   131  		HotplugGone:      hotplugGone,
   132  	}
   133  }
   134  
   135  func GetConnStateAttrs(conns map[string]*connState, connID string) (plugStatic, plugDynamic, slotStatic, SlotDynamic map[string]interface{}, ok bool) {
   136  	conn, ok := conns[connID]
   137  	if !ok {
   138  		return nil, nil, nil, nil, false
   139  	}
   140  	return conn.StaticPlugAttrs, conn.DynamicPlugAttrs, conn.StaticSlotAttrs, conn.DynamicSlotAttrs, true
   141  }
   142  
   143  // SystemSnapName returns actual name of the system snap - reimplemented by concrete mapper.
   144  func (m *IdentityMapper) SystemSnapName() string {
   145  	return "unknown"
   146  }
   147  
   148  // MockProfilesNeedRegeneration mocks the function checking if profiles need regeneration.
   149  func MockProfilesNeedRegeneration(fn func() bool) func() {
   150  	old := profilesNeedRegeneration
   151  	profilesNeedRegeneration = fn
   152  	return func() { profilesNeedRegeneration = old }
   153  }
   154  
   155  // MockWriteSystemKey mocks the function responsible for writing the system key.
   156  func MockWriteSystemKey(fn func() error) func() {
   157  	old := writeSystemKey
   158  	writeSystemKey = fn
   159  	return func() { writeSystemKey = old }
   160  }
   161  
   162  func (m *InterfaceManager) TransitionConnectionsCoreMigration(st *state.State, oldName, newName string) error {
   163  	return m.transitionConnectionsCoreMigration(st, oldName, newName)
   164  }