forked from juju/description
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payload_test.go
85 lines (71 loc) · 2.08 KB
/
payload_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
)
type PayloadSerializationSuite struct {
SliceSerializationSuite
}
var _ = gc.Suite(&PayloadSerializationSuite{})
func (s *PayloadSerializationSuite) SetUpTest(c *gc.C) {
s.SliceSerializationSuite.SetUpTest(c)
s.importName = "payloads"
s.sliceName = "payloads"
s.importFunc = func(m map[string]interface{}) (interface{}, error) {
return importPayloads(m)
}
s.testFields = func(m map[string]interface{}) {
m["payloads"] = []interface{}{}
}
}
func allPayloadArgs() PayloadArgs {
return PayloadArgs{
Name: "bob",
Type: "docker",
RawID: "d06f00d",
State: "running",
Labels: []string{"auto", "foo"},
}
}
func (s *PayloadSerializationSuite) TestNewPayload(c *gc.C) {
p := newPayload(allPayloadArgs())
c.Check(p.Name(), gc.Equals, "bob")
c.Check(p.Type(), gc.Equals, "docker")
c.Check(p.RawID(), gc.Equals, "d06f00d")
c.Check(p.State(), gc.Equals, "running")
c.Check(p.Labels(), jc.DeepEquals, []string{"auto", "foo"})
}
func (s *PayloadSerializationSuite) exportImport(c *gc.C, p *payload) *payload {
initial := payloads{
Version: 1,
Payloads_: []*payload{p},
}
bytes, err := yaml.Marshal(initial)
c.Assert(err, jc.ErrorIsNil)
var source map[string]interface{}
err = yaml.Unmarshal(bytes, &source)
c.Assert(err, jc.ErrorIsNil)
payloads, err := importPayloads(source)
c.Assert(err, jc.ErrorIsNil)
c.Assert(payloads, gc.HasLen, 1)
return payloads[0]
}
func (s *PayloadSerializationSuite) TestParsingSerializedData(c *gc.C) {
initial := newPayload(allPayloadArgs())
imported := s.exportImport(c, initial)
c.Assert(imported, jc.DeepEquals, initial)
}
func (s *PayloadSerializationSuite) TestImportEmpty(c *gc.C) {
payloads, err := importPayloads(emptyPayloadMap())
c.Assert(err, jc.ErrorIsNil)
c.Assert(payloads, gc.HasLen, 0)
}
func emptyPayloadMap() map[string]interface{} {
return map[string]interface{}{
"version": 1,
"payloads": []interface{}{},
}
}