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