github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/client/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 client_test
    21  
    22  import (
    23  	"encoding/json"
    24  
    25  	"gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/client"
    28  )
    29  
    30  func (cs *clientSuite) TestClientAliasCallsEndpoint(c *check.C) {
    31  	cs.cli.Alias("alias-snap", "cmd1", "alias1")
    32  	c.Check(cs.req.Method, check.Equals, "POST")
    33  	c.Check(cs.req.URL.Path, check.Equals, "/v2/aliases")
    34  }
    35  
    36  func (cs *clientSuite) TestClientAlias(c *check.C) {
    37  	cs.status = 202
    38  	cs.rsp = `{
    39  		"type": "async",
    40                  "status-code": 202,
    41  		"result": { },
    42                  "change": "chgid"
    43  	}`
    44  	id, err := cs.cli.Alias("alias-snap", "cmd1", "alias1")
    45  	c.Assert(err, check.IsNil)
    46  	c.Check(id, check.Equals, "chgid")
    47  	var body map[string]interface{}
    48  	decoder := json.NewDecoder(cs.req.Body)
    49  	err = decoder.Decode(&body)
    50  	c.Check(err, check.IsNil)
    51  	c.Check(body, check.DeepEquals, map[string]interface{}{
    52  		"action": "alias",
    53  		"snap":   "alias-snap",
    54  		"app":    "cmd1",
    55  		"alias":  "alias1",
    56  	})
    57  }
    58  
    59  func (cs *clientSuite) TestClientUnaliasCallsEndpoint(c *check.C) {
    60  	cs.cli.Unalias("alias1")
    61  	c.Check(cs.req.Method, check.Equals, "POST")
    62  	c.Check(cs.req.URL.Path, check.Equals, "/v2/aliases")
    63  }
    64  
    65  func (cs *clientSuite) TestClientUnalias(c *check.C) {
    66  	cs.status = 202
    67  	cs.rsp = `{
    68  		"type": "async",
    69                  "status-code": 202,
    70  		"result": { },
    71                  "change": "chgid"
    72  	}`
    73  	id, err := cs.cli.Unalias("alias1")
    74  	c.Assert(err, check.IsNil)
    75  	c.Check(id, check.Equals, "chgid")
    76  	var body map[string]interface{}
    77  	decoder := json.NewDecoder(cs.req.Body)
    78  	err = decoder.Decode(&body)
    79  	c.Check(err, check.IsNil)
    80  	c.Check(body, check.DeepEquals, map[string]interface{}{
    81  		"action": "unalias",
    82  		"snap":   "alias1",
    83  		"alias":  "alias1",
    84  	})
    85  }
    86  
    87  func (cs *clientSuite) TestClientDisableAllAliasesCallsEndpoint(c *check.C) {
    88  	cs.cli.DisableAllAliases("some-snap")
    89  	c.Check(cs.req.Method, check.Equals, "POST")
    90  	c.Check(cs.req.URL.Path, check.Equals, "/v2/aliases")
    91  }
    92  
    93  func (cs *clientSuite) TestClientDisableAllAliases(c *check.C) {
    94  	cs.status = 202
    95  	cs.rsp = `{
    96  		"type": "async",
    97                  "status-code": 202,
    98  		"result": { },
    99                  "change": "chgid"
   100  	}`
   101  	id, err := cs.cli.DisableAllAliases("some-snap")
   102  	c.Assert(err, check.IsNil)
   103  	c.Check(id, check.Equals, "chgid")
   104  	var body map[string]interface{}
   105  	decoder := json.NewDecoder(cs.req.Body)
   106  	err = decoder.Decode(&body)
   107  	c.Check(err, check.IsNil)
   108  	c.Check(body, check.DeepEquals, map[string]interface{}{
   109  		"action": "unalias",
   110  		"snap":   "some-snap",
   111  	})
   112  }
   113  
   114  func (cs *clientSuite) TestClientRemoveManualAliasCallsEndpoint(c *check.C) {
   115  	cs.cli.RemoveManualAlias("alias1")
   116  	c.Check(cs.req.Method, check.Equals, "POST")
   117  	c.Check(cs.req.URL.Path, check.Equals, "/v2/aliases")
   118  }
   119  
   120  func (cs *clientSuite) TestClientRemoveManualAlias(c *check.C) {
   121  	cs.status = 202
   122  	cs.rsp = `{
   123  		"type": "async",
   124                  "status-code": 202,
   125  		"result": { },
   126                  "change": "chgid"
   127  	}`
   128  	id, err := cs.cli.RemoveManualAlias("alias1")
   129  	c.Assert(err, check.IsNil)
   130  	c.Check(id, check.Equals, "chgid")
   131  	var body map[string]interface{}
   132  	decoder := json.NewDecoder(cs.req.Body)
   133  	err = decoder.Decode(&body)
   134  	c.Check(err, check.IsNil)
   135  	c.Check(body, check.DeepEquals, map[string]interface{}{
   136  		"action": "unalias",
   137  		"alias":  "alias1",
   138  	})
   139  }
   140  
   141  func (cs *clientSuite) TestClientPreferCallsEndpoint(c *check.C) {
   142  	cs.cli.Prefer("some-snap")
   143  	c.Check(cs.req.Method, check.Equals, "POST")
   144  	c.Check(cs.req.URL.Path, check.Equals, "/v2/aliases")
   145  }
   146  
   147  func (cs *clientSuite) TestClientPrefer(c *check.C) {
   148  	cs.status = 202
   149  	cs.rsp = `{
   150  		"type": "async",
   151                  "status-code": 202,
   152  		"result": { },
   153                  "change": "chgid"
   154  	}`
   155  	id, err := cs.cli.Prefer("some-snap")
   156  	c.Assert(err, check.IsNil)
   157  	c.Check(id, check.Equals, "chgid")
   158  	var body map[string]interface{}
   159  	decoder := json.NewDecoder(cs.req.Body)
   160  	err = decoder.Decode(&body)
   161  	c.Check(err, check.IsNil)
   162  	c.Check(body, check.DeepEquals, map[string]interface{}{
   163  		"action": "prefer",
   164  		"snap":   "some-snap",
   165  	})
   166  }
   167  
   168  func (cs *clientSuite) TestClientAliasesCallsEndpoint(c *check.C) {
   169  	_, _ = cs.cli.Aliases()
   170  	c.Check(cs.req.Method, check.Equals, "GET")
   171  	c.Check(cs.req.URL.Path, check.Equals, "/v2/aliases")
   172  }
   173  
   174  func (cs *clientSuite) TestClientAliases(c *check.C) {
   175  	cs.rsp = `{
   176  		"type": "sync",
   177  		"result": {
   178                      "foo": {
   179                          "foo0": {"command": "foo", "status": "auto", "auto": "foo"},
   180                          "foo_reset": {"command": "foo.reset", "manual": "reset", "status": "manual"}
   181                      },
   182                      "bar": {
   183                          "bar_dump": {"command": "bar.dump", "status": "manual", "manual": "dump"},
   184                          "bar_dump.1": {"command": "bar.dump", "status": "disabled", "auto": "dump"}
   185                      }
   186  		}
   187  	}`
   188  	allStatuses, err := cs.cli.Aliases()
   189  	c.Assert(err, check.IsNil)
   190  	c.Check(allStatuses, check.DeepEquals, map[string]map[string]client.AliasStatus{
   191  		"foo": {
   192  			"foo0":      {Command: "foo", Status: "auto", Auto: "foo"},
   193  			"foo_reset": {Command: "foo.reset", Status: "manual", Manual: "reset"},
   194  		},
   195  		"bar": {
   196  			"bar_dump":   {Command: "bar.dump", Status: "manual", Manual: "dump"},
   197  			"bar_dump.1": {Command: "bar.dump", Status: "disabled", Auto: "dump"},
   198  		},
   199  	})
   200  }