github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/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", DaemonScope: snap.SystemDaemon})
    82  	c.Check(apps["bar"], DeepEquals, &snap.AppInfo{Snap: info, Name: "bar", Daemon: "simple", DaemonScope: snap.SystemDaemon})
    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", DaemonScope: snap.SystemDaemon})
    88  	c.Check(apps["baz"], DeepEquals, &snap.AppInfo{Snap: info, Name: "baz", Daemon: "simple", DaemonScope: snap.SystemDaemon})
    89  }
    90  
    91  func (s *brokenSuite) TestGuessAppsForBrokenUserServices(c *C) {
    92  	touch(c, filepath.Join(dirs.SnapUserServicesDir, "snap.foo.foo.service"))
    93  	touch(c, filepath.Join(dirs.SnapUserServicesDir, "snap.foo.bar.service"))
    94  	touch(c, filepath.Join(dirs.SnapUserServicesDir, "snap.foo_instance.foo.service"))
    95  	touch(c, filepath.Join(dirs.SnapUserServicesDir, "snap.foo_instance.baz.service"))
    96  
    97  	info := &snap.Info{SuggestedName: "foo"}
    98  	apps := snap.GuessAppsForBroken(info)
    99  	c.Check(apps, HasLen, 2)
   100  	c.Check(apps["foo"], DeepEquals, &snap.AppInfo{Snap: info, Name: "foo", Daemon: "simple", DaemonScope: snap.UserDaemon})
   101  	c.Check(apps["bar"], DeepEquals, &snap.AppInfo{Snap: info, Name: "bar", Daemon: "simple", DaemonScope: snap.UserDaemon})
   102  
   103  	info = &snap.Info{SuggestedName: "foo", InstanceKey: "instance"}
   104  	apps = snap.GuessAppsForBroken(info)
   105  	c.Check(apps, HasLen, 2)
   106  	c.Check(apps["foo"], DeepEquals, &snap.AppInfo{Snap: info, Name: "foo", Daemon: "simple", DaemonScope: snap.UserDaemon})
   107  	c.Check(apps["baz"], DeepEquals, &snap.AppInfo{Snap: info, Name: "baz", Daemon: "simple", DaemonScope: snap.UserDaemon})
   108  }
   109  
   110  func (s *brokenSuite) TestForceRenamePlug(c *C) {
   111  	snapInfo := snaptest.MockInvalidInfo(c, `name: core
   112  version: 0
   113  plugs:
   114    old:
   115      interface: iface
   116  slots:
   117    old:
   118      interface: iface
   119  apps:
   120    app:
   121  hooks:
   122    configure:
   123  `, nil)
   124  	c.Assert(snapInfo.Plugs["old"], Not(IsNil))
   125  	c.Assert(snapInfo.Plugs["old"].Name, Equals, "old")
   126  	c.Assert(snapInfo.Slots["old"], Not(IsNil))
   127  	c.Assert(snapInfo.Slots["old"].Name, Equals, "old")
   128  	c.Assert(snapInfo.Apps["app"].Plugs["old"], DeepEquals, snapInfo.Plugs["old"])
   129  	c.Assert(snapInfo.Apps["app"].Slots["old"], DeepEquals, snapInfo.Slots["old"])
   130  	c.Assert(snapInfo.Hooks["configure"].Plugs["old"], DeepEquals, snapInfo.Plugs["old"])
   131  
   132  	// Rename the plug now.
   133  	snapInfo.ForceRenamePlug("old", "new")
   134  
   135  	// Check that there's no trace of the old plug name.
   136  	c.Assert(snapInfo.Plugs["old"], IsNil)
   137  	c.Assert(snapInfo.Plugs["new"], Not(IsNil))
   138  	c.Assert(snapInfo.Plugs["new"].Name, Equals, "new")
   139  	c.Assert(snapInfo.Apps["app"].Plugs["old"], IsNil)
   140  	c.Assert(snapInfo.Apps["app"].Plugs["new"], DeepEquals, snapInfo.Plugs["new"])
   141  	c.Assert(snapInfo.Hooks["configure"].Plugs["old"], IsNil)
   142  	c.Assert(snapInfo.Hooks["configure"].Plugs["new"], DeepEquals, snapInfo.Plugs["new"])
   143  
   144  	// Check that slots with the old name are unaffected.
   145  	c.Assert(snapInfo.Slots["old"], Not(IsNil))
   146  	c.Assert(snapInfo.Slots["old"].Name, Equals, "old")
   147  	c.Assert(snapInfo.Apps["app"].Slots["old"], DeepEquals, snapInfo.Slots["old"])
   148  
   149  	// Check that the rename made the snap valid now
   150  	c.Assert(snap.Validate(snapInfo), IsNil)
   151  }