github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/api/params/params_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package params_test 5 6 import ( 7 "encoding/json" 8 "testing" 9 10 gc "launchpad.net/gocheck" 11 12 "launchpad.net/juju-core/charm" 13 "launchpad.net/juju-core/constraints" 14 "launchpad.net/juju-core/instance" 15 "launchpad.net/juju-core/state" 16 "launchpad.net/juju-core/state/api/params" 17 ) 18 19 // TestPackage integrates the tests into gotest. 20 func TestPackage(t *testing.T) { 21 gc.TestingT(t) 22 } 23 24 type MarshalSuite struct{} 25 26 var _ = gc.Suite(&MarshalSuite{}) 27 28 var marshalTestCases = []struct { 29 about string 30 // Value holds a real Go struct. 31 value params.Delta 32 // JSON document. 33 json string 34 }{{ 35 about: "MachineInfo Delta", 36 value: params.Delta{ 37 Entity: ¶ms.MachineInfo{ 38 Id: "Benji", 39 InstanceId: "Shazam", 40 Status: "error", 41 StatusInfo: "foo", 42 }, 43 }, 44 json: `["machine","change",{"Id":"Benji","InstanceId":"Shazam","Status":"error","StatusInfo":"foo","StatusData":null}]`, 45 }, { 46 about: "ServiceInfo Delta", 47 value: params.Delta{ 48 Entity: ¶ms.ServiceInfo{ 49 Name: "Benji", 50 Exposed: true, 51 CharmURL: "cs:quantal/name", 52 Life: params.Life(state.Dying.String()), 53 OwnerTag: "test-owner", 54 MinUnits: 42, 55 Constraints: constraints.MustParse("arch=arm mem=1024M"), 56 Config: charm.Settings{ 57 "hello": "goodbye", 58 "foo": false, 59 }, 60 }, 61 }, 62 json: `["service","change",{"CharmURL": "cs:quantal/name","Name":"Benji","Exposed":true,"Life":"dying","OwnerTag":"test-owner","MinUnits":42,"Constraints":{"arch":"arm", "mem": 1024},"Config": {"hello":"goodbye","foo":false}}]`, 63 }, { 64 about: "UnitInfo Delta", 65 value: params.Delta{ 66 Entity: ¶ms.UnitInfo{ 67 Name: "Benji", 68 Service: "Shazam", 69 Series: "precise", 70 CharmURL: "cs:~user/precise/wordpress-42", 71 Ports: []instance.Port{ 72 { 73 Protocol: "http", 74 Number: 80}, 75 }, 76 PublicAddress: "testing.invalid", 77 PrivateAddress: "10.0.0.1", 78 MachineId: "1", 79 Status: "error", 80 StatusInfo: "foo", 81 }, 82 }, 83 json: `["unit", "change", {"CharmURL": "cs:~user/precise/wordpress-42", "MachineId": "1", "Series": "precise", "Name": "Benji", "PublicAddress": "testing.invalid", "Service": "Shazam", "PrivateAddress": "10.0.0.1", "Ports": [{"Protocol": "http", "Number": 80}], "Status": "error", "StatusInfo": "foo","StatusData":null}]`, 84 }, { 85 about: "RelationInfo Delta", 86 value: params.Delta{ 87 Entity: ¶ms.RelationInfo{ 88 Key: "Benji", 89 Id: 4711, 90 Endpoints: []params.Endpoint{ 91 {ServiceName: "logging", Relation: charm.Relation{Name: "logging-directory", Role: "requirer", Interface: "logging", Optional: false, Limit: 1, Scope: "container"}}, 92 {ServiceName: "wordpress", Relation: charm.Relation{Name: "logging-dir", Role: "provider", Interface: "logging", Optional: false, Limit: 0, Scope: "container"}}}, 93 }, 94 }, 95 json: `["relation","change",{"Key":"Benji", "Id": 4711, "Endpoints": [{"ServiceName":"logging", "Relation":{"Name":"logging-directory", "Role":"requirer", "Interface":"logging", "Optional":false, "Limit":1, "Scope":"container"}}, {"ServiceName":"wordpress", "Relation":{"Name":"logging-dir", "Role":"provider", "Interface":"logging", "Optional":false, "Limit":0, "Scope":"container"}}]}]`, 96 }, { 97 about: "AnnotationInfo Delta", 98 value: params.Delta{ 99 Entity: ¶ms.AnnotationInfo{ 100 Tag: "machine-0", 101 Annotations: map[string]string{ 102 "foo": "bar", 103 "arble": "2 4", 104 }, 105 }, 106 }, 107 json: `["annotation","change",{"Tag":"machine-0","Annotations":{"foo":"bar","arble":"2 4"}}]`, 108 }, { 109 about: "Delta Removed True", 110 value: params.Delta{ 111 Removed: true, 112 Entity: ¶ms.RelationInfo{ 113 Key: "Benji", 114 }, 115 }, 116 json: `["relation","remove",{"Key":"Benji", "Id": 0, "Endpoints": null}]`, 117 }} 118 119 func (s *MarshalSuite) TestDeltaMarshalJSON(c *gc.C) { 120 for _, t := range marshalTestCases { 121 c.Log(t.about) 122 output, err := t.value.MarshalJSON() 123 c.Check(err, gc.IsNil) 124 // We check unmarshalled output both to reduce the fragility of the 125 // tests (because ordering in the maps can change) and to verify that 126 // the output is well-formed. 127 var unmarshalledOutput interface{} 128 err = json.Unmarshal(output, &unmarshalledOutput) 129 c.Check(err, gc.IsNil) 130 var expected interface{} 131 err = json.Unmarshal([]byte(t.json), &expected) 132 c.Check(err, gc.IsNil) 133 c.Check(unmarshalledOutput, gc.DeepEquals, expected) 134 } 135 } 136 137 func (s *MarshalSuite) TestDeltaUnmarshalJSON(c *gc.C) { 138 for i, t := range marshalTestCases { 139 c.Logf("test %d. %s", i, t.about) 140 var unmarshalled params.Delta 141 err := json.Unmarshal([]byte(t.json), &unmarshalled) 142 c.Check(err, gc.IsNil) 143 c.Check(unmarshalled, gc.DeepEquals, t.value) 144 } 145 } 146 147 func (s *MarshalSuite) TestDeltaMarshalJSONCardinality(c *gc.C) { 148 err := json.Unmarshal([]byte(`[1,2]`), new(params.Delta)) 149 c.Check(err, gc.ErrorMatches, "Expected 3 elements in top-level of JSON but got 2") 150 } 151 152 func (s *MarshalSuite) TestDeltaMarshalJSONUnknownOperation(c *gc.C) { 153 err := json.Unmarshal([]byte(`["relation","masticate",{}]`), new(params.Delta)) 154 c.Check(err, gc.ErrorMatches, `Unexpected operation "masticate"`) 155 } 156 157 func (s *MarshalSuite) TestDeltaMarshalJSONUnknownEntity(c *gc.C) { 158 err := json.Unmarshal([]byte(`["qwan","change",{}]`), new(params.Delta)) 159 c.Check(err, gc.ErrorMatches, `Unexpected entity name "qwan"`) 160 }