github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/cmd/snap/cmd_routine_portal_info_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 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  	"errors"
    24  	"fmt"
    25  	"net/http"
    26  	"net/url"
    27  
    28  	. "gopkg.in/check.v1"
    29  
    30  	"github.com/snapcore/snapd/client"
    31  	snap "github.com/snapcore/snapd/cmd/snap"
    32  )
    33  
    34  // only used for /v2/snaps/hello
    35  const mockInfoJSONWithApps = `
    36  {
    37    "type": "sync",
    38    "status-code": 200,
    39    "status": "OK",
    40    "result": {
    41      "id": "mVyGrEwiqSi5PugCwyH7WgpoQLemtTd6",
    42      "title": "hello",
    43      "summary": "GNU Hello, the \"hello world\" snap",
    44      "description": "GNU hello prints a friendly greeting. This is part of the snapcraft tour at https://snapcraft.io/",
    45      "installed-size": 98304,
    46      "name": "hello",
    47      "publisher": {
    48        "id": "canonical",
    49        "username": "canonical",
    50        "display-name": "Canonical",
    51        "validation": "verified"
    52      },
    53      "developer": "canonical",
    54      "status": "active",
    55      "type": "app",
    56      "version": "2.10",
    57      "channel": "stable",
    58      "tracking-channel": "stable",
    59      "ignore-validation": false,
    60      "revision": "38",
    61      "confinement": "strict",
    62      "private": false,
    63      "devmode": false,
    64      "jailmode": false,
    65      "apps": [
    66        {
    67          "snap": "hello",
    68          "name": "hello",
    69          "desktop-file": "/path/to/hello_hello.desktop"
    70        },
    71        {
    72          "snap": "hello",
    73          "name": "universe",
    74          "desktop-file": "/path/to/hello_universe.desktop"
    75        }
    76      ],
    77      "contact": "mailto:snaps@canonical.com",
    78      "mounted-from": "/var/lib/snapd/snaps/hello_38.snap",
    79      "install-date": "2019-10-11T13:34:15.630955389+08:00"
    80    }
    81  }
    82  `
    83  
    84  func (s *SnapSuite) TestPortalInfo(c *C) {
    85  	restore := snap.MockCgroupSnapNameFromPid(func(pid int) (string, error) {
    86  		c.Check(pid, Equals, 42)
    87  		return "hello", nil
    88  	})
    89  	defer restore()
    90  	restore = snap.MockApparmorSnapAppFromPid(func(pid int) (string, string, string, error) {
    91  		c.Check(pid, Equals, 42)
    92  		return "hello", "universe", "", nil
    93  	})
    94  	defer restore()
    95  	n := 0
    96  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    97  		switch n {
    98  		case 0:
    99  			c.Check(r.Method, Equals, "GET")
   100  			c.Check(r.URL.Path, Equals, "/v2/snaps/hello")
   101  			fmt.Fprintln(w, mockInfoJSONWithApps)
   102  		case 1:
   103  			c.Check(r.Method, Equals, "GET")
   104  			c.Check(r.URL.Path, Equals, "/v2/connections")
   105  			c.Check(r.URL.Query(), DeepEquals, url.Values{
   106  				"snap":      []string{"hello"},
   107  				"interface": []string{"network-status"},
   108  			})
   109  			result := client.Connections{
   110  				Established: []client.Connection{
   111  					{
   112  						Slot: client.SlotRef{
   113  							Snap: "core",
   114  							Name: "network-status",
   115  						},
   116  						Plug: client.PlugRef{
   117  							Snap: "hello",
   118  							Name: "network-status",
   119  						},
   120  						Interface: "network-status",
   121  					},
   122  				},
   123  			}
   124  			EncodeResponseBody(c, w, map[string]interface{}{
   125  				"type":   "sync",
   126  				"result": result,
   127  			})
   128  		default:
   129  			c.Fatalf("expected to get 2 requests, now on %d (%v)", n+1, r)
   130  		}
   131  		n++
   132  	})
   133  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"routine", "portal-info", "42"})
   134  	c.Assert(err, IsNil)
   135  	c.Check(s.Stdout(), Equals, `[Snap Info]
   136  InstanceName=hello
   137  AppName=universe
   138  DesktopFile=hello_universe.desktop
   139  HasNetworkStatus=true
   140  `)
   141  	c.Check(s.Stderr(), Equals, "")
   142  }
   143  
   144  func (s *SnapSuite) TestPortalInfoNoAppInfo(c *C) {
   145  	restore := snap.MockCgroupSnapNameFromPid(func(pid int) (string, error) {
   146  		c.Check(pid, Equals, 42)
   147  		return "hello", nil
   148  	})
   149  	defer restore()
   150  	restore = snap.MockApparmorSnapAppFromPid(func(pid int) (string, string, string, error) {
   151  		c.Check(pid, Equals, 42)
   152  		return "", "", "", errors.New("no apparmor")
   153  	})
   154  	defer restore()
   155  	n := 0
   156  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   157  		switch n {
   158  		case 0:
   159  			c.Check(r.Method, Equals, "GET")
   160  			c.Check(r.URL.Path, Equals, "/v2/snaps/hello")
   161  			fmt.Fprintln(w, mockInfoJSONWithApps)
   162  		case 1:
   163  			c.Check(r.Method, Equals, "GET")
   164  			c.Check(r.URL.Path, Equals, "/v2/connections")
   165  			c.Check(r.URL.Query(), DeepEquals, url.Values{
   166  				"snap":      []string{"hello"},
   167  				"interface": []string{"network-status"},
   168  			})
   169  			result := client.Connections{}
   170  			EncodeResponseBody(c, w, map[string]interface{}{
   171  				"type":   "sync",
   172  				"result": result,
   173  			})
   174  		default:
   175  			c.Fatalf("expected to get 2 requests, now on %d (%v)", n+1, r)
   176  		}
   177  		n++
   178  	})
   179  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"routine", "portal-info", "42"})
   180  	c.Assert(err, IsNil)
   181  	c.Check(s.Stdout(), Equals, `[Snap Info]
   182  InstanceName=hello
   183  AppName=hello
   184  DesktopFile=hello_hello.desktop
   185  HasNetworkStatus=false
   186  `)
   187  	c.Check(s.Stderr(), Equals, "")
   188  }