1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
4
package application_test
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"
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"
21
type ApplicationSuite struct {
22
testing.IsolationSuite
24
application mockApplication
27
blockChecker mockBlockChecker
28
authorizer apiservertesting.FakeAuthorizer
32
var _ = gc.Suite(&ApplicationSuite{})
34
func (s *ApplicationSuite) SetUpTest(c *gc.C) {
35
s.IsolationSuite.SetUpTest(c)
36
s.authorizer = apiservertesting.FakeAuthorizer{
37
Tag: names.NewUserTag("admin"),
39
s.application = mockApplication{}
41
config: &charm.Config{
42
Options: map[string]charm.Option{
43
"stringOption": {Type: "string"},
44
"intOption": {Type: "int", Default: int(123)},
48
s.backend = mockBackend{
49
application: &s.application,
52
s.blockChecker = mockBlockChecker{}
53
api, err := application.NewAPI(
57
func(application.Charm) *state.Charm {
61
c.Assert(err, jc.ErrorIsNil)
65
func (s *ApplicationSuite) TestSetCharmStorageConstraints(c *gc.C) {
66
toUint64Ptr := func(v uint64) *uint64 {
69
err := s.api.SetCharm(params.ApplicationSetCharm{
70
ApplicationName: "postgresql",
71
CharmUrl: "cs:postgresql",
72
StorageConstraints: map[string]params.StorageConstraints{
74
"b": {Pool: "radiant"},
75
"c": {Size: toUint64Ptr(123)},
76
"d": {Count: toUint64Ptr(456)},
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{
86
"b": {Pool: "radiant"},
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"},
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"},
109
func (s *ApplicationSuite) TestSetCharmConfigSettingsYAML(c *gc.C) {
110
err := s.api.SetCharm(params.ApplicationSetCharm{
111
ApplicationName: "postgresql",
112
CharmUrl: "cs:postgresql",
113
ConfigSettingsYAML: `
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"},
128
type mockBackend struct {
131
application *mockApplication
135
func (b *mockBackend) ModelTag() names.ModelTag {
136
b.MethodCall(b, "ModelTag")
138
return coretesting.ModelTag
141
func (b *mockBackend) Application(name string) (application.Application, error) {
142
b.MethodCall(b, "Application", name)
143
if err := b.NextErr(); err != nil {
146
if b.application != nil {
147
return b.application, nil
149
return nil, errors.NotFoundf("application %q", name)
152
func (b *mockBackend) Charm(curl *charm.URL) (application.Charm, error) {
153
b.MethodCall(b, "Charm", curl)
154
if err := b.NextErr(); err != nil {
160
return nil, errors.NotFoundf("charm %q", curl)
163
type mockApplication struct {
164
application.Application
168
func (a *mockApplication) SetCharm(cfg state.SetCharmConfig) error {
169
a.MethodCall(a, "SetCharm", cfg)
173
type mockCharm struct {
179
func (c *mockCharm) Config() *charm.Config {
180
c.MethodCall(c, "Config")
185
type mockBlockChecker struct {
189
func (c *mockBlockChecker) ChangeAllowed() error {
190
c.MethodCall(c, "ChangeAllowed")
194
func (c *mockBlockChecker) RemoveAllowed() error {
195
c.MethodCall(c, "RemoveAllowed")