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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package description
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  	"gopkg.in/yaml.v2"
    10  )
    11  
    12  type ConstraintsSerializationSuite struct {
    13  	SerializationSuite
    14  }
    15  
    16  var _ = gc.Suite(&ConstraintsSerializationSuite{})
    17  
    18  func (s *ConstraintsSerializationSuite) SetUpTest(c *gc.C) {
    19  	s.importName = "constraints"
    20  	s.importFunc = func(m map[string]interface{}) (interface{}, error) {
    21  		return importConstraints(m)
    22  	}
    23  }
    24  
    25  func (s *ConstraintsSerializationSuite) allArgs() ConstraintsArgs {
    26  	// NOTE: using gig from package_test.go
    27  	return ConstraintsArgs{
    28  		Architecture: "amd64",
    29  		Container:    "lxd",
    30  		CpuCores:     8,
    31  		CpuPower:     4000,
    32  		InstanceType: "magic",
    33  		Memory:       16 * gig,
    34  		RootDisk:     200 * gig,
    35  		Spaces:       []string{"my", "own"},
    36  		Tags:         []string{"much", "strong"},
    37  	}
    38  }
    39  
    40  func (s *ConstraintsSerializationSuite) TestNewConstraints(c *gc.C) {
    41  	args := s.allArgs()
    42  	instance := newConstraints(args)
    43  
    44  	c.Assert(instance.Architecture(), gc.Equals, args.Architecture)
    45  	c.Assert(instance.Container(), gc.Equals, args.Container)
    46  	c.Assert(instance.CpuCores(), gc.Equals, args.CpuCores)
    47  	c.Assert(instance.CpuPower(), gc.Equals, args.CpuPower)
    48  	c.Assert(instance.InstanceType(), gc.Equals, args.InstanceType)
    49  	c.Assert(instance.Memory(), gc.Equals, args.Memory)
    50  	c.Assert(instance.RootDisk(), gc.Equals, args.RootDisk)
    51  
    52  	// Before we check tags and spaces, modify args to make sure that the
    53  	// instance ones don't change.
    54  	args.Spaces[0] = "weird"
    55  	args.Tags[0] = "weird"
    56  	spaces := instance.Spaces()
    57  	c.Assert(spaces, jc.DeepEquals, []string{"my", "own"})
    58  	tags := instance.Tags()
    59  	c.Assert(tags, jc.DeepEquals, []string{"much", "strong"})
    60  
    61  	// Also, changing the spaces tags returned, doesn't modify the instance
    62  	spaces[0] = "weird"
    63  	tags[0] = "weird"
    64  	c.Assert(instance.Spaces(), jc.DeepEquals, []string{"my", "own"})
    65  	c.Assert(instance.Tags(), jc.DeepEquals, []string{"much", "strong"})
    66  }
    67  
    68  func (s *ConstraintsSerializationSuite) TestNewConstraintsWithVirt(c *gc.C) {
    69  	args := s.allArgs()
    70  	args.VirtType = "kvm"
    71  	instance := newConstraints(args)
    72  	c.Assert(instance.VirtType(), gc.Equals, args.VirtType)
    73  }
    74  
    75  func (s *ConstraintsSerializationSuite) TestNewConstraintsEmpty(c *gc.C) {
    76  	instance := newConstraints(ConstraintsArgs{})
    77  	c.Assert(instance, gc.IsNil)
    78  }
    79  
    80  func (s *ConstraintsSerializationSuite) TestEmptyTagsAndSpaces(c *gc.C) {
    81  	instance := newConstraints(ConstraintsArgs{Architecture: "amd64"})
    82  	// We actually want them to be nil, not empty slices.
    83  	c.Assert(instance.Tags(), gc.IsNil)
    84  	c.Assert(instance.Spaces(), gc.IsNil)
    85  }
    86  
    87  func (s *ConstraintsSerializationSuite) TestEmptyVirt(c *gc.C) {
    88  	instance := newConstraints(ConstraintsArgs{Architecture: "amd64"})
    89  	c.Assert(instance.VirtType(), gc.Equals, "")
    90  }
    91  
    92  func (s *ConstraintsSerializationSuite) TestParsingSerializedData(c *gc.C) {
    93  	s.assertParsingSerializedConstraints(c, newConstraints(s.allArgs()))
    94  }
    95  
    96  func (s *ConstraintsSerializationSuite) assertParsingSerializedConstraints(c *gc.C, initial Constraints) {
    97  	bytes, err := yaml.Marshal(initial)
    98  	c.Assert(err, jc.ErrorIsNil)
    99  
   100  	var source map[string]interface{}
   101  	err = yaml.Unmarshal(bytes, &source)
   102  	c.Assert(err, jc.ErrorIsNil)
   103  
   104  	instance, err := importConstraints(source)
   105  	c.Assert(err, jc.ErrorIsNil)
   106  	c.Assert(instance, jc.DeepEquals, initial)
   107  }
   108  
   109  func (s *ConstraintsSerializationSuite) TestParsingSerializedVirt(c *gc.C) {
   110  	args := s.allArgs()
   111  	args.VirtType = "kvm"
   112  	s.assertParsingSerializedConstraints(c, newConstraints(args))
   113  }