github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/overlord/hookstate/ctlcmd/export_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
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"github.com/snapcore/snapd/overlord/hookstate"
    26  	"github.com/snapcore/snapd/overlord/servicestate"
    27  	"github.com/snapcore/snapd/overlord/state"
    28  	"github.com/snapcore/snapd/snap"
    29  )
    30  
    31  const (
    32  	NotASnapCode    = notASnapCode
    33  	ClassicSnapCode = classicSnapCode
    34  )
    35  
    36  var AttributesTask = attributesTask
    37  
    38  func MockServicestateControlFunc(f func(*state.State, []*snap.AppInfo, *servicestate.Instruction, *servicestate.Flags, *hookstate.Context) ([]*state.TaskSet, error)) (restore func()) {
    39  	old := servicestateControl
    40  	servicestateControl = f
    41  	return func() { servicestateControl = old }
    42  }
    43  
    44  func AddMockCommand(name string) *MockCommand {
    45  	return addMockCmd(name, false)
    46  }
    47  
    48  func AddHiddenMockCommand(name string) *MockCommand {
    49  	return addMockCmd(name, true)
    50  }
    51  
    52  func addMockCmd(name string, hidden bool) *MockCommand {
    53  	mockCommand := NewMockCommand()
    54  	cmd := addCommand(name, "", "", func() command { return mockCommand })
    55  	cmd.hidden = hidden
    56  	return mockCommand
    57  }
    58  
    59  func RemoveCommand(name string) {
    60  	delete(commands, name)
    61  }
    62  
    63  type MockCommand struct {
    64  	baseCommand
    65  
    66  	ExecuteError bool
    67  	FakeStdout   string
    68  	FakeStderr   string
    69  
    70  	Args []string
    71  }
    72  
    73  func NewMockCommand() *MockCommand {
    74  	return &MockCommand{
    75  		ExecuteError: false,
    76  	}
    77  }
    78  
    79  func (c *MockCommand) Execute(args []string) error {
    80  	c.Args = args
    81  
    82  	if c.FakeStdout != "" {
    83  		c.printf(c.FakeStdout)
    84  	}
    85  
    86  	if c.FakeStderr != "" {
    87  		c.errorf(c.FakeStderr)
    88  	}
    89  
    90  	if c.ExecuteError {
    91  		return fmt.Errorf("failed at user request")
    92  	}
    93  
    94  	return nil
    95  }
    96  
    97  func MockCgroupSnapNameFromPid(f func(int) (string, error)) (restore func()) {
    98  	old := cgroupSnapNameFromPid
    99  	cgroupSnapNameFromPid = f
   100  	return func() {
   101  		cgroupSnapNameFromPid = old
   102  	}
   103  }