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