github.com/stulluk/snapd@v0.0.0-20210611110309-f6d5d5bd24b0/cmd/snap/main_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 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 main_test
    21  
    22  import (
    23  	"bytes"
    24  	"encoding/json"
    25  	"fmt"
    26  	"io/ioutil"
    27  	"net/http"
    28  	"net/http/httptest"
    29  	"os"
    30  	"path/filepath"
    31  	"strings"
    32  	"testing"
    33  
    34  	"github.com/jessevdk/go-flags"
    35  	"golang.org/x/crypto/ssh/terminal"
    36  	. "gopkg.in/check.v1"
    37  
    38  	"github.com/snapcore/snapd/dirs"
    39  	"github.com/snapcore/snapd/interfaces"
    40  	"github.com/snapcore/snapd/logger"
    41  	"github.com/snapcore/snapd/osutil"
    42  	"github.com/snapcore/snapd/snapdtool"
    43  	"github.com/snapcore/snapd/testutil"
    44  
    45  	snap "github.com/snapcore/snapd/cmd/snap"
    46  )
    47  
    48  // Hook up check.v1 into the "go test" runner
    49  func Test(t *testing.T) { TestingT(t) }
    50  
    51  type BaseSnapSuite struct {
    52  	testutil.BaseTest
    53  	stdin    *bytes.Buffer
    54  	stdout   *bytes.Buffer
    55  	stderr   *bytes.Buffer
    56  	password string
    57  
    58  	AuthFile string
    59  }
    60  
    61  func (s *BaseSnapSuite) readPassword(fd int) ([]byte, error) {
    62  	return []byte(s.password), nil
    63  }
    64  
    65  func (s *BaseSnapSuite) SetUpTest(c *C) {
    66  	s.BaseTest.SetUpTest(c)
    67  	dirs.SetRootDir(c.MkDir())
    68  
    69  	path := os.Getenv("PATH")
    70  	s.AddCleanup(func() {
    71  		os.Setenv("PATH", path)
    72  	})
    73  	os.Setenv("PATH", path+":"+dirs.SnapBinariesDir)
    74  
    75  	s.stdin = bytes.NewBuffer(nil)
    76  	s.stdout = bytes.NewBuffer(nil)
    77  	s.stderr = bytes.NewBuffer(nil)
    78  	s.password = ""
    79  
    80  	snap.Stdin = s.stdin
    81  	snap.Stdout = s.stdout
    82  	snap.Stderr = s.stderr
    83  	snap.ReadPassword = s.readPassword
    84  	s.AuthFile = filepath.Join(c.MkDir(), "json")
    85  	os.Setenv(TestAuthFileEnvKey, s.AuthFile)
    86  
    87  	s.AddCleanup(interfaces.MockSystemKey(`
    88  {
    89  "build-id": "7a94e9736c091b3984bd63f5aebfc883c4d859e0",
    90  "apparmor-features": ["caps", "dbus"]
    91  }`))
    92  	err := os.MkdirAll(filepath.Dir(dirs.SnapSystemKeyFile), 0755)
    93  	c.Assert(err, IsNil)
    94  	err = interfaces.WriteSystemKey()
    95  	c.Assert(err, IsNil)
    96  
    97  	s.AddCleanup(snap.MockIsStdoutTTY(false))
    98  	s.AddCleanup(snap.MockIsStdinTTY(false))
    99  
   100  	s.AddCleanup(snap.MockSELinuxIsEnabled(func() (bool, error) { return false, nil }))
   101  
   102  	// mock an empty cmdline since we check the cmdline to check whether we are
   103  	// in install mode or not and we don't want to use the host's proc/cmdline
   104  	s.AddCleanup(osutil.MockProcCmdline(filepath.Join(c.MkDir(), "proc/cmdline")))
   105  }
   106  
   107  func (s *BaseSnapSuite) TearDownTest(c *C) {
   108  	snap.Stdin = os.Stdin
   109  	snap.Stdout = os.Stdout
   110  	snap.Stderr = os.Stderr
   111  	snap.ReadPassword = terminal.ReadPassword
   112  
   113  	c.Assert(s.AuthFile == "", Equals, false)
   114  	err := os.Unsetenv(TestAuthFileEnvKey)
   115  	c.Assert(err, IsNil)
   116  	dirs.SetRootDir("/")
   117  	s.BaseTest.TearDownTest(c)
   118  }
   119  
   120  func (s *BaseSnapSuite) Stdout() string {
   121  	return s.stdout.String()
   122  }
   123  
   124  func (s *BaseSnapSuite) Stderr() string {
   125  	return s.stderr.String()
   126  }
   127  
   128  func (s *BaseSnapSuite) ResetStdStreams() {
   129  	s.stdin.Reset()
   130  	s.stdout.Reset()
   131  	s.stderr.Reset()
   132  }
   133  
   134  func (s *BaseSnapSuite) RedirectClientToTestServer(handler func(http.ResponseWriter, *http.Request)) {
   135  	server := httptest.NewServer(http.HandlerFunc(handler))
   136  	s.BaseTest.AddCleanup(func() { server.Close() })
   137  	snap.ClientConfig.BaseURL = server.URL
   138  	s.BaseTest.AddCleanup(func() { snap.ClientConfig.BaseURL = "" })
   139  }
   140  
   141  func (s *BaseSnapSuite) Login(c *C) {
   142  	err := osutil.AtomicWriteFile(s.AuthFile, []byte(TestAuthFileContents), 0600, 0)
   143  	c.Assert(err, IsNil)
   144  }
   145  
   146  func (s *BaseSnapSuite) Logout(c *C) {
   147  	if osutil.FileExists(s.AuthFile) {
   148  		c.Assert(os.Remove(s.AuthFile), IsNil)
   149  	}
   150  }
   151  
   152  type SnapSuite struct {
   153  	BaseSnapSuite
   154  }
   155  
   156  var _ = Suite(&SnapSuite{})
   157  
   158  // DecodedRequestBody returns the JSON-decoded body of the request.
   159  func DecodedRequestBody(c *C, r *http.Request) map[string]interface{} {
   160  	var body map[string]interface{}
   161  	decoder := json.NewDecoder(r.Body)
   162  	decoder.UseNumber()
   163  	err := decoder.Decode(&body)
   164  	c.Assert(err, IsNil)
   165  	return body
   166  }
   167  
   168  // EncodeResponseBody writes JSON-serialized body to the response writer.
   169  func EncodeResponseBody(c *C, w http.ResponseWriter, body interface{}) {
   170  	encoder := json.NewEncoder(w)
   171  	err := encoder.Encode(body)
   172  	c.Assert(err, IsNil)
   173  }
   174  
   175  func mockArgs(args ...string) (restore func()) {
   176  	old := os.Args
   177  	os.Args = args
   178  	return func() { os.Args = old }
   179  }
   180  
   181  func mockSnapConfine(libExecDir string) func() {
   182  	snapConfine := filepath.Join(libExecDir, "snap-confine")
   183  	if err := os.MkdirAll(libExecDir, 0755); err != nil {
   184  		panic(err)
   185  	}
   186  	if err := ioutil.WriteFile(snapConfine, nil, 0644); err != nil {
   187  		panic(err)
   188  	}
   189  	return func() {
   190  		if err := os.Remove(snapConfine); err != nil {
   191  			panic(err)
   192  		}
   193  	}
   194  }
   195  
   196  const TestAuthFileEnvKey = "SNAPD_AUTH_DATA_FILENAME"
   197  const TestAuthFileContents = `{"id":123,"email":"hello@mail.com","macaroon":"MDAxM2xvY2F0aW9uIHNuYXBkCjAwMTJpZGVudGlmaWVyIDQzCjAwMmZzaWduYXR1cmUg5RfMua72uYop4t3cPOBmGUuaoRmoDH1HV62nMJq7eqAK"}`
   198  
   199  func (s *SnapSuite) TestErrorResult(c *C) {
   200  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   201  		fmt.Fprintln(w, `{"type": "error", "result": {"message": "cannot do something"}}`)
   202  	})
   203  
   204  	restore := mockArgs("snap", "install", "foo")
   205  	defer restore()
   206  
   207  	err := snap.RunMain()
   208  	c.Assert(err, ErrorMatches, `cannot do something`)
   209  }
   210  
   211  func (s *SnapSuite) TestAccessDeniedHint(c *C) {
   212  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   213  		fmt.Fprintln(w, `{"type": "error", "result": {"message": "access denied", "kind": "login-required"}, "status-code": 401}`)
   214  	})
   215  
   216  	restore := mockArgs("snap", "install", "foo")
   217  	defer restore()
   218  
   219  	err := snap.RunMain()
   220  	c.Assert(err, NotNil)
   221  	c.Check(err.Error(), Equals, `access denied (try with sudo)`)
   222  }
   223  
   224  func (s *SnapSuite) TestExtraArgs(c *C) {
   225  	restore := mockArgs("snap", "abort", "1", "xxx", "zzz")
   226  	defer restore()
   227  
   228  	err := snap.RunMain()
   229  	c.Assert(err, ErrorMatches, `too many arguments for command`)
   230  }
   231  
   232  func (s *SnapSuite) TestVersionOnClassic(c *C) {
   233  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   234  		fmt.Fprintln(w, `{"type":"sync","status-code":200,"status":"OK","result":{"on-classic":true,"os-release":{"id":"ubuntu","version-id":"12.34"},"series":"56","version":"7.89"}}`)
   235  	})
   236  	restore := mockArgs("snap", "--version")
   237  	defer restore()
   238  	restore = snapdtool.MockVersion("4.56")
   239  	defer restore()
   240  
   241  	c.Assert(func() { snap.RunMain() }, PanicMatches, `internal error: exitStatus\{0\} .*`)
   242  	c.Assert(s.Stdout(), Equals, "snap    4.56\nsnapd   7.89\nseries  56\nubuntu  12.34\n")
   243  	c.Assert(s.Stderr(), Equals, "")
   244  }
   245  
   246  func (s *SnapSuite) TestVersionOnAllSnap(c *C) {
   247  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   248  		fmt.Fprintln(w, `{"type":"sync","status-code":200,"status":"OK","result":{"os-release":{"id":"ubuntu","version-id":"12.34"},"series":"56","version":"7.89"}}`)
   249  	})
   250  	restore := mockArgs("snap", "--version")
   251  	defer restore()
   252  	restore = snapdtool.MockVersion("4.56")
   253  	defer restore()
   254  
   255  	c.Assert(func() { snap.RunMain() }, PanicMatches, `internal error: exitStatus\{0\} .*`)
   256  	c.Assert(s.Stdout(), Equals, "snap    4.56\nsnapd   7.89\nseries  56\n")
   257  	c.Assert(s.Stderr(), Equals, "")
   258  }
   259  
   260  func (s *SnapSuite) TestUnknownCommand(c *C) {
   261  	restore := mockArgs("snap", "unknowncmd")
   262  	defer restore()
   263  
   264  	err := snap.RunMain()
   265  	c.Assert(err, ErrorMatches, `unknown command "unknowncmd", see 'snap help'.`)
   266  }
   267  
   268  func (s *SnapSuite) TestResolveApp(c *C) {
   269  	err := os.MkdirAll(dirs.SnapBinariesDir, 0755)
   270  	c.Assert(err, IsNil)
   271  
   272  	// "wrapper" symlinks
   273  	err = os.Symlink("/usr/bin/snap", filepath.Join(dirs.SnapBinariesDir, "foo"))
   274  	c.Assert(err, IsNil)
   275  	err = os.Symlink("/usr/bin/snap", filepath.Join(dirs.SnapBinariesDir, "foo.bar"))
   276  	c.Assert(err, IsNil)
   277  
   278  	// alias symlinks
   279  	err = os.Symlink("foo", filepath.Join(dirs.SnapBinariesDir, "foo_"))
   280  	c.Assert(err, IsNil)
   281  	err = os.Symlink("foo.bar", filepath.Join(dirs.SnapBinariesDir, "foo_bar-1"))
   282  	c.Assert(err, IsNil)
   283  
   284  	snapApp, err := snap.ResolveApp("foo")
   285  	c.Assert(err, IsNil)
   286  	c.Check(snapApp, Equals, "foo")
   287  
   288  	snapApp, err = snap.ResolveApp("foo.bar")
   289  	c.Assert(err, IsNil)
   290  	c.Check(snapApp, Equals, "foo.bar")
   291  
   292  	snapApp, err = snap.ResolveApp("foo_")
   293  	c.Assert(err, IsNil)
   294  	c.Check(snapApp, Equals, "foo")
   295  
   296  	snapApp, err = snap.ResolveApp("foo_bar-1")
   297  	c.Assert(err, IsNil)
   298  	c.Check(snapApp, Equals, "foo.bar")
   299  
   300  	_, err = snap.ResolveApp("baz")
   301  	c.Check(err, NotNil)
   302  }
   303  
   304  func (s *SnapSuite) TestFirstNonOptionIsRun(c *C) {
   305  	osArgs := os.Args
   306  	defer func() {
   307  		os.Args = osArgs
   308  	}()
   309  	for _, negative := range []string{
   310  		"",
   311  		"snap",
   312  		"snap verb",
   313  		"snap verb --flag arg",
   314  		"snap verb arg --flag",
   315  		"snap --global verb --flag arg",
   316  	} {
   317  		os.Args = strings.Fields(negative)
   318  		c.Check(snap.FirstNonOptionIsRun(), Equals, false)
   319  	}
   320  
   321  	for _, positive := range []string{
   322  		"snap run",
   323  		"snap run --flag",
   324  		"snap run --flag arg",
   325  		"snap run arg --flag",
   326  		"snap --global run",
   327  		"snap --global run --flag",
   328  		"snap --global run --flag arg",
   329  		"snap --global run arg --flag",
   330  	} {
   331  		os.Args = strings.Fields(positive)
   332  		c.Check(snap.FirstNonOptionIsRun(), Equals, true)
   333  	}
   334  }
   335  
   336  func (s *SnapSuite) TestLintDesc(c *C) {
   337  	log, restore := logger.MockLogger()
   338  	defer restore()
   339  
   340  	// LintDesc is happy about capitalized description.
   341  	snap.LintDesc("command", "<option>", "Description ...", "")
   342  	c.Check(log.String(), HasLen, 0)
   343  	log.Reset()
   344  
   345  	// LintDesc complains about lowercase description and mentions the locale
   346  	// that the system is currently in.
   347  	prevValue := os.Getenv("LC_MESSAGES")
   348  	os.Setenv("LC_MESSAGES", "en_US")
   349  	defer func() {
   350  		os.Setenv("LC_MESSAGES", prevValue)
   351  	}()
   352  	snap.LintDesc("command", "<option>", "description", "")
   353  	c.Check(log.String(), testutil.Contains, `description of command's "<option>" is lowercase in locale "en_US": "description"`)
   354  	log.Reset()
   355  
   356  	// LintDesc does not complain about lowercase description starting with login.ubuntu.com
   357  	snap.LintDesc("command", "<option>", "login.ubuntu.com description", "")
   358  	c.Check(log.String(), HasLen, 0)
   359  	log.Reset()
   360  
   361  	// LintDesc panics when original description is present.
   362  	fn := func() {
   363  		snap.LintDesc("command", "<option>", "description", "original description")
   364  	}
   365  	c.Check(fn, PanicMatches, `description of command's "<option>" of "original description" set from tag \(=> no i18n\)`)
   366  	log.Reset()
   367  
   368  	// LintDesc panics when option name is empty.
   369  	fn = func() {
   370  		snap.LintDesc("command", "", "description", "")
   371  	}
   372  	c.Check(fn, PanicMatches, `option on "command" has no name`)
   373  	log.Reset()
   374  
   375  	snap.LintDesc("snap-advise", "from-apt", "snap-advise will run as a hook", "")
   376  	c.Check(log.String(), HasLen, 0)
   377  	log.Reset()
   378  }
   379  
   380  func (s *SnapSuite) TestLintArg(c *C) {
   381  	log, restore := logger.MockLogger()
   382  	defer restore()
   383  
   384  	// LintArg is happy when option is enclosed with < >.
   385  	snap.LintArg("command", "<option>", "Description", "")
   386  	c.Check(log.String(), HasLen, 0)
   387  	log.Reset()
   388  
   389  	// LintArg complains about when option is not properly enclosed with < >.
   390  	snap.LintArg("command", "option", "Description", "")
   391  	c.Check(log.String(), testutil.Contains, `argument "command"'s "option" should begin with < and end with >`)
   392  	log.Reset()
   393  	snap.LintArg("command", "<option", "Description", "")
   394  	c.Check(log.String(), testutil.Contains, `argument "command"'s "<option" should begin with < and end with >`)
   395  	log.Reset()
   396  	snap.LintArg("command", "option>", "Description", "")
   397  	c.Check(log.String(), testutil.Contains, `argument "command"'s "option>" should begin with < and end with >`)
   398  	log.Reset()
   399  
   400  	// LintArg ignores the special case of <option>s.
   401  	snap.LintArg("command", "<option>s", "Description", "")
   402  	c.Check(log.String(), HasLen, 0)
   403  	log.Reset()
   404  }
   405  
   406  func (s *SnapSuite) TestFixupArg(c *C) {
   407  	c.Check(snap.FixupArg("option"), Equals, "option")
   408  	c.Check(snap.FixupArg("<option>"), Equals, "<option>")
   409  	// Trailing ">s" is fixed to just >.
   410  	c.Check(snap.FixupArg("<option>s"), Equals, "<option>")
   411  }
   412  
   413  func (s *SnapSuite) TestSetsUserAgent(c *C) {
   414  	testServerHit := false
   415  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   416  		c.Check(r.Header.Get("User-Agent"), Matches, "snapd/.*")
   417  		testServerHit = true
   418  
   419  		fmt.Fprintln(w, `{"type": "error", "result": {"message": "cannot do something"}}`)
   420  	})
   421  	restore := mockArgs("snap", "install", "foo")
   422  	defer restore()
   423  
   424  	_ = snap.RunMain()
   425  	c.Assert(testServerHit, Equals, true)
   426  }
   427  
   428  func (s *SnapSuite) TestCompletionHandlerSkipsHidden(c *C) {
   429  	snap.MarkForNoCompletion(snap.HiddenCmd("bar yadda yack", false))
   430  	snap.MarkForNoCompletion(snap.HiddenCmd("bar yack yack yack", true))
   431  	snap.CompletionHandler([]flags.Completion{
   432  		{Item: "foo", Description: "foo yadda yadda"},
   433  		{Item: "bar", Description: "bar yadda yack"},
   434  		{Item: "baz", Description: "bar yack yack yack"},
   435  	})
   436  	c.Check(s.Stdout(), Equals, "foo\nbaz\n")
   437  }