github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/application/client_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package application_test 5 6 import ( 7 jc "github.com/juju/testing/checkers" 8 gc "gopkg.in/check.v1" 9 "gopkg.in/juju/charm.v6-unstable" 10 11 "github.com/juju/juju/api/application" 12 "github.com/juju/juju/apiserver/common" 13 "github.com/juju/juju/apiserver/params" 14 "github.com/juju/juju/charmstore" 15 "github.com/juju/juju/constraints" 16 "github.com/juju/juju/instance" 17 jujutesting "github.com/juju/juju/juju/testing" 18 "github.com/juju/juju/storage" 19 ) 20 21 type serviceSuite struct { 22 jujutesting.JujuConnSuite 23 24 client *application.Client 25 } 26 27 var _ = gc.Suite(&serviceSuite{}) 28 29 func (s *serviceSuite) SetUpTest(c *gc.C) { 30 s.JujuConnSuite.SetUpTest(c) 31 s.client = application.NewClient(s.APIState) 32 c.Assert(s.client, gc.NotNil) 33 } 34 35 func (s *serviceSuite) TestSetServiceMetricCredentials(c *gc.C) { 36 var called bool 37 application.PatchFacadeCall(s, s.client, func(request string, a, response interface{}) error { 38 called = true 39 c.Assert(request, gc.Equals, "SetMetricCredentials") 40 args, ok := a.(params.ApplicationMetricCredentials) 41 c.Assert(ok, jc.IsTrue) 42 c.Assert(args.Creds, gc.HasLen, 1) 43 c.Assert(args.Creds[0].ApplicationName, gc.Equals, "serviceA") 44 c.Assert(args.Creds[0].MetricCredentials, gc.DeepEquals, []byte("creds 1")) 45 46 result := response.(*params.ErrorResults) 47 result.Results = make([]params.ErrorResult, 1) 48 return nil 49 }) 50 err := s.client.SetMetricCredentials("serviceA", []byte("creds 1")) 51 c.Assert(err, jc.ErrorIsNil) 52 c.Assert(called, jc.IsTrue) 53 } 54 55 func (s *serviceSuite) TestSetServiceMetricCredentialsFails(c *gc.C) { 56 var called bool 57 application.PatchFacadeCall(s, s.client, func(request string, args, response interface{}) error { 58 called = true 59 c.Assert(request, gc.Equals, "SetMetricCredentials") 60 result := response.(*params.ErrorResults) 61 result.Results = make([]params.ErrorResult, 1) 62 result.Results[0].Error = common.ServerError(common.ErrPerm) 63 return result.OneError() 64 }) 65 err := s.client.SetMetricCredentials("application", []byte("creds")) 66 c.Assert(err, gc.ErrorMatches, "permission denied") 67 c.Assert(called, jc.IsTrue) 68 } 69 70 func (s *serviceSuite) TestSetServiceMetricCredentialsNoMocks(c *gc.C) { 71 application := s.Factory.MakeApplication(c, nil) 72 err := s.client.SetMetricCredentials(application.Name(), []byte("creds")) 73 c.Assert(err, jc.ErrorIsNil) 74 err = application.Refresh() 75 c.Assert(err, jc.ErrorIsNil) 76 c.Assert(application.MetricCredentials(), gc.DeepEquals, []byte("creds")) 77 } 78 79 func (s *serviceSuite) TestSetServiceDeploy(c *gc.C) { 80 var called bool 81 application.PatchFacadeCall(s, s.client, func(request string, a, response interface{}) error { 82 called = true 83 c.Assert(request, gc.Equals, "Deploy") 84 args, ok := a.(params.ApplicationsDeploy) 85 c.Assert(ok, jc.IsTrue) 86 c.Assert(args.Applications, gc.HasLen, 1) 87 c.Assert(args.Applications[0].CharmURL, gc.Equals, "cs:trusty/a-charm-1") 88 c.Assert(args.Applications[0].ApplicationName, gc.Equals, "serviceA") 89 c.Assert(args.Applications[0].Series, gc.Equals, "series") 90 c.Assert(args.Applications[0].NumUnits, gc.Equals, 2) 91 c.Assert(args.Applications[0].ConfigYAML, gc.Equals, "configYAML") 92 c.Assert(args.Applications[0].Constraints, gc.DeepEquals, constraints.MustParse("mem=4G")) 93 c.Assert(args.Applications[0].Placement, gc.DeepEquals, []*instance.Placement{{"scope", "directive"}}) 94 c.Assert(args.Applications[0].EndpointBindings, gc.DeepEquals, map[string]string{"foo": "bar"}) 95 c.Assert(args.Applications[0].Storage, gc.DeepEquals, map[string]storage.Constraints{"data": storage.Constraints{Pool: "pool"}}) 96 c.Assert(args.Applications[0].Resources, gc.DeepEquals, map[string]string{"foo": "bar"}) 97 98 result := response.(*params.ErrorResults) 99 result.Results = make([]params.ErrorResult, 1) 100 return nil 101 }) 102 103 args := application.DeployArgs{ 104 CharmID: charmstore.CharmID{ 105 URL: charm.MustParseURL("trusty/a-charm-1"), 106 }, 107 ApplicationName: "serviceA", 108 Series: "series", 109 NumUnits: 2, 110 ConfigYAML: "configYAML", 111 Cons: constraints.MustParse("mem=4G"), 112 Placement: []*instance.Placement{{"scope", "directive"}}, 113 Storage: map[string]storage.Constraints{"data": storage.Constraints{Pool: "pool"}}, 114 Resources: map[string]string{"foo": "bar"}, 115 EndpointBindings: map[string]string{"foo": "bar"}, 116 } 117 err := s.client.Deploy(args) 118 c.Assert(err, jc.ErrorIsNil) 119 c.Assert(called, jc.IsTrue) 120 } 121 122 func (s *serviceSuite) TestServiceGetCharmURL(c *gc.C) { 123 var called bool 124 application.PatchFacadeCall(s, s.client, func(request string, a, response interface{}) error { 125 called = true 126 c.Assert(request, gc.Equals, "GetCharmURL") 127 args, ok := a.(params.ApplicationGet) 128 c.Assert(ok, jc.IsTrue) 129 c.Assert(args.ApplicationName, gc.Equals, "application") 130 131 result := response.(*params.StringResult) 132 result.Result = "curl" 133 return nil 134 }) 135 curl, err := s.client.GetCharmURL("application") 136 c.Assert(err, jc.ErrorIsNil) 137 c.Assert(curl, gc.DeepEquals, charm.MustParseURL("curl")) 138 c.Assert(called, jc.IsTrue) 139 } 140 141 func (s *serviceSuite) TestServiceSetCharm(c *gc.C) { 142 var called bool 143 toUint64Ptr := func(v uint64) *uint64 { 144 return &v 145 } 146 application.PatchFacadeCall(s, s.client, func(request string, a, response interface{}) error { 147 called = true 148 c.Assert(request, gc.Equals, "SetCharm") 149 args, ok := a.(params.ApplicationSetCharm) 150 c.Assert(ok, jc.IsTrue) 151 c.Assert(args.ApplicationName, gc.Equals, "application") 152 c.Assert(args.CharmURL, gc.Equals, "cs:trusty/application-1") 153 c.Assert(args.ConfigSettings, jc.DeepEquals, map[string]string{ 154 "a": "b", 155 "c": "d", 156 }) 157 c.Assert(args.ConfigSettingsYAML, gc.Equals, "yaml") 158 c.Assert(args.ForceSeries, gc.Equals, true) 159 c.Assert(args.ForceUnits, gc.Equals, true) 160 c.Assert(args.StorageConstraints, jc.DeepEquals, map[string]params.StorageConstraints{ 161 "a": {Pool: "radiant"}, 162 "b": {Count: toUint64Ptr(123)}, 163 "c": {Size: toUint64Ptr(123)}, 164 }) 165 166 return nil 167 }) 168 cfg := application.SetCharmConfig{ 169 ApplicationName: "application", 170 CharmID: charmstore.CharmID{ 171 URL: charm.MustParseURL("trusty/application-1"), 172 }, 173 ConfigSettings: map[string]string{ 174 "a": "b", 175 "c": "d", 176 }, 177 ConfigSettingsYAML: "yaml", 178 ForceSeries: true, 179 ForceUnits: true, 180 StorageConstraints: map[string]storage.Constraints{ 181 "a": {Pool: "radiant"}, 182 "b": {Count: 123}, 183 "c": {Size: 123}, 184 }, 185 } 186 err := s.client.SetCharm(cfg) 187 c.Assert(err, jc.ErrorIsNil) 188 c.Assert(called, jc.IsTrue) 189 }