github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/client/conf_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 client_test
    21  
    22  import (
    23  	"encoding/json"
    24  
    25  	"gopkg.in/check.v1"
    26  )
    27  
    28  func (cs *clientSuite) TestClientSetConfCallsEndpoint(c *check.C) {
    29  	cs.cli.SetConf("snap-name", map[string]interface{}{"key": "value"})
    30  	c.Check(cs.req.Method, check.Equals, "PUT")
    31  	c.Check(cs.req.URL.Path, check.Equals, "/v2/snaps/snap-name/conf")
    32  }
    33  
    34  func (cs *clientSuite) TestClientGetConfCallsEndpoint(c *check.C) {
    35  	cs.cli.Conf("snap-name", []string{"test-key"})
    36  	c.Check(cs.req.Method, check.Equals, "GET")
    37  	c.Check(cs.req.URL.Path, check.Equals, "/v2/snaps/snap-name/conf")
    38  	c.Check(cs.req.URL.Query().Get("keys"), check.Equals, "test-key")
    39  }
    40  
    41  func (cs *clientSuite) TestClientGetConfCallsEndpointMultipleKeys(c *check.C) {
    42  	cs.cli.Conf("snap-name", []string{"test-key1", "test-key2"})
    43  	c.Check(cs.req.Method, check.Equals, "GET")
    44  	c.Check(cs.req.URL.Path, check.Equals, "/v2/snaps/snap-name/conf")
    45  	c.Check(cs.req.URL.Query().Get("keys"), check.Equals, "test-key1,test-key2")
    46  }
    47  
    48  func (cs *clientSuite) TestClientSetConf(c *check.C) {
    49  	cs.status = 202
    50  	cs.rsp = `{
    51  		"type": "async",
    52  		"status-code": 202,
    53  		"result": { },
    54  		"change": "foo"
    55  	}`
    56  	id, err := cs.cli.SetConf("snap-name", map[string]interface{}{"key": "value"})
    57  	c.Assert(err, check.IsNil)
    58  	c.Check(id, check.Equals, "foo")
    59  	var body map[string]interface{}
    60  	decoder := json.NewDecoder(cs.req.Body)
    61  	err = decoder.Decode(&body)
    62  	c.Check(err, check.IsNil)
    63  	c.Check(body, check.DeepEquals, map[string]interface{}{
    64  		"key": "value",
    65  	})
    66  }
    67  
    68  func (cs *clientSuite) TestClientGetConf(c *check.C) {
    69  	cs.rsp = `{
    70  		"type": "sync",
    71  		"status-code": 200,
    72  		"result": {"test-key": "test-value"}
    73  	}`
    74  	value, err := cs.cli.Conf("snap-name", []string{"test-key"})
    75  	c.Assert(err, check.IsNil)
    76  	c.Check(value, check.DeepEquals, map[string]interface{}{"test-key": "test-value"})
    77  }
    78  
    79  func (cs *clientSuite) TestClientGetConfBigInt(c *check.C) {
    80  	cs.rsp = `{
    81  		"type": "sync",
    82  		"status-code": 200,
    83  		"result": {"test-key": 1234567890}
    84  	}`
    85  	value, err := cs.cli.Conf("snap-name", []string{"test-key"})
    86  	c.Assert(err, check.IsNil)
    87  	c.Check(value, check.DeepEquals, map[string]interface{}{"test-key": json.Number("1234567890")})
    88  }
    89  
    90  func (cs *clientSuite) TestClientGetConfMultipleKeys(c *check.C) {
    91  	cs.rsp = `{
    92  		"type": "sync",
    93  		"status-code": 200,
    94  		"result": {
    95  			"test-key1": "test-value1",
    96  			"test-key2": "test-value2"
    97  		}
    98  	}`
    99  	value, err := cs.cli.Conf("snap-name", []string{"test-key1", "test-key2"})
   100  	c.Assert(err, check.IsNil)
   101  	c.Check(value, check.DeepEquals, map[string]interface{}{
   102  		"test-key1": "test-value1",
   103  		"test-key2": "test-value2",
   104  	})
   105  }