github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/provider/maas/volumes_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package maas
     5  
     6  import (
     7  	"github.com/juju/names"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/constraints"
    12  	"github.com/juju/juju/storage"
    13  	"github.com/juju/juju/testing"
    14  )
    15  
    16  type volumeSuite struct {
    17  	providerSuite
    18  }
    19  
    20  var _ = gc.Suite(&volumeSuite{})
    21  
    22  func (s *volumeSuite) TestBuildMAASVolumeParametersNoVolumes(c *gc.C) {
    23  	vInfo, err := buildMAASVolumeParameters(nil, constraints.Value{})
    24  	c.Assert(err, jc.ErrorIsNil)
    25  	c.Assert(vInfo, gc.HasLen, 0)
    26  }
    27  
    28  func (s *volumeSuite) TestBuildMAASVolumeParametersJustRootDisk(c *gc.C) {
    29  	var cons constraints.Value
    30  	rootSize := uint64(20000)
    31  	cons.RootDisk = &rootSize
    32  	vInfo, err := buildMAASVolumeParameters(nil, cons)
    33  	c.Assert(err, jc.ErrorIsNil)
    34  	c.Assert(vInfo, jc.DeepEquals, []volumeInfo{
    35  		{"root", 20, nil},
    36  	})
    37  }
    38  
    39  func (s *volumeSuite) TestBuildMAASVolumeParametersNoTags(c *gc.C) {
    40  	vInfo, err := buildMAASVolumeParameters([]storage.VolumeParams{
    41  		{Tag: names.NewVolumeTag("1"), Size: 2000000},
    42  	}, constraints.Value{})
    43  	c.Assert(err, jc.ErrorIsNil)
    44  	c.Assert(vInfo, jc.DeepEquals, []volumeInfo{
    45  		{"root", 0, nil}, //root disk
    46  		{"1", 1954, nil},
    47  	})
    48  }
    49  
    50  func (s *volumeSuite) TestBuildMAASVolumeParametersWithRootDisk(c *gc.C) {
    51  	var cons constraints.Value
    52  	rootSize := uint64(20000)
    53  	cons.RootDisk = &rootSize
    54  	vInfo, err := buildMAASVolumeParameters([]storage.VolumeParams{
    55  		{Tag: names.NewVolumeTag("1"), Size: 2000000},
    56  	}, cons)
    57  	c.Assert(err, jc.ErrorIsNil)
    58  	c.Assert(vInfo, jc.DeepEquals, []volumeInfo{
    59  		{"root", 20, nil}, //root disk
    60  		{"1", 1954, nil},
    61  	})
    62  }
    63  
    64  func (s *volumeSuite) TestBuildMAASVolumeParametersWithTags(c *gc.C) {
    65  	vInfo, err := buildMAASVolumeParameters([]storage.VolumeParams{
    66  		{Tag: names.NewVolumeTag("1"), Size: 2000000, Attributes: map[string]interface{}{"tags": "tag1,tag2"}},
    67  	}, constraints.Value{})
    68  	c.Assert(err, jc.ErrorIsNil)
    69  	c.Assert(vInfo, jc.DeepEquals, []volumeInfo{
    70  		{"root", 0, nil}, //root disk
    71  		{"1", 1954, []string{"tag1", "tag2"}},
    72  	})
    73  }
    74  
    75  func (s *volumeSuite) TestInstanceVolumes(c *gc.C) {
    76  	obj := s.testMAASObject.TestServer.NewNode(validVolumeJson)
    77  	instance := maasInstance{&obj}
    78  	mTag := names.NewMachineTag("1")
    79  	volumes, attachments, err := instance.volumes(mTag, []names.VolumeTag{
    80  		names.NewVolumeTag("1"),
    81  		names.NewVolumeTag("2"),
    82  	})
    83  	c.Assert(err, jc.ErrorIsNil)
    84  	// Expect 2 volumes - root volume is ignored.
    85  	c.Assert(volumes, gc.HasLen, 2)
    86  	c.Assert(attachments, gc.HasLen, 2)
    87  	c.Check(volumes, jc.DeepEquals, []storage.Volume{
    88  		{
    89  			// This volume has no id_path.
    90  			names.NewVolumeTag("1"),
    91  			storage.VolumeInfo{
    92  				HardwareId: "",
    93  				VolumeId:   "volume-1",
    94  				Size:       476893,
    95  				Persistent: false,
    96  			},
    97  		},
    98  		{
    99  			names.NewVolumeTag("2"),
   100  			storage.VolumeInfo{
   101  				HardwareId: "id_for_sdc",
   102  				VolumeId:   "volume-2",
   103  				Size:       238764,
   104  				Persistent: false,
   105  			},
   106  		},
   107  	})
   108  	c.Assert(attachments, jc.DeepEquals, []storage.VolumeAttachment{
   109  		{
   110  			names.NewVolumeTag("1"),
   111  			mTag,
   112  			storage.VolumeAttachmentInfo{
   113  				DeviceName: "sdb",
   114  				ReadOnly:   false,
   115  			},
   116  		},
   117  		// Device name not set because there's a hardware id in the volume.
   118  		{
   119  			names.NewVolumeTag("2"),
   120  			mTag,
   121  			storage.VolumeAttachmentInfo{
   122  				DeviceName: "",
   123  				ReadOnly:   false,
   124  			},
   125  		},
   126  	})
   127  }
   128  
   129  func (s *volumeSuite) TestInstanceVolumesOldMass(c *gc.C) {
   130  	obj := s.testMAASObject.TestServer.NewNode(`{"system_id": "node0"}`)
   131  	instance := maasInstance{&obj}
   132  	volumes, attachments, err := instance.volumes(names.NewMachineTag("1"), []names.VolumeTag{
   133  		names.NewVolumeTag("1"),
   134  		names.NewVolumeTag("2"),
   135  	})
   136  	c.Assert(err, jc.ErrorIsNil)
   137  	c.Assert(volumes, gc.HasLen, 0)
   138  	c.Assert(attachments, gc.HasLen, 0)
   139  }
   140  
   141  var validVolumeJson = `
   142  {
   143      "system_id": "node0",
   144      "physicalblockdevice_set": [
   145          {
   146              "name": "sda", 
   147              "tags": [
   148                  "ssd", 
   149                  "sata"
   150              ],
   151              "id": 1, 
   152              "id_path": "/dev/disk/by-id/id_for_sda", 
   153              "path": "/dev/sda", 
   154              "model": "Samsung_SSD_850_EVO_250GB", 
   155              "block_size": 4096, 
   156              "serial": "S21NNSAFC38075L", 
   157              "size": 250059350016
   158          }, 
   159          {
   160              "name": "sdb", 
   161              "tags": [
   162                  "ssd", 
   163                  "sata"
   164              ], 
   165              "id": 2, 
   166              "path": "/dev/sdb", 
   167              "model": "Samsung_SSD_850_EVO_500GB", 
   168              "block_size": 4096, 
   169              "serial": "S21NNSAFC38076L", 
   170              "size": 500059350016
   171          },
   172          {
   173              "name": "sdb", 
   174              "tags": [
   175                  "ssd", 
   176                  "sata"
   177              ], 
   178              "id": 3, 
   179              "id_path": "/dev/disk/by-id/id_for_sdc",
   180              "path": "/dev/sdc", 
   181              "model": "Samsung_SSD_850_EVO_250GB", 
   182              "block_size": 4096, 
   183              "serial": "S21NNSAFC38999L", 
   184              "size": 250362438230
   185          },
   186          {
   187              "name": "sdd", 
   188              "tags": [
   189                  "ssd", 
   190                  "sata"
   191              ], 
   192              "id": 4, 
   193              "id_path": "/dev/disk/by-id/id_for_sdd",
   194              "path": "/dev/sdd", 
   195              "model": "Samsung_SSD_850_EVO_250GB", 
   196              "block_size": 4096, 
   197              "serial": "S21NNSAFC386666L", 
   198              "size": 250362438230
   199          },
   200          {
   201              "name": "sde", 
   202              "tags": [
   203                  "ssd", 
   204                  "sata"
   205              ], 
   206              "id": 666, 
   207              "id_path": "/dev/disk/by-id/id_for_sde",
   208              "path": "/dev/sde", 
   209              "model": "Samsung_SSD_850_EVO_250GB", 
   210              "block_size": 4096, 
   211              "serial": "S21NNSAFC388888L", 
   212              "size": 250362438230
   213          }
   214      ], 
   215      "constraint_map": {
   216          "1": "root",
   217          "2": "1",
   218          "3": "2",
   219          "4": "3"
   220      }
   221  } 
   222  `[1:]
   223  
   224  type storageProviderSuite struct {
   225  	testing.BaseSuite
   226  }
   227  
   228  var _ = gc.Suite(&storageProviderSuite{})
   229  
   230  func (*storageProviderSuite) TestValidateConfigTags(c *gc.C) {
   231  	p := maasStorageProvider{}
   232  	validate := func(tags interface{}) {
   233  		cfg, err := storage.NewConfig("foo", maasStorageProviderType, map[string]interface{}{
   234  			"tags": tags,
   235  		})
   236  		c.Assert(err, jc.ErrorIsNil)
   237  		err = p.ValidateConfig(cfg)
   238  		c.Assert(err, jc.ErrorIsNil)
   239  	}
   240  	validate("singular")
   241  	validate("mul,ti,ple")
   242  	validate(" leading, spaces")
   243  	validate("trailing ,spaces ")
   244  	validate(" and,everything, in ,  between ")
   245  }
   246  
   247  func (*storageProviderSuite) TestValidateConfigInvalidConfig(c *gc.C) {
   248  	p := maasStorageProvider{}
   249  	cfg, err := storage.NewConfig("foo", maasStorageProviderType, map[string]interface{}{
   250  		"tags": "white space",
   251  	})
   252  	c.Assert(err, jc.ErrorIsNil)
   253  	err = p.ValidateConfig(cfg)
   254  	c.Assert(err, gc.ErrorMatches, `tags may not contain whitespace: "white space"`)
   255  }
   256  
   257  func (*storageProviderSuite) TestValidateConfigUnknownAttribute(c *gc.C) {
   258  	p := maasStorageProvider{}
   259  	cfg, err := storage.NewConfig("foo", maasStorageProviderType, map[string]interface{}{
   260  		"unknown": "config",
   261  	})
   262  	c.Assert(err, jc.ErrorIsNil)
   263  	err = p.ValidateConfig(cfg)
   264  	c.Assert(err, jc.ErrorIsNil) // unknown attributes are ignored
   265  }
   266  
   267  func (s *storageProviderSuite) TestSupports(c *gc.C) {
   268  	p := maasStorageProvider{}
   269  	c.Assert(p.Supports(storage.StorageKindBlock), jc.IsTrue)
   270  	c.Assert(p.Supports(storage.StorageKindFilesystem), jc.IsFalse)
   271  }
   272  
   273  func (s *storageProviderSuite) TestScope(c *gc.C) {
   274  	p := maasStorageProvider{}
   275  	c.Assert(p.Scope(), gc.Equals, storage.ScopeEnviron)
   276  }