-
Notifications
You must be signed in to change notification settings - Fork 23
/
action_test.go
201 lines (174 loc) · 5.92 KB
/
action_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"time"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
)
type ActionSerializationSuite struct {
SliceSerializationSuite
}
var _ = gc.Suite(&ActionSerializationSuite{})
func (s *ActionSerializationSuite) SetUpTest(c *gc.C) {
s.SliceSerializationSuite.SetUpTest(c)
s.importName = "actions"
s.sliceName = "actions"
s.importFunc = func(m map[string]interface{}) (interface{}, error) {
return importActions(m)
}
s.testFields = func(m map[string]interface{}) {
m["actions"] = []interface{}{}
}
}
func minimalActionMap() map[interface{}]interface{} {
return map[interface{}]interface{}{
"id": "foo",
"name": "bam",
"operation": "666",
"receiver": "bar",
"parallel": true,
"execution-group": "group",
"enqueued": "2019-01-01T06:06:06Z",
"started": "2019-01-02T06:06:06Z",
"completed": "2019-01-03T06:06:06Z",
"message": "a message",
"parameters": map[interface{}]interface{}{"bar": "bam", "foo": 3},
"results": map[interface{}]interface{}{"the": 3, "thing": "bam"},
"status": "happy",
}
}
func minimalActionMapWithLogs() map[interface{}]interface{} {
result := minimalActionMap()
result["logs"] = map[interface{}]interface{}{
"version": 1,
"messages": []interface{}{
map[interface{}]interface{}{
"timestamp": "2019-01-01T06:06:06Z",
"message": "hello",
},
},
}
return result
}
func minimalAction() *action {
action := newAction(ActionArgs{
Id: "foo",
Receiver: "bar",
Name: "bam",
Operation: "666",
Parameters: map[string]interface{}{"foo": 3, "bar": "bam"},
Parallel: true,
ExecutionGroup: "group",
Enqueued: time.Date(2019, 01, 01, 6, 6, 6, 0, time.UTC),
Started: time.Date(2019, 01, 02, 6, 6, 6, 0, time.UTC),
Completed: time.Date(2019, 01, 03, 6, 6, 6, 0, time.UTC),
Status: "happy",
Message: "a message",
Results: map[string]interface{}{"the": 3, "thing": "bam"},
})
action.setLogs([]*actionMessage{
{
Timestamp_: time.Date(2019, 01, 01, 6, 6, 6, 0, time.UTC),
Message_: "hello",
},
})
return action
}
func (s *ActionSerializationSuite) TestMinimalMatches(c *gc.C) {
bytes, err := yaml.Marshal(minimalAction())
c.Assert(err, jc.ErrorIsNil)
var source map[interface{}]interface{}
err = yaml.Unmarshal(bytes, &source)
c.Assert(err, jc.ErrorIsNil)
c.Assert(source, jc.DeepEquals, minimalActionMapWithLogs())
}
func (s *ActionSerializationSuite) TestNewAction(c *gc.C) {
args := ActionArgs{
Id: "foo",
Receiver: "bar",
Name: "bam",
Operation: "666",
Parameters: map[string]interface{}{"foo": 3, "bar": "bam"},
Parallel: true,
ExecutionGroup: "group",
Enqueued: time.Now(),
Started: time.Now(),
Completed: time.Now(),
Status: "happy",
Message: "a message",
Results: map[string]interface{}{"the": 3, "thing": "bam"},
Messages: []ActionMessage{
&actionMessage{Timestamp_: time.Now(), Message_: "hello"},
},
}
action := newAction(args)
c.Check(action.Id(), gc.Equals, args.Id)
c.Check(action.Receiver(), gc.Equals, args.Receiver)
c.Check(action.Name(), gc.Equals, args.Name)
c.Check(action.Operation(), gc.Equals, args.Operation)
c.Check(action.Parallel(), gc.Equals, args.Parallel)
c.Check(action.ExecutionGroup(), gc.Equals, args.ExecutionGroup)
c.Check(action.Parameters(), jc.DeepEquals, args.Parameters)
c.Check(action.Enqueued(), gc.Equals, args.Enqueued)
c.Check(action.Started(), gc.Equals, args.Started)
c.Check(action.Completed(), gc.Equals, args.Completed)
c.Check(action.Status(), gc.Equals, args.Status)
c.Check(action.Message(), gc.Equals, args.Message)
c.Check(action.Results(), jc.DeepEquals, args.Results)
c.Check(action.Logs(), jc.DeepEquals, args.Messages)
}
func (s *ActionSerializationSuite) exportImportVersion(c *gc.C, action_ *action, version int) *action {
initial := actions{
Version: version,
Actions_: []*action{action_},
}
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)
actions, err := importActions(source)
c.Assert(err, jc.ErrorIsNil)
c.Assert(actions, gc.HasLen, 1)
return actions[0]
}
func (s *ActionSerializationSuite) exportImportLatest(c *gc.C, action_ *action) *action {
return s.exportImportVersion(c, action_, 4)
}
func (s *ActionSerializationSuite) TestV1ParsingReturnsLatest(c *gc.C) {
actionV1 := minimalAction()
// Make an action with fields not in v1 removed.
actionLatest := minimalAction()
actionLatest.Logs_ = nil
actionLatest.Operation_ = ""
actionLatest.Parallel_ = false
actionLatest.ExecutionGroup_ = ""
actionResult := s.exportImportVersion(c, actionV1, 1)
c.Assert(actionResult, jc.DeepEquals, actionLatest)
}
func (s *ActionSerializationSuite) TestV2ParsingReturnsLatest(c *gc.C) {
actionV1 := minimalAction()
// Make an action with fields not in v2 removed.
actionLatest := minimalAction()
actionLatest.Operation_ = ""
actionLatest.Parallel_ = false
actionLatest.ExecutionGroup_ = ""
actionResult := s.exportImportVersion(c, actionV1, 2)
c.Assert(actionResult, jc.DeepEquals, actionLatest)
}
func (s *ActionSerializationSuite) TestV3ParsingReturnsLatest(c *gc.C) {
actionV1 := minimalAction()
// Make an action with fields not in v3 removed.
actionLatest := minimalAction()
actionLatest.Parallel_ = false
actionLatest.ExecutionGroup_ = ""
actionResult := s.exportImportVersion(c, actionV1, 3)
c.Assert(actionResult, jc.DeepEquals, actionLatest)
}
func (s *ActionSerializationSuite) TestParsingSerializedData(c *gc.C) {
action := minimalAction()
actionResult := s.exportImportLatest(c, action)
c.Assert(actionResult, jc.DeepEquals, action)
}