github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/snapshotstate/backend/export_test.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 backend
    21  
    22  import (
    23  	"os"
    24  	"os/user"
    25  	"time"
    26  
    27  	"github.com/snapcore/snapd/osutil/sys"
    28  )
    29  
    30  var (
    31  	AddDirToZip     = addDirToZip
    32  	TarAsUser       = tarAsUser
    33  	PickUserWrapper = pickUserWrapper
    34  )
    35  
    36  func MockIsTesting(newIsTesting bool) func() {
    37  	oldIsTesting := isTesting
    38  	isTesting = newIsTesting
    39  	return func() {
    40  		isTesting = oldIsTesting
    41  	}
    42  }
    43  
    44  func MockUserLookupId(newLookupId func(string) (*user.User, error)) func() {
    45  	oldLookupId := userLookupId
    46  	userLookupId = newLookupId
    47  	return func() {
    48  		userLookupId = oldLookupId
    49  	}
    50  }
    51  
    52  func MockOsOpen(newOsOpen func(string) (*os.File, error)) func() {
    53  	oldOsOpen := osOpen
    54  	osOpen = newOsOpen
    55  	return func() {
    56  		osOpen = oldOsOpen
    57  	}
    58  }
    59  
    60  func MockDirNames(newDirNames func(*os.File, int) ([]string, error)) func() {
    61  	oldDirNames := dirNames
    62  	dirNames = newDirNames
    63  	return func() {
    64  		dirNames = oldDirNames
    65  	}
    66  }
    67  
    68  func MockOpen(newOpen func(string) (*Reader, error)) func() {
    69  	oldOpen := backendOpen
    70  	backendOpen = newOpen
    71  	return func() {
    72  		backendOpen = oldOpen
    73  	}
    74  }
    75  
    76  func MockSysGeteuid(newGeteuid func() sys.UserID) (restore func()) {
    77  	oldGeteuid := sysGeteuid
    78  	sysGeteuid = newGeteuid
    79  	return func() {
    80  		sysGeteuid = oldGeteuid
    81  	}
    82  }
    83  
    84  func MockExecLookPath(newLookPath func(string) (string, error)) (restore func()) {
    85  	oldLookPath := execLookPath
    86  	execLookPath = newLookPath
    87  	return func() {
    88  		execLookPath = oldLookPath
    89  	}
    90  }
    91  
    92  func SetUserWrapper(newUserWrapper string) (restore func()) {
    93  	oldUserWrapper := userWrapper
    94  	userWrapper = newUserWrapper
    95  	return func() {
    96  		userWrapper = oldUserWrapper
    97  	}
    98  }
    99  
   100  func MockUsersForUsernames(f func(usernames []string) ([]*user.User, error)) (restore func()) {
   101  	old := usersForUsernames
   102  	usersForUsernames = f
   103  	return func() {
   104  		usersForUsernames = old
   105  	}
   106  }
   107  
   108  func MockTimeNow(f func() time.Time) (restore func()) {
   109  	oldTimeNow := timeNow
   110  	timeNow = f
   111  	return func() {
   112  		timeNow = oldTimeNow
   113  	}
   114  }