github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/cmd/snap-bootstrap/export_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016-2020 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 main
    21  
    22  import (
    23  	"fmt"
    24  	"time"
    25  
    26  	"github.com/snapcore/snapd/asserts"
    27  	"github.com/snapcore/snapd/osutil/disks"
    28  )
    29  
    30  var (
    31  	Parser = parser
    32  
    33  	DoSystemdMount = doSystemdMountImpl
    34  )
    35  
    36  type SystemdMountOptions = systemdMountOptions
    37  
    38  func MockTimeNow(f func() time.Time) (restore func()) {
    39  	old := timeNow
    40  	timeNow = f
    41  	return func() {
    42  		timeNow = old
    43  	}
    44  }
    45  
    46  func MockOsutilIsMounted(f func(string) (bool, error)) (restore func()) {
    47  	old := osutilIsMounted
    48  	osutilIsMounted = f
    49  	return func() {
    50  		osutilIsMounted = old
    51  	}
    52  }
    53  
    54  func MockSystemdMount(f func(_, _ string, opts *SystemdMountOptions) error) (restore func()) {
    55  	old := doSystemdMount
    56  	doSystemdMount = f
    57  	return func() {
    58  		doSystemdMount = old
    59  	}
    60  }
    61  
    62  func MockTriggerwatchWait(f func(_ time.Duration) error) (restore func()) {
    63  	oldTriggerwatchWait := triggerwatchWait
    64  	triggerwatchWait = f
    65  	return func() {
    66  		triggerwatchWait = oldTriggerwatchWait
    67  	}
    68  }
    69  
    70  var DefaultTimeout = defaultTimeout
    71  
    72  func MockDefaultMarkerFile(p string) (restore func()) {
    73  	old := defaultMarkerFile
    74  	defaultMarkerFile = p
    75  	return func() {
    76  		defaultMarkerFile = old
    77  	}
    78  }
    79  
    80  func MockSecbootUnlockVolumeIfEncrypted(f func(disk disks.Disk, name string, encryptionKeyDir string, lockKeysOnFinish bool) (string, bool, error)) (restore func()) {
    81  	old := secbootUnlockVolumeIfEncrypted
    82  	secbootUnlockVolumeIfEncrypted = f
    83  	return func() {
    84  		secbootUnlockVolumeIfEncrypted = old
    85  	}
    86  }
    87  
    88  func MockSecbootMeasureSnapSystemEpochWhenPossible(f func() error) (restore func()) {
    89  	old := secbootMeasureSnapSystemEpochWhenPossible
    90  	secbootMeasureSnapSystemEpochWhenPossible = f
    91  	return func() {
    92  		secbootMeasureSnapSystemEpochWhenPossible = old
    93  	}
    94  }
    95  
    96  func MockSecbootMeasureSnapModelWhenPossible(f func(findModel func() (*asserts.Model, error)) error) (restore func()) {
    97  	old := secbootMeasureSnapModelWhenPossible
    98  	secbootMeasureSnapModelWhenPossible = f
    99  	return func() {
   100  		secbootMeasureSnapModelWhenPossible = old
   101  	}
   102  }
   103  
   104  func MockPartitionUUIDForBootedKernelDisk(uuid string) (restore func()) {
   105  	old := bootFindPartitionUUIDForBootedKernelDisk
   106  	bootFindPartitionUUIDForBootedKernelDisk = func() (string, error) {
   107  		if uuid == "" {
   108  			// mock error
   109  			return "", fmt.Errorf("mocked error")
   110  		}
   111  		return uuid, nil
   112  	}
   113  
   114  	return func() {
   115  		bootFindPartitionUUIDForBootedKernelDisk = old
   116  	}
   117  }