gitee.com/mysnapcore/mysnapd@v0.1.0/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  	"gitee.com/mysnapcore/mysnapd/client"
    31  	snap "gitee.com/mysnapcore/mysnapd/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          "snap": "hello",
    78          "name": "common-id",
    79          "desktop-file": "/path/to/hello_common-id.desktop",
    80          "common-id": "io.snapcraft.hello.common-id"
    81        }
    82      ],
    83      "contact": "mailto:snaps@canonical.com",
    84      "mounted-from": "/var/lib/snapd/snaps/hello_38.snap",
    85      "install-date": "2019-10-11T13:34:15.630955389+08:00"
    86    }
    87  }
    88  `
    89  
    90  func (s *SnapSuite) TestPortalInfo(c *C) {
    91  	restore := snap.MockCgroupSnapNameFromPid(func(pid int) (string, error) {
    92  		c.Check(pid, Equals, 42)
    93  		return "hello", nil
    94  	})
    95  	defer restore()
    96  	restore = snap.MockApparmorSnapAppFromPid(func(pid int) (string, string, string, error) {
    97  		c.Check(pid, Equals, 42)
    98  		return "hello", "universe", "", nil
    99  	})
   100  	defer restore()
   101  	n := 0
   102  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   103  		switch n {
   104  		case 0:
   105  			c.Check(r.Method, Equals, "GET")
   106  			c.Check(r.URL.Path, Equals, "/v2/snaps/hello")
   107  			fmt.Fprint(w, mockInfoJSONWithApps)
   108  		case 1:
   109  			c.Check(r.Method, Equals, "GET")
   110  			c.Check(r.URL.Path, Equals, "/v2/connections")
   111  			c.Check(r.URL.Query(), DeepEquals, url.Values{
   112  				"snap":      []string{"hello"},
   113  				"interface": []string{"network-status"},
   114  			})
   115  			result := client.Connections{
   116  				Established: []client.Connection{
   117  					{
   118  						Slot: client.SlotRef{
   119  							Snap: "core",
   120  							Name: "network-status",
   121  						},
   122  						Plug: client.PlugRef{
   123  							Snap: "hello",
   124  							Name: "network-status",
   125  						},
   126  						Interface: "network-status",
   127  					},
   128  				},
   129  			}
   130  			EncodeResponseBody(c, w, map[string]interface{}{
   131  				"type":   "sync",
   132  				"result": result,
   133  			})
   134  		default:
   135  			c.Fatalf("expected to get 2 requests, now on %d (%v)", n+1, r)
   136  		}
   137  		n++
   138  	})
   139  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"routine", "portal-info", "42"})
   140  	c.Assert(err, IsNil)
   141  	c.Check(s.Stdout(), Equals, `[Snap Info]
   142  InstanceName=hello
   143  AppName=universe
   144  DesktopFile=hello_universe.desktop
   145  HasNetworkStatus=true
   146  `)
   147  	c.Check(s.Stderr(), Equals, "")
   148  }
   149  
   150  func (s *SnapSuite) TestPortalInfoCommonID(c *C) {
   151  	restore := snap.MockCgroupSnapNameFromPid(func(pid int) (string, error) {
   152  		c.Check(pid, Equals, 42)
   153  		return "hello", nil
   154  	})
   155  	defer restore()
   156  	restore = snap.MockApparmorSnapAppFromPid(func(pid int) (string, string, string, error) {
   157  		c.Check(pid, Equals, 42)
   158  		return "hello", "common-id", "", nil
   159  	})
   160  	defer restore()
   161  	n := 0
   162  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   163  		switch n {
   164  		case 0:
   165  			c.Check(r.Method, Equals, "GET")
   166  			c.Check(r.URL.Path, Equals, "/v2/snaps/hello")
   167  			fmt.Fprint(w, mockInfoJSONWithApps)
   168  		case 1:
   169  			c.Check(r.Method, Equals, "GET")
   170  			c.Check(r.URL.Path, Equals, "/v2/connections")
   171  			c.Check(r.URL.Query(), DeepEquals, url.Values{
   172  				"snap":      []string{"hello"},
   173  				"interface": []string{"network-status"},
   174  			})
   175  			result := client.Connections{}
   176  			EncodeResponseBody(c, w, map[string]interface{}{
   177  				"type":   "sync",
   178  				"result": result,
   179  			})
   180  		default:
   181  			c.Fatalf("expected to get 2 requests, now on %d (%v)", n+1, r)
   182  		}
   183  		n++
   184  	})
   185  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"routine", "portal-info", "42"})
   186  	c.Assert(err, IsNil)
   187  	c.Check(s.Stdout(), Equals, `[Snap Info]
   188  InstanceName=hello
   189  AppName=common-id
   190  DesktopFile=hello_common-id.desktop
   191  CommonID=io.snapcraft.hello.common-id
   192  HasNetworkStatus=false
   193  `)
   194  	c.Check(s.Stderr(), Equals, "")
   195  }
   196  
   197  func (s *SnapSuite) TestPortalInfoNoAppInfo(c *C) {
   198  	restore := snap.MockCgroupSnapNameFromPid(func(pid int) (string, error) {
   199  		c.Check(pid, Equals, 42)
   200  		return "hello", nil
   201  	})
   202  	defer restore()
   203  	restore = snap.MockApparmorSnapAppFromPid(func(pid int) (string, string, string, error) {
   204  		c.Check(pid, Equals, 42)
   205  		return "", "", "", errors.New("no apparmor")
   206  	})
   207  	defer restore()
   208  	n := 0
   209  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   210  		switch n {
   211  		case 0:
   212  			c.Check(r.Method, Equals, "GET")
   213  			c.Check(r.URL.Path, Equals, "/v2/snaps/hello")
   214  			fmt.Fprint(w, mockInfoJSONWithApps)
   215  		case 1:
   216  			c.Check(r.Method, Equals, "GET")
   217  			c.Check(r.URL.Path, Equals, "/v2/connections")
   218  			c.Check(r.URL.Query(), DeepEquals, url.Values{
   219  				"snap":      []string{"hello"},
   220  				"interface": []string{"network-status"},
   221  			})
   222  			result := client.Connections{}
   223  			EncodeResponseBody(c, w, map[string]interface{}{
   224  				"type":   "sync",
   225  				"result": result,
   226  			})
   227  		default:
   228  			c.Fatalf("expected to get 2 requests, now on %d (%v)", n+1, r)
   229  		}
   230  		n++
   231  	})
   232  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"routine", "portal-info", "42"})
   233  	c.Assert(err, IsNil)
   234  	c.Check(s.Stdout(), Equals, `[Snap Info]
   235  InstanceName=hello
   236  AppName=hello
   237  DesktopFile=hello_hello.desktop
   238  HasNetworkStatus=false
   239  `)
   240  	c.Check(s.Stderr(), Equals, "")
   241  }