github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/sandbox/cgroup/export_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 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  package cgroup
    20  
    21  import (
    22  	"github.com/godbus/dbus"
    23  )
    24  
    25  var (
    26  	Cgroup2SuperMagic       = cgroup2SuperMagic
    27  	ProbeCgroupVersion      = probeCgroupVersion
    28  	ParsePid                = parsePid
    29  	DoCreateTransientScope  = doCreateTransientScope
    30  	SessionOrMaybeSystemBus = sessionOrMaybeSystemBus
    31  
    32  	ErrDBusUnknownMethod    = errDBusUnknownMethod
    33  	ErrDBusNameHasNoOwner   = errDBusNameHasNoOwner
    34  	ErrDBusSpawnChildExited = errDBusSpawnChildExited
    35  
    36  	SecurityTagFromCgroupPath = securityTagFromCgroupPath
    37  
    38  	ApplyToSnap = applyToSnap
    39  )
    40  
    41  func MockFsTypeForPath(mock func(string) (int64, error)) (restore func()) {
    42  	old := fsTypeForPath
    43  	fsTypeForPath = mock
    44  	return func() {
    45  		fsTypeForPath = old
    46  	}
    47  }
    48  
    49  func MockRandomUUID(uuid string) func() {
    50  	old := randomUUID
    51  	randomUUID = func() (string, error) {
    52  		return uuid, nil
    53  	}
    54  	return func() {
    55  		randomUUID = old
    56  	}
    57  }
    58  
    59  func MockOsGetuid(uid int) func() {
    60  	old := osGetuid
    61  	osGetuid = func() int {
    62  		return uid
    63  	}
    64  	return func() {
    65  		osGetuid = old
    66  	}
    67  }
    68  
    69  func MockOsGetpid(pid int) func() {
    70  	old := osGetpid
    71  	osGetpid = func() int {
    72  		return pid
    73  	}
    74  	return func() {
    75  		osGetpid = old
    76  	}
    77  }
    78  
    79  func MockCgroupProcessPathInTrackingCgroup(fn func(pid int) (string, error)) func() {
    80  	old := cgroupProcessPathInTrackingCgroup
    81  	cgroupProcessPathInTrackingCgroup = fn
    82  	return func() {
    83  		cgroupProcessPathInTrackingCgroup = old
    84  	}
    85  }
    86  
    87  func MockDoCreateTransientScope(fn func(conn *dbus.Conn, unitName string, pid int) error) func() {
    88  	old := doCreateTransientScope
    89  	doCreateTransientScope = fn
    90  	return func() {
    91  		doCreateTransientScope = old
    92  	}
    93  }
    94  
    95  func FreezerCgroupV1Dir() string { return freezerCgroupV1Dir }