github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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/gomaasapi/v2"
     8  	"github.com/juju/names/v5"
     9  	"github.com/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/core/constraints"
    14  	"github.com/juju/juju/storage"
    15  )
    16  
    17  type volumeSuite struct {
    18  	maasSuite
    19  }
    20  
    21  var _ = gc.Suite(&volumeSuite{})
    22  
    23  func (s *volumeSuite) TestBuildMAASVolumeParametersNoVolumes(c *gc.C) {
    24  	vInfo, err := buildMAASVolumeParameters(nil, constraints.Value{})
    25  	c.Assert(err, jc.ErrorIsNil)
    26  	c.Assert(vInfo, gc.HasLen, 0)
    27  }
    28  
    29  func (s *volumeSuite) TestBuildMAASVolumeParametersJustRootDisk(c *gc.C) {
    30  	var cons constraints.Value
    31  	rootSize := uint64(20000)
    32  	cons.RootDisk = &rootSize
    33  	vInfo, err := buildMAASVolumeParameters(nil, cons)
    34  	c.Assert(err, jc.ErrorIsNil)
    35  	c.Assert(vInfo, jc.DeepEquals, []volumeInfo{
    36  		{"root", 20, nil},
    37  	})
    38  }
    39  
    40  func (s *volumeSuite) TestBuildMAASVolumeParametersNoTags(c *gc.C) {
    41  	vInfo, err := buildMAASVolumeParameters([]storage.VolumeParams{
    42  		{Tag: names.NewVolumeTag("1"), Size: 2000000},
    43  	}, constraints.Value{})
    44  	c.Assert(err, jc.ErrorIsNil)
    45  	c.Assert(vInfo, jc.DeepEquals, []volumeInfo{
    46  		{"root", 0, nil}, //root disk
    47  		{"1", 1954, nil},
    48  	})
    49  }
    50  
    51  func (s *volumeSuite) TestBuildMAASVolumeParametersWithRootDisk(c *gc.C) {
    52  	var cons constraints.Value
    53  	rootSize := uint64(20000)
    54  	cons.RootDisk = &rootSize
    55  	vInfo, err := buildMAASVolumeParameters([]storage.VolumeParams{
    56  		{Tag: names.NewVolumeTag("1"), Size: 2000000},
    57  	}, cons)
    58  	c.Assert(err, jc.ErrorIsNil)
    59  	c.Assert(vInfo, jc.DeepEquals, []volumeInfo{
    60  		{"root", 20, nil}, //root disk
    61  		{"1", 1954, nil},
    62  	})
    63  }
    64  
    65  func (s *volumeSuite) TestBuildMAASVolumeParametersWithTags(c *gc.C) {
    66  	vInfo, err := buildMAASVolumeParameters([]storage.VolumeParams{
    67  		{Tag: names.NewVolumeTag("1"), Size: 2000000, Attributes: map[string]interface{}{"tags": "tag1,tag2"}},
    68  	}, constraints.Value{})
    69  	c.Assert(err, jc.ErrorIsNil)
    70  	c.Assert(vInfo, jc.DeepEquals, []volumeInfo{
    71  		{"root", 0, nil}, //root disk
    72  		{"1", 1954, []string{"tag1", "tag2"}},
    73  	})
    74  }
    75  
    76  func (s *volumeSuite) TestInstanceVolumesMAAS2(c *gc.C) {
    77  	instance := maasInstance{
    78  		machine: &fakeMachine{},
    79  		constraintMatches: gomaasapi.ConstraintMatches{
    80  			Storage: map[string][]gomaasapi.StorageDevice{
    81  				"root": {&fakeBlockDevice{name: "sda", idPath: "/dev/disk/by-dname/sda", size: 250059350016}},
    82  				"1":    {&fakeBlockDevice{name: "sdb", idPath: "/dev/sdb", size: 500059350016}},
    83  				"2":    {&fakeBlockDevice{name: "sdc", idPath: "/dev/disk/by-id/foo", size: 250362438230}},
    84  				"3": {
    85  					&fakeBlockDevice{name: "sdd", idPath: "/dev/disk/by-dname/sdd", size: 250362438230},
    86  					&fakeBlockDevice{name: "sde", idPath: "/dev/disk/by-dname/sde", size: 250362438230},
    87  				},
    88  				"4": {
    89  					&fakeBlockDevice{name: "sdf", idPath: "/dev/disk/by-id/wwn-drbr", size: 280362438231},
    90  				},
    91  				"5": {
    92  					&fakePartition{name: "sde-part1", path: "/dev/disk/by-dname/sde-part1", size: 280362438231},
    93  				},
    94  				"6": {
    95  					&fakeBlockDevice{name: "sdg", idPath: "/dev/disk/by-dname/sdg", size: 280362438231},
    96  				},
    97  			},
    98  		},
    99  	}
   100  	mTag := names.NewMachineTag("1")
   101  	volumes, attachments, err := instance.volumes(mTag, []names.VolumeTag{
   102  		names.NewVolumeTag("1"),
   103  		names.NewVolumeTag("2"),
   104  		names.NewVolumeTag("3"),
   105  		names.NewVolumeTag("4"),
   106  		names.NewVolumeTag("5"),
   107  	})
   108  	c.Assert(err, jc.ErrorIsNil)
   109  	// Expect 4 volumes - root volume is ignored, as are volumes
   110  	// with tags we did not request.
   111  	c.Assert(volumes, gc.HasLen, 5)
   112  	c.Assert(attachments, gc.HasLen, 5)
   113  	c.Check(volumes, jc.SameContents, []storage.Volume{{
   114  		names.NewVolumeTag("1"),
   115  		storage.VolumeInfo{
   116  			VolumeId: "volume-1",
   117  			Size:     476893,
   118  		},
   119  	}, {
   120  		names.NewVolumeTag("2"),
   121  		storage.VolumeInfo{
   122  			VolumeId:   "volume-2",
   123  			Size:       238764,
   124  			HardwareId: "foo",
   125  		},
   126  	}, {
   127  		names.NewVolumeTag("3"),
   128  		storage.VolumeInfo{
   129  			VolumeId: "volume-3",
   130  			Size:     238764,
   131  		},
   132  	}, {
   133  		names.NewVolumeTag("4"),
   134  		storage.VolumeInfo{
   135  			VolumeId: "volume-4",
   136  			Size:     267374,
   137  			WWN:      "drbr",
   138  		},
   139  	}, {
   140  		names.NewVolumeTag("5"),
   141  		storage.VolumeInfo{
   142  			VolumeId: "volume-5",
   143  			Size:     267374,
   144  		},
   145  	}})
   146  	c.Assert(attachments, jc.SameContents, []storage.VolumeAttachment{{
   147  		names.NewVolumeTag("1"),
   148  		mTag,
   149  		storage.VolumeAttachmentInfo{
   150  			DeviceName: "sdb",
   151  		},
   152  	}, {
   153  		names.NewVolumeTag("2"),
   154  		mTag,
   155  		storage.VolumeAttachmentInfo{},
   156  	}, {
   157  		names.NewVolumeTag("3"),
   158  		mTag,
   159  		storage.VolumeAttachmentInfo{
   160  			DeviceLink: "/dev/disk/by-dname/sdd",
   161  		},
   162  	}, {
   163  		names.NewVolumeTag("4"),
   164  		mTag,
   165  		storage.VolumeAttachmentInfo{},
   166  	}, {
   167  		names.NewVolumeTag("5"),
   168  		mTag,
   169  		storage.VolumeAttachmentInfo{
   170  			DeviceLink: "/dev/disk/by-dname/sde-part1",
   171  		},
   172  	}})
   173  }
   174  
   175  type storageProviderSuite struct {
   176  	testing.IsolationSuite
   177  }
   178  
   179  var _ = gc.Suite(&storageProviderSuite{})
   180  
   181  func (*storageProviderSuite) TestValidateConfigTags(c *gc.C) {
   182  	p := maasStorageProvider{}
   183  	validate := func(tags interface{}) {
   184  		cfg, err := storage.NewConfig("foo", maasStorageProviderType, map[string]interface{}{
   185  			"tags": tags,
   186  		})
   187  		c.Assert(err, jc.ErrorIsNil)
   188  		err = p.ValidateConfig(cfg)
   189  		c.Assert(err, jc.ErrorIsNil)
   190  	}
   191  	validate("singular")
   192  	validate("mul,ti,ple")
   193  	validate(" leading, spaces")
   194  	validate("trailing ,spaces ")
   195  	validate(" and,everything, in ,  between ")
   196  }
   197  
   198  func (*storageProviderSuite) TestValidateConfigInvalidConfig(c *gc.C) {
   199  	p := maasStorageProvider{}
   200  	cfg, err := storage.NewConfig("foo", maasStorageProviderType, map[string]interface{}{
   201  		"tags": "white space",
   202  	})
   203  	c.Assert(err, jc.ErrorIsNil)
   204  	err = p.ValidateConfig(cfg)
   205  	c.Assert(err, gc.ErrorMatches, `tags may not contain whitespace: "white space"`)
   206  }
   207  
   208  func (*storageProviderSuite) TestValidateConfigUnknownAttribute(c *gc.C) {
   209  	p := maasStorageProvider{}
   210  	cfg, err := storage.NewConfig("foo", maasStorageProviderType, map[string]interface{}{
   211  		"unknown": "config",
   212  	})
   213  	c.Assert(err, jc.ErrorIsNil)
   214  	err = p.ValidateConfig(cfg)
   215  	c.Assert(err, jc.ErrorIsNil) // unknown attributes are ignored
   216  }
   217  
   218  func (s *storageProviderSuite) TestSupports(c *gc.C) {
   219  	p := maasStorageProvider{}
   220  	c.Assert(p.Supports(storage.StorageKindBlock), jc.IsTrue)
   221  	c.Assert(p.Supports(storage.StorageKindFilesystem), jc.IsFalse)
   222  }
   223  
   224  func (s *storageProviderSuite) TestScope(c *gc.C) {
   225  	p := maasStorageProvider{}
   226  	c.Assert(p.Scope(), gc.Equals, storage.ScopeEnviron)
   227  }