interfaces/builtin: distribute code of touching allInterfaces #3291

Merged
merged 3 commits into from May 10, 2017
@@ -72,3 +72,7 @@ func NewAccountControlInterface() interfaces.Interface {
reservedForOS: true,
}
}
+
+func init() {
+ registerIface(NewAccountControlInterface())
+}
@@ -108,3 +108,7 @@ func (s *AccountControlSuite) TestUsedSecuritySystems(c *C) {
c.Assert(seccompSpec.SecurityTags(), DeepEquals, []string{"snap.other.app2"})
c.Check(seccompSpec.SnippetForTag("snap.other.app2"), testutil.Contains, "\nfchown - 0 42\n")
}
+
+func (s *AccountControlSuite) TestInterfaces(c *C) {
+ c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
+}
View
@@ -20,102 +20,35 @@
package builtin
import (
+ "sort"
+
"github.com/snapcore/snapd/interfaces"
)
-var allInterfaces = []interfaces.Interface{
- &BluezInterface{},
- &BoolFileInterface{},
- &BrowserSupportInterface{},
- &ContentInterface{},
- &DbusInterface{},
- &DockerInterface{},
- &DockerSupportInterface{},
- &FramebufferInterface{},
- &FwupdInterface{},
- &GpioInterface{},
- &HardwareRandomControlInterface{},
- &HardwareRandomObserveInterface{},
- &HidrawInterface{},
- &I2cInterface{},
- &IioInterface{},
- &IioPortsControlInterface{},
- &JoystickInterface{},
- &LocationControlInterface{},
- &LocationObserveInterface{},
- &LxdInterface{},
- &LxdSupportInterface{},
- &MaliitInterface{},
- &MediaHubInterface{},
- &MirInterface{},
- &ModemManagerInterface{},
- &MprisInterface{},
- &NetworkManagerInterface{},
- &OfonoInterface{},
- &PhysicalMemoryControlInterface{},
- &PhysicalMemoryObserveInterface{},
- &PppInterface{},
- &PulseAudioInterface{},
- &SerialPortInterface{},
- &StorageFrameworkServiceInterface{},
- &ThumbnailerServiceInterface{},
- &TimeControlInterface{},
- &Unity7Interface{},
- &UDisks2Interface{},
- &UbuntuDownloadManagerInterface{},
- &UhidInterface{},
- &Unity8Interface{},
- &UpowerObserveInterface{},
- NewAccountControlInterface(),
- NewAlsaInterface(),
- NewAutopilotIntrospectionInterface(),
- NewAvahiObserveInterface(),
- NewBluetoothControlInterface(),
- NewCameraInterface(),
- NewClassicSupportInterface(),
- NewCoreSupportInterface(),
- NewCupsControlInterface(),
- NewDcdbasControlInterface(),
- NewFirewallControlInterface(),
- NewFuseSupportInterface(),
- NewGsettingsInterface(),
- NewHardwareObserveInterface(),
- NewHomeInterface(),
- NewKernelModuleControlInterface(),
- NewKubernetesSupportInterface(),
- NewLibvirtInterface(),
- NewLocaleControlInterface(),
- NewLogObserveInterface(),
- NewMountObserveInterface(),
- NewNetlinkAuditInterface(),
- NewNetlinkConnectorInterface(),
- NewNetworkBindInterface(),
- NewNetworkControlInterface(),
- NewNetworkInterface(),
- NewNetworkObserveInterface(),
- NewNetworkSetupControlInterface(),
- NewNetworkSetupObserveInterface(),
- NewOpenglInterface(),
- NewOpenvSwitchInterface(),
- NewOpenvSwitchSupportInterface(),
- NewOpticalDriveInterface(),
- NewProcessControlInterface(),
- NewRawUsbInterface(),
- NewRemovableMediaInterface(),
- NewScreenInhibitControlInterface(),
- NewShutdownInterface(),
- NewSnapdControlInterface(),
- NewSystemObserveInterface(),
- NewSystemTraceInterface(),
- NewTimeserverControlInterface(),
- NewTimezoneControlInterface(),
- NewTpmInterface(),
- NewUnity8CalendarInterface(),
- NewUnity8ContactsInterface(),
- NewX11Interface(),
-}
+var (
+ allInterfaces []interfaces.Interface
+ sorted bool
+)
// Interfaces returns all of the built-in interfaces.
func Interfaces() []interfaces.Interface {
+ if !sorted {
+ sort.Sort(byIfaceName(allInterfaces))
@mvo5

mvo5 May 10, 2017

Collaborator

Curious, why do we care about sorting?

@zyga

zyga May 10, 2017

Contributor

We don't care about sorting at all but I wanted to ensure that it's not changing this property. The old list used to be sorted so this one can be as well.

+ sorted = true
+ }
return allInterfaces
}
+
+// registerIface appends the given interface into the list of all known interfaces.
+func registerIface(iface interfaces.Interface) {
+ allInterfaces = append(allInterfaces, iface)
@mvo5

mvo5 May 10, 2017

Collaborator

Should we care about duplication here? I.e. do not allow to register an interface twice?

@zyga

zyga May 10, 2017

Contributor

Yes, I was thinking about it as well. Doing this branch has uncovered a number of smaller things that ought to be improved and I'm already preparing another one (and I have ideas for two more).

+ sorted = false
+}
+
+type byIfaceName []interfaces.Interface
+
+func (c byIfaceName) Len() int { return len(c) }
+func (c byIfaceName) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
+func (c byIfaceName) Less(i, j int) bool {
+ return c[i].Name() < c[j].Name()
+}
@@ -31,7 +31,6 @@ import (
"github.com/snapcore/snapd/interfaces/seccomp"
"github.com/snapcore/snapd/interfaces/systemd"
"github.com/snapcore/snapd/interfaces/udev"
- . "github.com/snapcore/snapd/testutil"
. "gopkg.in/check.v1"
)
@@ -40,85 +39,6 @@ type AllSuite struct{}
var _ = Suite(&AllSuite{})
-func (s *AllSuite) TestInterfaces(c *C) {
- all := builtin.Interfaces()
- c.Check(all, Contains, &builtin.StorageFrameworkServiceInterface{})
- c.Check(all, DeepContains, &builtin.BluezInterface{})
- c.Check(all, DeepContains, &builtin.BoolFileInterface{})
- c.Check(all, DeepContains, &builtin.BrowserSupportInterface{})
- c.Check(all, DeepContains, &builtin.DbusInterface{})
- c.Check(all, DeepContains, &builtin.DockerInterface{})
- c.Check(all, DeepContains, &builtin.DockerSupportInterface{})
- c.Check(all, DeepContains, &builtin.FramebufferInterface{})
- c.Check(all, DeepContains, &builtin.FwupdInterface{})
- c.Check(all, DeepContains, &builtin.GpioInterface{})
- c.Check(all, DeepContains, &builtin.HardwareRandomControlInterface{})
- c.Check(all, DeepContains, &builtin.HardwareRandomObserveInterface{})
- c.Check(all, DeepContains, &builtin.HidrawInterface{})
- c.Check(all, DeepContains, &builtin.I2cInterface{})
- c.Check(all, DeepContains, &builtin.IioInterface{})
- c.Check(all, DeepContains, &builtin.IioPortsControlInterface{})
- c.Check(all, DeepContains, &builtin.JoystickInterface{})
- c.Check(all, DeepContains, &builtin.LocationControlInterface{})
- c.Check(all, DeepContains, &builtin.LocationObserveInterface{})
- c.Check(all, DeepContains, &builtin.LxdSupportInterface{})
- c.Check(all, DeepContains, &builtin.MaliitInterface{})
- c.Check(all, DeepContains, &builtin.MediaHubInterface{})
- c.Check(all, DeepContains, &builtin.MirInterface{})
- c.Check(all, DeepContains, &builtin.MprisInterface{})
- c.Check(all, DeepContains, &builtin.PhysicalMemoryControlInterface{})
- c.Check(all, DeepContains, &builtin.PhysicalMemoryObserveInterface{})
- c.Check(all, DeepContains, &builtin.PulseAudioInterface{})
- c.Check(all, DeepContains, &builtin.SerialPortInterface{})
- c.Check(all, DeepContains, &builtin.ThumbnailerServiceInterface{})
- c.Check(all, DeepContains, &builtin.TimeControlInterface{})
- c.Check(all, DeepContains, &builtin.UDisks2Interface{})
- c.Check(all, DeepContains, &builtin.UbuntuDownloadManagerInterface{})
- c.Check(all, DeepContains, &builtin.UhidInterface{})
- c.Check(all, DeepContains, &builtin.Unity7Interface{})
- c.Check(all, DeepContains, &builtin.Unity8Interface{})
- c.Check(all, DeepContains, &builtin.UpowerObserveInterface{})
- c.Check(all, DeepContains, builtin.NewAccountControlInterface())
- c.Check(all, DeepContains, builtin.NewAlsaInterface())
- c.Check(all, DeepContains, builtin.NewAutopilotIntrospectionInterface())
- c.Check(all, DeepContains, builtin.NewAvahiObserveInterface())
- c.Check(all, DeepContains, builtin.NewBluetoothControlInterface())
- c.Check(all, DeepContains, builtin.NewCameraInterface())
- c.Check(all, DeepContains, builtin.NewCupsControlInterface())
- c.Check(all, DeepContains, builtin.NewFirewallControlInterface())
- c.Check(all, DeepContains, builtin.NewFuseSupportInterface())
- c.Check(all, DeepContains, builtin.NewGsettingsInterface())
- c.Check(all, DeepContains, builtin.NewHomeInterface())
- c.Check(all, DeepContains, builtin.NewKernelModuleControlInterface())
- c.Check(all, DeepContains, builtin.NewKubernetesSupportInterface())
- c.Check(all, DeepContains, builtin.NewLocaleControlInterface())
- c.Check(all, DeepContains, builtin.NewLogObserveInterface())
- c.Check(all, DeepContains, builtin.NewMountObserveInterface())
- c.Check(all, DeepContains, builtin.NewNetlinkAuditInterface())
- c.Check(all, DeepContains, builtin.NewNetlinkConnectorInterface())
- c.Check(all, DeepContains, builtin.NewNetworkBindInterface())
- c.Check(all, DeepContains, builtin.NewNetworkControlInterface())
- c.Check(all, DeepContains, builtin.NewNetworkInterface())
- c.Check(all, DeepContains, builtin.NewNetworkObserveInterface())
- c.Check(all, DeepContains, builtin.NewOpenglInterface())
- c.Check(all, DeepContains, builtin.NewOpenvSwitchInterface())
- c.Check(all, DeepContains, builtin.NewOpenvSwitchSupportInterface())
- c.Check(all, DeepContains, builtin.NewOpticalDriveInterface())
- c.Check(all, DeepContains, builtin.NewProcessControlInterface())
- c.Check(all, DeepContains, builtin.NewRawUsbInterface())
- c.Check(all, DeepContains, builtin.NewRemovableMediaInterface())
- c.Check(all, DeepContains, builtin.NewScreenInhibitControlInterface())
- c.Check(all, DeepContains, builtin.NewSnapdControlInterface())
- c.Check(all, DeepContains, builtin.NewSystemObserveInterface())
- c.Check(all, DeepContains, builtin.NewSystemTraceInterface())
- c.Check(all, DeepContains, builtin.NewTimeserverControlInterface())
- c.Check(all, DeepContains, builtin.NewTimezoneControlInterface())
- c.Check(all, DeepContains, builtin.NewTpmInterface())
- c.Check(all, DeepContains, builtin.NewUnity8CalendarInterface())
- c.Check(all, DeepContains, builtin.NewUnity8ContactsInterface())
- c.Check(all, DeepContains, builtin.NewX11Interface())
-}
-
// This section contains a list of *valid* defines that represent methods that
// backends recognize and call. They are in individual interfaces as each snapd
// interface can define a subset that it is interested in providing. Those are,
@@ -288,6 +208,56 @@ type legacyAutoConnect interface {
LegacyAutoConnect() bool
}
+// specification definers before the introduction of connection attributes
+type oldApparmorDefiner1 interface {
+ AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.Plug, slot *interfaces.Slot) error
+}
+type oldApparmorDefiner2 interface {
+ AppArmorConnestedSlot(spec *apparmor.Specification, plug *interfaces.Plug, slot *interfaces.Slot) error
+}
+
+type oldDbusDefiner1 interface {
+ DBusConnectedPlug(spec *dbus.Specification, plug *interfaces.Plug, slot *interfaces.Slot) error
+}
+type oldDbusDefiner2 interface {
+ DBusConnectedSlot(spec *dbus.Specification, plug *interfaces.Plug, slot *interfaces.Slot) error
+}
+
+type oldKmodDefiner1 interface {
+ KModConnectedPlug(spec *kmod.Specification, plug *interfaces.Plug, slot *interfaces.Slot) error
+}
+type oldKmodDefiner2 interface {
+ KModConnectedSlot(spec *kmod.Specification, plug *interfaces.Plug, slot *interfaces.Slot) error
+}
+
+type oldMountDefiner1 interface {
+ MountConnectedPlug(spec *mount.Specification, plug *interfaces.Plug, slot *interfaces.Slot) error
+}
+type oldMountDefiner2 interface {
+ MountConnectedSlot(spec *mount.Specification, plug *interfaces.Plug, slot *interfaces.Slot) error
+}
+
+type oldSeccompDefiner1 interface {
+ SecCompConnectedPlug(spec *seccomp.Specification, plug *interfaces.Plug, slot *interfaces.Slot) error
+}
+type oldSeccompDefiner2 interface {
+ SecCompConnectedSlot(spec *seccomp.Specification, plug *interfaces.Plug, slot *interfaces.Slot) error
+}
+
+type oldSystemdDefiner1 interface {
+ SystemdConnectedPlug(spec *systemd.Specification, plug *interfaces.Plug, slot *interfaces.Slot) error
+}
+type oldSystemdDefiner2 interface {
+ SystemdConnectedSlot(spec *systemd.Specification, plug *interfaces.Plug, slot *interfaces.Slot) error
+}
+
+type oldUdevDefiner1 interface {
+ UDevConnectedPlug(spec *udev.Specification, plug *interfaces.Plug, slot *interfaces.Slot) error
+}
+type oldUdevDefiner2 interface {
+ UDevConnectedSlot(spec *udev.Specification, plug *interfaces.Plug, slot *interfaces.Slot) error
+}
+
// allBadDefiners contains all old/unused specification definers for all known backends.
var allBadDefiners = []reflect.Type{
// pre-specification snippet methods
@@ -297,6 +267,21 @@ var allBadDefiners = []reflect.Type{
reflect.TypeOf((*snippetDefiner4)(nil)).Elem(),
// old auto-connect function
reflect.TypeOf((*legacyAutoConnect)(nil)).Elem(),
+ // pre-attribute definers
+ reflect.TypeOf((*oldApparmorDefiner1)(nil)).Elem(),
+ reflect.TypeOf((*oldApparmorDefiner2)(nil)).Elem(),
+ reflect.TypeOf((*oldDbusDefiner1)(nil)).Elem(),
+ reflect.TypeOf((*oldDbusDefiner2)(nil)).Elem(),
+ reflect.TypeOf((*oldKmodDefiner1)(nil)).Elem(),
+ reflect.TypeOf((*oldKmodDefiner2)(nil)).Elem(),
+ reflect.TypeOf((*oldMountDefiner1)(nil)).Elem(),
+ reflect.TypeOf((*oldMountDefiner2)(nil)).Elem(),
+ reflect.TypeOf((*oldSeccompDefiner1)(nil)).Elem(),
+ reflect.TypeOf((*oldSeccompDefiner2)(nil)).Elem(),
+ reflect.TypeOf((*oldSystemdDefiner1)(nil)).Elem(),
+ reflect.TypeOf((*oldSystemdDefiner2)(nil)).Elem(),
+ reflect.TypeOf((*oldUdevDefiner1)(nil)).Elem(),
+ reflect.TypeOf((*oldUdevDefiner2)(nil)).Elem(),
}
// Check that no interface defines older definer methods.
@@ -40,3 +40,7 @@ func NewAlsaInterface() interfaces.Interface {
reservedForOS: true,
}
}
+
+func init() {
+ registerIface(NewAlsaInterface())
+}
@@ -93,3 +93,7 @@ func (s *AlsaInterfaceSuite) TestUsedSecuritySystems(c *C) {
c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.other.app"})
c.Check(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, "/dev/snd/* rw,")
}
+
+func (s *AlsaInterfaceSuite) TestInterfaces(c *C) {
+ c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
+}
@@ -66,3 +66,7 @@ func NewAutopilotIntrospectionInterface() interfaces.Interface {
reservedForOS: true,
}
}
+
+func init() {
+ registerIface(NewAutopilotIntrospectionInterface())
+}
@@ -102,3 +102,7 @@ func (s *AutopilotInterfaceSuite) TestUsedSecuritySystems(c *C) {
c.Assert(seccompSpec.SecurityTags(), DeepEquals, []string{"snap.other.app"})
c.Check(seccompSpec.SnippetForTag("snap.other.app"), testutil.Contains, "recvmsg\n")
}
+
+func (s *AutopilotInterfaceSuite) TestInterfaces(c *C) {
+ c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
+}
@@ -120,3 +120,7 @@ func NewAvahiObserveInterface() interfaces.Interface {
reservedForOS: true,
}
}
+
+func init() {
+ registerIface(NewAvahiObserveInterface())
+}
@@ -93,3 +93,7 @@ func (s *AvahiObserveInterfaceSuite) TestUsedSecuritySystems(c *C) {
c.Assert(apparmorSpec.SecurityTags(), DeepEquals, []string{"snap.other.app"})
c.Check(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, "name=org.freedesktop.Avahi")
}
+
+func (s *AvahiObserveInterfaceSuite) TestInterfaces(c *C) {
+ c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
+}
@@ -58,3 +58,7 @@ func NewBluetoothControlInterface() interfaces.Interface {
reservedForOS: true,
}
}
+
+func init() {
+ registerIface(NewBluetoothControlInterface())
+}
@@ -108,3 +108,7 @@ func (s *BluetoothControlInterfaceSuite) TestUsedSecuritySystems(c *C) {
c.Assert(seccompSpec.SecurityTags(), DeepEquals, []string{"snap.other.app2"})
c.Check(seccompSpec.SnippetForTag("snap.other.app2"), testutil.Contains, "\nbind\n")
}
+
+func (s *BluetoothControlInterfaceSuite) TestInterfaces(c *C) {
+ c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
+}
@@ -230,3 +230,7 @@ func (iface *BluezInterface) AutoConnect(*interfaces.Plug, *interfaces.Slot) boo
// allow what declarations allowed
return true
}
+
+func init() {
+ registerIface(&BluezInterface{})
+}
@@ -164,3 +164,7 @@ func (s *BluezInterfaceSuite) TestUsedSecuritySystems(c *C) {
c.Assert(seccompSpec.SecurityTags(), DeepEquals, []string{"snap.bluez.app1"})
c.Check(seccompSpec.SnippetForTag("snap.bluez.app1"), testutil.Contains, "listen\n")
}
+
+func (s *BluezInterfaceSuite) TestInterfaces(c *C) {
+ c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
+}
Oops, something went wrong.