github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/kmod/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 kmod_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/interfaces/kmod"
    28  	"github.com/snapcore/snapd/snap"
    29  )
    30  
    31  type specSuite struct {
    32  	iface1, iface2 *ifacetest.TestInterface
    33  	spec           *kmod.Specification
    34  	plugInfo       *snap.PlugInfo
    35  	plug           *interfaces.ConnectedPlug
    36  	slotInfo       *snap.SlotInfo
    37  	slot           *interfaces.ConnectedSlot
    38  }
    39  
    40  var _ = Suite(&specSuite{
    41  	iface1: &ifacetest.TestInterface{
    42  		InterfaceName: "test",
    43  		KModConnectedPlugCallback: func(spec *kmod.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
    44  			return spec.AddModule("module1")
    45  		},
    46  		KModConnectedSlotCallback: func(spec *kmod.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
    47  			return spec.AddModule("module2")
    48  		},
    49  		KModPermanentPlugCallback: func(spec *kmod.Specification, plug *snap.PlugInfo) error {
    50  			return spec.AddModule("module3")
    51  		},
    52  		KModPermanentSlotCallback: func(spec *kmod.Specification, slot *snap.SlotInfo) error {
    53  			return spec.AddModule("module4")
    54  		},
    55  	},
    56  	iface2: &ifacetest.TestInterface{
    57  		InterfaceName: "test-two",
    58  		KModConnectedPlugCallback: func(spec *kmod.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
    59  			return spec.AddModule("module1")
    60  		},
    61  		KModConnectedSlotCallback: func(spec *kmod.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
    62  			return spec.AddModule("module2")
    63  		},
    64  		KModPermanentPlugCallback: func(spec *kmod.Specification, plug *snap.PlugInfo) error {
    65  			return spec.AddModule("module5")
    66  		},
    67  		KModPermanentSlotCallback: func(spec *kmod.Specification, slot *snap.SlotInfo) error {
    68  			return spec.AddModule("module6")
    69  		},
    70  	},
    71  	plugInfo: &snap.PlugInfo{
    72  		Snap:      &snap.Info{SuggestedName: "snap"},
    73  		Name:      "name",
    74  		Interface: "test",
    75  	},
    76  	slotInfo: &snap.SlotInfo{
    77  		Snap:      &snap.Info{SuggestedName: "snap"},
    78  		Name:      "name",
    79  		Interface: "test",
    80  	},
    81  })
    82  
    83  func (s *specSuite) SetUpTest(c *C) {
    84  	s.spec = &kmod.Specification{}
    85  	s.plug = interfaces.NewConnectedPlug(s.plugInfo, nil, nil)
    86  	s.slot = interfaces.NewConnectedSlot(s.slotInfo, nil, nil)
    87  }
    88  
    89  // AddModule is not broken
    90  func (s *specSuite) TestSmoke(c *C) {
    91  	m1 := "module1"
    92  	m2 := "module2"
    93  	c.Assert(s.spec.AddModule(m1), IsNil)
    94  	c.Assert(s.spec.AddModule(m2), IsNil)
    95  	c.Assert(s.spec.Modules(), DeepEquals, map[string]bool{
    96  		"module1": true, "module2": true})
    97  }
    98  
    99  // AddModule ignores duplicated modules
   100  func (s *specSuite) TestDeduplication(c *C) {
   101  	mod := "module1"
   102  	c.Assert(s.spec.AddModule(mod), IsNil)
   103  	c.Assert(s.spec.AddModule(mod), IsNil)
   104  	c.Assert(s.spec.Modules(), DeepEquals, map[string]bool{"module1": true})
   105  
   106  	var r interfaces.Specification = s.spec
   107  	c.Assert(r.AddConnectedPlug(s.iface1, s.plug, s.slot), IsNil)
   108  	c.Assert(r.AddConnectedSlot(s.iface1, s.plug, s.slot), IsNil)
   109  	c.Assert(r.AddPermanentPlug(s.iface1, s.plugInfo), IsNil)
   110  	c.Assert(r.AddPermanentSlot(s.iface1, s.slotInfo), IsNil)
   111  
   112  	c.Assert(r.AddConnectedPlug(s.iface2, s.plug, s.slot), IsNil)
   113  	c.Assert(r.AddConnectedSlot(s.iface2, s.plug, s.slot), IsNil)
   114  	c.Assert(r.AddPermanentPlug(s.iface2, s.plugInfo), IsNil)
   115  	c.Assert(r.AddPermanentSlot(s.iface2, s.slotInfo), IsNil)
   116  	c.Assert(s.spec.Modules(), DeepEquals, map[string]bool{
   117  		"module1": true, "module2": true, "module3": true, "module4": true, "module5": true, "module6": true})
   118  }
   119  
   120  // The kmod.Specification can be used through the interfaces.Specification interface
   121  func (s *specSuite) TestSpecificationIface(c *C) {
   122  	var r interfaces.Specification = s.spec
   123  	c.Assert(r.AddConnectedPlug(s.iface1, s.plug, s.slot), IsNil)
   124  	c.Assert(r.AddConnectedSlot(s.iface1, s.plug, s.slot), IsNil)
   125  	c.Assert(r.AddPermanentPlug(s.iface1, s.plugInfo), IsNil)
   126  	c.Assert(r.AddPermanentSlot(s.iface1, s.slotInfo), IsNil)
   127  	c.Assert(s.spec.Modules(), DeepEquals, map[string]bool{
   128  		"module1": true, "module2": true, "module3": true, "module4": true})
   129  }