github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/interfaces/ifacetest/spec_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2015-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 ifacetest_test
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	"github.com/snapcore/snapd/interfaces"
    26  	"github.com/snapcore/snapd/interfaces/ifacetest"
    27  	"github.com/snapcore/snapd/snap"
    28  )
    29  
    30  type SpecificationSuite struct {
    31  	iface    *ifacetest.TestInterface
    32  	spec     *ifacetest.Specification
    33  	plugInfo *snap.PlugInfo
    34  	plug     *interfaces.ConnectedPlug
    35  	slotInfo *snap.SlotInfo
    36  	slot     *interfaces.ConnectedSlot
    37  }
    38  
    39  var _ = Suite(&SpecificationSuite{
    40  	iface: &ifacetest.TestInterface{
    41  		InterfaceName: "test",
    42  		TestConnectedPlugCallback: func(spec *ifacetest.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
    43  			spec.AddSnippet("connected-plug")
    44  			return nil
    45  		},
    46  		TestConnectedSlotCallback: func(spec *ifacetest.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
    47  			spec.AddSnippet("connected-slot")
    48  			return nil
    49  		},
    50  		TestPermanentPlugCallback: func(spec *ifacetest.Specification, plug *snap.PlugInfo) error {
    51  			spec.AddSnippet("permanent-plug")
    52  			return nil
    53  		},
    54  		TestPermanentSlotCallback: func(spec *ifacetest.Specification, slot *snap.SlotInfo) error {
    55  			spec.AddSnippet("permanent-slot")
    56  			return nil
    57  		},
    58  	},
    59  	plugInfo: &snap.PlugInfo{
    60  		Snap:      &snap.Info{SuggestedName: "snap"},
    61  		Name:      "name",
    62  		Interface: "test",
    63  	},
    64  	slotInfo: &snap.SlotInfo{
    65  		Snap:      &snap.Info{SuggestedName: "snap"},
    66  		Name:      "name",
    67  		Interface: "test",
    68  	},
    69  })
    70  
    71  func (s *SpecificationSuite) SetUpTest(c *C) {
    72  	s.spec = &ifacetest.Specification{}
    73  	s.plug = interfaces.NewConnectedPlug(s.plugInfo, nil, nil)
    74  	s.slot = interfaces.NewConnectedSlot(s.slotInfo, nil, nil)
    75  }
    76  
    77  // AddSnippet is not broken
    78  func (s *SpecificationSuite) TestAddSnippet(c *C) {
    79  	s.spec.AddSnippet("hello")
    80  	s.spec.AddSnippet("world")
    81  	c.Assert(s.spec.Snippets, DeepEquals, []string{"hello", "world"})
    82  }
    83  
    84  // The Specification can be used through the interfaces.Specification interface
    85  func (s *SpecificationSuite) SpecificationIface(c *C) {
    86  	var r interfaces.Specification = s.spec
    87  	c.Assert(r.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
    88  	c.Assert(r.AddConnectedSlot(s.iface, s.plug, s.slot), IsNil)
    89  	c.Assert(r.AddPermanentPlug(s.iface, s.plugInfo), IsNil)
    90  	c.Assert(r.AddPermanentSlot(s.iface, s.slotInfo), IsNil)
    91  	c.Assert(s.spec.Snippets, DeepEquals, []string{
    92  		"connected-plug", "connected-slot", "permanent-plug", "permanent-slot"})
    93  }