Skip to content

Commit

Permalink
many: use the helper for checking current binary is a test one
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
  • Loading branch information
bboozzoo committed Jul 17, 2020
1 parent e14d9d5 commit d127b29
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 32 deletions.
10 changes: 3 additions & 7 deletions bootloader/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ package assets

import (
"fmt"
"os"
"regexp"

"github.com/snapcore/snapd/osutil"
)

var registeredAssets = map[string][]byte{}
Expand All @@ -43,11 +43,7 @@ func Internal(name string) []byte {

// MockInternal mocks the contents of an internal asset for use in testing.
func MockInternal(name string, data []byte) (restore func()) {
var goTestExeRe = regexp.MustCompile(`^.*/.*go-build.*/.*\.test$`)
var isSnapdTest = len(os.Args) > 0 && goTestExeRe.MatchString(os.Args[0])
if !isSnapdTest {
panic("mocking can be done only in tests")
}
osutil.MustTestBinary("mocking can be done only in tests")

old, ok := registeredAssets[name]
registeredAssets[name] = data
Expand Down
8 changes: 1 addition & 7 deletions bootloader/efi/efi.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"unicode/utf16"

"github.com/snapcore/snapd/dirs"
Expand All @@ -48,9 +47,6 @@ const (
)

var (
goTestExeRe = regexp.MustCompile(`^.*/.*go-build.*/.*\.test$`)
isSnapdTest = len(os.Args) > 0 && goTestExeRe.MatchString(os.Args[0])

openEFIVar = openEFIVarImpl
)

Expand Down Expand Up @@ -166,9 +162,7 @@ func ReadVarString(name string) (string, VariableAttr, error) {
// MockVars mocks EFI variables as read by ReadVar*, only to be used
// from tests. Set vars to nil to mock a non-EFI system.
func MockVars(vars map[string][]byte, attrs map[string]VariableAttr) (restore func()) {
if !isSnapdTest {
panic("MockVars only to be used from tests")
}
osutil.MustTestBinary("MockVars only to be used from tests")
old := openEFIVar
openEFIVar = func(name string) (io.ReadCloser, VariableAttr, int64, error) {
if vars == nil {
Expand Down
8 changes: 4 additions & 4 deletions osutil/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ func ParseRawExpandableEnv(entries []string) (ExpandableEnv, error) {
// this is weird to use in a test, but it is so that we can test the actual
// implementation of LoadMountInfo, which normally panics during tests if not
// properly mocked
func MockIsSnapdTest(new bool) (restore func()) {
old := isSnapdTest
isSnapdTest = new
func MountInfoMustMock(new bool) (restore func()) {
old := mountInfoMustMockInTests
mountInfoMustMockInTests = new
return func() {
isSnapdTest = old
mountInfoMustMockInTests = old
}
}

Expand Down
4 changes: 1 addition & 3 deletions osutil/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"io"
"os"
"path/filepath"
"regexp"
"syscall"

"github.com/snapcore/snapd/osutil/sys"
Expand All @@ -43,8 +42,7 @@ const (
// Allow disabling sync for testing. This brings massive improvements on
// certain filesystems (like btrfs) and very much noticeable improvements in
// all unit tests in genreal.
var goTestExeRe = regexp.MustCompile(`^.*/.*go-build.*/.*\.test$`)
var snapdUnsafeIO bool = len(os.Args) > 0 && goTestExeRe.MatchString(os.Args[0]) && GetenvBool("SNAPD_UNSAFE_IO", true)
var snapdUnsafeIO bool = IsTestBinary() && GetenvBool("SNAPD_UNSAFE_IO", true)

// An AtomicFile is similar to an os.File but it has an additional
// Commit() method that does whatever needs to be done so the
Expand Down
11 changes: 6 additions & 5 deletions osutil/mountinfo_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ type MountInfoEntry struct {
SuperOptions map[string]string
}

var isSnapdTest = len(os.Args) > 0 && goTestExeRe.MatchString(os.Args[0])

func flattenMap(m map[string]string) string {
keys := make([]string, 0, len(m))
for key := range m {
Expand Down Expand Up @@ -93,18 +91,21 @@ func (mi *MountInfoEntry) String() string {
flattenMap(mi.SuperOptions))
}

var mountInfoMustMockInTests = true

// LoadMountInfo loads list of mounted entries from /proc/self/mountinfo. This
// can be mocked by using osutil.MockMountInfo to hard-code a specific mountinfo
// file content to be loaded by this function
func LoadMountInfo() ([]*MountInfoEntry, error) {
if mockedMountInfo != nil {
return ReadMountInfo(bytes.NewBufferString(*mockedMountInfo))
}
// if we are in testing and we didn't mock a mountinfo panic, since the
// mountinfo is used in many places and really should be mocked for tests
if isSnapdTest {
if IsTestBinary() && mountInfoMustMockInTests {
// if we are in testing and we didn't mock a mountinfo panic, since the
// mountinfo is used in many places and really should be mocked for tests
panic("/proc/self/mountinfo must be mocked in tests")
}

f, err := os.Open(procSelfMountInfo)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions osutil/mountinfo_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (s *mountinfoSuite) TestLoadMountInfo1(c *C) {
fname := filepath.Join(c.MkDir(), "mountinfo")
err := ioutil.WriteFile(fname, []byte(mountInfoSample), 0644)
c.Assert(err, IsNil)
restore := osutil.MockIsSnapdTest(false)
restore := osutil.MountInfoMustMock(false)
defer restore()
restore = osutil.MockProcSelfMountInfoLocation(fname)
defer restore()
Expand All @@ -178,7 +178,7 @@ func (s *mountinfoSuite) TestLoadMountInfo1(c *C) {
// Test that loading mountinfo from a missing file reports an error.
func (s *mountinfoSuite) TestLoadMountInfo2(c *C) {
fname := filepath.Join(c.MkDir(), "mountinfo")
restore := osutil.MockIsSnapdTest(false)
restore := osutil.MountInfoMustMock(false)
defer restore()
restore = osutil.MockProcSelfMountInfoLocation(fname)
defer restore()
Expand All @@ -193,7 +193,7 @@ func (s *mountinfoSuite) TestLoadMountInfo3(c *C) {
c.Assert(err, IsNil)
err = os.Chmod(fname, 0000)
c.Assert(err, IsNil)
restore := osutil.MockIsSnapdTest(false)
restore := osutil.MountInfoMustMock(false)
defer restore()
restore = osutil.MockProcSelfMountInfoLocation(fname)
defer restore()
Expand Down
6 changes: 3 additions & 3 deletions progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ package progress
import (
"fmt"
"os"
"regexp"

"golang.org/x/crypto/ssh/terminal"

"github.com/snapcore/snapd/osutil"
)

// Meter is an interface to show progress to the user
Expand Down Expand Up @@ -82,8 +83,7 @@ func MockMeter(meter Meter) func() {
}
}

var goTestExeRe = regexp.MustCompile(`^.*/.*go-build.*/.*\.test$`)
var inTesting bool = (len(os.Args) > 0 && goTestExeRe.MatchString(os.Args[0])) || os.Getenv("SPREAD_SYSTEM") != ""
var inTesting bool = (osutil.IsTestBinary()) || os.Getenv("SPREAD_SYSTEM") != ""

// MakeProgressBar creates an appropriate progress.Meter for the environ in
// which it is called:
Expand Down

0 comments on commit d127b29

Please sign in to comment.