github.com/rigado/snapd@v2.42.5-go-mod+incompatible/snap/broken_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2016 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 snap_test
    21  
    22  import (
    23  	"io/ioutil"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/dirs"
    30  	"github.com/snapcore/snapd/snap"
    31  	"github.com/snapcore/snapd/snap/snaptest"
    32  )
    33  
    34  type brokenSuite struct{}
    35  
    36  var _ = Suite(&brokenSuite{})
    37  
    38  func (s *brokenSuite) SetUpTest(c *C) {
    39  	dirs.SetRootDir(c.MkDir())
    40  }
    41  
    42  func (s *brokenSuite) TearDownTest(c *C) {
    43  	dirs.SetRootDir("")
    44  }
    45  
    46  func touch(c *C, path string) {
    47  	err := os.MkdirAll(filepath.Dir(path), 0755)
    48  	c.Assert(err, IsNil)
    49  	err = ioutil.WriteFile(path, nil, 0644)
    50  	c.Assert(err, IsNil)
    51  }
    52  
    53  func (s *brokenSuite) TestGuessAppsForBrokenBinaries(c *C) {
    54  	touch(c, filepath.Join(dirs.SnapBinariesDir, "foo"))
    55  	touch(c, filepath.Join(dirs.SnapBinariesDir, "foo.bar"))
    56  	touch(c, filepath.Join(dirs.SnapBinariesDir, "foo_instance"))
    57  	touch(c, filepath.Join(dirs.SnapBinariesDir, "foo_instance.baz"))
    58  
    59  	info := &snap.Info{SuggestedName: "foo"}
    60  	apps := snap.GuessAppsForBroken(info)
    61  	c.Check(apps, HasLen, 2)
    62  	c.Check(apps["foo"], DeepEquals, &snap.AppInfo{Snap: info, Name: "foo"})
    63  	c.Check(apps["bar"], DeepEquals, &snap.AppInfo{Snap: info, Name: "bar"})
    64  
    65  	info = &snap.Info{SuggestedName: "foo", InstanceKey: "instance"}
    66  	apps = snap.GuessAppsForBroken(info)
    67  	c.Check(apps, HasLen, 2)
    68  	c.Check(apps["foo"], DeepEquals, &snap.AppInfo{Snap: info, Name: "foo"})
    69  	c.Check(apps["baz"], DeepEquals, &snap.AppInfo{Snap: info, Name: "baz"})
    70  }
    71  
    72  func (s *brokenSuite) TestGuessAppsForBrokenServices(c *C) {
    73  	touch(c, filepath.Join(dirs.SnapServicesDir, "snap.foo.foo.service"))
    74  	touch(c, filepath.Join(dirs.SnapServicesDir, "snap.foo.bar.service"))
    75  	touch(c, filepath.Join(dirs.SnapServicesDir, "snap.foo_instance.foo.service"))
    76  	touch(c, filepath.Join(dirs.SnapServicesDir, "snap.foo_instance.baz.service"))
    77  
    78  	info := &snap.Info{SuggestedName: "foo"}
    79  	apps := snap.GuessAppsForBroken(info)
    80  	c.Check(apps, HasLen, 2)
    81  	c.Check(apps["foo"], DeepEquals, &snap.AppInfo{Snap: info, Name: "foo", Daemon: "simple"})
    82  	c.Check(apps["bar"], DeepEquals, &snap.AppInfo{Snap: info, Name: "bar", Daemon: "simple"})
    83  
    84  	info = &snap.Info{SuggestedName: "foo", InstanceKey: "instance"}
    85  	apps = snap.GuessAppsForBroken(info)
    86  	c.Check(apps, HasLen, 2)
    87  	c.Check(apps["foo"], DeepEquals, &snap.AppInfo{Snap: info, Name: "foo", Daemon: "simple"})
    88  	c.Check(apps["baz"], DeepEquals, &snap.AppInfo{Snap: info, Name: "baz", Daemon: "simple"})
    89  }
    90  
    91  func (s *brokenSuite) TestForceRenamePlug(c *C) {
    92  	snapInfo := snaptest.MockInvalidInfo(c, `name: core
    93  version: 0
    94  plugs:
    95    old:
    96      interface: iface
    97  slots:
    98    old:
    99      interface: iface
   100  apps:
   101    app:
   102  hooks:
   103    configure:
   104  `, nil)
   105  	c.Assert(snapInfo.Plugs["old"], Not(IsNil))
   106  	c.Assert(snapInfo.Plugs["old"].Name, Equals, "old")
   107  	c.Assert(snapInfo.Slots["old"], Not(IsNil))
   108  	c.Assert(snapInfo.Slots["old"].Name, Equals, "old")
   109  	c.Assert(snapInfo.Apps["app"].Plugs["old"], DeepEquals, snapInfo.Plugs["old"])
   110  	c.Assert(snapInfo.Apps["app"].Slots["old"], DeepEquals, snapInfo.Slots["old"])
   111  	c.Assert(snapInfo.Hooks["configure"].Plugs["old"], DeepEquals, snapInfo.Plugs["old"])
   112  
   113  	// Rename the plug now.
   114  	snapInfo.ForceRenamePlug("old", "new")
   115  
   116  	// Check that there's no trace of the old plug name.
   117  	c.Assert(snapInfo.Plugs["old"], IsNil)
   118  	c.Assert(snapInfo.Plugs["new"], Not(IsNil))
   119  	c.Assert(snapInfo.Plugs["new"].Name, Equals, "new")
   120  	c.Assert(snapInfo.Apps["app"].Plugs["old"], IsNil)
   121  	c.Assert(snapInfo.Apps["app"].Plugs["new"], DeepEquals, snapInfo.Plugs["new"])
   122  	c.Assert(snapInfo.Hooks["configure"].Plugs["old"], IsNil)
   123  	c.Assert(snapInfo.Hooks["configure"].Plugs["new"], DeepEquals, snapInfo.Plugs["new"])
   124  
   125  	// Check that slots with the old name are unaffected.
   126  	c.Assert(snapInfo.Slots["old"], Not(IsNil))
   127  	c.Assert(snapInfo.Slots["old"].Name, Equals, "old")
   128  	c.Assert(snapInfo.Apps["app"].Slots["old"], DeepEquals, snapInfo.Slots["old"])
   129  
   130  	// Check that the rename made the snap valid now
   131  	c.Assert(snap.Validate(snapInfo), IsNil)
   132  }