github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/interfaces/builtin/system_source_code_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 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/dirs"
    26  	"github.com/snapcore/snapd/interfaces"
    27  	"github.com/snapcore/snapd/interfaces/apparmor"
    28  	"github.com/snapcore/snapd/interfaces/builtin"
    29  	"github.com/snapcore/snapd/snap"
    30  	"github.com/snapcore/snapd/testutil"
    31  )
    32  
    33  type systemSourceCodeSuite struct {
    34  	iface        interfaces.Interface
    35  	coreSlotInfo *snap.SlotInfo
    36  	coreSlot     *interfaces.ConnectedSlot
    37  	plugInfo     *snap.PlugInfo
    38  	plug         *interfaces.ConnectedPlug
    39  }
    40  
    41  var _ = Suite(&systemSourceCodeSuite{iface: builtin.MustInterface("system-source-code")})
    42  
    43  const systemSourceCodeConsumerYaml = `name: consumer
    44  version: 0
    45  apps:
    46   app:
    47    plugs: [system-source-code]
    48  `
    49  
    50  const systemSourceCodeCoreYaml = `name: core
    51  version: 0
    52  type: os
    53  slots:
    54    system-source-code:
    55  `
    56  
    57  func (s *systemSourceCodeSuite) SetUpTest(c *C) {
    58  	s.plug, s.plugInfo = MockConnectedPlug(c, systemSourceCodeConsumerYaml, nil, "system-source-code")
    59  	s.coreSlot, s.coreSlotInfo = MockConnectedSlot(c, systemSourceCodeCoreYaml, nil, "system-source-code")
    60  }
    61  
    62  func (s *systemSourceCodeSuite) TearDownTest(c *C) {
    63  	dirs.SetRootDir("/")
    64  }
    65  
    66  func (s *systemSourceCodeSuite) TestName(c *C) {
    67  	c.Assert(s.iface.Name(), Equals, "system-source-code")
    68  }
    69  
    70  func (s *systemSourceCodeSuite) TestSanitizeSlot(c *C) {
    71  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.coreSlotInfo), IsNil)
    72  }
    73  
    74  func (s *systemSourceCodeSuite) TestSanitizePlug(c *C) {
    75  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    76  }
    77  
    78  func (s *systemSourceCodeSuite) TestAppArmorSpec(c *C) {
    79  	spec := &apparmor.Specification{}
    80  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.coreSlot), IsNil)
    81  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
    82  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "# Description: can access /usr/src for kernel headers, etc")
    83  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "/usr/src/{,**} r,")
    84  }
    85  
    86  func (s *systemSourceCodeSuite) TestStaticInfo(c *C) {
    87  	si := interfaces.StaticInfoOf(s.iface)
    88  	c.Assert(si.ImplicitOnCore, Equals, true)
    89  	c.Assert(si.ImplicitOnClassic, Equals, true)
    90  	c.Assert(si.Summary, Equals, `allows read-only access to /usr/src on the system`)
    91  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "system-source-code")
    92  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "deny-auto-connection: true")
    93  }
    94  
    95  func (s *systemSourceCodeSuite) TestInterfaces(c *C) {
    96  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
    97  }