github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/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  	"encoding/json"
    24  
    25  	"github.com/snapcore/snapd/client"
    26  
    27  	"gopkg.in/check.v1"
    28  )
    29  
    30  func (cs *clientSuite) TestClientRunSnapctlCallsEndpoint(c *check.C) {
    31  	options := &client.SnapCtlOptions{
    32  		ContextID: "1234ABCD",
    33  		Args:      []string{"foo", "bar"},
    34  	}
    35  	cs.cli.RunSnapctl(options)
    36  	c.Check(cs.req.Method, check.Equals, "POST")
    37  	c.Check(cs.req.URL.Path, check.Equals, "/v2/snapctl")
    38  }
    39  
    40  func (cs *clientSuite) TestClientRunSnapctl(c *check.C) {
    41  	cs.rsp = `{
    42  		"type": "sync",
    43          "status-code": 200,
    44  		"result": {
    45  			"stdout": "test stdout",
    46  			"stderr": "test stderr"
    47  		}
    48  	}`
    49  
    50  	options := &client.SnapCtlOptions{
    51  		ContextID: "1234ABCD",
    52  		Args:      []string{"foo", "bar"},
    53  	}
    54  
    55  	stdout, stderr, err := cs.cli.RunSnapctl(options)
    56  	c.Assert(err, check.IsNil)
    57  	c.Check(string(stdout), check.Equals, "test stdout")
    58  	c.Check(string(stderr), check.Equals, "test stderr")
    59  
    60  	var body map[string]interface{}
    61  	decoder := json.NewDecoder(cs.req.Body)
    62  	err = decoder.Decode(&body)
    63  	c.Check(err, check.IsNil)
    64  	c.Check(body, check.DeepEquals, map[string]interface{}{
    65  		"context-id": "1234ABCD",
    66  		"args":       []interface{}{"foo", "bar"},
    67  	})
    68  }