gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/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) SetUpTest(c *check.C) {
    43  	s.apiBaseSuite.SetUpTest(c)
    44  
    45  	s.expectWriteAccess(daemon.SnapAccess{})
    46  }
    47  
    48  func (s *snapctlSuite) TestSnapctlGetNoUID(c *check.C) {
    49  	s.daemon(c)
    50  	buf := bytes.NewBufferString(`{"context-id": "some-context", "args": ["get", "something"]}`)
    51  	req, err := http.NewRequest("POST", "/v2/snapctl", buf)
    52  	c.Assert(err, check.IsNil)
    53  	rsp := s.errorReq(c, req, nil)
    54  	c.Assert(rsp.Status, check.Equals, 403)
    55  }
    56  
    57  func (s *snapctlSuite) TestSnapctlForbiddenError(c *check.C) {
    58  	s.daemon(c)
    59  
    60  	defer daemon.MockUcrednetGet(func(string) (*daemon.Ucrednet, error) {
    61  		return &daemon.Ucrednet{Uid: 100, Pid: 9999, Socket: dirs.SnapSocket}, nil
    62  	})()
    63  
    64  	defer daemon.MockCtlcmdRun(func(ctx *hookstate.Context, arg []string, uid uint32) ([]byte, []byte, error) {
    65  		return nil, nil, &ctlcmd.ForbiddenCommandError{}
    66  	})()
    67  
    68  	buf := bytes.NewBufferString(fmt.Sprintf(`{"context-id": "some-context", "args": [%q, %q]}`, "set", "foo=bar"))
    69  	req, err := http.NewRequest("POST", "/v2/snapctl", buf)
    70  	c.Assert(err, check.IsNil)
    71  	rsp := s.errorReq(c, req, nil)
    72  	c.Assert(rsp.Status, check.Equals, 403)
    73  }
    74  
    75  func (s *snapctlSuite) TestSnapctlUnsuccesfulError(c *check.C) {
    76  	s.daemon(c)
    77  
    78  	defer daemon.MockUcrednetGet(func(string) (*daemon.Ucrednet, error) {
    79  		return &daemon.Ucrednet{Uid: 100, Pid: 9999, Socket: dirs.SnapSocket}, nil
    80  	})()
    81  
    82  	defer daemon.MockCtlcmdRun(func(ctx *hookstate.Context, arg []string, uid uint32) ([]byte, []byte, error) {
    83  		return nil, nil, &ctlcmd.UnsuccessfulError{ExitCode: 123}
    84  	})()
    85  
    86  	buf := bytes.NewBufferString(fmt.Sprintf(`{"context-id": "some-context", "args": [%q, %q]}`, "is-connected", "plug"))
    87  	req, err := http.NewRequest("POST", "/v2/snapctl", buf)
    88  	c.Assert(err, check.IsNil)
    89  	rspe := s.errorReq(c, req, nil)
    90  	c.Check(rspe.Status, check.Equals, 200)
    91  	c.Check(rspe.Kind, check.Equals, client.ErrorKindUnsuccessful)
    92  	c.Check(rspe.Value, check.DeepEquals, map[string]interface{}{
    93  		"stdout":    "",
    94  		"stderr":    "",
    95  		"exit-code": 123,
    96  	})
    97  }