github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/relation_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 RelationSerializationSuite struct { 13 SliceSerializationSuite 14 } 15 16 var _ = gc.Suite(&RelationSerializationSuite{}) 17 18 func (s *RelationSerializationSuite) SetUpTest(c *gc.C) { 19 s.SliceSerializationSuite.SetUpTest(c) 20 s.importName = "relations" 21 s.sliceName = "relations" 22 s.importFunc = func(m map[string]interface{}) (interface{}, error) { 23 return importRelations(m) 24 } 25 s.testFields = func(m map[string]interface{}) { 26 m["relations"] = []interface{}{} 27 } 28 } 29 30 func (s *RelationSerializationSuite) completeRelation() *relation { 31 relation := newRelation(RelationArgs{ 32 Id: 42, 33 Key: "special", 34 }) 35 36 endpoint := relation.AddEndpoint(minimalEndpointArgs()) 37 u1Settings := map[string]interface{}{ 38 "name": "unit one", 39 "key": 42, 40 } 41 u2Settings := map[string]interface{}{ 42 "name": "unit two", 43 "foo": "bar", 44 } 45 endpoint.SetUnitSettings("ubuntu/0", u1Settings) 46 endpoint.SetUnitSettings("ubuntu/1", u2Settings) 47 48 return relation 49 } 50 51 func (s *RelationSerializationSuite) TestNewRelation(c *gc.C) { 52 relation := newRelation(RelationArgs{ 53 Id: 42, 54 Key: "special", 55 }) 56 57 c.Assert(relation.Id(), gc.Equals, 42) 58 c.Assert(relation.Key(), gc.Equals, "special") 59 c.Assert(relation.Endpoints(), gc.HasLen, 0) 60 } 61 62 func (s *RelationSerializationSuite) TestRelationEndpoints(c *gc.C) { 63 relation := s.completeRelation() 64 65 endpoints := relation.Endpoints() 66 c.Assert(endpoints, gc.HasLen, 1) 67 68 ep := endpoints[0] 69 c.Assert(ep.ApplicationName(), gc.Equals, "ubuntu") 70 // Not going to check the exact contents, we expect that there 71 // should be two entries. 72 c.Assert(ep.Settings("ubuntu/0"), gc.HasLen, 2) 73 } 74 75 func (s *RelationSerializationSuite) TestParsingSerializedData(c *gc.C) { 76 initial := relations{ 77 Version: 1, 78 Relations_: []*relation{s.completeRelation()}, 79 } 80 81 bytes, err := yaml.Marshal(initial) 82 c.Assert(err, jc.ErrorIsNil) 83 84 var source map[string]interface{} 85 err = yaml.Unmarshal(bytes, &source) 86 c.Assert(err, jc.ErrorIsNil) 87 88 relations, err := importRelations(source) 89 c.Assert(err, jc.ErrorIsNil) 90 91 c.Assert(relations, jc.DeepEquals, initial.Relations_) 92 } 93 94 type EndpointSerializationSuite struct { 95 SliceSerializationSuite 96 } 97 98 var _ = gc.Suite(&EndpointSerializationSuite{}) 99 100 func (s *EndpointSerializationSuite) SetUpTest(c *gc.C) { 101 s.SliceSerializationSuite.SetUpTest(c) 102 s.importName = "endpoints" 103 s.sliceName = "endpoints" 104 s.importFunc = func(m map[string]interface{}) (interface{}, error) { 105 return importEndpoints(m) 106 } 107 s.testFields = func(m map[string]interface{}) { 108 m["endpoints"] = []interface{}{} 109 } 110 } 111 112 func minimalEndpointMap() map[interface{}]interface{} { 113 return map[interface{}]interface{}{ 114 "application-name": "ubuntu", 115 "name": "juju-meta", 116 "role": "peer", 117 "interface": "something", 118 "optional": true, 119 "limit": 1, 120 "scope": "container", 121 "unit-settings": map[interface{}]interface{}{}, 122 } 123 } 124 125 func minimalEndpoint() *endpoint { 126 return newEndpoint(minimalEndpointArgs()) 127 } 128 129 func minimalEndpointArgs() EndpointArgs { 130 return EndpointArgs{ 131 ApplicationName: "ubuntu", 132 Name: "juju-meta", 133 Role: "peer", 134 Interface: "something", 135 Optional: true, 136 Limit: 1, 137 Scope: "container", 138 } 139 } 140 141 func endpointWithSettings() *endpoint { 142 endpoint := minimalEndpoint() 143 u1Settings := map[string]interface{}{ 144 "name": "unit one", 145 "key": 42, 146 } 147 u2Settings := map[string]interface{}{ 148 "name": "unit two", 149 "foo": "bar", 150 } 151 endpoint.SetUnitSettings("ubuntu/0", u1Settings) 152 endpoint.SetUnitSettings("ubuntu/1", u2Settings) 153 return endpoint 154 } 155 156 func (s *EndpointSerializationSuite) TestNewEndpoint(c *gc.C) { 157 endpoint := endpointWithSettings() 158 159 c.Assert(endpoint.ApplicationName(), gc.Equals, "ubuntu") 160 c.Assert(endpoint.Name(), gc.Equals, "juju-meta") 161 c.Assert(endpoint.Role(), gc.Equals, "peer") 162 c.Assert(endpoint.Interface(), gc.Equals, "something") 163 c.Assert(endpoint.Optional(), jc.IsTrue) 164 c.Assert(endpoint.Limit(), gc.Equals, 1) 165 c.Assert(endpoint.Scope(), gc.Equals, "container") 166 c.Assert(endpoint.UnitCount(), gc.Equals, 2) 167 c.Assert(endpoint.Settings("ubuntu/0"), jc.DeepEquals, map[string]interface{}{ 168 "name": "unit one", 169 "key": 42, 170 }) 171 c.Assert(endpoint.Settings("ubuntu/1"), jc.DeepEquals, map[string]interface{}{ 172 "name": "unit two", 173 "foo": "bar", 174 }) 175 } 176 177 func (s *EndpointSerializationSuite) TestMinimalMatches(c *gc.C) { 178 bytes, err := yaml.Marshal(minimalEndpoint()) 179 c.Assert(err, jc.ErrorIsNil) 180 181 var source map[interface{}]interface{} 182 err = yaml.Unmarshal(bytes, &source) 183 c.Assert(err, jc.ErrorIsNil) 184 c.Assert(source, jc.DeepEquals, minimalEndpointMap()) 185 } 186 187 func (s *EndpointSerializationSuite) TestParsingSerializedData(c *gc.C) { 188 initial := endpoints{ 189 Version: 1, 190 Endpoints_: []*endpoint{endpointWithSettings()}, 191 } 192 193 bytes, err := yaml.Marshal(initial) 194 c.Assert(err, jc.ErrorIsNil) 195 196 var source map[string]interface{} 197 err = yaml.Unmarshal(bytes, &source) 198 c.Assert(err, jc.ErrorIsNil) 199 200 endpoints, err := importEndpoints(source) 201 c.Assert(err, jc.ErrorIsNil) 202 203 c.Assert(endpoints, jc.DeepEquals, initial.Endpoints_) 204 }