github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/cmd/snap/cmd_connectivity_check_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 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 "fmt" 24 "io/ioutil" 25 "net/http" 26 27 "gopkg.in/check.v1" 28 29 snap "github.com/snapcore/snapd/cmd/snap" 30 ) 31 32 func (s *SnapSuite) TestConnectivityHappy(c *check.C) { 33 n := 0 34 s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) { 35 switch n { 36 case 0: 37 c.Check(r.Method, check.Equals, "GET") 38 c.Check(r.URL.Path, check.Equals, "/v2/debug") 39 c.Check(r.URL.RawQuery, check.Equals, "aspect=connectivity") 40 data, err := ioutil.ReadAll(r.Body) 41 c.Check(err, check.IsNil) 42 c.Check(data, check.HasLen, 0) 43 fmt.Fprintln(w, `{"type": "sync", "result": {}}`) 44 default: 45 c.Fatalf("expected to get 1 requests, now on %d", n+1) 46 } 47 48 n++ 49 }) 50 rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"debug", "connectivity"}) 51 c.Assert(err, check.IsNil) 52 c.Assert(rest, check.DeepEquals, []string{}) 53 c.Check(s.Stdout(), check.Equals, `Connectivity status: 54 * PASS 55 `) 56 c.Check(s.Stderr(), check.Equals, "") 57 } 58 59 func (s *SnapSuite) TestConnectivityUnhappy(c *check.C) { 60 n := 0 61 s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) { 62 switch n { 63 case 0: 64 c.Check(r.Method, check.Equals, "GET") 65 c.Check(r.URL.Path, check.Equals, "/v2/debug") 66 c.Check(r.URL.RawQuery, check.Equals, "aspect=connectivity") 67 data, err := ioutil.ReadAll(r.Body) 68 c.Check(err, check.IsNil) 69 c.Check(data, check.HasLen, 0) 70 fmt.Fprintln(w, `{"type": "sync", "result": {"connectivity":false,"unreachable":["foo.bar.com"]}}`) 71 default: 72 c.Fatalf("expected to get 1 requests, now on %d", n+1) 73 } 74 75 n++ 76 }) 77 _, err := snap.Parser(snap.Client()).ParseArgs([]string{"debug", "connectivity"}) 78 c.Assert(err, check.ErrorMatches, "1 servers unreachable") 79 // note that only the unreachable hosts are displayed 80 c.Check(s.Stdout(), check.Equals, `Connectivity status: 81 * foo.bar.com: unreachable 82 `) 83 c.Check(s.Stderr(), check.Equals, "") 84 }