github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/cmd/snapctl/main_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 main
    21  
    22  import (
    23  	"encoding/json"
    24  	"fmt"
    25  	"io/ioutil"
    26  	"net/http"
    27  	"net/http/httptest"
    28  	"os"
    29  	"path/filepath"
    30  	"testing"
    31  
    32  	"github.com/snapcore/snapd/client"
    33  
    34  	. "gopkg.in/check.v1"
    35  )
    36  
    37  func TestT(t *testing.T) { TestingT(t) }
    38  
    39  type snapctlSuite struct {
    40  	server            *httptest.Server
    41  	oldArgs           []string
    42  	expectedContextID string
    43  	expectedArgs      []string
    44  }
    45  
    46  var _ = Suite(&snapctlSuite{})
    47  
    48  func (s *snapctlSuite) SetUpTest(c *C) {
    49  	os.Setenv("SNAP_COOKIE", "snap-context-test")
    50  	// don't use SNAP_CONTEXT, in case other tests accidentally leak this
    51  	os.Unsetenv("SNAP_CONTEXT")
    52  	n := 0
    53  	s.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    54  		switch n {
    55  		case 0:
    56  			c.Assert(r.Method, Equals, "POST")
    57  			c.Assert(r.URL.Path, Equals, "/v2/snapctl")
    58  			c.Assert(r.Header.Get("Authorization"), Equals, "")
    59  
    60  			var snapctlOptions client.SnapCtlOptions
    61  			decoder := json.NewDecoder(r.Body)
    62  			c.Assert(decoder.Decode(&snapctlOptions), IsNil)
    63  			c.Assert(snapctlOptions.ContextID, Equals, s.expectedContextID)
    64  			c.Assert(snapctlOptions.Args, DeepEquals, s.expectedArgs)
    65  
    66  			fmt.Fprintln(w, `{"type": "sync", "result": {"stdout": "test stdout", "stderr": "test stderr"}}`)
    67  		default:
    68  			c.Fatalf("expected to get 1 request, now on %d", n+1)
    69  		}
    70  
    71  		n++
    72  	}))
    73  	clientConfig.BaseURL = s.server.URL
    74  	s.oldArgs = os.Args
    75  	os.Args = []string{"snapctl"}
    76  	s.expectedContextID = "snap-context-test"
    77  	s.expectedArgs = []string{}
    78  
    79  	fakeAuthPath := filepath.Join(c.MkDir(), "auth.json")
    80  	os.Setenv("SNAPD_AUTH_DATA_FILENAME", fakeAuthPath)
    81  	err := ioutil.WriteFile(fakeAuthPath, []byte(`{"macaroon":"user-macaroon"}`), 0644)
    82  	c.Assert(err, IsNil)
    83  }
    84  
    85  func (s *snapctlSuite) TearDownTest(c *C) {
    86  	os.Unsetenv("SNAP_COOKIE")
    87  	os.Unsetenv("SNAPD_AUTH_DATA_FILENAME")
    88  	clientConfig.BaseURL = ""
    89  	s.server.Close()
    90  	os.Args = s.oldArgs
    91  }
    92  
    93  func (s *snapctlSuite) TestSnapctl(c *C) {
    94  	stdout, stderr, err := run()
    95  	c.Check(err, IsNil)
    96  	c.Check(string(stdout), Equals, "test stdout")
    97  	c.Check(string(stderr), Equals, "test stderr")
    98  }
    99  
   100  func (s *snapctlSuite) TestSnapctlWithArgs(c *C) {
   101  	os.Args = []string{"snapctl", "foo", "--bar"}
   102  
   103  	s.expectedArgs = []string{"foo", "--bar"}
   104  	stdout, stderr, err := run()
   105  	c.Check(err, IsNil)
   106  	c.Check(string(stdout), Equals, "test stdout")
   107  	c.Check(string(stderr), Equals, "test stderr")
   108  }
   109  
   110  func (s *snapctlSuite) TestSnapctlHelp(c *C) {
   111  	os.Unsetenv("SNAP_COOKIE")
   112  	s.expectedContextID = ""
   113  
   114  	os.Args = []string{"snapctl", "-h"}
   115  	s.expectedArgs = []string{"-h"}
   116  
   117  	_, _, err := run()
   118  	c.Check(err, IsNil)
   119  }