github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/storageconstraint_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 StorageConstraintSerializationSuite struct { 13 SerializationSuite 14 } 15 16 var _ = gc.Suite(&StorageConstraintSerializationSuite{}) 17 18 func (s *StorageConstraintSerializationSuite) SetUpTest(c *gc.C) { 19 s.SerializationSuite.SetUpTest(c) 20 s.importName = "storageconstraint" 21 s.importFunc = func(m map[string]interface{}) (interface{}, error) { 22 return importStorageConstraint(m) 23 } 24 s.testFields = func(m map[string]interface{}) { 25 m["pool"] = "" 26 m["size"] = 0 27 m["count"] = 0 28 } 29 } 30 31 func (s *StorageConstraintSerializationSuite) TestMissingValue(c *gc.C) { 32 testMap := s.makeMap(1) 33 delete(testMap, "pool") 34 _, err := importStorageConstraint(testMap) 35 c.Check(err.Error(), gc.Equals, "storageconstraint v1 schema check failed: pool: expected string, got nothing") 36 } 37 38 func (*StorageConstraintSerializationSuite) TestParsing(c *gc.C) { 39 addr, err := importStorageConstraint(map[string]interface{}{ 40 "version": 1, 41 "pool": "olympic", 42 "size": 50, 43 "count": 2, 44 }) 45 c.Assert(err, jc.ErrorIsNil) 46 expected := &storageconstraint{ 47 Version: 1, 48 Pool_: "olympic", 49 Size_: 50, 50 Count_: 2, 51 } 52 c.Assert(addr, jc.DeepEquals, expected) 53 } 54 55 func (*StorageConstraintSerializationSuite) TestParsingSerializedData(c *gc.C) { 56 initial := &storageconstraint{ 57 Version: 1, 58 Pool_: "olympic", 59 Size_: 50, 60 Count_: 2, 61 } 62 63 bytes, err := yaml.Marshal(initial) 64 c.Assert(err, jc.ErrorIsNil) 65 66 var source map[string]interface{} 67 err = yaml.Unmarshal(bytes, &source) 68 c.Assert(err, jc.ErrorIsNil) 69 70 storageconstraints, err := importStorageConstraint(source) 71 c.Assert(err, jc.ErrorIsNil) 72 73 c.Assert(storageconstraints, jc.DeepEquals, initial) 74 }