gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/client/snapctl_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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 client_test
    21  
    22  import (
    23  	"bytes"
    24  	"encoding/base64"
    25  	"encoding/json"
    26  
    27  	"gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/client"
    30  )
    31  
    32  func (cs *clientSuite) TestClientRunSnapctlCallsEndpoint(c *check.C) {
    33  	options := &client.SnapCtlOptions{
    34  		ContextID: "1234ABCD",
    35  		Args:      []string{"foo", "bar"},
    36  	}
    37  	cs.cli.RunSnapctl(options, nil)
    38  	c.Check(cs.req.Method, check.Equals, "POST")
    39  	c.Check(cs.req.URL.Path, check.Equals, "/v2/snapctl")
    40  }
    41  
    42  func (cs *clientSuite) TestClientRunSnapctl(c *check.C) {
    43  	cs.rsp = `{
    44  		"type": "sync",
    45          "status-code": 200,
    46  		"result": {
    47  			"stdout": "test stdout",
    48  			"stderr": "test stderr"
    49  		}
    50  	}`
    51  
    52  	mockStdin := bytes.NewBufferString("some-input")
    53  	options := &client.SnapCtlOptions{
    54  		ContextID: "1234ABCD",
    55  		Args:      []string{"foo", "bar"},
    56  	}
    57  
    58  	stdout, stderr, err := cs.cli.RunSnapctl(options, mockStdin)
    59  	c.Assert(err, check.IsNil)
    60  	c.Check(string(stdout), check.Equals, "test stdout")
    61  	c.Check(string(stderr), check.Equals, "test stderr")
    62  
    63  	var body map[string]interface{}
    64  	decoder := json.NewDecoder(cs.req.Body)
    65  	err = decoder.Decode(&body)
    66  	c.Check(err, check.IsNil)
    67  	c.Check(body, check.DeepEquals, map[string]interface{}{
    68  		"context-id": "1234ABCD",
    69  		"args":       []interface{}{"foo", "bar"},
    70  
    71  		// json byte-stream is b64 encoded
    72  		"stdin": base64.StdEncoding.EncodeToString([]byte("some-input")),
    73  	})
    74  }
    75  
    76  func (cs *clientSuite) TestInternalSnapctlCmdNeedsStdin(c *check.C) {
    77  	res := client.InternalSnapctlCmdNeedsStdin("fde-setup-result")
    78  	c.Check(res, check.Equals, true)
    79  
    80  	for _, s := range []string{"help", "other"} {
    81  		res := client.InternalSnapctlCmdNeedsStdin(s)
    82  		c.Check(res, check.Equals, false)
    83  	}
    84  }
    85  
    86  func (cs *clientSuite) TestClientRunSnapctlReadLimitOneTooMuch(c *check.C) {
    87  	cs.rsp = `{
    88  		"type": "sync",
    89          "status-code": 200,
    90  		"result": {
    91  		}
    92  	}`
    93  
    94  	restore := client.MockStdinReadLimit(10)
    95  	defer restore()
    96  
    97  	mockStdin := bytes.NewBufferString("12345678901")
    98  	options := &client.SnapCtlOptions{
    99  		ContextID: "1234ABCD",
   100  		Args:      []string{"foo", "bar"},
   101  	}
   102  
   103  	_, _, err := cs.cli.RunSnapctl(options, mockStdin)
   104  	c.Check(err, check.ErrorMatches, "cannot read more than 10 bytes of data from stdin")
   105  }
   106  
   107  func (cs *clientSuite) TestClientRunSnapctlReadLimitExact(c *check.C) {
   108  	cs.rsp = `{
   109  		"type": "sync",
   110          "status-code": 200,
   111  		"result": {
   112  		}
   113  	}`
   114  
   115  	restore := client.MockStdinReadLimit(10)
   116  	defer restore()
   117  
   118  	mockStdin := bytes.NewBufferString("1234567890")
   119  	options := &client.SnapCtlOptions{
   120  		ContextID: "1234ABCD",
   121  		Args:      []string{"foo", "bar"},
   122  	}
   123  
   124  	_, _, err := cs.cli.RunSnapctl(options, mockStdin)
   125  	c.Check(err, check.IsNil)
   126  }