github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/client/connections_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  	"net/url"
    24  
    25  	"gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/client"
    28  )
    29  
    30  func (cs *clientSuite) TestClientConnectionsCallsEndpoint(c *check.C) {
    31  	_, _ = cs.cli.Connections(nil)
    32  	c.Check(cs.req.Method, check.Equals, "GET")
    33  	c.Check(cs.req.URL.Path, check.Equals, "/v2/connections")
    34  }
    35  
    36  func (cs *clientSuite) TestClientConnectionsDefault(c *check.C) {
    37  	cs.rsp = `{
    38  		"type": "sync",
    39  		"result": {
    40  			"established": [
    41  				{
    42  					"slot": {"snap": "keyboard-lights", "slot": "capslock-led"},
    43  					"plug": {"snap": "canonical-pi2", "plug": "pin-13"},
    44  					"interface": "bool-file",
    45  					"gadget": true
    46                                  }
    47  			],
    48  			"plugs": [
    49  				{
    50  					"snap": "canonical-pi2",
    51  					"plug": "pin-13",
    52  					"interface": "bool-file",
    53  					"label": "Pin 13",
    54  					"connections": [
    55  						{"snap": "keyboard-lights", "slot": "capslock-led"}
    56  					]
    57  				}
    58  			],
    59  			"slots": [
    60  				{
    61  					"snap": "keyboard-lights",
    62  					"slot": "capslock-led",
    63  					"interface": "bool-file",
    64  					"label": "Capslock indicator LED",
    65  					"connections": [
    66  						{"snap": "canonical-pi2", "plug": "pin-13"}
    67  					]
    68  				}
    69  			]
    70  		}
    71  	}`
    72  	conns, err := cs.cli.Connections(nil)
    73  	c.Assert(err, check.IsNil)
    74  	c.Check(cs.req.URL.Path, check.Equals, "/v2/connections")
    75  	c.Check(conns, check.DeepEquals, client.Connections{
    76  		Established: []client.Connection{
    77  			{
    78  				Plug:      client.PlugRef{Snap: "canonical-pi2", Name: "pin-13"},
    79  				Slot:      client.SlotRef{Snap: "keyboard-lights", Name: "capslock-led"},
    80  				Interface: "bool-file",
    81  				Gadget:    true,
    82  			},
    83  		},
    84  		Plugs: []client.Plug{
    85  			{
    86  				Snap:      "canonical-pi2",
    87  				Name:      "pin-13",
    88  				Interface: "bool-file",
    89  				Label:     "Pin 13",
    90  				Connections: []client.SlotRef{
    91  					{
    92  						Snap: "keyboard-lights",
    93  						Name: "capslock-led",
    94  					},
    95  				},
    96  			},
    97  		},
    98  		Slots: []client.Slot{
    99  			{
   100  				Snap:      "keyboard-lights",
   101  				Name:      "capslock-led",
   102  				Interface: "bool-file",
   103  				Label:     "Capslock indicator LED",
   104  				Connections: []client.PlugRef{
   105  					{
   106  						Snap: "canonical-pi2",
   107  						Name: "pin-13",
   108  					},
   109  				},
   110  			},
   111  		},
   112  	})
   113  }
   114  
   115  func (cs *clientSuite) TestClientConnectionsAll(c *check.C) {
   116  	cs.rsp = `{
   117  		"type": "sync",
   118  		"result": {
   119  			"established": [
   120  				{
   121  					"slot": {"snap": "keyboard-lights", "slot": "capslock-led"},
   122  					"plug": {"snap": "canonical-pi2", "plug": "pin-13"},
   123  					"interface": "bool-file",
   124  					"gadget": true
   125                                  }
   126  			],
   127  			"undesired": [
   128  				{
   129  					"slot": {"snap": "keyboard-lights", "slot": "numlock-led"},
   130  					"plug": {"snap": "canonical-pi2", "plug": "pin-14"},
   131  					"interface": "bool-file",
   132  					"gadget": true,
   133  					"manual": true
   134                                  }
   135  			],
   136  			"plugs": [
   137  				{
   138  					"snap": "canonical-pi2",
   139  					"plug": "pin-13",
   140  					"interface": "bool-file",
   141  					"label": "Pin 13",
   142  					"connections": [
   143  						{"snap": "keyboard-lights", "slot": "capslock-led"}
   144  					]
   145  				},
   146  				{
   147  					"snap": "canonical-pi2",
   148  					"plug": "pin-14",
   149  					"interface": "bool-file",
   150  					"label": "Pin 14"
   151  				}
   152  			],
   153  			"slots": [
   154  				{
   155  					"snap": "keyboard-lights",
   156  					"slot": "capslock-led",
   157  					"interface": "bool-file",
   158  					"label": "Capslock indicator LED",
   159  					"connections": [
   160  						{"snap": "canonical-pi2", "plug": "pin-13"}
   161  					]
   162  				},
   163  				{
   164  					"snap": "keyboard-lights",
   165  					"slot": "numlock-led",
   166  					"interface": "bool-file",
   167  					"label": "Numlock LED"
   168  				}
   169  			]
   170  		}
   171  	}`
   172  	conns, err := cs.cli.Connections(&client.ConnectionOptions{All: true})
   173  	c.Assert(err, check.IsNil)
   174  	c.Check(cs.req.URL.Path, check.Equals, "/v2/connections")
   175  	c.Check(cs.req.URL.RawQuery, check.Equals, "select=all")
   176  	c.Check(conns, check.DeepEquals, client.Connections{
   177  		Established: []client.Connection{
   178  			{
   179  				Plug:      client.PlugRef{Snap: "canonical-pi2", Name: "pin-13"},
   180  				Slot:      client.SlotRef{Snap: "keyboard-lights", Name: "capslock-led"},
   181  				Interface: "bool-file",
   182  				Gadget:    true,
   183  			},
   184  		},
   185  		Undesired: []client.Connection{
   186  			{
   187  				Plug:      client.PlugRef{Snap: "canonical-pi2", Name: "pin-14"},
   188  				Slot:      client.SlotRef{Snap: "keyboard-lights", Name: "numlock-led"},
   189  				Interface: "bool-file",
   190  				Gadget:    true,
   191  				Manual:    true,
   192  			},
   193  		},
   194  		Plugs: []client.Plug{
   195  			{
   196  				Snap:      "canonical-pi2",
   197  				Name:      "pin-13",
   198  				Interface: "bool-file",
   199  				Label:     "Pin 13",
   200  				Connections: []client.SlotRef{
   201  					{
   202  						Snap: "keyboard-lights",
   203  						Name: "capslock-led",
   204  					},
   205  				},
   206  			},
   207  			{
   208  				Snap:      "canonical-pi2",
   209  				Name:      "pin-14",
   210  				Interface: "bool-file",
   211  				Label:     "Pin 14",
   212  			},
   213  		},
   214  		Slots: []client.Slot{
   215  			{
   216  				Snap:      "keyboard-lights",
   217  				Name:      "capslock-led",
   218  				Interface: "bool-file",
   219  				Label:     "Capslock indicator LED",
   220  				Connections: []client.PlugRef{
   221  					{
   222  						Snap: "canonical-pi2",
   223  						Name: "pin-13",
   224  					},
   225  				},
   226  			},
   227  			{
   228  				Snap:      "keyboard-lights",
   229  				Name:      "numlock-led",
   230  				Interface: "bool-file",
   231  				Label:     "Numlock LED",
   232  			},
   233  		},
   234  	})
   235  }
   236  
   237  func (cs *clientSuite) TestClientConnectionsFilter(c *check.C) {
   238  	cs.rsp = `{
   239  		"type": "sync",
   240  		"result": {
   241  			"established": [],
   242  			"plugs": [],
   243  			"slots": []
   244  		}
   245  	}`
   246  
   247  	_, err := cs.cli.Connections(&client.ConnectionOptions{All: true})
   248  	c.Assert(err, check.IsNil)
   249  	c.Check(cs.req.URL.Path, check.Equals, "/v2/connections")
   250  	c.Check(cs.req.URL.RawQuery, check.Equals, "select=all")
   251  
   252  	_, err = cs.cli.Connections(&client.ConnectionOptions{Snap: "foo"})
   253  	c.Assert(err, check.IsNil)
   254  	c.Check(cs.req.URL.Path, check.Equals, "/v2/connections")
   255  	c.Check(cs.req.URL.RawQuery, check.Equals, "snap=foo")
   256  
   257  	_, err = cs.cli.Connections(&client.ConnectionOptions{Interface: "test"})
   258  	c.Assert(err, check.IsNil)
   259  	c.Check(cs.req.URL.Path, check.Equals, "/v2/connections")
   260  	c.Check(cs.req.URL.RawQuery, check.Equals, "interface=test")
   261  
   262  	_, err = cs.cli.Connections(&client.ConnectionOptions{All: true, Snap: "foo", Interface: "test"})
   263  	c.Assert(err, check.IsNil)
   264  	query := cs.req.URL.Query()
   265  	c.Check(query, check.DeepEquals, url.Values{
   266  		"select":    []string{"all"},
   267  		"interface": []string{"test"},
   268  		"snap":      []string{"foo"},
   269  	})
   270  }