1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package ctlcmd_test
import (
"fmt"
"strings"
"testing"
"github.com/jessevdk/go-flags"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/overlord/hookstate"
"github.com/snapcore/snapd/overlord/hookstate/ctlcmd"
"github.com/snapcore/snapd/overlord/hookstate/hooktest"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/testutil"
)
func Test(t *testing.T) { TestingT(t) }
type ctlcmdSuite struct {
mockContext *hookstate.Context
}
var _ = Suite(&ctlcmdSuite{})
func (s *ctlcmdSuite) SetUpTest(c *C) {
handler := hooktest.NewMockHandler()
state := state.New(nil)
state.Lock()
defer state.Unlock()
task := state.NewTask("test-task", "my test task")
setup := &hookstate.HookSetup{Snap: "test-snap", Revision: snap.R(1), Hook: "test-hook"}
var err error
s.mockContext, err = hookstate.NewContext(task, task.State(), setup, handler, "")
c.Assert(err, IsNil)
}
func (s *ctlcmdSuite) TestNonExistingCommand(c *C) {
stdout, stderr, err := ctlcmd.Run(s.mockContext, []string{"foo"}, 0)
c.Check(string(stdout), Equals, "")
c.Check(string(stderr), Equals, "")
c.Check(err, ErrorMatches, ".*[Uu]nknown command.*")
}
func (s *ctlcmdSuite) TestCommandOutput(c *C) {
mockCommand := ctlcmd.AddMockCommand("mock")
defer ctlcmd.RemoveCommand("mock")
mockCommand.FakeStdout = "test stdout"
mockCommand.FakeStderr = "test stderr"
stdout, stderr, err := ctlcmd.Run(s.mockContext, []string{"mock", "foo"}, 0)
c.Check(err, IsNil)
c.Check(string(stdout), Equals, "test stdout")
c.Check(string(stderr), Equals, "test stderr")
c.Check(mockCommand.Args, DeepEquals, []string{"foo"})
}
func taskKinds(tasks []*state.Task) []string {
kinds := make([]string, len(tasks))
for i, task := range tasks {
k := task.Kind()
if k == "run-hook" {
var hooksup hookstate.HookSetup
if err := task.Get("hook-setup", &hooksup); err != nil {
panic(err)
}
k = fmt.Sprintf("%s[%s]", k, hooksup.Hook)
}
kinds[i] = k
}
return kinds
}
func (s *ctlcmdSuite) TestHiddenCommand(c *C) {
ctlcmd.AddHiddenMockCommand("mock-hidden")
ctlcmd.AddMockCommand("mock-shown")
defer ctlcmd.RemoveCommand("mock-hidden")
defer ctlcmd.RemoveCommand("mock-shown")
_, _, err := ctlcmd.Run(s.mockContext, []string{"--help"}, 0)
// help message output is returned as *flags.Error with
// Type as flags.ErrHelp
c.Assert(err, FitsTypeOf, &flags.Error{})
c.Check(err.(*flags.Error).Type, Equals, flags.ErrHelp)
// snapctl is mentioned (not snapd)
c.Check(err.Error(), testutil.Contains, "snapctl")
// mock-shown is in the help message
c.Check(err.Error(), testutil.Contains, " mock-shown\n")
// mock-hidden is not in the help message
c.Check(err.Error(), Not(testutil.Contains), " mock-hidden\n")
}
func (s *ctlcmdSuite) TestRootRequiredCommandFailure(c *C) {
_, _, err := ctlcmd.Run(s.mockContext, []string{"start"}, 1000)
c.Check(err, FitsTypeOf, &ctlcmd.ForbiddenCommandError{})
c.Check(err.Error(), Equals, `cannot use "start" with uid 1000, try with sudo`)
}
func (s *ctlcmdSuite) TestRootRequiredCommandFailureParsingTweak(c *C) {
_, _, err := ctlcmd.Run(s.mockContext, []string{"start", "--", "--help"}, 1000)
c.Check(err, FitsTypeOf, &ctlcmd.ForbiddenCommandError{})
c.Check(err.Error(), Equals, `cannot use "start" with uid 1000, try with sudo`)
}
func (s *ctlcmdSuite) TestRunNoArgsFailure(c *C) {
_, _, err := ctlcmd.Run(s.mockContext, []string{}, 0)
c.Check(err, NotNil)
}
func (s *ctlcmdSuite) TestRunOnlyHelp(c *C) {
_, _, err := ctlcmd.Run(s.mockContext, []string{"-h"}, 1000)
c.Check(err, NotNil)
c.Assert(strings.HasPrefix(err.Error(), "Usage:"), Equals, true)
_, _, err = ctlcmd.Run(s.mockContext, []string{"--help"}, 1000)
c.Check(err, NotNil)
c.Assert(strings.HasPrefix(err.Error(), "Usage:"), Equals, true)
}
func (s *ctlcmdSuite) TestRunHelpAtAnyPosition(c *C) {
_, _, err := ctlcmd.Run(s.mockContext, []string{"start", "a", "-h"}, 1000)
c.Check(err, NotNil)
c.Assert(strings.HasPrefix(err.Error(), "Usage:"), Equals, true)
_, _, err = ctlcmd.Run(s.mockContext, []string{"start", "a", "b", "--help"}, 1000)
c.Check(err, NotNil)
c.Assert(strings.HasPrefix(err.Error(), "Usage:"), Equals, true)
}
func (s *ctlcmdSuite) TestRunNonRootAllowedCommandWithAllowedCmdAsArg(c *C) {
// this test protects us against a future refactor introducing a bug that allows
// a root-only command to run without root if an arg is in the nonRootAllowed list
_, _, err := ctlcmd.Run(s.mockContext, []string{"set", "get", "a"}, 1000)
c.Check(err, FitsTypeOf, &ctlcmd.ForbiddenCommandError{})
c.Check(err.Error(), Equals, `cannot use "set" with uid 1000, try with sudo`)
}
|