github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/wrappers/binaries_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 wrappers_test
    21  
    22  import (
    23  	"io/ioutil"
    24  	"os"
    25  	"path/filepath"
    26  	"testing"
    27  
    28  	. "gopkg.in/check.v1"
    29  
    30  	"github.com/snapcore/snapd/dirs"
    31  	"github.com/snapcore/snapd/osutil"
    32  	"github.com/snapcore/snapd/snap"
    33  	"github.com/snapcore/snapd/snap/snaptest"
    34  	"github.com/snapcore/snapd/wrappers"
    35  )
    36  
    37  func TestWrappers(t *testing.T) { TestingT(t) }
    38  
    39  type binariesTestSuite struct {
    40  	tempdir string
    41  	base    string
    42  }
    43  
    44  // silly wrappers to get better failure messages
    45  type noBaseBinariesSuite struct{ binariesTestSuite }
    46  type withBaseBinariesSuite struct{ binariesTestSuite }
    47  type withSnapdBinariesSuite struct{ binariesTestSuite }
    48  
    49  var _ = Suite(&noBaseBinariesSuite{})
    50  var _ = Suite(&withBaseBinariesSuite{binariesTestSuite{base: "core99"}})
    51  var _ = Suite(&withSnapdBinariesSuite{binariesTestSuite{base: "core-with-snapd"}})
    52  
    53  func (s *binariesTestSuite) SetUpTest(c *C) {
    54  	s.tempdir = c.MkDir()
    55  	dirs.SetRootDir(s.tempdir)
    56  }
    57  
    58  func (s *binariesTestSuite) TearDownTest(c *C) {
    59  	dirs.SetRootDir("")
    60  }
    61  
    62  const packageHello = `name: hello-snap
    63  version: 1.10
    64  summary: hello
    65  description: Hello...
    66  apps:
    67   hello:
    68     command: bin/hello
    69   world:
    70     command: bin/world
    71     completer: world-completer.sh
    72   svc1:
    73    command: bin/hello
    74    stop-command: bin/goodbye
    75    post-stop-command: bin/missya
    76    daemon: forking
    77  `
    78  
    79  func (s *binariesTestSuite) TestAddSnapBinariesAndRemove(c *C) {
    80  	// no completers support -> no problem \o/
    81  	c.Assert(osutil.FileExists(dirs.CompletersDir), Equals, false)
    82  
    83  	s.testAddSnapBinariesAndRemove(c)
    84  }
    85  
    86  func (s *binariesTestSuite) TestAddSnapBinariesAndRemoveWithCompleters(c *C) {
    87  	c.Assert(os.MkdirAll(dirs.CompletersDir, 0755), IsNil)
    88  	if s.base == "core-with-snapd" {
    89  		c.Check(os.MkdirAll(filepath.Join(dirs.SnapMountDir, "snapd/current/usr/lib/snapd"), 0755), IsNil)
    90  	}
    91  	c.Assert(os.MkdirAll(filepath.Dir(dirs.CompleteShPath(s.base)), 0755), IsNil)
    92  	c.Assert(ioutil.WriteFile(dirs.CompleteShPath(s.base), nil, 0644), IsNil)
    93  	// full completers support -> we get completers \o/
    94  
    95  	s.testAddSnapBinariesAndRemove(c)
    96  }
    97  
    98  func (s *binariesTestSuite) TestAddSnapBinariesAndRemoveWithExistingCompleters(c *C) {
    99  	c.Assert(os.MkdirAll(dirs.CompletersDir, 0755), IsNil)
   100  	if s.base == "core-with-snapd" {
   101  		c.Check(os.MkdirAll(filepath.Join(dirs.SnapMountDir, "snapd/current/usr/lib/snapd"), 0755), IsNil)
   102  	}
   103  	c.Assert(os.MkdirAll(filepath.Dir(dirs.CompleteShPath(s.base)), 0755), IsNil)
   104  	c.Assert(ioutil.WriteFile(dirs.CompleteShPath(s.base), nil, 0644), IsNil)
   105  	// existing completers -> they're left alone \o/
   106  	c.Assert(ioutil.WriteFile(filepath.Join(dirs.CompletersDir, "hello-snap.world"), nil, 0644), IsNil)
   107  
   108  	s.testAddSnapBinariesAndRemove(c)
   109  }
   110  
   111  func (s *binariesTestSuite) testAddSnapBinariesAndRemove(c *C) {
   112  	info := snaptest.MockSnap(c, packageHello+"base: "+s.base+"\n", &snap.SideInfo{Revision: snap.R(11)})
   113  	completer := filepath.Join(dirs.CompletersDir, "hello-snap.world")
   114  	completerExisted := osutil.FileExists(completer)
   115  
   116  	err := wrappers.AddSnapBinaries(info)
   117  	c.Assert(err, IsNil)
   118  
   119  	bins := []string{"hello-snap.hello", "hello-snap.world"}
   120  
   121  	for _, bin := range bins {
   122  		link := filepath.Join(dirs.SnapBinariesDir, bin)
   123  		target, err := os.Readlink(link)
   124  		c.Assert(err, IsNil, Commentf(bin))
   125  		c.Check(target, Equals, "/usr/bin/snap", Commentf(bin))
   126  	}
   127  
   128  	if osutil.FileExists(dirs.CompletersDir) {
   129  		if completerExisted {
   130  			// there was a completer there before, so it should _not_ be a symlink to our complete.sh
   131  			c.Assert(osutil.IsSymlink(completer), Equals, false)
   132  		} else {
   133  			target, err := os.Readlink(completer)
   134  			c.Assert(err, IsNil)
   135  			c.Check(target, Equals, dirs.CompleteShPath(s.base))
   136  		}
   137  	}
   138  
   139  	err = wrappers.RemoveSnapBinaries(info)
   140  	c.Assert(err, IsNil)
   141  
   142  	for _, bin := range bins {
   143  		link := filepath.Join(dirs.SnapBinariesDir, bin)
   144  		c.Check(osutil.FileExists(link), Equals, false, Commentf(bin))
   145  	}
   146  
   147  	// we left the existing completer alone, but removed it otherwise
   148  	c.Check(osutil.FileExists(completer), Equals, completerExisted)
   149  }
   150  
   151  func (s *binariesTestSuite) TestAddSnapBinariesCleansUpOnFailure(c *C) {
   152  	link := filepath.Join(dirs.SnapBinariesDir, "hello-snap.hello")
   153  	c.Assert(osutil.FileExists(link), Equals, false)
   154  	c.Assert(os.MkdirAll(filepath.Join(dirs.SnapBinariesDir, "hello-snap.bye", "potato"), 0755), IsNil)
   155  
   156  	info := snaptest.MockSnap(c, packageHello+`
   157   bye:
   158    command: bin/bye
   159  `, &snap.SideInfo{Revision: snap.R(11)})
   160  
   161  	err := wrappers.AddSnapBinaries(info)
   162  	c.Assert(err, NotNil)
   163  
   164  	c.Check(osutil.FileExists(link), Equals, false)
   165  }