github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/snap/cmd_get_base_declaration_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2017 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) TestGetBaseDeclaration(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, "POST") 38 c.Check(r.URL.Path, check.Equals, "/v2/debug") 39 c.Check(r.URL.RawQuery, check.Equals, "") 40 data, err := ioutil.ReadAll(r.Body) 41 c.Check(err, check.IsNil) 42 c.Check(data, check.DeepEquals, []byte(`{"action":"get-base-declaration"}`)) 43 fmt.Fprintln(w, `{"type": "sync", "result": {"base-declaration": "hello"}}`) 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", "get-base-declaration"}) 51 c.Assert(err, check.IsNil) 52 c.Assert(rest, check.DeepEquals, []string{}) 53 c.Check(s.Stdout(), check.Equals, "hello\n") 54 c.Check(s.Stderr(), check.Equals, "") 55 } 56 57 func (s *SnapSuite) TestBaseDeclaration(c *check.C) { 58 n := 0 59 s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) { 60 switch n { 61 case 0: 62 c.Check(r.Method, check.Equals, "GET") 63 c.Check(r.URL.Path, check.Equals, "/v2/debug") 64 c.Check(r.URL.RawQuery, check.Equals, "aspect=base-declaration") 65 data, err := ioutil.ReadAll(r.Body) 66 c.Check(err, check.IsNil) 67 c.Check(data, check.HasLen, 0) 68 fmt.Fprintln(w, `{"type": "sync", "result": {"base-declaration": "hello"}}`) 69 default: 70 c.Fatalf("expected to get 1 requests, now on %d", n+1) 71 } 72 73 n++ 74 }) 75 rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"debug", "base-declaration"}) 76 c.Assert(err, check.IsNil) 77 c.Assert(rest, check.DeepEquals, []string{}) 78 c.Check(s.Stdout(), check.Equals, "hello\n") 79 c.Check(s.Stderr(), check.Equals, "") 80 }