github.com/ubuntu-core/snappy@v0.0.0-20210827154228-9e584df982bb/interfaces/builtin/system_packages_doc_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/interfaces/mount"
    30  	"github.com/snapcore/snapd/release"
    31  	"github.com/snapcore/snapd/snap"
    32  	"github.com/snapcore/snapd/testutil"
    33  )
    34  
    35  type systemPackagesDocSuite struct {
    36  	iface        interfaces.Interface
    37  	coreSlotInfo *snap.SlotInfo
    38  	coreSlot     *interfaces.ConnectedSlot
    39  	plugInfo     *snap.PlugInfo
    40  	plug         *interfaces.ConnectedPlug
    41  }
    42  
    43  var _ = Suite(&systemPackagesDocSuite{iface: builtin.MustInterface("system-packages-doc")})
    44  
    45  const systemPackagesDocConsumerYaml = `name: consumer
    46  version: 0
    47  apps:
    48   app:
    49    plugs: [system-packages-doc]
    50  `
    51  
    52  const systemPackagesDocCoreYaml = `name: core
    53  version: 0
    54  type: os
    55  slots:
    56    system-packages-doc:
    57  `
    58  
    59  func (s *systemPackagesDocSuite) SetUpTest(c *C) {
    60  	s.plug, s.plugInfo = MockConnectedPlug(c, systemPackagesDocConsumerYaml, nil, "system-packages-doc")
    61  	s.coreSlot, s.coreSlotInfo = MockConnectedSlot(c, systemPackagesDocCoreYaml, nil, "system-packages-doc")
    62  }
    63  
    64  func (s *systemPackagesDocSuite) TearDownTest(c *C) {
    65  	dirs.SetRootDir("/")
    66  }
    67  
    68  func (s *systemPackagesDocSuite) TestName(c *C) {
    69  	c.Assert(s.iface.Name(), Equals, "system-packages-doc")
    70  }
    71  
    72  func (s *systemPackagesDocSuite) TestSanitizeSlot(c *C) {
    73  	c.Assert(interfaces.BeforePrepareSlot(s.iface, s.coreSlotInfo), IsNil)
    74  }
    75  
    76  func (s *systemPackagesDocSuite) TestSanitizePlug(c *C) {
    77  	c.Assert(interfaces.BeforePreparePlug(s.iface, s.plugInfo), IsNil)
    78  }
    79  
    80  func (s *systemPackagesDocSuite) TestAppArmorSpec(c *C) {
    81  	restore := release.MockOnClassic(true)
    82  	defer restore()
    83  
    84  	spec := &apparmor.Specification{}
    85  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.coreSlot), IsNil)
    86  	c.Assert(spec.SecurityTags(), DeepEquals, []string{"snap.consumer.app"})
    87  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "# Description: can access documentation of system packages.")
    88  	c.Assert(spec.SnippetForTag("snap.consumer.app"), testutil.Contains, "/usr/share/doc/{,**} r,")
    89  
    90  	updateNS := spec.UpdateNS()
    91  	c.Check(updateNS, testutil.Contains, "  # Mount documentation of system packages\n")
    92  	c.Check(updateNS, testutil.Contains, "  mount options=(bind) /var/lib/snapd/hostfs/usr/share/doc/ -> /usr/share/doc/,\n")
    93  	c.Check(updateNS, testutil.Contains, "  remount options=(bind, ro) /usr/share/doc/,\n")
    94  	c.Check(updateNS, testutil.Contains, "  umount /usr/share/doc/,\n")
    95  }
    96  
    97  func (s *systemPackagesDocSuite) TestMountSpec(c *C) {
    98  	restore := release.MockOnClassic(true)
    99  	defer restore()
   100  
   101  	spec := &mount.Specification{}
   102  	c.Assert(spec.AddConnectedPlug(s.iface, s.plug, s.coreSlot), IsNil)
   103  
   104  	entries := spec.MountEntries()
   105  	c.Assert(entries, HasLen, 1)
   106  	c.Check(entries[0].Name, Equals, "/var/lib/snapd/hostfs/usr/share/doc")
   107  	c.Check(entries[0].Dir, Equals, "/usr/share/doc")
   108  	c.Check(entries[0].Options, DeepEquals, []string{"bind", "ro"})
   109  
   110  	entries = spec.UserMountEntries()
   111  	c.Assert(entries, HasLen, 0)
   112  }
   113  
   114  func (s *systemPackagesDocSuite) TestStaticInfo(c *C) {
   115  	si := interfaces.StaticInfoOf(s.iface)
   116  	c.Assert(si.ImplicitOnCore, Equals, false)
   117  	c.Assert(si.ImplicitOnClassic, Equals, true)
   118  	c.Assert(si.Summary, Equals, `allows access to documentation of system packages`)
   119  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "system-packages-doc")
   120  	c.Assert(si.BaseDeclarationSlots, testutil.Contains, "deny-auto-connection: true")
   121  	c.Assert(si.AffectsPlugOnRefresh, Equals, true)
   122  }
   123  
   124  func (s *systemPackagesDocSuite) TestInterfaces(c *C) {
   125  	c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
   126  }