github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	"gopkg.in/juju/names.v2"
    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  	// sda is (re)partitioned and the filesystem created
    55  	// on the partition.
    56  	s.commands.expect("sgdisk", "--zap-all", "/dev/sda")
    57  	s.commands.expect("sgdisk", "-n", "1:0:-1", "/dev/sda")
    58  	s.commands.expect("mkfs.ext4", "/dev/sda1")
    59  	// xvdf1 is assumed to not require a partition, on
    60  	// account of ending with a digit.
    61  	s.commands.expect("mkfs.ext4", "/dev/xvdf1")
    62  
    63  	s.blockDevices[names.NewVolumeTag("0")] = storage.BlockDevice{
    64  		DeviceName: "sda",
    65  		HardwareId: "capncrunch",
    66  		Size:       2,
    67  	}
    68  	s.blockDevices[names.NewVolumeTag("1")] = storage.BlockDevice{
    69  		DeviceName: "xvdf1",
    70  		HardwareId: "weetbix",
    71  		Size:       3,
    72  	}
    73  	results, err := source.CreateFilesystems([]storage.FilesystemParams{{
    74  		Tag:    names.NewFilesystemTag("0/0"),
    75  		Volume: names.NewVolumeTag("0"),
    76  		Size:   2,
    77  	}, {
    78  		Tag:    names.NewFilesystemTag("0/1"),
    79  		Volume: names.NewVolumeTag("1"),
    80  		Size:   3,
    81  	}})
    82  	c.Assert(err, jc.ErrorIsNil)
    83  	c.Assert(results, jc.DeepEquals, []storage.CreateFilesystemsResult{{
    84  		Filesystem: &storage.Filesystem{
    85  			names.NewFilesystemTag("0/0"),
    86  			names.NewVolumeTag("0"),
    87  			storage.FilesystemInfo{
    88  				FilesystemId: "filesystem-0-0",
    89  				Size:         2,
    90  			},
    91  		},
    92  	}, {
    93  		Filesystem: &storage.Filesystem{
    94  			names.NewFilesystemTag("0/1"),
    95  			names.NewVolumeTag("1"),
    96  			storage.FilesystemInfo{
    97  				FilesystemId: "filesystem-0-1",
    98  				Size:         3,
    99  			},
   100  		},
   101  	}})
   102  }
   103  
   104  func (s *managedfsSuite) TestCreateFilesystemsNoBlockDevice(c *gc.C) {
   105  	source := s.initSource(c)
   106  	results, err := source.CreateFilesystems([]storage.FilesystemParams{{
   107  		Tag:    names.NewFilesystemTag("0/0"),
   108  		Volume: names.NewVolumeTag("0"),
   109  		Size:   2,
   110  	}})
   111  	c.Assert(err, jc.ErrorIsNil)
   112  	c.Assert(results[0].Error, gc.ErrorMatches, "backing-volume 0 is not yet attached")
   113  }
   114  
   115  func (s *managedfsSuite) TestAttachFilesystems(c *gc.C) {
   116  	s.testAttachFilesystems(c, false, false)
   117  }
   118  
   119  func (s *managedfsSuite) TestAttachFilesystemsReadOnly(c *gc.C) {
   120  	s.testAttachFilesystems(c, true, false)
   121  }
   122  
   123  func (s *managedfsSuite) TestAttachFilesystemsReattach(c *gc.C) {
   124  	s.testAttachFilesystems(c, true, true)
   125  }
   126  
   127  func (s *managedfsSuite) testAttachFilesystems(c *gc.C, readOnly, reattach bool) {
   128  	const testMountPoint = "/in/the/place"
   129  
   130  	source := s.initSource(c)
   131  	cmd := s.commands.expect("df", "--output=source", filepath.Dir(testMountPoint))
   132  	cmd.respond("headers\n/same/as/rootfs", nil)
   133  	cmd = s.commands.expect("df", "--output=source", testMountPoint)
   134  	if reattach {
   135  		cmd.respond("headers\n/different/to/rootfs", nil)
   136  	} else {
   137  		cmd.respond("headers\n/same/as/rootfs", nil)
   138  		var args []string
   139  		if readOnly {
   140  			args = append(args, "-o", "ro")
   141  		}
   142  		args = append(args, "/dev/sda1", testMountPoint)
   143  		s.commands.expect("mount", args...)
   144  	}
   145  
   146  	s.blockDevices[names.NewVolumeTag("0")] = storage.BlockDevice{
   147  		DeviceName: "sda",
   148  		HardwareId: "capncrunch",
   149  		Size:       2,
   150  	}
   151  	s.filesystems[names.NewFilesystemTag("0/0")] = storage.Filesystem{
   152  		Tag:    names.NewFilesystemTag("0/0"),
   153  		Volume: names.NewVolumeTag("0"),
   154  	}
   155  
   156  	results, err := source.AttachFilesystems([]storage.FilesystemAttachmentParams{{
   157  		Filesystem:   names.NewFilesystemTag("0/0"),
   158  		FilesystemId: "filesystem-0-0",
   159  		AttachmentParams: storage.AttachmentParams{
   160  			Machine:    names.NewMachineTag("0"),
   161  			InstanceId: "inst-ance",
   162  			ReadOnly:   readOnly,
   163  		},
   164  		Path: testMountPoint,
   165  	}})
   166  	c.Assert(err, jc.ErrorIsNil)
   167  	c.Assert(results, jc.DeepEquals, []storage.AttachFilesystemsResult{{
   168  		FilesystemAttachment: &storage.FilesystemAttachment{
   169  			names.NewFilesystemTag("0/0"),
   170  			names.NewMachineTag("0"),
   171  			storage.FilesystemAttachmentInfo{
   172  				Path:     testMountPoint,
   173  				ReadOnly: readOnly,
   174  			},
   175  		},
   176  	}})
   177  }
   178  
   179  func (s *managedfsSuite) TestDetachFilesystems(c *gc.C) {
   180  	source := s.initSource(c)
   181  	testDetachFilesystems(c, s.commands, source, true)
   182  }
   183  
   184  func (s *managedfsSuite) TestDetachFilesystemsUnattached(c *gc.C) {
   185  	source := s.initSource(c)
   186  	testDetachFilesystems(c, s.commands, source, false)
   187  }