github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/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  
    39  func MockFsTypeForPath(mock func(string) (int64, error)) (restore func()) {
    40  	old := fsTypeForPath
    41  	fsTypeForPath = mock
    42  	return func() {
    43  		fsTypeForPath = old
    44  	}
    45  }
    46  
    47  func MockFsRootPath(p string) (restore func()) {
    48  	old := rootPath
    49  	rootPath = p
    50  	return func() {
    51  		rootPath = old
    52  	}
    53  }
    54  
    55  func MockRandomUUID(uuid string) func() {
    56  	old := randomUUID
    57  	randomUUID = func() (string, error) {
    58  		return uuid, nil
    59  	}
    60  	return func() {
    61  		randomUUID = old
    62  	}
    63  }
    64  
    65  func MockOsGetuid(uid int) func() {
    66  	old := osGetuid
    67  	osGetuid = func() int {
    68  		return uid
    69  	}
    70  	return func() {
    71  		osGetuid = old
    72  	}
    73  }
    74  
    75  func MockOsGetpid(pid int) func() {
    76  	old := osGetpid
    77  	osGetpid = func() int {
    78  		return pid
    79  	}
    80  	return func() {
    81  		osGetpid = old
    82  	}
    83  }
    84  
    85  func MockCgroupProcessPathInTrackingCgroup(fn func(pid int) (string, error)) func() {
    86  	old := cgroupProcessPathInTrackingCgroup
    87  	cgroupProcessPathInTrackingCgroup = fn
    88  	return func() {
    89  		cgroupProcessPathInTrackingCgroup = old
    90  	}
    91  }
    92  
    93  func MockDoCreateTransientScope(fn func(conn *dbus.Conn, unitName string, pid int) error) func() {
    94  	old := doCreateTransientScope
    95  	doCreateTransientScope = fn
    96  	return func() {
    97  		doCreateTransientScope = old
    98  	}
    99  }