github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/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 "github.com/juju/juju/charm" 13 "github.com/juju/juju/constraints" 14 "github.com/juju/juju/instance" 15 "github.com/juju/juju/state" 16 "github.com/juju/juju/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 Life: params.Alive, 43 Series: "trusty", 44 SupportedContainers: []instance.ContainerType{instance.LXC}, 45 Jobs: []params.MachineJob{state.JobManageEnviron.ToParams()}, 46 Addresses: []instance.Address{}, 47 HardwareCharacteristics: &instance.HardwareCharacteristics{}, 48 }, 49 }, 50 json: `["machine","change",{"Id":"Benji","InstanceId":"Shazam","Status":"error","StatusInfo":"foo","StatusData":null,"Life":"alive","Series":"trusty","SupportedContainers":["lxc"],"SupportedContainersKnown":false,"Jobs":["JobManageEnviron"],"Addresses":[],"HardwareCharacteristics":{}}]`, 51 }, { 52 about: "ServiceInfo Delta", 53 value: params.Delta{ 54 Entity: ¶ms.ServiceInfo{ 55 Name: "Benji", 56 Exposed: true, 57 CharmURL: "cs:quantal/name", 58 Life: params.Dying, 59 OwnerTag: "test-owner", 60 MinUnits: 42, 61 Constraints: constraints.MustParse("arch=armhf mem=1024M"), 62 Config: charm.Settings{ 63 "hello": "goodbye", 64 "foo": false, 65 }, 66 }, 67 }, 68 json: `["service","change",{"CharmURL": "cs:quantal/name","Name":"Benji","Exposed":true,"Life":"dying","OwnerTag":"test-owner","MinUnits":42,"Constraints":{"arch":"armhf", "mem": 1024},"Config": {"hello":"goodbye","foo":false}}]`, 69 }, { 70 about: "UnitInfo Delta", 71 value: params.Delta{ 72 Entity: ¶ms.UnitInfo{ 73 Name: "Benji", 74 Service: "Shazam", 75 Series: "precise", 76 CharmURL: "cs:~user/precise/wordpress-42", 77 Ports: []instance.Port{ 78 { 79 Protocol: "http", 80 Number: 80}, 81 }, 82 PublicAddress: "testing.invalid", 83 PrivateAddress: "10.0.0.1", 84 MachineId: "1", 85 Status: "error", 86 StatusInfo: "foo", 87 }, 88 }, 89 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}]`, 90 }, { 91 about: "RelationInfo Delta", 92 value: params.Delta{ 93 Entity: ¶ms.RelationInfo{ 94 Key: "Benji", 95 Id: 4711, 96 Endpoints: []params.Endpoint{ 97 {ServiceName: "logging", Relation: charm.Relation{Name: "logging-directory", Role: "requirer", Interface: "logging", Optional: false, Limit: 1, Scope: "container"}}, 98 {ServiceName: "wordpress", Relation: charm.Relation{Name: "logging-dir", Role: "provider", Interface: "logging", Optional: false, Limit: 0, Scope: "container"}}}, 99 }, 100 }, 101 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"}}]}]`, 102 }, { 103 about: "AnnotationInfo Delta", 104 value: params.Delta{ 105 Entity: ¶ms.AnnotationInfo{ 106 Tag: "machine-0", 107 Annotations: map[string]string{ 108 "foo": "bar", 109 "arble": "2 4", 110 }, 111 }, 112 }, 113 json: `["annotation","change",{"Tag":"machine-0","Annotations":{"foo":"bar","arble":"2 4"}}]`, 114 }, { 115 about: "Delta Removed True", 116 value: params.Delta{ 117 Removed: true, 118 Entity: ¶ms.RelationInfo{ 119 Key: "Benji", 120 }, 121 }, 122 json: `["relation","remove",{"Key":"Benji", "Id": 0, "Endpoints": null}]`, 123 }} 124 125 func (s *MarshalSuite) TestDeltaMarshalJSON(c *gc.C) { 126 for _, t := range marshalTestCases { 127 c.Log(t.about) 128 output, err := t.value.MarshalJSON() 129 c.Check(err, gc.IsNil) 130 // We check unmarshalled output both to reduce the fragility of the 131 // tests (because ordering in the maps can change) and to verify that 132 // the output is well-formed. 133 var unmarshalledOutput interface{} 134 err = json.Unmarshal(output, &unmarshalledOutput) 135 c.Check(err, gc.IsNil) 136 var expected interface{} 137 err = json.Unmarshal([]byte(t.json), &expected) 138 c.Check(err, gc.IsNil) 139 c.Check(unmarshalledOutput, gc.DeepEquals, expected) 140 } 141 } 142 143 func (s *MarshalSuite) TestDeltaUnmarshalJSON(c *gc.C) { 144 for i, t := range marshalTestCases { 145 c.Logf("test %d. %s", i, t.about) 146 var unmarshalled params.Delta 147 err := json.Unmarshal([]byte(t.json), &unmarshalled) 148 c.Check(err, gc.IsNil) 149 c.Check(unmarshalled, gc.DeepEquals, t.value) 150 } 151 } 152 153 func (s *MarshalSuite) TestDeltaMarshalJSONCardinality(c *gc.C) { 154 err := json.Unmarshal([]byte(`[1,2]`), new(params.Delta)) 155 c.Check(err, gc.ErrorMatches, "Expected 3 elements in top-level of JSON but got 2") 156 } 157 158 func (s *MarshalSuite) TestDeltaMarshalJSONUnknownOperation(c *gc.C) { 159 err := json.Unmarshal([]byte(`["relation","masticate",{}]`), new(params.Delta)) 160 c.Check(err, gc.ErrorMatches, `Unexpected operation "masticate"`) 161 } 162 163 func (s *MarshalSuite) TestDeltaMarshalJSONUnknownEntity(c *gc.C) { 164 err := json.Unmarshal([]byte(`["qwan","change",{}]`), new(params.Delta)) 165 c.Check(err, gc.ErrorMatches, `Unexpected entity name "qwan"`) 166 }