github.com/rigado/snapd@v2.42.5-go-mod+incompatible/interfaces/builtin/gpio_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 builtin_test 21 22 import ( 23 . "gopkg.in/check.v1" 24 25 "github.com/snapcore/snapd/interfaces" 26 "github.com/snapcore/snapd/interfaces/builtin" 27 "github.com/snapcore/snapd/interfaces/systemd" 28 "github.com/snapcore/snapd/snap" 29 "github.com/snapcore/snapd/snap/snaptest" 30 "github.com/snapcore/snapd/testutil" 31 ) 32 33 type GpioInterfaceSuite struct { 34 iface interfaces.Interface 35 gadgetGpioSlotInfo *snap.SlotInfo 36 gadgetGpioSlot *interfaces.ConnectedSlot 37 gadgetMissingNumberSlotInfo *snap.SlotInfo 38 gadgetMissingNumberSlot *interfaces.ConnectedSlot 39 gadgetBadNumberSlotInfo *snap.SlotInfo 40 gadgetBadNumberSlot *interfaces.ConnectedSlot 41 gadgetBadInterfaceSlotInfo *snap.SlotInfo 42 gadgetBadInterfaceSlot *interfaces.ConnectedSlot 43 gadgetPlugInfo *snap.PlugInfo 44 gadgetPlug *interfaces.ConnectedPlug 45 gadgetBadInterfacePlugInfo *snap.PlugInfo 46 gadgetBadInterfacePlug *interfaces.ConnectedPlug 47 osGpioSlotInfo *snap.SlotInfo 48 osGpioSlot *interfaces.ConnectedSlot 49 } 50 51 var _ = Suite(&GpioInterfaceSuite{ 52 iface: builtin.MustInterface("gpio"), 53 }) 54 55 func (s *GpioInterfaceSuite) SetUpTest(c *C) { 56 gadgetInfo := snaptest.MockInfo(c, ` 57 name: my-device 58 version: 0 59 type: gadget 60 slots: 61 my-pin: 62 interface: gpio 63 number: 100 64 missing-number: 65 interface: gpio 66 bad-number: 67 interface: gpio 68 number: forty-two 69 bad-interface-slot: other-interface 70 plugs: 71 plug: gpio 72 bad-interface-plug: other-interface 73 `, nil) 74 s.gadgetGpioSlotInfo = gadgetInfo.Slots["my-pin"] 75 s.gadgetGpioSlot = interfaces.NewConnectedSlot(s.gadgetGpioSlotInfo, nil, nil) 76 s.gadgetMissingNumberSlotInfo = gadgetInfo.Slots["missing-number"] 77 s.gadgetMissingNumberSlot = interfaces.NewConnectedSlot(s.gadgetMissingNumberSlotInfo, nil, nil) 78 s.gadgetBadNumberSlotInfo = gadgetInfo.Slots["bad-number"] 79 s.gadgetBadNumberSlot = interfaces.NewConnectedSlot(s.gadgetBadNumberSlotInfo, nil, nil) 80 s.gadgetBadInterfaceSlotInfo = gadgetInfo.Slots["bad-interface-slot"] 81 s.gadgetBadInterfaceSlot = interfaces.NewConnectedSlot(s.gadgetBadInterfaceSlotInfo, nil, nil) 82 s.gadgetPlugInfo = gadgetInfo.Plugs["plug"] 83 s.gadgetPlug = interfaces.NewConnectedPlug(s.gadgetPlugInfo, nil, nil) 84 s.gadgetBadInterfacePlugInfo = gadgetInfo.Plugs["bad-interface-plug"] 85 s.gadgetBadInterfacePlug = interfaces.NewConnectedPlug(s.gadgetBadInterfacePlugInfo, nil, nil) 86 87 osInfo := snaptest.MockInfo(c, ` 88 name: my-core 89 version: 0 90 type: os 91 slots: 92 my-pin: 93 interface: gpio 94 number: 777 95 direction: out 96 `, nil) 97 s.osGpioSlotInfo = osInfo.Slots["my-pin"] 98 s.osGpioSlot = interfaces.NewConnectedSlot(s.osGpioSlotInfo, nil, nil) 99 } 100 101 func (s *GpioInterfaceSuite) TestName(c *C) { 102 c.Assert(s.iface.Name(), Equals, "gpio") 103 } 104 105 func (s *GpioInterfaceSuite) TestSanitizeSlotGadgetSnap(c *C) { 106 // gpio slot on gadget accepeted 107 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.gadgetGpioSlotInfo), IsNil) 108 109 // slots without number attribute are rejected 110 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.gadgetMissingNumberSlotInfo), ErrorMatches, 111 "gpio slot must have a number attribute") 112 113 // slots with number attribute that isnt a number 114 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.gadgetBadNumberSlotInfo), ErrorMatches, 115 "gpio slot number attribute must be an int") 116 } 117 118 func (s *GpioInterfaceSuite) TestSanitizeSlotOsSnap(c *C) { 119 // gpio slot on OS accepeted 120 c.Assert(interfaces.BeforePrepareSlot(s.iface, s.osGpioSlotInfo), IsNil) 121 } 122 123 func (s *GpioInterfaceSuite) TestSanitizePlug(c *C) { 124 c.Assert(interfaces.BeforePreparePlug(s.iface, s.gadgetPlugInfo), IsNil) 125 } 126 127 func (s *GpioInterfaceSuite) TestSystemdConnectedSlot(c *C) { 128 spec := &systemd.Specification{} 129 err := spec.AddConnectedSlot(s.iface, s.gadgetPlug, s.gadgetGpioSlot) 130 c.Assert(err, IsNil) 131 c.Assert(spec.Services(), DeepEquals, map[string]*systemd.Service{ 132 "snap.my-device.interface.gpio-100.service": { 133 Type: "oneshot", 134 RemainAfterExit: true, 135 ExecStart: `/bin/sh -c 'test -e /sys/class/gpio/gpio100 || echo 100 > /sys/class/gpio/export'`, 136 ExecStop: `/bin/sh -c 'test ! -e /sys/class/gpio/gpio100 || echo 100 > /sys/class/gpio/unexport'`, 137 }, 138 }) 139 } 140 141 func (s *GpioInterfaceSuite) TestInterfaces(c *C) { 142 c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface) 143 }