github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/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  	"github.com/snapcore/snapd/client"
    28  
    29  	"gopkg.in/check.v1"
    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  }