github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/osutil/mockable.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 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 osutil
    21  
    22  import (
    23  	"os"
    24  	"os/user"
    25  	"syscall"
    26  )
    27  
    28  // MockMountInfo is meant for tests to mock the content of /proc/self/mountinfo.
    29  func MockMountInfo(content string) (restore func()) {
    30  	old := mockedMountInfo
    31  	mockedMountInfo = &content
    32  	return func() {
    33  		mockedMountInfo = old
    34  	}
    35  }
    36  
    37  func MockFindUid(f func(string) (uint64, error)) (restore func()) {
    38  	old := FindUid
    39  	FindUid = f
    40  	return func() {
    41  		FindUid = old
    42  	}
    43  }
    44  
    45  func MockFindGid(f func(string) (uint64, error)) (restore func()) {
    46  	old := FindGid
    47  	FindGid = f
    48  	return func() {
    49  		FindGid = old
    50  	}
    51  }
    52  
    53  var (
    54  	mockedMountInfo *string
    55  
    56  	userLookup  = user.Lookup
    57  	userCurrent = user.Current
    58  
    59  	osReadlink = os.Readlink
    60  
    61  	syscallKill    = syscall.Kill
    62  	syscallGetpgid = syscall.Getpgid
    63  
    64  	etcFstab    = "/etc/fstab"
    65  	sudoersDotD = "/etc/sudoers.d"
    66  
    67  	procSelfMountInfo = "/proc/self/mountinfo"
    68  )