github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/interfaces/dbus/spec_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 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 dbus_test
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	"github.com/snapcore/snapd/interfaces"
    26  	"github.com/snapcore/snapd/interfaces/dbus"
    27  	"github.com/snapcore/snapd/interfaces/ifacetest"
    28  	"github.com/snapcore/snapd/snap"
    29  )
    30  
    31  type specSuite struct {
    32  	iface    *ifacetest.TestInterface
    33  	spec     *dbus.Specification
    34  	plugInfo *snap.PlugInfo
    35  	plug     *interfaces.ConnectedPlug
    36  	slotInfo *snap.SlotInfo
    37  	slot     *interfaces.ConnectedSlot
    38  }
    39  
    40  var _ = Suite(&specSuite{
    41  	iface: &ifacetest.TestInterface{
    42  		InterfaceName: "test",
    43  		DBusConnectedPlugCallback: func(spec *dbus.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
    44  			spec.AddSnippet("connected-plug")
    45  			return nil
    46  		},
    47  		DBusConnectedSlotCallback: func(spec *dbus.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
    48  			spec.AddSnippet("connected-slot")
    49  			return nil
    50  		},
    51  		DBusPermanentPlugCallback: func(spec *dbus.Specification, plug *snap.PlugInfo) error {
    52  			spec.AddSnippet("permanent-plug")
    53  			return nil
    54  		},
    55  		DBusPermanentSlotCallback: func(spec *dbus.Specification, slot *snap.SlotInfo) error {
    56  			spec.AddSnippet("permanent-slot")
    57  			return nil
    58  		},
    59  	},
    60  	plugInfo: &snap.PlugInfo{
    61  		Snap:      &snap.Info{SuggestedName: "snap1"},
    62  		Name:      "name",
    63  		Interface: "test",
    64  		Apps: map[string]*snap.AppInfo{
    65  			"app1": {
    66  				Snap: &snap.Info{
    67  					SuggestedName: "snap1",
    68  				},
    69  				Name: "app1"}},
    70  	},
    71  	slotInfo: &snap.SlotInfo{
    72  		Snap:      &snap.Info{SuggestedName: "snap2"},
    73  		Name:      "name",
    74  		Interface: "test",
    75  		Apps: map[string]*snap.AppInfo{
    76  			"app2": {
    77  				Snap: &snap.Info{
    78  					SuggestedName: "snap2",
    79  				},
    80  				Name: "app2"}},
    81  	},
    82  })
    83  
    84  func (s *specSuite) SetUpTest(c *C) {
    85  	s.spec = &dbus.Specification{}
    86  	s.plug = interfaces.NewConnectedPlug(s.plugInfo, nil, nil)
    87  	s.slot = interfaces.NewConnectedSlot(s.slotInfo, nil, nil)
    88  }
    89  
    90  // The spec.Specification can be used through the interfaces.Specification interface
    91  func (s *specSuite) TestSpecificationIface(c *C) {
    92  	var r interfaces.Specification = s.spec
    93  	c.Assert(r.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    94  	c.Assert(r.AddConnectedSlot(s.iface, s.plug, s.slot), IsNil)
    95  	c.Assert(r.AddPermanentPlug(s.iface, s.plugInfo), IsNil)
    96  	c.Assert(r.AddPermanentSlot(s.iface, s.slotInfo), IsNil)
    97  	c.Assert(s.spec.Snippets(), DeepEquals, map[string][]string{
    98  		"snap.snap1.app1": {"connected-plug", "permanent-plug"},
    99  		"snap.snap2.app2": {"connected-slot", "permanent-slot"},
   100  	})
   101  	c.Assert(s.spec.SecurityTags(), DeepEquals, []string{"snap.snap1.app1", "snap.snap2.app2"})
   102  	c.Assert(s.spec.SnippetForTag("snap.snap1.app1"), Equals, "connected-plug\npermanent-plug\n")
   103  
   104  	c.Assert(s.spec.SnippetForTag("non-existing"), Equals, "")
   105  }