github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/daemon/api_snapctl_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2014-2020 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 daemon_test 21 22 import ( 23 "bytes" 24 "fmt" 25 "net/http" 26 27 "gopkg.in/check.v1" 28 29 "github.com/snapcore/snapd/client" 30 "github.com/snapcore/snapd/daemon" 31 "github.com/snapcore/snapd/dirs" 32 "github.com/snapcore/snapd/overlord/hookstate" 33 "github.com/snapcore/snapd/overlord/hookstate/ctlcmd" 34 ) 35 36 var _ = check.Suite(&snapctlSuite{}) 37 38 type snapctlSuite struct { 39 apiBaseSuite 40 } 41 42 func (s *snapctlSuite) TestSnapctlGetNoUID(c *check.C) { 43 s.daemon(c) 44 buf := bytes.NewBufferString(`{"context-id": "some-context", "args": ["get", "something"]}`) 45 req, err := http.NewRequest("POST", "/v2/snapctl", buf) 46 c.Assert(err, check.IsNil) 47 rsp := s.req(c, req, nil).(*daemon.Resp) 48 c.Assert(rsp.Status, check.Equals, 403) 49 } 50 51 func (s *snapctlSuite) TestSnapctlForbiddenError(c *check.C) { 52 s.daemon(c) 53 54 defer daemon.MockUcrednetGet(func(string) (int32, uint32, string, error) { 55 return 100, 9999, dirs.SnapSocket, nil 56 })() 57 58 defer daemon.MockCtlcmdRun(func(ctx *hookstate.Context, arg []string, uid uint32) ([]byte, []byte, error) { 59 return nil, nil, &ctlcmd.ForbiddenCommandError{} 60 })() 61 62 buf := bytes.NewBufferString(fmt.Sprintf(`{"context-id": "some-context", "args": [%q, %q]}`, "set", "foo=bar")) 63 req, err := http.NewRequest("POST", "/v2/snapctl", buf) 64 c.Assert(err, check.IsNil) 65 rsp := s.req(c, req, nil).(*daemon.Resp) 66 c.Assert(rsp.Status, check.Equals, 403) 67 } 68 69 func (s *snapctlSuite) TestSnapctlUnsuccesfulError(c *check.C) { 70 s.daemon(c) 71 72 defer daemon.MockUcrednetGet(func(string) (int32, uint32, string, error) { 73 return 100, 9999, dirs.SnapSocket, nil 74 })() 75 76 defer daemon.MockCtlcmdRun(func(ctx *hookstate.Context, arg []string, uid uint32) ([]byte, []byte, error) { 77 return nil, nil, &ctlcmd.UnsuccessfulError{ExitCode: 123} 78 })() 79 80 buf := bytes.NewBufferString(fmt.Sprintf(`{"context-id": "some-context", "args": [%q, %q]}`, "is-connected", "plug")) 81 req, err := http.NewRequest("POST", "/v2/snapctl", buf) 82 c.Assert(err, check.IsNil) 83 rsp := s.req(c, req, nil).(*daemon.Resp) 84 c.Check(rsp.Status, check.Equals, 200) 85 c.Check(rsp.Result.(*daemon.ErrorResult).Kind, check.Equals, client.ErrorKindUnsuccessful) 86 c.Check(rsp.Result.(*daemon.ErrorResult).Value, check.DeepEquals, map[string]interface{}{ 87 "stdout": "", 88 "stderr": "", 89 "exit-code": 123, 90 }) 91 }