Skip to content

Commit

Permalink
interfaces: special-case "snapd" in sanitizeSlotReservedForOS* helpers (
Browse files Browse the repository at this point in the history
snapcore#6844)

* Special-case "snapd" in sanitizeSlotReservedForOSOrGadget and sanitizeSlotReservedForOS helpers.

* Added a minimal test.
  • Loading branch information
stolowski committed May 9, 2019
1 parent 5a9245d commit 58fa393
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions interfaces/builtin/utils.go
Expand Up @@ -75,15 +75,15 @@ func plugAppLabelExpr(plug *interfaces.ConnectedPlug) string {

// sanitizeSlotReservedForOS checks if slot is of type os.
func sanitizeSlotReservedForOS(iface interfaces.Interface, slot *snap.SlotInfo) error {
if slot.Snap.Type != snap.TypeOS {
if slot.Snap.Type != snap.TypeOS && slot.Snap.InstanceName() != "snapd" {
return fmt.Errorf("%s slots are reserved for the core snap", iface.Name())
}
return nil
}

// sanitizeSlotReservedForOSOrGadget checks if the slot is of type os or gadget.
func sanitizeSlotReservedForOSOrGadget(iface interfaces.Interface, slot *snap.SlotInfo) error {
if slot.Snap.Type != snap.TypeOS && slot.Snap.Type != snap.TypeGadget {
if slot.Snap.Type != snap.TypeOS && slot.Snap.Type != snap.TypeGadget && slot.Snap.InstanceName() != "snapd" {
return fmt.Errorf("%s slots are reserved for the core and gadget snaps", iface.Name())
}
return nil
Expand Down
4 changes: 4 additions & 0 deletions interfaces/builtin/utils_test.go
Expand Up @@ -35,26 +35,30 @@ type utilsSuite struct {
iface interfaces.Interface
slotOS *snap.SlotInfo
slotApp *snap.SlotInfo
slotSnapd *snap.SlotInfo
slotGadget *snap.SlotInfo
}

var _ = Suite(&utilsSuite{
iface: &ifacetest.TestInterface{InterfaceName: "iface"},
slotOS: &snap.SlotInfo{Snap: &snap.Info{Type: snap.TypeOS}},
slotApp: &snap.SlotInfo{Snap: &snap.Info{Type: snap.TypeApp}},
slotSnapd: &snap.SlotInfo{Snap: &snap.Info{Type: snap.TypeApp, SuggestedName: "snapd"}},
slotGadget: &snap.SlotInfo{Snap: &snap.Info{Type: snap.TypeGadget}},
})

func (s *utilsSuite) TestSanitizeSlotReservedForOS(c *C) {
errmsg := "iface slots are reserved for the core snap"
c.Assert(builtin.SanitizeSlotReservedForOS(s.iface, s.slotOS), IsNil)
c.Assert(builtin.SanitizeSlotReservedForOS(s.iface, s.slotSnapd), IsNil)
c.Assert(builtin.SanitizeSlotReservedForOS(s.iface, s.slotApp), ErrorMatches, errmsg)
c.Assert(builtin.SanitizeSlotReservedForOS(s.iface, s.slotGadget), ErrorMatches, errmsg)
}

func (s *utilsSuite) TestSanitizeSlotReservedForOSOrGadget(c *C) {
errmsg := "iface slots are reserved for the core and gadget snaps"
c.Assert(builtin.SanitizeSlotReservedForOSOrGadget(s.iface, s.slotOS), IsNil)
c.Assert(builtin.SanitizeSlotReservedForOSOrGadget(s.iface, s.slotSnapd), IsNil)
c.Assert(builtin.SanitizeSlotReservedForOSOrGadget(s.iface, s.slotApp), ErrorMatches, errmsg)
c.Assert(builtin.SanitizeSlotReservedForOSOrGadget(s.iface, s.slotGadget), IsNil)
}
Expand Down

0 comments on commit 58fa393

Please sign in to comment.