gitee.com/mysnapcore/mysnapd@v0.1.0/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  	"time"
    23  
    24  	"github.com/godbus/dbus"
    25  
    26  	"gitee.com/mysnapcore/mysnapd/testutil"
    27  )
    28  
    29  var (
    30  	Cgroup2SuperMagic       = cgroup2SuperMagic
    31  	ProbeCgroupVersion      = probeCgroupVersion
    32  	ParsePid                = parsePid
    33  	DoCreateTransientScope  = doCreateTransientScope
    34  	SessionOrMaybeSystemBus = sessionOrMaybeSystemBus
    35  
    36  	ErrDBusUnknownMethod    = errDBusUnknownMethod
    37  	ErrDBusNameHasNoOwner   = errDBusNameHasNoOwner
    38  	ErrDBusSpawnChildExited = errDBusSpawnChildExited
    39  
    40  	SecurityTagFromCgroupPath = securityTagFromCgroupPath
    41  
    42  	ApplyToSnap = applyToSnap
    43  )
    44  
    45  func MockFsTypeForPath(mock func(string) (int64, error)) (restore func()) {
    46  	old := fsTypeForPath
    47  	fsTypeForPath = mock
    48  	return func() {
    49  		fsTypeForPath = old
    50  	}
    51  }
    52  
    53  func MockRandomUUID(uuid string) func() {
    54  	old := randomUUID
    55  	randomUUID = func() (string, error) {
    56  		return uuid, nil
    57  	}
    58  	return func() {
    59  		randomUUID = old
    60  	}
    61  }
    62  
    63  func MockOsGetuid(uid int) func() {
    64  	old := osGetuid
    65  	osGetuid = func() int {
    66  		return uid
    67  	}
    68  	return func() {
    69  		osGetuid = old
    70  	}
    71  }
    72  
    73  func MockOsGetpid(pid int) func() {
    74  	old := osGetpid
    75  	osGetpid = func() int {
    76  		return pid
    77  	}
    78  	return func() {
    79  		osGetpid = old
    80  	}
    81  }
    82  
    83  func MockCgroupProcessPathInTrackingCgroup(fn func(pid int) (string, error)) func() {
    84  	old := cgroupProcessPathInTrackingCgroup
    85  	cgroupProcessPathInTrackingCgroup = fn
    86  	return func() {
    87  		cgroupProcessPathInTrackingCgroup = old
    88  	}
    89  }
    90  
    91  func MockDoCreateTransientScope(fn func(conn *dbus.Conn, unitName string, pid int) error) func() {
    92  	old := doCreateTransientScope
    93  	doCreateTransientScope = fn
    94  	return func() {
    95  		doCreateTransientScope = old
    96  	}
    97  }
    98  
    99  func FreezerCgroupV1Dir() string { return freezerCgroupV1Dir }
   100  
   101  func MockCreateScopeJobTimeout(d time.Duration) (restore func()) {
   102  	oldCreateScopeJobTimeout := createScopeJobTimeout
   103  	createScopeJobTimeout = d
   104  	return func() {
   105  		createScopeJobTimeout = oldCreateScopeJobTimeout
   106  	}
   107  }
   108  
   109  func MockCgroupsFilePath(path string) (restore func()) {
   110  	r := testutil.Backup(&cgroupsFilePath)
   111  	cgroupsFilePath = path
   112  	return r
   113  }