-
Notifications
You must be signed in to change notification settings - Fork 23
/
operation_test.go
145 lines (124 loc) · 4.43 KB
/
operation_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
// Copyright 2020 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 OperationSerializationSuite struct {
SliceSerializationSuite
}
var _ = gc.Suite(&OperationSerializationSuite{})
func (s *OperationSerializationSuite) SetUpTest(c *gc.C) {
s.SliceSerializationSuite.SetUpTest(c)
s.importName = "operations"
s.sliceName = "operations"
s.importFunc = func(m map[string]interface{}) (interface{}, error) {
return importOperations(m)
}
s.testFields = func(m map[string]interface{}) {
m["operations"] = []interface{}{}
}
}
func minimalOperationMap() map[interface{}]interface{} {
return map[interface{}]interface{}{
"id": "foo",
"summary": "bam",
"fail": "fail",
"enqueued": "2019-01-01T06:06:06Z",
"started": "2019-01-02T06:06:06Z",
"completed": "2019-01-03T06:06:06Z",
"complete-task-count": 666,
"spawned-task-count": 667,
"status": "happy",
}
}
func minimalOperation() *operation {
return newOperation(OperationArgs{
Id: "foo",
Summary: "bam",
Fail: "fail",
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",
CompleteTaskCount: 666,
SpawnedTaskCount: 667,
})
}
func (s *OperationSerializationSuite) TestMinimalMatches(c *gc.C) {
bytes, err := yaml.Marshal(minimalOperation())
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, minimalOperationMap())
}
func (s *OperationSerializationSuite) TestNewOperation(c *gc.C) {
args := OperationArgs{
Id: "foo",
Summary: "bam",
Fail: "fail",
Enqueued: time.Now(),
Started: time.Now(),
Completed: time.Now(),
Status: "happy",
CompleteTaskCount: 666,
SpawnedTaskCount: 667,
}
operation := newOperation(args)
c.Check(operation.Id(), gc.Equals, args.Id)
c.Check(operation.Summary(), gc.Equals, args.Summary)
c.Check(operation.Fail(), gc.Equals, args.Fail)
c.Check(operation.Enqueued(), gc.Equals, args.Enqueued)
c.Check(operation.Started(), gc.Equals, args.Started)
c.Check(operation.Completed(), gc.Equals, args.Completed)
c.Check(operation.Status(), gc.Equals, args.Status)
c.Check(operation.CompleteTaskCount(), gc.Equals, args.CompleteTaskCount)
c.Check(operation.SpawnedTaskCount(), gc.Equals, args.SpawnedTaskCount)
}
func (s *OperationSerializationSuite) exportImportVersion(c *gc.C, operation_ *operation, version int) *operation {
initial := operations{
Version: version,
Operations_: []*operation{operation_},
}
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)
operations, err := importOperations(source)
c.Assert(err, jc.ErrorIsNil)
c.Assert(operations, gc.HasLen, 1)
return operations[0]
}
func (s *OperationSerializationSuite) exportImportV1(c *gc.C, operation_ *operation) *operation {
return s.exportImportVersion(c, operation_, 1)
}
func (s *OperationSerializationSuite) exportImportV2(c *gc.C, operation_ *operation) *operation {
return s.exportImportVersion(c, operation_, 2)
}
func (s *OperationSerializationSuite) TestParsingSerializedDataV1(c *gc.C) {
operation := minimalOperation()
operation.Fail_ = ""
operation.SpawnedTaskCount_ = 0
operationResult := s.exportImportV1(c, operation)
c.Assert(operationResult, jc.DeepEquals, operation)
}
func (s *OperationSerializationSuite) TestParsingSerializedDataV2(c *gc.C) {
operation := minimalOperation()
operation.Fail_ = ""
operation.SpawnedTaskCount_ = 0
operationResult := s.exportImportV2(c, operation)
c.Assert(operationResult, jc.DeepEquals, operation)
}
func (s *OperationSerializationSuite) exportImportLatest(c *gc.C, operation_ *operation) *operation {
return s.exportImportVersion(c, operation_, 3)
}
func (s *OperationSerializationSuite) TestParsingSerializedData(c *gc.C) {
operation := minimalOperation()
operationResult := s.exportImportLatest(c, operation)
c.Assert(operationResult, jc.DeepEquals, operation)
}