github.com/rigado/snapd@v2.42.5-go-mod+incompatible/interfaces/builtin/utils_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_test
    21  
    22  import (
    23  	"fmt"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/interfaces"
    28  	"github.com/snapcore/snapd/interfaces/builtin"
    29  	"github.com/snapcore/snapd/interfaces/ifacetest"
    30  	"github.com/snapcore/snapd/snap"
    31  	"github.com/snapcore/snapd/snap/snaptest"
    32  )
    33  
    34  type utilsSuite struct {
    35  	iface        interfaces.Interface
    36  	slotOS       *snap.SlotInfo
    37  	slotApp      *snap.SlotInfo
    38  	slotSnapd    *snap.SlotInfo
    39  	slotGadget   *snap.SlotInfo
    40  	conSlotOS    *interfaces.ConnectedSlot
    41  	conSlotSnapd *interfaces.ConnectedSlot
    42  	conSlotApp   *interfaces.ConnectedSlot
    43  }
    44  
    45  var _ = Suite(&utilsSuite{
    46  	iface:        &ifacetest.TestInterface{InterfaceName: "iface"},
    47  	slotOS:       &snap.SlotInfo{Snap: &snap.Info{SnapType: snap.TypeOS}},
    48  	slotApp:      &snap.SlotInfo{Snap: &snap.Info{SnapType: snap.TypeApp}},
    49  	slotSnapd:    &snap.SlotInfo{Snap: &snap.Info{SnapType: snap.TypeSnapd, SuggestedName: "snapd"}},
    50  	slotGadget:   &snap.SlotInfo{Snap: &snap.Info{SnapType: snap.TypeGadget}},
    51  	conSlotOS:    interfaces.NewConnectedSlot(&snap.SlotInfo{Snap: &snap.Info{SnapType: snap.TypeOS}}, nil, nil),
    52  	conSlotSnapd: interfaces.NewConnectedSlot(&snap.SlotInfo{Snap: &snap.Info{SnapType: snap.TypeSnapd}}, nil, nil),
    53  	conSlotApp:   interfaces.NewConnectedSlot(&snap.SlotInfo{Snap: &snap.Info{SnapType: snap.TypeApp}}, nil, nil),
    54  })
    55  
    56  func (s *utilsSuite) TestIsSlotSystemSlot(c *C) {
    57  	c.Assert(builtin.ImplicitSystemPermanentSlot(s.slotApp), Equals, false)
    58  	c.Assert(builtin.ImplicitSystemPermanentSlot(s.slotOS), Equals, true)
    59  	c.Assert(builtin.ImplicitSystemPermanentSlot(s.slotSnapd), Equals, true)
    60  }
    61  
    62  func (s *utilsSuite) TestImplicitSystemConnectedSlot(c *C) {
    63  	c.Assert(builtin.ImplicitSystemConnectedSlot(s.conSlotApp), Equals, false)
    64  	c.Assert(builtin.ImplicitSystemConnectedSlot(s.conSlotOS), Equals, true)
    65  	c.Assert(builtin.ImplicitSystemConnectedSlot(s.conSlotSnapd), Equals, true)
    66  }
    67  
    68  func MockPlug(c *C, yaml string, si *snap.SideInfo, plugName string) *snap.PlugInfo {
    69  	return builtin.MockPlug(c, yaml, si, plugName)
    70  }
    71  
    72  func MockSlot(c *C, yaml string, si *snap.SideInfo, slotName string) *snap.SlotInfo {
    73  	return builtin.MockSlot(c, yaml, si, slotName)
    74  }
    75  
    76  func MockConnectedPlug(c *C, yaml string, si *snap.SideInfo, plugName string) (*interfaces.ConnectedPlug, *snap.PlugInfo) {
    77  	info := snaptest.MockInfo(c, yaml, si)
    78  	if plugInfo, ok := info.Plugs[plugName]; ok {
    79  		return interfaces.NewConnectedPlug(plugInfo, nil, nil), plugInfo
    80  	}
    81  	panic(fmt.Sprintf("cannot find plug %q in snap %q", plugName, info.InstanceName()))
    82  }
    83  
    84  func MockConnectedSlot(c *C, yaml string, si *snap.SideInfo, slotName string) (*interfaces.ConnectedSlot, *snap.SlotInfo) {
    85  	info := snaptest.MockInfo(c, yaml, si)
    86  	if slotInfo, ok := info.Slots[slotName]; ok {
    87  		return interfaces.NewConnectedSlot(slotInfo, nil, nil), slotInfo
    88  	}
    89  	panic(fmt.Sprintf("cannot find slot %q in snap %q", slotName, info.InstanceName()))
    90  }
    91  
    92  func MockHotplugSlot(c *C, yaml string, si *snap.SideInfo, hotplugKey snap.HotplugKey, ifaceName, slotName string, staticAttrs map[string]interface{}) *snap.SlotInfo {
    93  	info := snaptest.MockInfo(c, yaml, si)
    94  	if _, ok := info.Slots[slotName]; ok {
    95  		panic(fmt.Sprintf("slot %q already present in the snap yaml", slotName))
    96  	}
    97  	return &snap.SlotInfo{
    98  		Snap:       info,
    99  		Name:       slotName,
   100  		Attrs:      staticAttrs,
   101  		HotplugKey: hotplugKey,
   102  	}
   103  }