github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/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  	IsSnapshotFilename = isSnapshotFilename
    36  )
    37  
    38  func MockIsTesting(newIsTesting bool) func() {
    39  	oldIsTesting := isTesting
    40  	isTesting = newIsTesting
    41  	return func() {
    42  		isTesting = oldIsTesting
    43  	}
    44  }
    45  
    46  func MockUserLookupId(newLookupId func(string) (*user.User, error)) func() {
    47  	oldLookupId := userLookupId
    48  	userLookupId = newLookupId
    49  	return func() {
    50  		userLookupId = oldLookupId
    51  	}
    52  }
    53  
    54  func MockOsOpen(newOsOpen func(string) (*os.File, error)) func() {
    55  	oldOsOpen := osOpen
    56  	osOpen = newOsOpen
    57  	return func() {
    58  		osOpen = oldOsOpen
    59  	}
    60  }
    61  
    62  func MockDirNames(newDirNames func(*os.File, int) ([]string, error)) func() {
    63  	oldDirNames := dirNames
    64  	dirNames = newDirNames
    65  	return func() {
    66  		dirNames = oldDirNames
    67  	}
    68  }
    69  
    70  func MockOpen(newOpen func(string, uint64) (*Reader, error)) func() {
    71  	oldOpen := backendOpen
    72  	backendOpen = newOpen
    73  	return func() {
    74  		backendOpen = oldOpen
    75  	}
    76  }
    77  
    78  func MockSysGeteuid(newGeteuid func() sys.UserID) (restore func()) {
    79  	oldGeteuid := sysGeteuid
    80  	sysGeteuid = newGeteuid
    81  	return func() {
    82  		sysGeteuid = oldGeteuid
    83  	}
    84  }
    85  
    86  func MockExecLookPath(newLookPath func(string) (string, error)) (restore func()) {
    87  	oldLookPath := execLookPath
    88  	execLookPath = newLookPath
    89  	return func() {
    90  		execLookPath = oldLookPath
    91  	}
    92  }
    93  
    94  func SetUserWrapper(newUserWrapper string) (restore func()) {
    95  	oldUserWrapper := userWrapper
    96  	userWrapper = newUserWrapper
    97  	return func() {
    98  		userWrapper = oldUserWrapper
    99  	}
   100  }
   101  
   102  func MockUsersForUsernames(f func(usernames []string) ([]*user.User, error)) (restore func()) {
   103  	old := usersForUsernames
   104  	usersForUsernames = f
   105  	return func() {
   106  		usersForUsernames = old
   107  	}
   108  }
   109  
   110  func MockTimeNow(f func() time.Time) (restore func()) {
   111  	oldTimeNow := timeNow
   112  	timeNow = f
   113  	return func() {
   114  		timeNow = oldTimeNow
   115  	}
   116  }