github.com/tompreston/snapd@v0.0.0-20210817193607-954edfcb9611/cmd/snap/cmd_set_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  	"encoding/json"
    24  	"fmt"
    25  	"net/http"
    26  
    27  	"gopkg.in/check.v1"
    28  
    29  	snapset "github.com/snapcore/snapd/cmd/snap"
    30  )
    31  
    32  type snapSetSuite struct {
    33  	BaseSnapSuite
    34  
    35  	setConfApiCalls int
    36  }
    37  
    38  var _ = check.Suite(&snapSetSuite{})
    39  
    40  func (s *snapSetSuite) SetUpTest(c *check.C) {
    41  	s.BaseSnapSuite.SetUpTest(c)
    42  	s.setConfApiCalls = 0
    43  }
    44  
    45  func (s *snapSetSuite) TestInvalidSetParameters(c *check.C) {
    46  	invalidParameters := []string{"set", "snap-name", "key", "value"}
    47  	_, err := snapset.Parser(snapset.Client()).ParseArgs(invalidParameters)
    48  	c.Check(err, check.ErrorMatches, ".*invalid configuration:.*(want key=value).*")
    49  	c.Check(s.setConfApiCalls, check.Equals, 0)
    50  }
    51  
    52  func (s *snapSetSuite) TestSnapSetIntegrationString(c *check.C) {
    53  	// and mock the server
    54  	s.mockSetConfigServer(c, "value")
    55  
    56  	// Set a config value for the active snap
    57  	_, err := snapset.Parser(snapset.Client()).ParseArgs([]string{"set", "snapname", "key=value"})
    58  	c.Assert(err, check.IsNil)
    59  	c.Check(s.setConfApiCalls, check.Equals, 1)
    60  }
    61  
    62  func (s *snapSetSuite) TestSnapSetIntegrationNumber(c *check.C) {
    63  	// and mock the server
    64  	s.mockSetConfigServer(c, json.Number("1.2"))
    65  
    66  	// Set a config value for the active snap
    67  	_, err := snapset.Parser(snapset.Client()).ParseArgs([]string{"set", "snapname", "key=1.2"})
    68  	c.Assert(err, check.IsNil)
    69  	c.Check(s.setConfApiCalls, check.Equals, 1)
    70  }
    71  
    72  func (s *snapSetSuite) TestSnapSetIntegrationBigInt(c *check.C) {
    73  	// and mock the server
    74  	s.mockSetConfigServer(c, json.Number("1234567890"))
    75  
    76  	// Set a config value for the active snap
    77  	_, err := snapset.Parser(snapset.Client()).ParseArgs([]string{"set", "snapname", "key=1234567890"})
    78  	c.Assert(err, check.IsNil)
    79  	c.Check(s.setConfApiCalls, check.Equals, 1)
    80  }
    81  
    82  func (s *snapSetSuite) TestSnapSetIntegrationJson(c *check.C) {
    83  	// and mock the server
    84  	s.mockSetConfigServer(c, map[string]interface{}{"subkey": "value"})
    85  
    86  	// Set a config value for the active snap
    87  	_, err := snapset.Parser(snapset.Client()).ParseArgs([]string{"set", "snapname", `key={"subkey":"value"}`})
    88  	c.Assert(err, check.IsNil)
    89  	c.Check(s.setConfApiCalls, check.Equals, 1)
    90  }
    91  
    92  func (s *snapSetSuite) TestSnapSetIntegrationUnsetWithExclamationMark(c *check.C) {
    93  	// and mock the server
    94  	s.mockSetConfigServer(c, nil)
    95  
    96  	// Unset config value via exclamation mark
    97  	_, err := snapset.Parser(snapset.Client()).ParseArgs([]string{"set", "snapname", "key!"})
    98  	c.Assert(err, check.IsNil)
    99  	c.Check(s.setConfApiCalls, check.Equals, 1)
   100  }
   101  
   102  func (s *snapSetSuite) TestSnapSetIntegrationStringWithExclamationMark(c *check.C) {
   103  	// and mock the server
   104  	s.mockSetConfigServer(c, "value!")
   105  
   106  	// Set a config value ending with exclamation mark
   107  	_, err := snapset.Parser(snapset.Client()).ParseArgs([]string{"set", "snapname", "key=value!"})
   108  	c.Assert(err, check.IsNil)
   109  	c.Check(s.setConfApiCalls, check.Equals, 1)
   110  }
   111  
   112  func (s *snapSetSuite) TestSnapSetParseStrictJSON(c *check.C) {
   113  	// mock server
   114  	s.mockSetConfigServer(c, map[string]interface{}{"a": "b", "c": json.Number("1"), "d": map[string]interface{}{"e": "f"}})
   115  
   116  	_, err := snapset.Parser(snapset.Client()).ParseArgs([]string{"set", "snapname", "-t", `key={"a":"b", "c":1, "d": {"e": "f"}}`})
   117  	c.Assert(err, check.IsNil)
   118  	c.Check(s.setConfApiCalls, check.Equals, 1)
   119  }
   120  
   121  func (s *snapSetSuite) TestSnapSetFailParsingWithStrictJSON(c *check.C) {
   122  	_, err := snapset.Parser(snapset.Client()).ParseArgs([]string{"set", "snapname", "-t", `key=notJSON`})
   123  	c.Assert(err, check.ErrorMatches, "failed to parse JSON:.*")
   124  }
   125  
   126  func (s *snapSetSuite) TestSnapSetFailOnStrictJSONAndString(c *check.C) {
   127  	_, err := snapset.Parser(snapset.Client()).ParseArgs([]string{"set", "snapname", "-t", "-s", "key={}"})
   128  	c.Assert(err, check.ErrorMatches, "cannot use -t and -s together")
   129  }
   130  
   131  func (s *snapSetSuite) TestSnapSetAsString(c *check.C) {
   132  	// mock server
   133  	value := `{"a":"b", "c":1}`
   134  	s.mockSetConfigServer(c, value)
   135  
   136  	_, err := snapset.Parser(snapset.Client()).ParseArgs([]string{"set", "snapname", "-s", fmt.Sprintf("key=%s", value)})
   137  	c.Assert(err, check.IsNil)
   138  	c.Check(s.setConfApiCalls, check.Equals, 1)
   139  }
   140  
   141  func (s *snapSetSuite) mockSetConfigServer(c *check.C, expectedValue interface{}) {
   142  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   143  		switch r.URL.Path {
   144  		case "/v2/snaps/snapname/conf":
   145  			c.Check(r.Method, check.Equals, "PUT")
   146  			c.Check(DecodedRequestBody(c, r), check.DeepEquals, map[string]interface{}{
   147  				"key": expectedValue,
   148  			})
   149  			w.WriteHeader(202)
   150  			fmt.Fprintln(w, `{"type":"async", "status-code": 202, "change": "zzz"}`)
   151  			s.setConfApiCalls += 1
   152  		case "/v2/changes/zzz":
   153  			c.Check(r.Method, check.Equals, "GET")
   154  			fmt.Fprintln(w, `{"type":"sync", "result":{"ready": true, "status": "Done"}}`)
   155  		default:
   156  			c.Fatalf("unexpected path %q", r.URL.Path)
   157  		}
   158  	})
   159  }