github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/hookstate/ctlcmd/ctlcmd_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 ctlcmd_test
    21  
    22  import (
    23  	"fmt"
    24  	"testing"
    25  
    26  	"github.com/jessevdk/go-flags"
    27  	"github.com/snapcore/snapd/overlord/hookstate"
    28  	"github.com/snapcore/snapd/overlord/hookstate/ctlcmd"
    29  	"github.com/snapcore/snapd/overlord/hookstate/hooktest"
    30  	"github.com/snapcore/snapd/overlord/state"
    31  	"github.com/snapcore/snapd/snap"
    32  	"github.com/snapcore/snapd/testutil"
    33  
    34  	. "gopkg.in/check.v1"
    35  )
    36  
    37  func Test(t *testing.T) { TestingT(t) }
    38  
    39  type ctlcmdSuite struct {
    40  	mockContext *hookstate.Context
    41  }
    42  
    43  var _ = Suite(&ctlcmdSuite{})
    44  
    45  func (s *ctlcmdSuite) SetUpTest(c *C) {
    46  	handler := hooktest.NewMockHandler()
    47  
    48  	state := state.New(nil)
    49  	state.Lock()
    50  	defer state.Unlock()
    51  
    52  	task := state.NewTask("test-task", "my test task")
    53  	setup := &hookstate.HookSetup{Snap: "test-snap", Revision: snap.R(1), Hook: "test-hook"}
    54  
    55  	var err error
    56  	s.mockContext, err = hookstate.NewContext(task, task.State(), setup, handler, "")
    57  	c.Assert(err, IsNil)
    58  }
    59  
    60  func (s *ctlcmdSuite) TestNonExistingCommand(c *C) {
    61  	stdout, stderr, err := ctlcmd.Run(s.mockContext, []string{"foo"}, 0)
    62  	c.Check(string(stdout), Equals, "")
    63  	c.Check(string(stderr), Equals, "")
    64  	c.Check(err, ErrorMatches, ".*[Uu]nknown command.*")
    65  }
    66  
    67  func (s *ctlcmdSuite) TestCommandOutput(c *C) {
    68  	mockCommand := ctlcmd.AddMockCommand("mock")
    69  	defer ctlcmd.RemoveCommand("mock")
    70  
    71  	mockCommand.FakeStdout = "test stdout"
    72  	mockCommand.FakeStderr = "test stderr"
    73  
    74  	stdout, stderr, err := ctlcmd.Run(s.mockContext, []string{"mock", "foo"}, 0)
    75  	c.Check(err, IsNil)
    76  	c.Check(string(stdout), Equals, "test stdout")
    77  	c.Check(string(stderr), Equals, "test stderr")
    78  	c.Check(mockCommand.Args, DeepEquals, []string{"foo"})
    79  }
    80  
    81  func taskKinds(tasks []*state.Task) []string {
    82  	kinds := make([]string, len(tasks))
    83  	for i, task := range tasks {
    84  		k := task.Kind()
    85  		if k == "run-hook" {
    86  			var hooksup hookstate.HookSetup
    87  			if err := task.Get("hook-setup", &hooksup); err != nil {
    88  				panic(err)
    89  			}
    90  			k = fmt.Sprintf("%s[%s]", k, hooksup.Hook)
    91  		}
    92  		kinds[i] = k
    93  	}
    94  	return kinds
    95  }
    96  
    97  func (s *ctlcmdSuite) TestHiddenCommand(c *C) {
    98  	ctlcmd.AddHiddenMockCommand("mock-hidden")
    99  	ctlcmd.AddMockCommand("mock-shown")
   100  	defer ctlcmd.RemoveCommand("mock-hidden")
   101  	defer ctlcmd.RemoveCommand("mock-shown")
   102  
   103  	_, _, err := ctlcmd.Run(s.mockContext, []string{"--help"}, 0)
   104  	// help message output is returned as *flags.Error with
   105  	// Type as flags.ErrHelp
   106  	c.Assert(err, FitsTypeOf, &flags.Error{})
   107  	c.Check(err.(*flags.Error).Type, Equals, flags.ErrHelp)
   108  	// snapctl is mentioned (not snapd)
   109  	c.Check(err.Error(), testutil.Contains, "snapctl")
   110  	// mock-shown is in the help message
   111  	c.Check(err.Error(), testutil.Contains, "  mock-shown\n")
   112  	// mock-hidden is not in the help message
   113  	c.Check(err.Error(), Not(testutil.Contains), "  mock-hidden\n")
   114  }