github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/blockdevice_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package description
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  	"gopkg.in/yaml.v2"
    10  )
    11  
    12  type BlockDeviceSerializationSuite struct {
    13  	SliceSerializationSuite
    14  }
    15  
    16  var _ = gc.Suite(&BlockDeviceSerializationSuite{})
    17  
    18  func (s *BlockDeviceSerializationSuite) SetUpTest(c *gc.C) {
    19  	s.SliceSerializationSuite.SetUpTest(c)
    20  	s.importName = "block devices"
    21  	s.sliceName = "block-devices"
    22  	s.importFunc = func(m map[string]interface{}) (interface{}, error) {
    23  		return importBlockDevices(m)
    24  	}
    25  	s.testFields = func(m map[string]interface{}) {
    26  		m["block-devices"] = []interface{}{}
    27  	}
    28  }
    29  
    30  func allBlockDeviceArgs() BlockDeviceArgs {
    31  	return BlockDeviceArgs{
    32  		Name:           "/dev/sda",
    33  		Links:          []string{"some", "data"},
    34  		Label:          "sda",
    35  		UUID:           "some-uuid",
    36  		HardwareID:     "magic",
    37  		BusAddress:     "bus stop",
    38  		Size:           16 * 1024 * 1024 * 1024,
    39  		FilesystemType: "ext4",
    40  		InUse:          true,
    41  		MountPoint:     "/",
    42  	}
    43  }
    44  
    45  func (s *BlockDeviceSerializationSuite) TestNewBlockDevice(c *gc.C) {
    46  	d := newBlockDevice(allBlockDeviceArgs())
    47  	c.Check(d.Name(), gc.Equals, "/dev/sda")
    48  	c.Check(d.Links(), jc.DeepEquals, []string{"some", "data"})
    49  	c.Check(d.Label(), gc.Equals, "sda")
    50  	c.Check(d.UUID(), gc.Equals, "some-uuid")
    51  	c.Check(d.HardwareID(), gc.Equals, "magic")
    52  	c.Check(d.BusAddress(), gc.Equals, "bus stop")
    53  	c.Check(d.Size(), gc.Equals, uint64(16*1024*1024*1024))
    54  	c.Check(d.FilesystemType(), gc.Equals, "ext4")
    55  	c.Check(d.InUse(), jc.IsTrue)
    56  	c.Check(d.MountPoint(), gc.Equals, "/")
    57  }
    58  
    59  func (s *BlockDeviceSerializationSuite) exportImport(c *gc.C, dev *blockdevice) *blockdevice {
    60  	initial := blockdevices{
    61  		Version:       1,
    62  		BlockDevices_: []*blockdevice{dev},
    63  	}
    64  
    65  	bytes, err := yaml.Marshal(initial)
    66  	c.Assert(err, jc.ErrorIsNil)
    67  
    68  	var source map[string]interface{}
    69  	err = yaml.Unmarshal(bytes, &source)
    70  	c.Assert(err, jc.ErrorIsNil)
    71  
    72  	devices, err := importBlockDevices(source)
    73  	c.Assert(err, jc.ErrorIsNil)
    74  	c.Assert(devices, gc.HasLen, 1)
    75  	return devices[0]
    76  }
    77  
    78  func (s *BlockDeviceSerializationSuite) TestParsingSerializedData(c *gc.C) {
    79  	initial := newBlockDevice(allBlockDeviceArgs())
    80  	imported := s.exportImport(c, initial)
    81  	c.Assert(imported, jc.DeepEquals, initial)
    82  }
    83  
    84  func (s *BlockDeviceSerializationSuite) TestImportEmpty(c *gc.C) {
    85  	devices, err := importBlockDevices(emptyBlockDeviceMap())
    86  	c.Assert(err, jc.ErrorIsNil)
    87  	c.Assert(devices, gc.HasLen, 0)
    88  }
    89  
    90  func emptyBlockDeviceMap() map[interface{}]interface{} {
    91  	return map[interface{}]interface{}{
    92  		"version":       1,
    93  		"block-devices": []interface{}{},
    94  	}
    95  }