github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/cmd/snap/cmd_list_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  	"fmt"
    24  	"net/http"
    25  
    26  	"gopkg.in/check.v1"
    27  
    28  	snap "github.com/snapcore/snapd/cmd/snap"
    29  )
    30  
    31  func (s *SnapSuite) TestListHelp(c *check.C) {
    32  	msg := `Usage:
    33    snap.test list [list-OPTIONS] [<snap>...]
    34  
    35  The list command displays a summary of snaps installed in the current system.
    36  
    37  A green check mark (given color and unicode support) after a publisher name
    38  indicates that the publisher has been verified.
    39  
    40  [list command options]
    41        --all                           Show all revisions
    42        --color=[auto|never|always]     Use a little bit of color to highlight
    43                                        some things. (default: auto)
    44        --unicode=[auto|never|always]   Use a little bit of Unicode to improve
    45                                        legibility. (default: auto)
    46  `
    47  	s.testSubCommandHelp(c, "list", msg)
    48  }
    49  
    50  func (s *SnapSuite) TestList(c *check.C) {
    51  	n := 0
    52  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    53  		switch n {
    54  		case 0:
    55  			c.Check(r.Method, check.Equals, "GET")
    56  			c.Check(r.URL.Path, check.Equals, "/v2/snaps")
    57  			c.Check(r.URL.RawQuery, check.Equals, "")
    58  			fmt.Fprintln(w, `{"type": "sync", "result": [
    59  {
    60    "name": "foo",
    61    "status": "active",
    62    "version": "4.2",
    63    "developer": "bar",
    64    "publisher": {"id": "bar-id", "username": "bar", "display-name": "Bar", "validation": "unproven"},
    65    "health": {"status": "blocked"},
    66    "revision": 17,
    67    "tracking-channel": "potatoes"
    68  }]}`)
    69  		default:
    70  			c.Fatalf("expected to get 1 requests, now on %d", n+1)
    71  		}
    72  
    73  		n++
    74  	})
    75  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"list"})
    76  	c.Assert(err, check.IsNil)
    77  	c.Assert(rest, check.DeepEquals, []string{})
    78  	c.Check(s.Stdout(), check.Equals, `
    79  Name  Version  Rev  Tracking  Publisher  Notes
    80  foo   4.2      17   potatoes  bar        blocked
    81  `[1:])
    82  	c.Check(s.Stderr(), check.Equals, "")
    83  }
    84  
    85  func (s *SnapSuite) TestListAll(c *check.C) {
    86  	n := 0
    87  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    88  		switch n {
    89  		case 0:
    90  			c.Check(r.Method, check.Equals, "GET")
    91  			c.Check(r.URL.Path, check.Equals, "/v2/snaps")
    92  			c.Check(r.URL.RawQuery, check.Equals, "select=all")
    93  			fmt.Fprintln(w, `{"type": "sync", "result": [{"name": "foo", "status": "active", "version": "4.2", "developer": "bar", "publisher": {"id": "bar-id", "username": "bar", "display-name": "Bar", "validation": "unproven"}, "revision":17, "tracking-channel": "stable"}]}`)
    94  		default:
    95  			c.Fatalf("expected to get 1 requests, now on %d", n+1)
    96  		}
    97  
    98  		n++
    99  	})
   100  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"list", "--all"})
   101  	c.Assert(err, check.IsNil)
   102  	c.Assert(rest, check.DeepEquals, []string{})
   103  	c.Check(s.Stdout(), check.Matches, `Name +Version +Rev +Tracking +Publisher +Notes
   104  foo +4.2 +17 +stable +bar +-
   105  `)
   106  	c.Check(s.Stderr(), check.Equals, "")
   107  }
   108  
   109  func (s *SnapSuite) TestListEmpty(c *check.C) {
   110  	n := 0
   111  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   112  		switch n {
   113  		case 0:
   114  			c.Check(r.Method, check.Equals, "GET")
   115  			c.Check(r.URL.Path, check.Equals, "/v2/snaps")
   116  			fmt.Fprintln(w, `{"type": "sync", "result": []}`)
   117  		default:
   118  			c.Fatalf("expected to get 1 requests, now on %d", n+1)
   119  		}
   120  
   121  		n++
   122  	})
   123  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"list"})
   124  	c.Assert(err, check.IsNil)
   125  	c.Assert(rest, check.DeepEquals, []string{})
   126  	c.Check(s.Stdout(), check.Equals, "")
   127  	c.Check(s.Stderr(), check.Equals, "No snaps are installed yet. Try 'snap install hello-world'.\n")
   128  }
   129  
   130  func (s *SnapSuite) TestListEmptyWithQuery(c *check.C) {
   131  	n := 0
   132  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   133  		switch n {
   134  		case 0:
   135  			c.Check(r.Method, check.Equals, "GET")
   136  			c.Check(r.URL.Path, check.Equals, "/v2/snaps")
   137  			fmt.Fprintln(w, `{"type": "sync", "result": []}`)
   138  		default:
   139  			c.Fatalf("expected to get 1 requests, now on %d", n+1)
   140  		}
   141  
   142  		n++
   143  	})
   144  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"list", "quux"})
   145  	c.Assert(err, check.ErrorMatches, `no matching snaps installed`)
   146  }
   147  
   148  func (s *SnapSuite) TestListWithNoMatchingQuery(c *check.C) {
   149  	n := 0
   150  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   151  		switch n {
   152  		case 0:
   153  			c.Check(r.Method, check.Equals, "GET")
   154  			c.Check(r.URL.Path, check.Equals, "/v2/snaps")
   155  			c.Check(r.URL.Query().Get("snaps"), check.Equals, "quux")
   156  			fmt.Fprintln(w, `{"type": "sync", "result": []}`)
   157  		default:
   158  			c.Fatalf("expected to get 1 requests, now on %d", n+1)
   159  		}
   160  
   161  		n++
   162  	})
   163  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"list", "quux"})
   164  	c.Assert(err, check.ErrorMatches, "no matching snaps installed")
   165  }
   166  
   167  func (s *SnapSuite) TestListWithQuery(c *check.C) {
   168  	n := 0
   169  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   170  		switch n {
   171  		case 0:
   172  			c.Check(r.Method, check.Equals, "GET")
   173  			c.Check(r.URL.Path, check.Equals, "/v2/snaps")
   174  			c.Check(r.URL.Query().Get("snaps"), check.Equals, "foo")
   175  			fmt.Fprintln(w, `{"type": "sync", "result": [{"name": "foo", "status": "active", "version": "4.2", "developer": "bar", "publisher": {"id": "bar-id", "username": "bar", "display-name": "Bar", "validation": "unproven"}, "revision":17, "tracking-channel": "1.10/stable/fix1234"}]}`)
   176  		default:
   177  			c.Fatalf("expected to get 1 requests, now on %d", n+1)
   178  		}
   179  
   180  		n++
   181  	})
   182  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"list", "foo"})
   183  	c.Assert(err, check.IsNil)
   184  	c.Assert(rest, check.DeepEquals, []string{})
   185  	c.Check(s.Stdout(), check.Matches, `Name +Version +Rev +Tracking +Publisher +Notes
   186  foo +4.2 +17 +1\.10/stable/… +bar +-
   187  `)
   188  	c.Check(s.Stderr(), check.Equals, "")
   189  	// ensure that the fake server api was actually hit
   190  	c.Check(n, check.Equals, 1)
   191  }
   192  
   193  func (s *SnapSuite) TestListWithNotes(c *check.C) {
   194  	n := 0
   195  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   196  		switch n {
   197  		case 0:
   198  			c.Check(r.Method, check.Equals, "GET")
   199  			c.Check(r.URL.Path, check.Equals, "/v2/snaps")
   200  			fmt.Fprintln(w, `{"type": "sync", "result": [
   201  {"name": "foo", "status": "active", "version": "4.2", "developer": "bar", "publisher": {"id": "bar-id", "username": "bar", "display-name": "Bar", "validation": "unproven"}, "revision":17, "trymode": true}
   202  ,{"name": "dm1", "status": "active", "version": "5", "revision":1, "devmode": true, "confinement": "devmode"}
   203  ,{"name": "dm2", "status": "active", "version": "5", "revision":1, "devmode": true, "confinement": "strict"}
   204  ,{"name": "cf1", "status": "active", "version": "6", "revision":2, "confinement": "devmode", "jailmode": true}
   205  ]}`)
   206  		default:
   207  			c.Fatalf("expected to get 1 requests, now on %d", n+1)
   208  		}
   209  
   210  		n++
   211  	})
   212  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"list"})
   213  	c.Assert(err, check.IsNil)
   214  	c.Assert(rest, check.DeepEquals, []string{})
   215  	c.Check(s.Stdout(), check.Matches, `(?ms)^Name +Version +Rev +Tracking +Publisher +Notes$`)
   216  	c.Check(s.Stdout(), check.Matches, `(?ms).*^foo +4.2 +17 +- +bar +try$`)
   217  	c.Check(s.Stdout(), check.Matches, `(?ms).*^dm1 +.* +devmode$`)
   218  	c.Check(s.Stdout(), check.Matches, `(?ms).*^dm2 +.* +devmode$`)
   219  	c.Check(s.Stdout(), check.Matches, `(?ms).*^cf1 +.* +jailmode$`)
   220  	c.Check(s.Stderr(), check.Equals, "")
   221  }
   222  
   223  func (s *SnapSuite) TestFormatChannel(c *check.C) {
   224  	type tableT struct {
   225  		channel  string
   226  		expected string
   227  	}
   228  	for _, t := range []tableT{
   229  		{"", "-"},
   230  		{"latest/stable", "latest/stable"},
   231  		{"foo/stable", "foo/stable"},
   232  		{"foo/edge", "foo/edge"},
   233  		{"foo/stable/bar", "foo/stable/…"},
   234  		{"foo/edge/bar", "foo/edge/…"},
   235  	} {
   236  		c.Check(snap.FormatChannel(t.channel), check.Equals, t.expected, check.Commentf(t.channel))
   237  	}
   238  
   239  	// and some SISO tests just to check it doesn't panic nor return empty string
   240  	// (the former would break scripts)
   241  	for _, ch := range []string{
   242  		"",
   243  		"\x00",
   244  		"/",
   245  		"//",
   246  		"///",
   247  		"////",
   248  		"a/",
   249  		"/b",
   250  		"a//b",
   251  		"/stable",
   252  		"/edge",
   253  	} {
   254  		c.Check(snap.FormatChannel(ch), check.Not(check.Equals), "", check.Commentf(ch))
   255  	}
   256  }