github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/storage/constraints_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/storage"
    11  	"github.com/juju/juju/testing"
    12  )
    13  
    14  type ConstraintsSuite struct {
    15  	testing.BaseSuite
    16  }
    17  
    18  var _ = gc.Suite(&ConstraintsSuite{})
    19  
    20  func (s *ConstraintsSuite) TestParseConstraintsStoragePool(c *gc.C) {
    21  	s.testParse(c, "pool,1M", storage.Constraints{
    22  		Pool:  "pool",
    23  		Count: 1,
    24  		Size:  1,
    25  	})
    26  	s.testParse(c, "pool,", storage.Constraints{
    27  		Pool:  "pool",
    28  		Count: 1,
    29  	})
    30  	s.testParse(c, "1M", storage.Constraints{
    31  		Size:  1,
    32  		Count: 1,
    33  	})
    34  }
    35  
    36  func (s *ConstraintsSuite) TestParseConstraintsCountSize(c *gc.C) {
    37  	s.testParse(c, "p,1G", storage.Constraints{
    38  		Pool:  "p",
    39  		Count: 1,
    40  		Size:  1024,
    41  	})
    42  	s.testParse(c, "p,1,0.5T", storage.Constraints{
    43  		Pool:  "p",
    44  		Count: 1,
    45  		Size:  1024 * 512,
    46  	})
    47  	s.testParse(c, "p,0.125P,3", storage.Constraints{
    48  		Pool:  "p",
    49  		Count: 3,
    50  		Size:  1024 * 1024 * 128,
    51  	})
    52  }
    53  
    54  func (s *ConstraintsSuite) TestParseConstraintsOptions(c *gc.C) {
    55  	s.testParse(c, "p,1M,", storage.Constraints{
    56  		Pool:  "p",
    57  		Count: 1,
    58  		Size:  1,
    59  	})
    60  	s.testParse(c, "p,anyoldjunk", storage.Constraints{
    61  		Pool:  "p",
    62  		Count: 1,
    63  	})
    64  }
    65  
    66  func (s *ConstraintsSuite) TestParseConstraintsCountRange(c *gc.C) {
    67  	s.testParseError(c, "p,0,100M", `cannot parse count: count must be greater than zero, got "0"`)
    68  	s.testParseError(c, "p,00,100M", `cannot parse count: count must be greater than zero, got "00"`)
    69  	s.testParseError(c, "p,-1,100M", `cannot parse count: count must be greater than zero, got "-1"`)
    70  	s.testParseError(c, "", `storage constraints require at least one field to be specified`)
    71  	s.testParseError(c, ",", `storage constraints require at least one field to be specified`)
    72  }
    73  
    74  func (s *ConstraintsSuite) TestParseConstraintsSizeRange(c *gc.C) {
    75  	s.testParseError(c, "p,-100M", `cannot parse size: expected a non-negative number, got "-100M"`)
    76  }
    77  
    78  func (*ConstraintsSuite) testParse(c *gc.C, s string, expect storage.Constraints) {
    79  	cons, err := storage.ParseConstraints(s)
    80  	c.Check(err, jc.ErrorIsNil)
    81  	c.Check(cons, gc.DeepEquals, expect)
    82  }
    83  
    84  func (*ConstraintsSuite) testParseError(c *gc.C, s, expectErr string) {
    85  	_, err := storage.ParseConstraints(s)
    86  	c.Check(err, gc.ErrorMatches, expectErr)
    87  }
    88  
    89  func (s *ConstraintsSuite) TestValidPoolName(c *gc.C) {
    90  	c.Assert(storage.IsValidPoolName("pool"), jc.IsTrue)
    91  	c.Assert(storage.IsValidPoolName("p-ool"), jc.IsTrue)
    92  	c.Assert(storage.IsValidPoolName("p-00l"), jc.IsTrue)
    93  	c.Assert(storage.IsValidPoolName("p?00l"), jc.IsTrue)
    94  	c.Assert(storage.IsValidPoolName("p-?00l"), jc.IsTrue)
    95  	c.Assert(storage.IsValidPoolName("p"), jc.IsTrue)
    96  	c.Assert(storage.IsValidPoolName("P"), jc.IsTrue)
    97  	c.Assert(storage.IsValidPoolName("p?0?l"), jc.IsTrue)
    98  }
    99  
   100  func (s *ConstraintsSuite) TestInvalidPoolName(c *gc.C) {
   101  	c.Assert(storage.IsValidPoolName("7ool"), jc.IsFalse)
   102  	c.Assert(storage.IsValidPoolName("/ool"), jc.IsFalse)
   103  	c.Assert(storage.IsValidPoolName("-00l"), jc.IsFalse)
   104  	c.Assert(storage.IsValidPoolName("*00l"), jc.IsFalse)
   105  }
   106  
   107  func (s *ConstraintsSuite) TestParseStorageConstraints(c *gc.C) {
   108  	s.testParseStorageConstraints(c,
   109  		[]string{"data=p,1M,"}, true,
   110  		map[string]storage.Constraints{"data": storage.Constraints{
   111  			Pool:  "p",
   112  			Count: 1,
   113  			Size:  1,
   114  		}})
   115  	s.testParseStorageConstraints(c,
   116  		[]string{"data"}, false,
   117  		map[string]storage.Constraints{"data": storage.Constraints{
   118  			Count: 1,
   119  		}})
   120  	s.testParseStorageConstraints(c,
   121  		[]string{"data=3", "cache"}, false,
   122  		map[string]storage.Constraints{
   123  			"data": storage.Constraints{
   124  				Count: 3,
   125  			},
   126  			"cache": storage.Constraints{
   127  				Count: 1,
   128  			},
   129  		})
   130  }
   131  
   132  func (s *ConstraintsSuite) TestParseStorageConstraintsErrors(c *gc.C) {
   133  	s.testStorageConstraintsError(c,
   134  		[]string{"data"}, true,
   135  		`.*where "constraints" must be specified.*`)
   136  	s.testStorageConstraintsError(c,
   137  		[]string{"data=p,=1M,"}, false,
   138  		`.*expected "name=constraints" or "name", got .*`)
   139  	s.testStorageConstraintsError(c,
   140  		[]string{"data", "data"}, false,
   141  		`storage "data" specified more than once`)
   142  	s.testStorageConstraintsError(c,
   143  		[]string{"data=-1"}, false,
   144  		`.*cannot parse constraints for storage "data".*`)
   145  	s.testStorageConstraintsError(c,
   146  		[]string{"data="}, false,
   147  		`.*cannot parse constraints for storage "data".*`)
   148  }
   149  
   150  func (*ConstraintsSuite) testParseStorageConstraints(c *gc.C,
   151  	s []string,
   152  	mustHave bool,
   153  	expect map[string]storage.Constraints,
   154  ) {
   155  	cons, err := storage.ParseConstraintsMap(s, mustHave)
   156  	c.Check(err, jc.ErrorIsNil)
   157  	c.Assert(len(cons), gc.Equals, len(expect))
   158  	for k, v := range expect {
   159  		c.Check(cons[k], gc.DeepEquals, v)
   160  	}
   161  }
   162  
   163  func (*ConstraintsSuite) testStorageConstraintsError(c *gc.C, s []string, mustHave bool, e string) {
   164  	_, err := storage.ParseConstraintsMap(s, mustHave)
   165  	c.Check(err, gc.ErrorMatches, e)
   166  }