github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/cmd/snap/cmd_aliases_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  	"io/ioutil"
    24  	"net/http"
    25  
    26  	. "gopkg.in/check.v1"
    27  
    28  	"github.com/snapcore/snapd/client"
    29  	. "github.com/snapcore/snapd/cmd/snap"
    30  )
    31  
    32  func (s *SnapSuite) TestAliasesHelp(c *C) {
    33  	msg := `Usage:
    34    snap.test aliases [<snap>]
    35  
    36  The aliases command lists all aliases available in the system and their status.
    37  
    38  $ snap aliases <snap>
    39  
    40  Lists only the aliases defined by the specified snap.
    41  
    42  An alias noted as undefined means it was explicitly enabled or disabled but is
    43  not defined in the current revision of the snap, possibly temporarily (e.g.
    44  because of a revert). This can cleared with 'snap alias --reset'.
    45  `
    46  	s.testSubCommandHelp(c, "aliases", msg)
    47  }
    48  
    49  func (s *SnapSuite) TestAliases(c *C) {
    50  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    51  		c.Check(r.Method, Equals, "GET")
    52  		c.Check(r.URL.Path, Equals, "/v2/aliases")
    53  		body, err := ioutil.ReadAll(r.Body)
    54  		c.Check(err, IsNil)
    55  		c.Check(body, DeepEquals, []byte{})
    56  		EncodeResponseBody(c, w, map[string]interface{}{
    57  			"type": "sync",
    58  			"result": map[string]map[string]client.AliasStatus{
    59  				"foo": {
    60  					"foo0":      {Command: "foo", Status: "auto", Auto: "foo"},
    61  					"foo_reset": {Command: "foo.reset", Manual: "reset", Status: "manual"},
    62  				},
    63  				"bar": {
    64  					"bar_dump":    {Command: "bar.dump", Status: "manual", Manual: "dump"},
    65  					"bar_dump.1":  {Command: "bar.dump", Status: "disabled", Auto: "dump"},
    66  					"bar_restore": {Command: "bar.safe-restore", Status: "manual", Auto: "restore", Manual: "safe-restore"},
    67  				},
    68  			},
    69  		})
    70  	})
    71  	rest, err := Parser(Client()).ParseArgs([]string{"aliases"})
    72  	c.Assert(err, IsNil)
    73  	c.Assert(rest, DeepEquals, []string{})
    74  	expectedStdout := "" +
    75  		"Command           Alias        Notes\n" +
    76  		"bar.dump          bar_dump     manual\n" +
    77  		"bar.dump          bar_dump.1   disabled\n" +
    78  		"bar.safe-restore  bar_restore  manual,override\n" +
    79  		"foo               foo0         -\n" +
    80  		"foo.reset         foo_reset    manual\n"
    81  	c.Assert(s.Stdout(), Equals, expectedStdout)
    82  	c.Assert(s.Stderr(), Equals, "")
    83  }
    84  
    85  func (s *SnapSuite) TestAliasesFilterSnap(c *C) {
    86  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    87  		c.Check(r.Method, Equals, "GET")
    88  		c.Check(r.URL.Path, Equals, "/v2/aliases")
    89  		body, err := ioutil.ReadAll(r.Body)
    90  		c.Check(err, IsNil)
    91  		c.Check(body, DeepEquals, []byte{})
    92  		EncodeResponseBody(c, w, map[string]interface{}{
    93  			"type": "sync",
    94  			"result": map[string]map[string]client.AliasStatus{
    95  				"foo": {
    96  					"foo0":      {Command: "foo", Status: "auto", Auto: "foo"},
    97  					"foo_reset": {Command: "foo.reset", Manual: "reset", Status: "manual"},
    98  				},
    99  				"bar": {
   100  					"bar_dump":   {Command: "bar.dump", Status: "manual", Manual: "dump"},
   101  					"bar_dump.1": {Command: "bar.dump", Status: "disabled", Auto: "dump"},
   102  				},
   103  			},
   104  		})
   105  	})
   106  	rest, err := Parser(Client()).ParseArgs([]string{"aliases", "foo"})
   107  	c.Assert(err, IsNil)
   108  	c.Assert(rest, DeepEquals, []string{})
   109  	expectedStdout := "" +
   110  		"Command    Alias      Notes\n" +
   111  		"foo        foo0       -\n" +
   112  		"foo.reset  foo_reset  manual\n"
   113  	c.Assert(s.Stdout(), Equals, expectedStdout)
   114  	c.Assert(s.Stderr(), Equals, "")
   115  }
   116  
   117  func (s *SnapSuite) TestAliasesNone(c *C) {
   118  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   119  		c.Check(r.Method, Equals, "GET")
   120  		c.Check(r.URL.Path, Equals, "/v2/aliases")
   121  		body, err := ioutil.ReadAll(r.Body)
   122  		c.Check(err, IsNil)
   123  		c.Check(body, DeepEquals, []byte{})
   124  		EncodeResponseBody(c, w, map[string]interface{}{
   125  			"type":   "sync",
   126  			"result": map[string]map[string]client.AliasStatus{},
   127  		})
   128  	})
   129  	_, err := Parser(Client()).ParseArgs([]string{"aliases"})
   130  	c.Assert(err, IsNil)
   131  	c.Assert(s.Stdout(), Equals, "")
   132  	c.Assert(s.Stderr(), Equals, "No aliases are currently defined.\n\nUse 'snap help alias' to learn how to create aliases manually.\n")
   133  }
   134  
   135  func (s *SnapSuite) TestAliasesNoneFilterSnap(c *C) {
   136  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   137  		c.Check(r.Method, Equals, "GET")
   138  		c.Check(r.URL.Path, Equals, "/v2/aliases")
   139  		body, err := ioutil.ReadAll(r.Body)
   140  		c.Check(err, IsNil)
   141  		c.Check(body, DeepEquals, []byte{})
   142  		EncodeResponseBody(c, w, map[string]interface{}{
   143  			"type": "sync",
   144  			"result": map[string]map[string]client.AliasStatus{
   145  				"bar": {
   146  					"bar0": {Command: "foo", Status: "auto", Auto: "foo"},
   147  				}},
   148  		})
   149  	})
   150  	_, err := Parser(Client()).ParseArgs([]string{"aliases", "not-bar"})
   151  	c.Assert(err, IsNil)
   152  	c.Assert(s.Stdout(), Equals, "")
   153  	c.Assert(s.Stderr(), Equals, "No aliases are currently defined for snap \"not-bar\".\n\nUse 'snap help alias' to learn how to create aliases manually.\n")
   154  }
   155  
   156  func (s *SnapSuite) TestAliasesSorting(c *C) {
   157  	tests := []struct {
   158  		snap1  string
   159  		cmd1   string
   160  		alias1 string
   161  		snap2  string
   162  		cmd2   string
   163  		alias2 string
   164  	}{
   165  		{"bar", "bar", "r", "baz", "baz", "z"},
   166  		{"bar", "bar", "bar0", "bar", "bar.app", "bapp"},
   167  		{"bar", "bar.app1", "bapp1", "bar", "bar.app2", "bapp2"},
   168  		{"bar", "bar.app1", "appx", "bar", "bar.app1", "appy"},
   169  	}
   170  
   171  	for _, test := range tests {
   172  		res := AliasInfoLess(test.snap1, test.alias1, test.cmd1, test.snap2, test.alias2, test.cmd2)
   173  		c.Check(res, Equals, true, Commentf("%v", test))
   174  
   175  		rres := AliasInfoLess(test.snap2, test.alias2, test.cmd2, test.snap1, test.alias1, test.cmd1)
   176  		c.Check(rres, Equals, false, Commentf("reversed %v", test))
   177  	}
   178  
   179  }