github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/storage/provider/export_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package provider
     5  
     6  import (
     7  	"os"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/juju/names"
    12  	"github.com/juju/utils/set"
    13  
    14  	"github.com/juju/juju/storage"
    15  )
    16  
    17  var Getpagesize = &getpagesize
    18  
    19  func LoopVolumeSource(
    20  	storageDir string,
    21  	run func(string, ...string) (string, error),
    22  	insideLXC bool,
    23  ) (storage.VolumeSource, *MockDirFuncs) {
    24  	dirFuncs := &MockDirFuncs{
    25  		osDirFuncs{run},
    26  		set.NewStrings(),
    27  	}
    28  	return &loopVolumeSource{dirFuncs, run, storageDir, insideLXC}, dirFuncs
    29  }
    30  
    31  func LoopProvider(
    32  	run func(string, ...string) (string, error),
    33  	insideLXC func() (bool, error),
    34  ) storage.Provider {
    35  	return &loopProvider{run, insideLXC}
    36  }
    37  
    38  func NewMockManagedFilesystemSource(
    39  	run func(string, ...string) (string, error),
    40  	volumeBlockDevices map[names.VolumeTag]storage.BlockDevice,
    41  	filesystems map[names.FilesystemTag]storage.Filesystem,
    42  ) (storage.FilesystemSource, *MockDirFuncs) {
    43  	dirFuncs := &MockDirFuncs{
    44  		osDirFuncs{run},
    45  		set.NewStrings(),
    46  	}
    47  	return &managedFilesystemSource{
    48  		run, dirFuncs,
    49  		volumeBlockDevices, filesystems,
    50  	}, dirFuncs
    51  }
    52  
    53  var _ dirFuncs = (*MockDirFuncs)(nil)
    54  
    55  // MockDirFuncs stub out the real mkdir and lstat functions from stdlib.
    56  type MockDirFuncs struct {
    57  	osDirFuncs
    58  	Dirs set.Strings
    59  }
    60  
    61  func (m *MockDirFuncs) mkDirAll(path string, perm os.FileMode) error {
    62  	m.Dirs.Add(path)
    63  	return nil
    64  }
    65  
    66  type MockFileInfo struct {
    67  	isDir bool
    68  }
    69  
    70  func (m *MockFileInfo) IsDir() bool {
    71  	return m.isDir
    72  }
    73  
    74  func (m *MockFileInfo) Name() string {
    75  	return ""
    76  }
    77  
    78  func (m *MockFileInfo) Size() int64 {
    79  	return 0
    80  }
    81  
    82  func (m *MockFileInfo) Mode() os.FileMode {
    83  	return 0
    84  }
    85  
    86  func (m *MockFileInfo) ModTime() time.Time {
    87  	return time.Now()
    88  }
    89  func (m *MockFileInfo) Sys() interface{} {
    90  	return nil
    91  }
    92  
    93  func (m *MockDirFuncs) lstat(name string) (os.FileInfo, error) {
    94  	if name == "file" || name == "exists" || m.Dirs.Contains(name) {
    95  		return &MockFileInfo{name != "file"}, nil
    96  	}
    97  	return nil, os.ErrNotExist
    98  }
    99  
   100  func (m *MockDirFuncs) fileCount(name string) (int, error) {
   101  	if strings.HasSuffix(name, "/666") {
   102  		return 2, nil
   103  	}
   104  	return 0, nil
   105  }
   106  
   107  func RootfsFilesystemSource(storageDir string, run func(string, ...string) (string, error)) (storage.FilesystemSource, *MockDirFuncs) {
   108  	d := &MockDirFuncs{
   109  		osDirFuncs{run},
   110  		set.NewStrings(),
   111  	}
   112  	return &rootfsFilesystemSource{d, run, storageDir}, d
   113  }
   114  
   115  func RootfsProvider(run func(string, ...string) (string, error)) storage.Provider {
   116  	return &rootfsProvider{run}
   117  }
   118  
   119  func TmpfsFilesystemSource(storageDir string, run func(string, ...string) (string, error)) storage.FilesystemSource {
   120  	return &tmpfsFilesystemSource{
   121  		&MockDirFuncs{
   122  			osDirFuncs{run},
   123  			set.NewStrings(),
   124  		},
   125  		run,
   126  		storageDir,
   127  	}
   128  }
   129  
   130  func TmpfsProvider(run func(string, ...string) (string, error)) storage.Provider {
   131  	return &tmpfsProvider{run}
   132  }
   133  
   134  // MountedDirs returns all the Dirs which have been created during any CreateFilesystem calls
   135  // on the specified filesystem source..
   136  func MountedDirs(fsSource storage.FilesystemSource) set.Strings {
   137  	switch fs := fsSource.(type) {
   138  	case *rootfsFilesystemSource:
   139  		return fs.dirFuncs.(*MockDirFuncs).Dirs
   140  	case *tmpfsFilesystemSource:
   141  		return fs.dirFuncs.(*MockDirFuncs).Dirs
   142  	}
   143  	panic("unexpectd type")
   144  }