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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cloudsigma
     5  
     6  import (
     7  	"github.com/altoros/gosigma"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/core/constraints"
    11  	"github.com/juju/juju/environs/imagemetadata"
    12  	"github.com/juju/juju/testing"
    13  )
    14  
    15  const (
    16  	validImageId = "473adb38-3b64-43b2-93bd-f1a3443c19ea"
    17  )
    18  
    19  type constraintsSuite struct {
    20  	testing.BaseSuite
    21  }
    22  
    23  var _ = gc.Suite(&constraintsSuite{})
    24  
    25  type strv struct{ v string }
    26  type uint64v struct{ v uint64 }
    27  
    28  var defConstraints = map[string]sigmaConstraints{
    29  	"bootstrap-trusty": {
    30  		driveTemplate: validImageId,
    31  		driveSize:     5 * gosigma.Gigabyte,
    32  		cores:         1,
    33  		power:         2000,
    34  		mem:           2 * gosigma.Gigabyte,
    35  	},
    36  	"trusty": {
    37  		driveTemplate: validImageId,
    38  		driveSize:     0,
    39  		cores:         1,
    40  		power:         2000,
    41  		mem:           2 * gosigma.Gigabyte,
    42  	},
    43  	"trusty-c2-p4000": {
    44  		driveTemplate: validImageId,
    45  		driveSize:     0,
    46  		cores:         2,
    47  		power:         4000,
    48  		mem:           2 * gosigma.Gigabyte,
    49  	},
    50  }
    51  
    52  var img = &imagemetadata.ImageMetadata{
    53  	Id: validImageId,
    54  }
    55  
    56  var newConstraintTests = []struct {
    57  	bootstrap bool
    58  	arch      *strv
    59  	cores     *uint64v
    60  	power     *uint64v
    61  	mem       *uint64v
    62  	disk      *uint64v
    63  	series    string
    64  	expected  sigmaConstraints
    65  	err       *strv
    66  }{
    67  	{true, nil, nil, nil, nil, nil, "trusty", defConstraints["bootstrap-trusty"], nil},
    68  	{false, nil, nil, nil, nil, nil, "trusty", defConstraints["trusty"], nil},
    69  	{true, &strv{"amd64"}, nil, nil, nil, nil, "trusty", defConstraints["bootstrap-trusty"], nil},
    70  	{false, &strv{"amd64"}, nil, nil, nil, nil, "trusty", defConstraints["trusty"], nil},
    71  	{true, nil, &uint64v{1}, nil, nil, nil, "trusty", defConstraints["bootstrap-trusty"], nil},
    72  	{false, nil, &uint64v{1}, nil, nil, nil, "trusty", defConstraints["trusty"], nil},
    73  	{false, nil, &uint64v{2}, nil, nil, nil, "trusty", defConstraints["trusty-c2-p4000"], nil},
    74  	{false, nil, &uint64v{2}, &uint64v{4000}, nil, nil, "trusty", defConstraints["trusty-c2-p4000"], nil},
    75  	{false, nil, nil, nil, &uint64v{2 * 1024}, nil, "trusty", defConstraints["trusty"], nil},
    76  	{false, nil, nil, nil, nil, &uint64v{5 * 1024}, "trusty", defConstraints["bootstrap-trusty"], nil},
    77  }
    78  
    79  func (s *constraintsSuite) TestConstraints(c *gc.C) {
    80  	for i, t := range newConstraintTests {
    81  		var cv constraints.Value
    82  		if t.arch != nil {
    83  			cv.Arch = &t.arch.v
    84  		}
    85  		if t.cores != nil {
    86  			cv.CpuCores = &t.cores.v
    87  		}
    88  		if t.power != nil {
    89  			cv.CpuPower = &t.power.v
    90  		}
    91  		if t.mem != nil {
    92  			cv.Mem = &t.mem.v
    93  		}
    94  		if t.disk != nil {
    95  			cv.RootDisk = &t.disk.v
    96  		}
    97  		v := newConstraints(t.bootstrap, cv, img)
    98  		if !c.Check(*v, gc.Equals, t.expected) {
    99  			c.Logf("test (%d): %+v", i, t)
   100  		}
   101  	}
   102  }
   103  
   104  func (s *constraintsSuite) TestConstraintsArch(c *gc.C) {
   105  	var cv constraints.Value
   106  	var expected = sigmaConstraints{
   107  		driveTemplate: validImageId,
   108  		driveSize:     5 * gosigma.Gigabyte,
   109  		cores:         1,
   110  		power:         2000,
   111  		mem:           2 * gosigma.Gigabyte,
   112  	}
   113  
   114  	sc := newConstraints(true, cv, img)
   115  	c.Check(*sc, gc.Equals, expected)
   116  }