github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/apiserver/application/get_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package application_test 5 6 import ( 7 "fmt" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 "gopkg.in/juju/charm.v6-unstable" 12 13 apiapplication "github.com/juju/juju/api/application" 14 "github.com/juju/juju/apiserver/application" 15 "github.com/juju/juju/apiserver/common" 16 "github.com/juju/juju/apiserver/params" 17 apiservertesting "github.com/juju/juju/apiserver/testing" 18 "github.com/juju/juju/constraints" 19 jujutesting "github.com/juju/juju/juju/testing" 20 ) 21 22 type getSuite struct { 23 jujutesting.JujuConnSuite 24 25 serviceAPI *application.API 26 authorizer apiservertesting.FakeAuthorizer 27 } 28 29 var _ = gc.Suite(&getSuite{}) 30 31 func (s *getSuite) SetUpTest(c *gc.C) { 32 s.JujuConnSuite.SetUpTest(c) 33 34 s.authorizer = apiservertesting.FakeAuthorizer{ 35 Tag: s.AdminUserTag(c), 36 } 37 var err error 38 backend := application.NewStateBackend(s.State) 39 blockChecker := common.NewBlockChecker(s.State) 40 s.serviceAPI, err = application.NewAPI( 41 backend, s.authorizer, blockChecker, 42 application.CharmToStateCharm, 43 ) 44 c.Assert(err, jc.ErrorIsNil) 45 } 46 47 func (s *getSuite) TestClientServiceGetSmoketest(c *gc.C) { 48 s.AddTestingService(c, "wordpress", s.AddTestingCharm(c, "wordpress")) 49 results, err := s.serviceAPI.Get(params.ApplicationGet{"wordpress"}) 50 c.Assert(err, jc.ErrorIsNil) 51 c.Assert(results, gc.DeepEquals, params.ApplicationGetResults{ 52 Application: "wordpress", 53 Charm: "wordpress", 54 Config: map[string]interface{}{ 55 "blog-title": map[string]interface{}{ 56 "type": "string", 57 "value": "My Title", 58 "description": "A descriptive title used for the blog.", 59 "default": true, 60 }, 61 }, 62 Series: "quantal", 63 }) 64 } 65 66 func (s *getSuite) TestServiceGetUnknownService(c *gc.C) { 67 _, err := s.serviceAPI.Get(params.ApplicationGet{"unknown"}) 68 c.Assert(err, gc.ErrorMatches, `application "unknown" not found`) 69 } 70 71 var getTests = []struct { 72 about string 73 charm string 74 constraints string 75 config charm.Settings 76 expect params.ApplicationGetResults 77 }{{ 78 about: "deployed service", 79 charm: "dummy", 80 constraints: "mem=2G cpu-power=400", 81 config: charm.Settings{ 82 // Different from default. 83 "title": "Look To Windward", 84 // Same as default. 85 "username": "admin001", 86 // Use default (but there's no charm default) 87 "skill-level": nil, 88 // Outlook is left unset. 89 }, 90 expect: params.ApplicationGetResults{ 91 Config: map[string]interface{}{ 92 "title": map[string]interface{}{ 93 "description": "A descriptive title used for the application.", 94 "type": "string", 95 "value": "Look To Windward", 96 }, 97 "outlook": map[string]interface{}{ 98 "description": "No default outlook.", 99 "type": "string", 100 "default": true, 101 }, 102 "username": map[string]interface{}{ 103 "description": "The name of the initial account (given admin permissions).", 104 "type": "string", 105 "value": "admin001", 106 }, 107 "skill-level": map[string]interface{}{ 108 "description": "A number indicating skill.", 109 "type": "int", 110 "default": true, 111 }, 112 }, 113 Series: "quantal", 114 }, 115 }, { 116 about: "deployed service #2", 117 charm: "dummy", 118 config: charm.Settings{ 119 // Set title to default. 120 "title": nil, 121 // Value when there's a default. 122 "username": "foobie", 123 // Numeric value. 124 "skill-level": 0, 125 // String value. 126 "outlook": "phlegmatic", 127 }, 128 expect: params.ApplicationGetResults{ 129 Config: map[string]interface{}{ 130 "title": map[string]interface{}{ 131 "description": "A descriptive title used for the application.", 132 "type": "string", 133 "value": "My Title", 134 "default": true, 135 }, 136 "outlook": map[string]interface{}{ 137 "description": "No default outlook.", 138 "type": "string", 139 "value": "phlegmatic", 140 }, 141 "username": map[string]interface{}{ 142 "description": "The name of the initial account (given admin permissions).", 143 "type": "string", 144 "value": "foobie", 145 }, 146 "skill-level": map[string]interface{}{ 147 "description": "A number indicating skill.", 148 "type": "int", 149 // TODO(jam): 2013-08-28 bug #1217742 150 // we have to use float64() here, because the 151 // API does not preserve int types. This used 152 // to be int64() but we end up with a type 153 // mismatch when comparing the content 154 "value": float64(0), 155 }, 156 }, 157 Series: "quantal", 158 }, 159 }, { 160 about: "subordinate service", 161 charm: "logging", 162 expect: params.ApplicationGetResults{ 163 Config: map[string]interface{}{}, 164 Series: "quantal", 165 }, 166 }} 167 168 func (s *getSuite) TestServiceGet(c *gc.C) { 169 for i, t := range getTests { 170 c.Logf("test %d. %s", i, t.about) 171 ch := s.AddTestingCharm(c, t.charm) 172 svc := s.AddTestingService(c, fmt.Sprintf("test%d", i), ch) 173 174 var constraintsv constraints.Value 175 if t.constraints != "" { 176 constraintsv = constraints.MustParse(t.constraints) 177 err := svc.SetConstraints(constraintsv) 178 c.Assert(err, jc.ErrorIsNil) 179 } 180 if t.config != nil { 181 err := svc.UpdateConfigSettings(t.config) 182 c.Assert(err, jc.ErrorIsNil) 183 } 184 expect := t.expect 185 expect.Constraints = constraintsv 186 expect.Application = svc.Name() 187 expect.Charm = ch.Meta().Name 188 client := apiapplication.NewClient(s.APIState) 189 got, err := client.Get(svc.Name()) 190 c.Assert(err, jc.ErrorIsNil) 191 c.Assert(*got, gc.DeepEquals, expect) 192 } 193 } 194 195 func (s *getSuite) TestGetMaxResolutionInt(c *gc.C) { 196 // See the bug http://pad.lv/1217742 197 // Get ends up pushing a map[string]interface{} which containts 198 // an int64 through a JSON Marshal & Unmarshal which ends up changing 199 // the int64 into a float64. We will fix it if we find it is actually a 200 // problem. 201 const nonFloatInt = (int64(1) << 54) + 1 202 const asFloat = float64(nonFloatInt) 203 c.Assert(int64(asFloat), gc.Not(gc.Equals), nonFloatInt) 204 c.Assert(int64(asFloat)+1, gc.Equals, nonFloatInt) 205 206 ch := s.AddTestingCharm(c, "dummy") 207 svc := s.AddTestingService(c, "test-service", ch) 208 209 err := svc.UpdateConfigSettings(map[string]interface{}{"skill-level": nonFloatInt}) 210 c.Assert(err, jc.ErrorIsNil) 211 client := apiapplication.NewClient(s.APIState) 212 got, err := client.Get(svc.Name()) 213 c.Assert(err, jc.ErrorIsNil) 214 c.Assert(got.Config["skill-level"], jc.DeepEquals, map[string]interface{}{ 215 "description": "A number indicating skill.", 216 "type": "int", 217 "value": asFloat, 218 }) 219 }