github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/core/devices/constraints_test.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package devices_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/core/devices"
    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 (*ConstraintsSuite) testParse(c *gc.C, s string, expect devices.Constraints) {
    21  	cons, err := devices.ParseConstraints(s)
    22  	c.Assert(err, jc.ErrorIsNil)
    23  	c.Assert(cons, gc.DeepEquals, expect)
    24  }
    25  
    26  func (*ConstraintsSuite) testParseError(c *gc.C, s, expectErr string) {
    27  	_, err := devices.ParseConstraints(s)
    28  	c.Assert(err, gc.ErrorMatches, expectErr)
    29  }
    30  
    31  func (s *ConstraintsSuite) TestParseConstraintsDeviceGood(c *gc.C) {
    32  	s.testParse(c, "nvidia.com/gpu", devices.Constraints{
    33  		Type:  "nvidia.com/gpu",
    34  		Count: 1,
    35  	})
    36  	s.testParse(c, "2,nvidia.com/gpu", devices.Constraints{
    37  		Type:  "nvidia.com/gpu",
    38  		Count: 2,
    39  	})
    40  	s.testParse(c, "3,nvidia.com/gpu,gpu=nvidia-tesla-p100", devices.Constraints{
    41  		Type:  "nvidia.com/gpu",
    42  		Count: 3,
    43  		Attributes: map[string]string{
    44  			"gpu": "nvidia-tesla-p100",
    45  		},
    46  	})
    47  	s.testParse(c, "3,nvidia.com/gpu,gpu=nvidia-tesla-p100;2ndattr=another-attr", devices.Constraints{
    48  		Type:  "nvidia.com/gpu",
    49  		Count: 3,
    50  		Attributes: map[string]string{
    51  			"gpu":     "nvidia-tesla-p100",
    52  			"2ndattr": "another-attr",
    53  		},
    54  	})
    55  }
    56  
    57  func (s *ConstraintsSuite) TestParseConstraintsDeviceBad(c *gc.C) {
    58  	s.testParseError(c, "2,nvidia.com/gpu,gpu=nvidia-tesla-p100,a=b", `cannot parse device constraints string, supported format is \[<count>,\]<device-class>|<vendor/type>\[,<key>=<value>;...\]`)
    59  	s.testParseError(c, "2,nvidia.com/gpu,gpu=b=c", `device attribute key/value pair has bad format: \"gpu=b=c\"`)
    60  	s.testParseError(c, "badCount,nvidia.com/gpu", `count must be greater than zero, got \"badCount\"`)
    61  	s.testParseError(c, "0,nvidia.com/gpu", `count must be greater than zero, got \"0\"`)
    62  	s.testParseError(c, "-1,nvidia.com/gpu", `count must be greater than zero, got \"-1\"`)
    63  }