github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/storagepool_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 StoragePoolSerializationSuite struct { 13 SliceSerializationSuite 14 } 15 16 var _ = gc.Suite(&StoragePoolSerializationSuite{}) 17 18 func (s *StoragePoolSerializationSuite) SetUpTest(c *gc.C) { 19 s.SliceSerializationSuite.SetUpTest(c) 20 s.importName = "storagepools" 21 s.sliceName = "pools" 22 s.importFunc = func(m map[string]interface{}) (interface{}, error) { 23 return importStoragePools(m) 24 } 25 s.testFields = func(m map[string]interface{}) { 26 m["pools"] = []interface{}{} 27 } 28 } 29 30 func testStoragePool() *storagepool { 31 v := newStoragePool(testStoragePoolArgs()) 32 return v 33 } 34 35 func testStoragePoolArgs() StoragePoolArgs { 36 return StoragePoolArgs{ 37 Name: "test", 38 Provider: "magic", 39 Attributes: map[string]interface{}{ 40 "method": "madness", 41 }, 42 } 43 } 44 45 func (s *StoragePoolSerializationSuite) TestNewStoragePool(c *gc.C) { 46 storagepool := testStoragePool() 47 48 c.Check(storagepool.Name(), gc.Equals, "test") 49 c.Check(storagepool.Provider(), gc.Equals, "magic") 50 c.Check(storagepool.Attributes(), jc.DeepEquals, map[string]interface{}{ 51 "method": "madness", 52 }) 53 } 54 55 func (s *StoragePoolSerializationSuite) exportImport(c *gc.C, storagepool_ *storagepool) *storagepool { 56 initial := storagepools{ 57 Version: 1, 58 Pools_: []*storagepool{storagepool_}, 59 } 60 61 bytes, err := yaml.Marshal(initial) 62 c.Assert(err, jc.ErrorIsNil) 63 64 var source map[string]interface{} 65 err = yaml.Unmarshal(bytes, &source) 66 c.Assert(err, jc.ErrorIsNil) 67 68 storagepools, err := importStoragePools(source) 69 c.Assert(err, jc.ErrorIsNil) 70 c.Assert(storagepools, gc.HasLen, 1) 71 return storagepools[0] 72 } 73 74 func (s *StoragePoolSerializationSuite) TestParsingSerializedData(c *gc.C) { 75 original := testStoragePool() 76 storagepool := s.exportImport(c, original) 77 c.Assert(storagepool, jc.DeepEquals, original) 78 }