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