github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/payload_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 PayloadSerializationSuite struct { 13 SliceSerializationSuite 14 } 15 16 var _ = gc.Suite(&PayloadSerializationSuite{}) 17 18 func (s *PayloadSerializationSuite) SetUpTest(c *gc.C) { 19 s.SliceSerializationSuite.SetUpTest(c) 20 s.importName = "payloads" 21 s.sliceName = "payloads" 22 s.importFunc = func(m map[string]interface{}) (interface{}, error) { 23 return importPayloads(m) 24 } 25 s.testFields = func(m map[string]interface{}) { 26 m["payloads"] = []interface{}{} 27 } 28 } 29 30 func allPayloadArgs() PayloadArgs { 31 return PayloadArgs{ 32 Name: "bob", 33 Type: "docker", 34 RawID: "d06f00d", 35 State: "running", 36 Labels: []string{"auto", "foo"}, 37 } 38 } 39 40 func (s *PayloadSerializationSuite) TestNewPayload(c *gc.C) { 41 p := newPayload(allPayloadArgs()) 42 c.Check(p.Name(), gc.Equals, "bob") 43 c.Check(p.Type(), gc.Equals, "docker") 44 c.Check(p.RawID(), gc.Equals, "d06f00d") 45 c.Check(p.State(), gc.Equals, "running") 46 c.Check(p.Labels(), jc.DeepEquals, []string{"auto", "foo"}) 47 } 48 49 func (s *PayloadSerializationSuite) exportImport(c *gc.C, p *payload) *payload { 50 initial := payloads{ 51 Version: 1, 52 Payloads_: []*payload{p}, 53 } 54 55 bytes, err := yaml.Marshal(initial) 56 c.Assert(err, jc.ErrorIsNil) 57 58 var source map[string]interface{} 59 err = yaml.Unmarshal(bytes, &source) 60 c.Assert(err, jc.ErrorIsNil) 61 62 payloads, err := importPayloads(source) 63 c.Assert(err, jc.ErrorIsNil) 64 c.Assert(payloads, gc.HasLen, 1) 65 return payloads[0] 66 } 67 68 func (s *PayloadSerializationSuite) TestParsingSerializedData(c *gc.C) { 69 initial := newPayload(allPayloadArgs()) 70 imported := s.exportImport(c, initial) 71 c.Assert(imported, jc.DeepEquals, initial) 72 } 73 74 func (s *PayloadSerializationSuite) TestImportEmpty(c *gc.C) { 75 payloads, err := importPayloads(emptyPayloadMap()) 76 c.Assert(err, jc.ErrorIsNil) 77 c.Assert(payloads, gc.HasLen, 0) 78 } 79 80 func emptyPayloadMap() map[string]interface{} { 81 return map[string]interface{}{ 82 "version": 1, 83 "payloads": []interface{}{}, 84 } 85 }