github.com/rigado/snapd@v2.42.5-go-mod+incompatible/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  	n := 0
    51  	s.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    52  		switch n {
    53  		case 0:
    54  			c.Assert(r.Method, Equals, "POST")
    55  			c.Assert(r.URL.Path, Equals, "/v2/snapctl")
    56  			c.Assert(r.Header.Get("Authorization"), Equals, "")
    57  
    58  			var snapctlOptions client.SnapCtlOptions
    59  			decoder := json.NewDecoder(r.Body)
    60  			c.Assert(decoder.Decode(&snapctlOptions), IsNil)
    61  			c.Assert(snapctlOptions.ContextID, Equals, s.expectedContextID)
    62  			c.Assert(snapctlOptions.Args, DeepEquals, s.expectedArgs)
    63  
    64  			fmt.Fprintln(w, `{"type": "sync", "result": {"stdout": "test stdout", "stderr": "test stderr"}}`)
    65  		default:
    66  			c.Fatalf("expected to get 1 request, now on %d", n+1)
    67  		}
    68  
    69  		n++
    70  	}))
    71  	clientConfig.BaseURL = s.server.URL
    72  	s.oldArgs = os.Args
    73  	os.Args = []string{"snapctl"}
    74  	s.expectedContextID = "snap-context-test"
    75  	s.expectedArgs = []string{}
    76  
    77  	fakeAuthPath := filepath.Join(c.MkDir(), "auth.json")
    78  	os.Setenv("SNAPD_AUTH_DATA_FILENAME", fakeAuthPath)
    79  	err := ioutil.WriteFile(fakeAuthPath, []byte(`{"macaroon":"user-macaroon"}`), 0644)
    80  	c.Assert(err, IsNil)
    81  }
    82  
    83  func (s *snapctlSuite) TearDownTest(c *C) {
    84  	os.Unsetenv("SNAP_COOKIE")
    85  	os.Unsetenv("SNAPD_AUTH_DATA_FILENAME")
    86  	clientConfig.BaseURL = ""
    87  	s.server.Close()
    88  	os.Args = s.oldArgs
    89  }
    90  
    91  func (s *snapctlSuite) TestSnapctl(c *C) {
    92  	stdout, stderr, err := run()
    93  	c.Check(err, IsNil)
    94  	c.Check(string(stdout), Equals, "test stdout")
    95  	c.Check(string(stderr), Equals, "test stderr")
    96  }
    97  
    98  func (s *snapctlSuite) TestSnapctlWithArgs(c *C) {
    99  	os.Args = []string{"snapctl", "foo", "--bar"}
   100  
   101  	s.expectedArgs = []string{"foo", "--bar"}
   102  	stdout, stderr, err := run()
   103  	c.Check(err, IsNil)
   104  	c.Check(string(stdout), Equals, "test stdout")
   105  	c.Check(string(stderr), Equals, "test stderr")
   106  }
   107  
   108  func (s *snapctlSuite) TestSnapctlHelp(c *C) {
   109  	os.Unsetenv("SNAP_COOKIE")
   110  	s.expectedContextID = ""
   111  
   112  	os.Args = []string{"snapctl", "-h"}
   113  	s.expectedArgs = []string{"-h"}
   114  
   115  	_, _, err := run()
   116  	c.Check(err, IsNil)
   117  }