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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package provider_test
     5  
     6  import (
     7  	"path/filepath"
     8  
     9  	"github.com/juju/names"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/storage"
    14  	"github.com/juju/juju/storage/provider"
    15  	"github.com/juju/juju/testing"
    16  )
    17  
    18  var _ = gc.Suite(&managedfsSuite{})
    19  
    20  type managedfsSuite struct {
    21  	testing.BaseSuite
    22  	commands     *mockRunCommand
    23  	dirFuncs     *provider.MockDirFuncs
    24  	blockDevices map[names.VolumeTag]storage.BlockDevice
    25  	filesystems  map[names.FilesystemTag]storage.Filesystem
    26  }
    27  
    28  func (s *managedfsSuite) SetUpTest(c *gc.C) {
    29  	s.BaseSuite.SetUpTest(c)
    30  	s.blockDevices = make(map[names.VolumeTag]storage.BlockDevice)
    31  	s.filesystems = make(map[names.FilesystemTag]storage.Filesystem)
    32  }
    33  
    34  func (s *managedfsSuite) TearDownTest(c *gc.C) {
    35  	if s.commands != nil {
    36  		s.commands.assertDrained()
    37  	}
    38  	s.BaseSuite.TearDownTest(c)
    39  }
    40  
    41  func (s *managedfsSuite) initSource(c *gc.C) storage.FilesystemSource {
    42  	s.commands = &mockRunCommand{c: c}
    43  	source, mockDirFuncs := provider.NewMockManagedFilesystemSource(
    44  		s.commands.run,
    45  		s.blockDevices,
    46  		s.filesystems,
    47  	)
    48  	s.dirFuncs = mockDirFuncs
    49  	return source
    50  }
    51  
    52  func (s *managedfsSuite) TestCreateFilesystems(c *gc.C) {
    53  	source := s.initSource(c)
    54  	s.commands.expect("mkfs.ext4", "/dev/sda")
    55  	s.commands.expect("mkfs.ext4", "/dev/disk/by-id/weetbix")
    56  
    57  	s.blockDevices[names.NewVolumeTag("0")] = storage.BlockDevice{
    58  		DeviceName: "sda",
    59  		HardwareId: "capncrunch",
    60  		Size:       2,
    61  	}
    62  	s.blockDevices[names.NewVolumeTag("1")] = storage.BlockDevice{
    63  		HardwareId: "weetbix",
    64  		Size:       3,
    65  	}
    66  	filesystems, err := source.CreateFilesystems([]storage.FilesystemParams{{
    67  		Tag:    names.NewFilesystemTag("0/0"),
    68  		Volume: names.NewVolumeTag("0"),
    69  		Size:   2,
    70  	}, {
    71  		Tag:    names.NewFilesystemTag("0/1"),
    72  		Volume: names.NewVolumeTag("1"),
    73  		Size:   3,
    74  	}})
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	c.Assert(filesystems, jc.DeepEquals, []storage.Filesystem{{
    77  		names.NewFilesystemTag("0/0"),
    78  		names.NewVolumeTag("0"),
    79  		storage.FilesystemInfo{
    80  			FilesystemId: "filesystem-0-0",
    81  			Size:         2,
    82  		},
    83  	}, {
    84  		names.NewFilesystemTag("0/1"),
    85  		names.NewVolumeTag("1"),
    86  		storage.FilesystemInfo{
    87  			FilesystemId: "filesystem-0-1",
    88  			Size:         3,
    89  		},
    90  	}})
    91  }
    92  
    93  func (s *managedfsSuite) TestCreateFilesystemsNoBlockDevice(c *gc.C) {
    94  	source := s.initSource(c)
    95  	_, err := source.CreateFilesystems([]storage.FilesystemParams{{
    96  		Tag:    names.NewFilesystemTag("0/0"),
    97  		Volume: names.NewVolumeTag("0"),
    98  		Size:   2,
    99  	}})
   100  	c.Assert(err, gc.ErrorMatches, "creating filesystem 0/0: backing-volume 0 is not yet attached")
   101  }
   102  
   103  func (s *managedfsSuite) TestAttachFilesystems(c *gc.C) {
   104  	s.testAttachFilesystems(c, false, false)
   105  }
   106  
   107  func (s *managedfsSuite) TestAttachFilesystemsReadOnly(c *gc.C) {
   108  	s.testAttachFilesystems(c, true, false)
   109  }
   110  
   111  func (s *managedfsSuite) TestAttachFilesystemsReattach(c *gc.C) {
   112  	s.testAttachFilesystems(c, true, true)
   113  }
   114  
   115  func (s *managedfsSuite) testAttachFilesystems(c *gc.C, readOnly, reattach bool) {
   116  	const testMountPoint = "/in/the/place"
   117  
   118  	source := s.initSource(c)
   119  	cmd := s.commands.expect("df", "--output=source", filepath.Dir(testMountPoint))
   120  	cmd.respond("headers\n/same/as/rootfs", nil)
   121  	cmd = s.commands.expect("df", "--output=source", testMountPoint)
   122  	if reattach {
   123  		cmd.respond("headers\n/different/to/rootfs", nil)
   124  	} else {
   125  		cmd.respond("headers\n/same/as/rootfs", nil)
   126  		var args []string
   127  		if readOnly {
   128  			args = append(args, "-o", "ro")
   129  		}
   130  		args = append(args, "/dev/sda", testMountPoint)
   131  		s.commands.expect("mount", args...)
   132  	}
   133  
   134  	s.blockDevices[names.NewVolumeTag("0")] = storage.BlockDevice{
   135  		DeviceName: "sda",
   136  		HardwareId: "capncrunch",
   137  		Size:       2,
   138  	}
   139  	s.filesystems[names.NewFilesystemTag("0/0")] = storage.Filesystem{
   140  		Tag:    names.NewFilesystemTag("0/0"),
   141  		Volume: names.NewVolumeTag("0"),
   142  	}
   143  
   144  	filesystemAttachments, err := source.AttachFilesystems([]storage.FilesystemAttachmentParams{{
   145  		Filesystem:   names.NewFilesystemTag("0/0"),
   146  		FilesystemId: "filesystem-0-0",
   147  		AttachmentParams: storage.AttachmentParams{
   148  			Machine:    names.NewMachineTag("0"),
   149  			InstanceId: "inst-ance",
   150  			ReadOnly:   readOnly,
   151  		},
   152  		Path: testMountPoint,
   153  	}})
   154  	c.Assert(err, jc.ErrorIsNil)
   155  	c.Assert(filesystemAttachments, jc.DeepEquals, []storage.FilesystemAttachment{{
   156  		names.NewFilesystemTag("0/0"),
   157  		names.NewMachineTag("0"),
   158  		storage.FilesystemAttachmentInfo{
   159  			Path:     testMountPoint,
   160  			ReadOnly: readOnly,
   161  		},
   162  	}})
   163  }
   164  
   165  func (s *managedfsSuite) TestDetachFilesystems(c *gc.C) {
   166  	source := s.initSource(c)
   167  	testDetachFilesystems(c, s.commands, source, true)
   168  }
   169  
   170  func (s *managedfsSuite) TestDetachFilesystemsUnattached(c *gc.C) {
   171  	source := s.initSource(c)
   172  	testDetachFilesystems(c, s.commands, source, false)
   173  }