github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/cmd/snap/cmd_get_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  	"strings"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	snapset "github.com/snapcore/snapd/cmd/snap"
    30  )
    31  
    32  type getCmdArgs struct {
    33  	args, stdout, stderr, error string
    34  	isTerminal                  bool
    35  }
    36  
    37  var getTests = []getCmdArgs{{
    38  	args:  "get snap-name --foo",
    39  	error: ".*unknown flag.*foo.*",
    40  }, {
    41  	args:   "get snapname test-key1",
    42  	stdout: "test-value1\n",
    43  }, {
    44  	args:   "get snapname test-key2",
    45  	stdout: "2\n",
    46  }, {
    47  	args:   "get snapname missing-key",
    48  	stdout: "\n",
    49  }, {
    50  	args:   "get -t snapname test-key1",
    51  	stdout: "\"test-value1\"\n",
    52  }, {
    53  	args:   "get -t snapname test-key2",
    54  	stdout: "2\n",
    55  }, {
    56  	args:   "get -t snapname missing-key",
    57  	stdout: "null\n",
    58  }, {
    59  	args:   "get -d snapname test-key1",
    60  	stdout: "{\n\t\"test-key1\": \"test-value1\"\n}\n",
    61  }, {
    62  	args:   "get -l snapname test-key1",
    63  	stdout: "Key        Value\ntest-key1  test-value1\n",
    64  }, {
    65  	args:   "get snapname -l test-key1 test-key2",
    66  	stdout: "Key        Value\ntest-key1  test-value1\ntest-key2  2\n",
    67  }, {
    68  	args:   "get snapname document",
    69  	stderr: `WARNING: The output of 'snap get' will become a list with columns - use -d or -l to force the output format.\n`,
    70  	stdout: "{\n\t\"document\": {\n\t\t\"key1\": \"value1\",\n\t\t\"key2\": \"value2\"\n\t}\n}\n",
    71  }, {
    72  	isTerminal: true,
    73  	args:       "get snapname document",
    74  	stdout:     "Key            Value\ndocument.key1  value1\ndocument.key2  value2\n",
    75  }, {
    76  	args:   "get snapname -d test-key1 test-key2",
    77  	stdout: "{\n\t\"test-key1\": \"test-value1\",\n\t\"test-key2\": 2\n}\n",
    78  }, {
    79  	args:   "get snapname -l document",
    80  	stdout: "Key            Value\ndocument.key1  value1\ndocument.key2  value2\n",
    81  }, {
    82  	args:   "get -d snapname document",
    83  	stdout: "{\n\t\"document\": {\n\t\t\"key1\": \"value1\",\n\t\t\"key2\": \"value2\"\n\t}\n}\n",
    84  }, {
    85  	args:   "get -l snapname",
    86  	stdout: "Key  Value\nbar  100\nfoo  {...}\n",
    87  }, {
    88  	args:   "get snapname -l test-key3 test-key4",
    89  	stdout: "Key          Value\ntest-key3.a  1\ntest-key3.b  2\ntest-key3-a  9\ntest-key4.a  3\ntest-key4.b  4\n",
    90  }, {
    91  	args:   "get -d snapname",
    92  	stdout: "{\n\t\"bar\": 100,\n\t\"foo\": {\n\t\t\"key1\": \"value1\",\n\t\t\"key2\": \"value2\"\n\t}\n}\n",
    93  }, {
    94  	isTerminal: true,
    95  	args:       "get snapname  test-key1 test-key2",
    96  	stdout:     "Key        Value\ntest-key1  test-value1\ntest-key2  2\n",
    97  }, {
    98  	isTerminal: false,
    99  	args:       "get snapname  test-key1 test-key2",
   100  	stdout:     "{\n\t\"test-key1\": \"test-value1\",\n\t\"test-key2\": 2\n}\n",
   101  	stderr:     `WARNING: The output of 'snap get' will become a list with columns - use -d or -l to force the output format.\n`,
   102  },
   103  }
   104  
   105  func (s *SnapSuite) runTests(cmds []getCmdArgs, c *C) {
   106  	for _, test := range cmds {
   107  		s.stdout.Truncate(0)
   108  		s.stderr.Truncate(0)
   109  
   110  		c.Logf("Test: %s", test.args)
   111  
   112  		restore := snapset.MockIsStdinTTY(test.isTerminal)
   113  		defer restore()
   114  
   115  		_, err := snapset.Parser(snapset.Client()).ParseArgs(strings.Fields(test.args))
   116  		if test.error != "" {
   117  			c.Check(err, ErrorMatches, test.error)
   118  		} else {
   119  			c.Check(err, IsNil)
   120  			c.Check(s.Stderr(), Equals, test.stderr)
   121  			c.Check(s.Stdout(), Equals, test.stdout)
   122  		}
   123  	}
   124  }
   125  
   126  func (s *SnapSuite) TestSnapGetTests(c *C) {
   127  	s.mockGetConfigServer(c)
   128  	s.runTests(getTests, c)
   129  }
   130  
   131  var getNoConfigTests = []getCmdArgs{{
   132  	args:  "get -l snapname",
   133  	error: `snap "snapname" has no configuration`,
   134  }, {
   135  	args:  "get snapname",
   136  	error: `snap "snapname" has no configuration`,
   137  }, {
   138  	args:   "get -d snapname",
   139  	stdout: "{}\n",
   140  }}
   141  
   142  func (s *SnapSuite) TestSnapGetNoConfiguration(c *C) {
   143  	s.mockGetEmptyConfigServer(c)
   144  	s.runTests(getNoConfigTests, c)
   145  }
   146  
   147  func (s *SnapSuite) TestSortByPath(c *C) {
   148  	values := []snapset.ConfigValue{
   149  		{Path: "test-key3.b"},
   150  		{Path: "a"},
   151  		{Path: "test-key3.a"},
   152  		{Path: "a.b.c"},
   153  		{Path: "test-key4.a"},
   154  		{Path: "test-key4.b"},
   155  		{Path: "a-b"},
   156  		{Path: "zzz"},
   157  		{Path: "aa"},
   158  		{Path: "test-key3-a"},
   159  		{Path: "a.b"},
   160  	}
   161  	snapset.SortByPath(values)
   162  
   163  	expected := []string{
   164  		"a",
   165  		"a.b",
   166  		"a.b.c",
   167  		"a-b",
   168  		"aa",
   169  		"test-key3.a",
   170  		"test-key3.b",
   171  		"test-key3-a",
   172  		"test-key4.a",
   173  		"test-key4.b",
   174  		"zzz",
   175  	}
   176  
   177  	c.Assert(values, HasLen, len(expected))
   178  
   179  	for i, e := range expected {
   180  		c.Assert(values[i].Path, Equals, e)
   181  	}
   182  }
   183  
   184  func (s *SnapSuite) mockGetConfigServer(c *C) {
   185  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   186  		if r.URL.Path != "/v2/snaps/snapname/conf" {
   187  			c.Errorf("unexpected path %q", r.URL.Path)
   188  			return
   189  		}
   190  
   191  		c.Check(r.Method, Equals, "GET")
   192  
   193  		query := r.URL.Query()
   194  		switch query.Get("keys") {
   195  		case "test-key1":
   196  			fmt.Fprintln(w, `{"type":"sync", "status-code": 200, "result": {"test-key1":"test-value1"}}`)
   197  		case "test-key2":
   198  			fmt.Fprintln(w, `{"type":"sync", "status-code": 200, "result": {"test-key2":2}}`)
   199  		case "test-key1,test-key2":
   200  			fmt.Fprintln(w, `{"type":"sync", "status-code": 200, "result": {"test-key1":"test-value1","test-key2":2}}`)
   201  		case "test-key3,test-key4":
   202  			fmt.Fprintln(w, `{"type":"sync", "status-code": 200, "result": {"test-key3":{"a":1,"b":2},"test-key3-a":9,"test-key4":{"a":3,"b":4}}}`)
   203  		case "missing-key":
   204  			fmt.Fprintln(w, `{"type":"sync", "status-code": 200, "result": {}}`)
   205  		case "document":
   206  			fmt.Fprintln(w, `{"type":"sync", "status-code": 200, "result": {"document":{"key1":"value1","key2":"value2"}}}`)
   207  		case "":
   208  			fmt.Fprintln(w, `{"type":"sync", "status-code": 200, "result": {"foo":{"key1":"value1","key2":"value2"},"bar":100}}`)
   209  		default:
   210  			c.Errorf("unexpected keys %q", query.Get("keys"))
   211  		}
   212  	})
   213  }
   214  
   215  func (s *SnapSuite) mockGetEmptyConfigServer(c *C) {
   216  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   217  		if r.URL.Path != "/v2/snaps/snapname/conf" {
   218  			c.Errorf("unexpected path %q", r.URL.Path)
   219  			return
   220  		}
   221  
   222  		c.Check(r.Method, Equals, "GET")
   223  
   224  		fmt.Fprintln(w, `{"type":"sync", "status-code": 200, "result": {}}`)
   225  	})
   226  }