github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/apiserver/application/application_unit_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package application_test 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/testing" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 "gopkg.in/juju/charm.v6-unstable" 12 "gopkg.in/juju/names.v2" 13 14 "github.com/juju/juju/apiserver/application" 15 "github.com/juju/juju/apiserver/params" 16 apiservertesting "github.com/juju/juju/apiserver/testing" 17 "github.com/juju/juju/state" 18 coretesting "github.com/juju/juju/testing" 19 ) 20 21 type ApplicationSuite struct { 22 testing.IsolationSuite 23 backend mockBackend 24 application mockApplication 25 charm mockCharm 26 27 blockChecker mockBlockChecker 28 authorizer apiservertesting.FakeAuthorizer 29 api *application.API 30 } 31 32 var _ = gc.Suite(&ApplicationSuite{}) 33 34 func (s *ApplicationSuite) SetUpTest(c *gc.C) { 35 s.IsolationSuite.SetUpTest(c) 36 s.authorizer = apiservertesting.FakeAuthorizer{ 37 Tag: names.NewUserTag("admin"), 38 } 39 s.application = mockApplication{} 40 s.charm = mockCharm{ 41 config: &charm.Config{ 42 Options: map[string]charm.Option{ 43 "stringOption": {Type: "string"}, 44 "intOption": {Type: "int", Default: int(123)}, 45 }, 46 }, 47 } 48 s.backend = mockBackend{ 49 application: &s.application, 50 charm: &s.charm, 51 } 52 s.blockChecker = mockBlockChecker{} 53 api, err := application.NewAPI( 54 &s.backend, 55 s.authorizer, 56 &s.blockChecker, 57 func(application.Charm) *state.Charm { 58 return &state.Charm{} 59 }, 60 ) 61 c.Assert(err, jc.ErrorIsNil) 62 s.api = api 63 } 64 65 func (s *ApplicationSuite) TestSetCharmStorageConstraints(c *gc.C) { 66 toUint64Ptr := func(v uint64) *uint64 { 67 return &v 68 } 69 err := s.api.SetCharm(params.ApplicationSetCharm{ 70 ApplicationName: "postgresql", 71 CharmURL: "cs:postgresql", 72 StorageConstraints: map[string]params.StorageConstraints{ 73 "a": {}, 74 "b": {Pool: "radiant"}, 75 "c": {Size: toUint64Ptr(123)}, 76 "d": {Count: toUint64Ptr(456)}, 77 }, 78 }) 79 c.Assert(err, jc.ErrorIsNil) 80 s.backend.CheckCallNames(c, "ModelTag", "Application", "Charm") 81 s.application.CheckCallNames(c, "SetCharm") 82 s.application.CheckCall(c, 0, "SetCharm", state.SetCharmConfig{ 83 Charm: &state.Charm{}, 84 StorageConstraints: map[string]state.StorageConstraints{ 85 "a": {}, 86 "b": {Pool: "radiant"}, 87 "c": {Size: 123}, 88 "d": {Count: 456}, 89 }, 90 }) 91 } 92 93 func (s *ApplicationSuite) TestSetCharmConfigSettings(c *gc.C) { 94 err := s.api.SetCharm(params.ApplicationSetCharm{ 95 ApplicationName: "postgresql", 96 CharmURL: "cs:postgresql", 97 ConfigSettings: map[string]string{"stringOption": "value"}, 98 }) 99 c.Assert(err, jc.ErrorIsNil) 100 s.backend.CheckCallNames(c, "ModelTag", "Application", "Charm") 101 s.charm.CheckCallNames(c, "Config") 102 s.application.CheckCallNames(c, "SetCharm") 103 s.application.CheckCall(c, 0, "SetCharm", state.SetCharmConfig{ 104 Charm: &state.Charm{}, 105 ConfigSettings: charm.Settings{"stringOption": "value"}, 106 }) 107 } 108 109 func (s *ApplicationSuite) TestSetCharmConfigSettingsYAML(c *gc.C) { 110 err := s.api.SetCharm(params.ApplicationSetCharm{ 111 ApplicationName: "postgresql", 112 CharmURL: "cs:postgresql", 113 ConfigSettingsYAML: ` 114 postgresql: 115 stringOption: value 116 `, 117 }) 118 c.Assert(err, jc.ErrorIsNil) 119 s.backend.CheckCallNames(c, "ModelTag", "Application", "Charm") 120 s.charm.CheckCallNames(c, "Config") 121 s.application.CheckCallNames(c, "SetCharm") 122 s.application.CheckCall(c, 0, "SetCharm", state.SetCharmConfig{ 123 Charm: &state.Charm{}, 124 ConfigSettings: charm.Settings{"stringOption": "value"}, 125 }) 126 } 127 128 type mockBackend struct { 129 application.Backend 130 testing.Stub 131 application *mockApplication 132 charm *mockCharm 133 } 134 135 func (b *mockBackend) ModelTag() names.ModelTag { 136 b.MethodCall(b, "ModelTag") 137 b.PopNoErr() 138 return coretesting.ModelTag 139 } 140 141 func (b *mockBackend) Application(name string) (application.Application, error) { 142 b.MethodCall(b, "Application", name) 143 if err := b.NextErr(); err != nil { 144 return nil, err 145 } 146 if b.application != nil { 147 return b.application, nil 148 } 149 return nil, errors.NotFoundf("application %q", name) 150 } 151 152 func (b *mockBackend) Charm(curl *charm.URL) (application.Charm, error) { 153 b.MethodCall(b, "Charm", curl) 154 if err := b.NextErr(); err != nil { 155 return nil, err 156 } 157 if b.charm != nil { 158 return b.charm, nil 159 } 160 return nil, errors.NotFoundf("charm %q", curl) 161 } 162 163 type mockApplication struct { 164 application.Application 165 testing.Stub 166 } 167 168 func (a *mockApplication) SetCharm(cfg state.SetCharmConfig) error { 169 a.MethodCall(a, "SetCharm", cfg) 170 return a.NextErr() 171 } 172 173 type mockCharm struct { 174 application.Charm 175 testing.Stub 176 config *charm.Config 177 } 178 179 func (c *mockCharm) Config() *charm.Config { 180 c.MethodCall(c, "Config") 181 c.PopNoErr() 182 return c.config 183 } 184 185 type mockBlockChecker struct { 186 testing.Stub 187 } 188 189 func (c *mockBlockChecker) ChangeAllowed() error { 190 c.MethodCall(c, "ChangeAllowed") 191 return c.NextErr() 192 } 193 194 func (c *mockBlockChecker) RemoveAllowed() error { 195 c.MethodCall(c, "RemoveAllowed") 196 return c.NextErr() 197 }