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