gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/overlord/ifacestate/implicit_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016-2017 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 ifacestate_test
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	"github.com/snapcore/snapd/overlord/ifacestate"
    26  	"github.com/snapcore/snapd/overlord/state"
    27  	"github.com/snapcore/snapd/release"
    28  	"github.com/snapcore/snapd/snap"
    29  	"github.com/snapcore/snapd/snap/snaptest"
    30  )
    31  
    32  type implicitSuite struct{}
    33  
    34  var _ = Suite(&implicitSuite{})
    35  
    36  func (implicitSuite) TestAddImplicitSlotsOnCore(c *C) {
    37  	restore := release.MockOnClassic(false)
    38  	defer restore()
    39  
    40  	st := state.New(nil)
    41  	hotplugSlots := map[string]*ifacestate.HotplugSlotInfo{
    42  		"foo": {
    43  			Name:        "foo",
    44  			Interface:   "dummy",
    45  			StaticAttrs: map[string]interface{}{"attr": "value"},
    46  			HotplugKey:  "1234",
    47  		}}
    48  	st.Lock()
    49  	defer st.Unlock()
    50  	st.Set("hotplug-slots", hotplugSlots)
    51  
    52  	info := snaptest.MockInfo(c, "{name: core, type: os, version: 0}", nil)
    53  	c.Assert(ifacestate.AddImplicitSlots(st, info), IsNil)
    54  	// Ensure that some slots that exist in core systems are present.
    55  	for _, name := range []string{"network"} {
    56  		slot := info.Slots[name]
    57  		c.Assert(slot.Interface, Equals, name)
    58  		c.Assert(slot.Name, Equals, name)
    59  		c.Assert(slot.Snap, Equals, info)
    60  	}
    61  	// Ensure that some slots that exist is just classic systems are absent.
    62  	for _, name := range []string{"unity7"} {
    63  		c.Assert(info.Slots[name], IsNil)
    64  	}
    65  
    66  	// Ensure that we have *some* implicit slots
    67  	c.Assert(len(info.Slots) > 10, Equals, true)
    68  
    69  	// Ensure hotplug slots were added
    70  	slot := info.Slots["foo"]
    71  	c.Assert(slot, NotNil)
    72  	c.Assert(slot.Interface, Equals, "dummy")
    73  	c.Assert(slot.Attrs, DeepEquals, map[string]interface{}{"attr": "value"})
    74  	c.Assert(slot.HotplugKey, DeepEquals, snap.HotplugKey("1234"))
    75  }
    76  
    77  func (implicitSuite) TestAddImplicitSlotsOnClassic(c *C) {
    78  	restore := release.MockOnClassic(true)
    79  	defer restore()
    80  
    81  	info := snaptest.MockInfo(c, "{name: core, type: os, version: 0}", nil)
    82  
    83  	st := state.New(nil)
    84  	st.Lock()
    85  	defer st.Unlock()
    86  
    87  	c.Assert(ifacestate.AddImplicitSlots(st, info), IsNil)
    88  	// Ensure that some slots that exist in classic systems are present.
    89  	for _, name := range []string{"network", "unity7"} {
    90  		slot := info.Slots[name]
    91  		c.Assert(slot.Interface, Equals, name)
    92  		c.Assert(slot.Name, Equals, name)
    93  		c.Assert(slot.Snap, Equals, info)
    94  	}
    95  	// Ensure that we have *some* implicit slots
    96  	c.Assert(len(info.Slots) > 10, Equals, true)
    97  }
    98  
    99  func (implicitSuite) TestAddImplicitSlotsErrorSlotExists(c *C) {
   100  	restore := release.MockOnClassic(true)
   101  	defer restore()
   102  
   103  	info := snaptest.MockInfo(c, "{name: core, type: os, version: 0}", nil)
   104  
   105  	st := state.New(nil)
   106  	hotplugSlots := map[string]*ifacestate.HotplugSlotInfo{
   107  		"unity7": {
   108  			Name:       "unity7",
   109  			Interface:  "unity7",
   110  			HotplugKey: "1234",
   111  		}}
   112  	st.Lock()
   113  	defer st.Unlock()
   114  	st.Set("hotplug-slots", hotplugSlots)
   115  
   116  	c.Assert(ifacestate.AddImplicitSlots(st, info), ErrorMatches, `cannot add hotplug slot unity7: slot already exists`)
   117  }