github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/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) mockSetConfigServer(c *check.C, expectedValue interface{}) {
   113  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   114  		switch r.URL.Path {
   115  		case "/v2/snaps/snapname/conf":
   116  			c.Check(r.Method, check.Equals, "PUT")
   117  			c.Check(DecodedRequestBody(c, r), check.DeepEquals, map[string]interface{}{
   118  				"key": expectedValue,
   119  			})
   120  			w.WriteHeader(202)
   121  			fmt.Fprintln(w, `{"type":"async", "status-code": 202, "change": "zzz"}`)
   122  			s.setConfApiCalls += 1
   123  		case "/v2/changes/zzz":
   124  			c.Check(r.Method, check.Equals, "GET")
   125  			fmt.Fprintln(w, `{"type":"sync", "result":{"ready": true, "status": "Done"}}`)
   126  		default:
   127  			c.Fatalf("unexpected path %q", r.URL.Path)
   128  		}
   129  	})
   130  }