github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/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  	FirstTaskAfterBootWhenPreseeding = firstTaskAfterBootWhenPreseeding
    63  )
    64  
    65  type ConnectOpts = connectOpts
    66  
    67  func NewConnectOptsWithAutoSet() connectOpts {
    68  	return connectOpts{AutoConnect: true, ByGadget: false}
    69  }
    70  
    71  func NewDisconnectOptsWithByHotplugSet() disconnectOpts {
    72  	return disconnectOpts{ByHotplug: true}
    73  }
    74  
    75  func NewConnectOptsWithDelayProfilesSet() connectOpts {
    76  	return connectOpts{AutoConnect: true, ByGadget: false, DelayedSetupProfiles: true}
    77  }
    78  
    79  func MockRemoveStaleConnections(f func(st *state.State) error) (restore func()) {
    80  	old := removeStaleConnections
    81  	removeStaleConnections = f
    82  	return func() { removeStaleConnections = old }
    83  }
    84  
    85  func MockContentLinkRetryTimeout(d time.Duration) (restore func()) {
    86  	old := contentLinkRetryTimeout
    87  	contentLinkRetryTimeout = d
    88  	return func() { contentLinkRetryTimeout = old }
    89  }
    90  
    91  func MockHotplugRetryTimeout(d time.Duration) (restore func()) {
    92  	old := hotplugRetryTimeout
    93  	hotplugRetryTimeout = d
    94  	return func() { hotplugRetryTimeout = old }
    95  }
    96  
    97  func MockCreateUDevMonitor(new func(udevmonitor.DeviceAddedFunc, udevmonitor.DeviceRemovedFunc, udevmonitor.EnumerationDoneFunc) udevmonitor.Interface) (restore func()) {
    98  	old := createUDevMonitor
    99  	createUDevMonitor = new
   100  	return func() {
   101  		createUDevMonitor = old
   102  	}
   103  }
   104  
   105  func MockUDevInitRetryTimeout(t time.Duration) (restore func()) {
   106  	old := udevInitRetryTimeout
   107  	udevInitRetryTimeout = t
   108  	return func() {
   109  		udevInitRetryTimeout = old
   110  	}
   111  }
   112  
   113  // UpperCaseConnState returns a canned connection state map.
   114  // This allows us to keep connState private and still write some tests for it.
   115  func UpperCaseConnState() map[string]*connState {
   116  	return map[string]*connState{
   117  		"APP:network CORE:network": {Auto: true, Interface: "network"},
   118  	}
   119  }
   120  
   121  func UpdateConnectionInConnState(conns map[string]*connState, conn *interfaces.Connection, autoConnect, byGadget, undesired, hotplugGone bool) {
   122  	connRef := &interfaces.ConnRef{
   123  		PlugRef: *conn.Plug.Ref(),
   124  		SlotRef: *conn.Slot.Ref(),
   125  	}
   126  
   127  	conns[connRef.ID()] = &connState{
   128  		Interface:        conn.Interface(),
   129  		StaticPlugAttrs:  conn.Plug.StaticAttrs(),
   130  		DynamicPlugAttrs: conn.Plug.DynamicAttrs(),
   131  		StaticSlotAttrs:  conn.Slot.StaticAttrs(),
   132  		DynamicSlotAttrs: conn.Slot.DynamicAttrs(),
   133  		Auto:             autoConnect,
   134  		ByGadget:         byGadget,
   135  		Undesired:        undesired,
   136  		HotplugGone:      hotplugGone,
   137  	}
   138  }
   139  
   140  func GetConnStateAttrs(conns map[string]*connState, connID string) (plugStatic, plugDynamic, slotStatic, SlotDynamic map[string]interface{}, ok bool) {
   141  	conn, ok := conns[connID]
   142  	if !ok {
   143  		return nil, nil, nil, nil, false
   144  	}
   145  	return conn.StaticPlugAttrs, conn.DynamicPlugAttrs, conn.StaticSlotAttrs, conn.DynamicSlotAttrs, true
   146  }
   147  
   148  // SystemSnapName returns actual name of the system snap - reimplemented by concrete mapper.
   149  func (m *IdentityMapper) SystemSnapName() string {
   150  	return "unknown"
   151  }
   152  
   153  // MockProfilesNeedRegeneration mocks the function checking if profiles need regeneration.
   154  func MockProfilesNeedRegeneration(fn func() bool) func() {
   155  	old := profilesNeedRegeneration
   156  	profilesNeedRegeneration = fn
   157  	return func() { profilesNeedRegeneration = old }
   158  }
   159  
   160  // MockWriteSystemKey mocks the function responsible for writing the system key.
   161  func MockWriteSystemKey(fn func() error) func() {
   162  	old := writeSystemKey
   163  	writeSystemKey = fn
   164  	return func() { writeSystemKey = old }
   165  }
   166  
   167  func (m *InterfaceManager) TransitionConnectionsCoreMigration(st *state.State, oldName, newName string) error {
   168  	return m.transitionConnectionsCoreMigration(st, oldName, newName)
   169  }
   170  
   171  func (m *InterfaceManager) SetupSecurityByBackend(task *state.Task, snaps []*snap.Info, opts []interfaces.ConfinementOptions, tm timings.Measurer) error {
   172  	return m.setupSecurityByBackend(task, snaps, opts, tm)
   173  }