github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/status_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 "time" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 "gopkg.in/yaml.v2" 12 ) 13 14 type StatusSerializationSuite struct { 15 SerializationSuite 16 statusFields map[string]interface{} 17 } 18 19 var _ = gc.Suite(&StatusSerializationSuite{}) 20 21 func minimalStatus() *status { 22 return newStatus(minimalStatusArgs()) 23 } 24 25 func minimalStatusMap() map[interface{}]interface{} { 26 return map[interface{}]interface{}{ 27 "version": 1, 28 "status": map[interface{}]interface{}{ 29 "value": "running", 30 "updated": "2016-01-28T11:50:00Z", 31 }, 32 } 33 } 34 35 func minimalStatusArgs() StatusArgs { 36 return StatusArgs{ 37 Value: "running", 38 Updated: time.Date(2016, 1, 28, 11, 50, 0, 0, time.UTC), 39 } 40 } 41 42 func (s *StatusSerializationSuite) SetUpTest(c *gc.C) { 43 s.SerializationSuite.SetUpTest(c) 44 s.importName = "status" 45 s.importFunc = func(m map[string]interface{}) (interface{}, error) { 46 return importStatus(m) 47 } 48 s.statusFields = map[string]interface{}{} 49 s.testFields = func(m map[string]interface{}) { 50 m["status"] = s.statusFields 51 } 52 } 53 54 func (s *StatusSerializationSuite) TestMinimalMatches(c *gc.C) { 55 bytes, err := yaml.Marshal(minimalStatus()) 56 c.Assert(err, jc.ErrorIsNil) 57 58 var source map[interface{}]interface{} 59 err = yaml.Unmarshal(bytes, &source) 60 c.Assert(err, jc.ErrorIsNil) 61 c.Assert(source, jc.DeepEquals, minimalStatusMap()) 62 } 63 64 func (s *StatusSerializationSuite) TestMissingValue(c *gc.C) { 65 testMap := s.makeMap(1) 66 s.statusFields["updated"] = "2016-01-28T11:50:00Z" 67 _, err := importStatus(testMap) 68 c.Check(err.Error(), gc.Equals, "status v1 schema check failed: value: expected string, got nothing") 69 } 70 71 func (s *StatusSerializationSuite) TestMissingUpdated(c *gc.C) { 72 testMap := s.makeMap(1) 73 s.statusFields["value"] = "running" 74 _, err := importStatus(testMap) 75 c.Check(err.Error(), gc.Equals, "status v1 schema check failed: updated: expected string or time.Time, got nothing") 76 } 77 78 func (s *StatusSerializationSuite) TestNewStatus(c *gc.C) { 79 args := StatusArgs{ 80 Value: "value", 81 Message: "message", 82 Data: map[string]interface{}{ 83 "extra": "anther", 84 }, 85 Updated: time.Date(2016, 1, 28, 11, 50, 0, 0, time.UTC), 86 } 87 status := newStatus(args) 88 c.Assert(status.Value(), gc.Equals, args.Value) 89 c.Assert(status.Message(), gc.Equals, args.Message) 90 c.Assert(status.Data(), jc.DeepEquals, args.Data) 91 c.Assert(status.Updated(), gc.Equals, args.Updated) 92 } 93 94 func (s *StatusSerializationSuite) exportImport(c *gc.C, status_ *status) *status { 95 bytes, err := yaml.Marshal(status_) 96 c.Assert(err, jc.ErrorIsNil) 97 98 var source map[string]interface{} 99 err = yaml.Unmarshal(bytes, &source) 100 c.Assert(err, jc.ErrorIsNil) 101 102 status, err := importStatus(source) 103 c.Assert(err, jc.ErrorIsNil) 104 return status 105 } 106 107 func (s *StatusSerializationSuite) TestParsing(c *gc.C) { 108 initial := newStatus(StatusArgs{ 109 Value: "started", 110 Message: "a message", 111 Data: map[string]interface{}{ 112 "extra": "anther", 113 }, 114 Updated: time.Date(2016, 1, 28, 11, 50, 0, 0, time.UTC), 115 }) 116 status := s.exportImport(c, initial) 117 c.Assert(status, jc.DeepEquals, initial) 118 } 119 120 func (s *StatusSerializationSuite) TestOptionalValues(c *gc.C) { 121 initial := newStatus(StatusArgs{ 122 Value: "started", 123 Updated: time.Date(2016, 1, 28, 11, 50, 0, 0, time.UTC), 124 }) 125 status := s.exportImport(c, initial) 126 c.Assert(status, jc.DeepEquals, initial) 127 } 128 129 type StatusHistorySerializationSuite struct { 130 SerializationSuite 131 } 132 133 var _ = gc.Suite(&StatusHistorySerializationSuite{}) 134 135 func emptyStatusHistoryMap() map[interface{}]interface{} { 136 return map[interface{}]interface{}{ 137 "version": 1, 138 "history": []interface{}{}, 139 } 140 } 141 142 func testStatusHistoryArgs() []StatusArgs { 143 return []StatusArgs{{ 144 Value: "running", 145 Updated: time.Date(2016, 1, 28, 11, 50, 0, 0, time.UTC), 146 }, { 147 Value: "stopped", 148 Updated: time.Date(2016, 1, 28, 12, 50, 0, 0, time.UTC), 149 }, { 150 Value: "running", 151 Updated: time.Date(2016, 1, 28, 13, 50, 0, 0, time.UTC), 152 }} 153 } 154 155 func (s *StatusHistorySerializationSuite) SetUpTest(c *gc.C) { 156 s.SerializationSuite.SetUpTest(c) 157 s.importName = "status" 158 s.importFunc = func(m map[string]interface{}) (interface{}, error) { 159 history := newStatusHistory() 160 if err := importStatusHistory(&history, m); err != nil { 161 return nil, err 162 } 163 return &history, nil 164 } 165 s.testFields = func(m map[string]interface{}) { 166 m["history"] = []interface{}{} 167 } 168 } 169 170 func (s *StatusHistorySerializationSuite) TestSetStatusHistory(c *gc.C) { 171 // Make sure all the arg values are set. 172 args := []StatusArgs{{ 173 Value: "running", 174 Message: "all good", 175 Data: map[string]interface{}{ 176 "key": "value", 177 }, 178 Updated: time.Date(2016, 1, 28, 11, 50, 0, 0, time.UTC), 179 }, { 180 Value: "stopped", 181 Updated: time.Date(2016, 1, 28, 12, 50, 0, 0, time.UTC), 182 }} 183 history := newStatusHistory() 184 history.SetStatusHistory(args) 185 186 for i, point := range history.StatusHistory() { 187 c.Check(point.Value(), gc.Equals, args[i].Value) 188 c.Check(point.Message(), gc.Equals, args[i].Message) 189 c.Check(point.Data(), jc.DeepEquals, args[i].Data) 190 c.Check(point.Updated(), gc.Equals, args[i].Updated) 191 } 192 } 193 194 func (s *StatusHistorySerializationSuite) exportImport(c *gc.C, status_ StatusHistory_) StatusHistory_ { 195 bytes, err := yaml.Marshal(status_) 196 c.Assert(err, jc.ErrorIsNil) 197 198 var source map[string]interface{} 199 err = yaml.Unmarshal(bytes, &source) 200 c.Assert(err, jc.ErrorIsNil) 201 202 history := newStatusHistory() 203 err = importStatusHistory(&history, source) 204 c.Assert(err, jc.ErrorIsNil) 205 return history 206 } 207 208 func (s *StatusHistorySerializationSuite) TestParsing(c *gc.C) { 209 initial := newStatusHistory() 210 initial.SetStatusHistory(testStatusHistoryArgs()) 211 history := s.exportImport(c, initial) 212 c.Assert(history, jc.DeepEquals, initial) 213 } 214 215 type StatusHistoryMixinSuite struct { 216 creator func() HasStatusHistory 217 serializer func(*gc.C, interface{}) HasStatusHistory 218 } 219 220 func (s *StatusHistoryMixinSuite) TestStatusHistory(c *gc.C) { 221 initial := s.creator() 222 args := testStatusHistoryArgs() 223 initial.SetStatusHistory(args) 224 225 entity := s.serializer(c, initial) 226 for i, point := range entity.StatusHistory() { 227 c.Check(point.Value(), gc.Equals, args[i].Value) 228 c.Check(point.Message(), gc.Equals, args[i].Message) 229 c.Check(point.Data(), jc.DeepEquals, args[i].Data) 230 c.Check(point.Updated(), gc.Equals, args[i].Updated) 231 } 232 }