github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/agent/caasoperator/operator_test.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package caasoperator_test 5 6 import ( 7 "github.com/juju/errors" 8 jc "github.com/juju/testing/checkers" 9 "github.com/juju/version" 10 gc "gopkg.in/check.v1" 11 "gopkg.in/juju/names.v2" 12 "gopkg.in/juju/worker.v1/workertest" 13 14 "github.com/juju/juju/apiserver/common" 15 "github.com/juju/juju/apiserver/facades/agent/caasoperator" 16 "github.com/juju/juju/apiserver/params" 17 apiservertesting "github.com/juju/juju/apiserver/testing" 18 "github.com/juju/juju/core/status" 19 coretesting "github.com/juju/juju/testing" 20 ) 21 22 var _ = gc.Suite(&CAASOperatorSuite{}) 23 24 type CAASOperatorSuite struct { 25 coretesting.BaseSuite 26 27 resources *common.Resources 28 authorizer *apiservertesting.FakeAuthorizer 29 facade *caasoperator.Facade 30 st *mockState 31 } 32 33 func (s *CAASOperatorSuite) SetUpTest(c *gc.C) { 34 s.BaseSuite.SetUpTest(c) 35 36 s.resources = common.NewResources() 37 s.AddCleanup(func(_ *gc.C) { s.resources.StopAll() }) 38 39 s.authorizer = &apiservertesting.FakeAuthorizer{ 40 Tag: names.NewApplicationTag("gitlab"), 41 } 42 43 s.st = newMockState() 44 s.AddCleanup(func(c *gc.C) { 45 workertest.CleanKill(c, s.st.app.unitsWatcher) 46 }) 47 48 facade, err := caasoperator.NewFacade(s.resources, s.authorizer, s.st) 49 c.Assert(err, jc.ErrorIsNil) 50 s.facade = facade 51 } 52 53 func (s *CAASOperatorSuite) TestPermission(c *gc.C) { 54 s.authorizer = &apiservertesting.FakeAuthorizer{ 55 Tag: names.NewMachineTag("0"), 56 } 57 _, err := caasoperator.NewFacade(s.resources, s.authorizer, s.st) 58 c.Assert(err, gc.ErrorMatches, "permission denied") 59 } 60 61 func (s *CAASOperatorSuite) TestSetStatus(c *gc.C) { 62 args := params.SetStatus{ 63 Entities: []params.EntityStatusArgs{{ 64 Tag: "application-gitlab", 65 Status: "bar", 66 Info: "baz", 67 Data: map[string]interface{}{ 68 "qux": "quux", 69 }, 70 }, { 71 Tag: "machine-0", 72 Status: "nope", 73 }}, 74 } 75 76 results, err := s.facade.SetStatus(args) 77 c.Assert(err, jc.ErrorIsNil) 78 c.Assert(results, jc.DeepEquals, params.ErrorResults{ 79 Results: []params.ErrorResult{ 80 {}, 81 {¶ms.Error{Message: `"machine-0" is not a valid application tag`}}, 82 }, 83 }) 84 85 s.st.CheckCallNames(c, "Model", "Application") 86 s.st.CheckCall(c, 1, "Application", "gitlab") 87 s.st.app.CheckCallNames(c, "SetOperatorStatus") 88 s.st.app.CheckCall(c, 0, "SetOperatorStatus", status.StatusInfo{ 89 Status: "bar", 90 Message: "baz", 91 Data: map[string]interface{}{ 92 "qux": "quux", 93 }, 94 }) 95 } 96 97 func (s *CAASOperatorSuite) TestCharm(c *gc.C) { 98 args := params.Entities{ 99 Entities: []params.Entity{ 100 {Tag: "application-gitlab"}, 101 {Tag: "application-other"}, 102 {Tag: "machine-0"}, 103 }, 104 } 105 106 results, err := s.facade.Charm(args) 107 c.Assert(err, jc.ErrorIsNil) 108 c.Assert(results, jc.DeepEquals, params.ApplicationCharmResults{ 109 Results: []params.ApplicationCharmResult{{ 110 Result: ¶ms.ApplicationCharm{ 111 URL: "cs:gitlab-1", 112 ForceUpgrade: false, 113 SHA256: "fake-sha256", 114 CharmModifiedVersion: 666, 115 }, 116 }, { 117 Error: ¶ms.Error{ 118 Code: "unauthorized access", 119 Message: "permission denied", 120 }, 121 }, { 122 Error: ¶ms.Error{Message: `"machine-0" is not a valid application tag`}, 123 }}, 124 }) 125 126 s.st.CheckCallNames(c, "Model", "Application") 127 s.st.CheckCall(c, 1, "Application", "gitlab") 128 s.st.app.CheckCallNames(c, "Charm", "CharmModifiedVersion") 129 } 130 131 func (s *CAASOperatorSuite) TestWatchUnits(c *gc.C) { 132 s.st.app.unitsChanges <- []string{"gitlab/0", "gitlab/1"} 133 134 results, err := s.facade.WatchUnits(params.Entities{ 135 Entities: []params.Entity{ 136 {Tag: "application-gitlab"}, 137 {Tag: "unit-gitlab-0"}, 138 }, 139 }) 140 c.Assert(err, jc.ErrorIsNil) 141 c.Assert(results.Results, gc.HasLen, 2) 142 c.Assert(results.Results[0].Error, gc.IsNil) 143 c.Assert(results.Results[1].Error, jc.DeepEquals, ¶ms.Error{ 144 Message: `"unit-gitlab-0" is not a valid application tag`, 145 }) 146 147 c.Assert(results.Results[0].StringsWatcherId, gc.Equals, "1") 148 c.Assert(results.Results[0].Changes, jc.DeepEquals, []string{"gitlab/0", "gitlab/1"}) 149 resource := s.resources.Get("1") 150 c.Assert(resource, gc.Equals, s.st.app.unitsWatcher) 151 } 152 153 func (s *CAASOperatorSuite) TestLife(c *gc.C) { 154 results, err := s.facade.Life(params.Entities{ 155 Entities: []params.Entity{ 156 {Tag: "unit-gitlab-0"}, 157 {Tag: "application-gitlab"}, 158 {Tag: "machine-0"}, 159 }, 160 }) 161 c.Assert(err, jc.ErrorIsNil) 162 c.Assert(results, jc.DeepEquals, params.LifeResults{ 163 Results: []params.LifeResult{{ 164 Life: params.Dying, 165 }, { 166 Life: params.Alive, 167 }, { 168 Error: ¶ms.Error{ 169 Code: "unauthorized access", 170 Message: "permission denied", 171 }, 172 }}, 173 }) 174 } 175 176 func (s *CAASOperatorSuite) TestRemove(c *gc.C) { 177 results, err := s.facade.Remove(params.Entities{ 178 Entities: []params.Entity{ 179 {Tag: "unit-gitlab-0"}, 180 {Tag: "machine-0"}, 181 {Tag: "unit-mysql-0"}, 182 }, 183 }) 184 c.Assert(err, jc.ErrorIsNil) 185 c.Assert(results, jc.DeepEquals, params.ErrorResults{ 186 Results: []params.ErrorResult{ 187 {}, 188 { 189 Error: ¶ms.Error{ 190 Code: "unauthorized access", 191 Message: "permission denied", 192 }, 193 }, 194 { 195 Error: ¶ms.Error{ 196 Code: "unauthorized access", 197 Message: "permission denied", 198 }, 199 }}, 200 }) 201 } 202 203 func (s *CAASOperatorSuite) TestSetPodSpec(c *gc.C) { 204 validSpecStr := ` 205 containers: 206 - name: gitlab 207 image: gitlab/latest 208 `[1:] 209 210 args := params.SetPodSpecParams{ 211 Specs: []params.EntityString{ 212 {Tag: "application-gitlab", Value: validSpecStr}, 213 {Tag: "application-gitlab", Value: validSpecStr}, 214 {Tag: "application-gitlab", Value: "bad spec"}, 215 {Tag: "unit-gitlab-0"}, 216 {Tag: "application-other"}, 217 {Tag: "unit-other-0"}, 218 {Tag: "machine-0"}, 219 }, 220 } 221 222 s.st.model.SetErrors(nil, errors.New("bloop")) 223 224 results, err := s.facade.SetPodSpec(args) 225 c.Assert(err, jc.ErrorIsNil) 226 c.Assert(results, jc.DeepEquals, params.ErrorResults{ 227 Results: []params.ErrorResult{{ 228 Error: nil, 229 }, { 230 Error: ¶ms.Error{ 231 Message: "bloop", 232 }, 233 }, { 234 Error: ¶ms.Error{ 235 Message: "invalid pod spec", 236 }, 237 }, { 238 Error: ¶ms.Error{ 239 Code: "unauthorized access", 240 Message: "permission denied", 241 }, 242 }, { 243 Error: ¶ms.Error{ 244 Code: "unauthorized access", 245 Message: "permission denied", 246 }, 247 }, { 248 Error: ¶ms.Error{ 249 Code: "unauthorized access", 250 Message: "permission denied", 251 }, 252 }, { 253 Error: ¶ms.Error{ 254 Code: "unauthorized access", 255 Message: "permission denied", 256 }, 257 }}, 258 }) 259 260 s.st.CheckCallNames(c, "Model") 261 s.st.model.CheckCallNames(c, "SetPodSpec", "SetPodSpec") 262 s.st.model.CheckCall(c, 0, "SetPodSpec", names.NewApplicationTag("gitlab"), validSpecStr) 263 } 264 265 func (s *CAASOperatorSuite) TestModel(c *gc.C) { 266 result, err := s.facade.CurrentModel() 267 c.Assert(err, jc.ErrorIsNil) 268 c.Assert(result, jc.DeepEquals, params.ModelResult{ 269 Name: "some-model", 270 UUID: "deadbeef", 271 Type: "iaas", 272 }) 273 } 274 275 func (s *CAASOperatorSuite) TestWatch(c *gc.C) { 276 s.st.app.appChanges <- struct{}{} 277 278 c.Assert(s.resources.Count(), gc.Equals, 0) 279 280 args := params.Entities{Entities: []params.Entity{ 281 {Tag: "application-gitlab"}, 282 {Tag: "application-mysql"}, 283 {Tag: "unit-mysql-0"}, 284 }} 285 result, err := s.facade.Watch(args) 286 c.Assert(err, jc.ErrorIsNil) 287 c.Assert(result, gc.DeepEquals, params.NotifyWatchResults{ 288 Results: []params.NotifyWatchResult{ 289 {NotifyWatcherId: "1"}, 290 {Error: apiservertesting.NotFoundError("application mysql")}, 291 {Error: apiservertesting.NotFoundError("unit mysql/0")}, 292 }, 293 }) 294 295 // Verify the resource was registered and stop when done 296 c.Assert(s.resources.Count(), gc.Equals, 1) 297 c.Assert(result.Results[0].NotifyWatcherId, gc.Equals, "1") 298 resource := s.resources.Get("1") 299 c.Assert(resource, gc.Equals, s.st.app.watcher) 300 } 301 302 func (s *CAASOperatorSuite) TestSetTools(c *gc.C) { 303 vers := version.MustParseBinary("2.99.0-bionic-amd64") 304 results, err := s.facade.SetTools(params.EntitiesVersion{ 305 AgentTools: []params.EntityVersion{ 306 {Tag: "application-gitlab", Tools: ¶ms.Version{Version: vers}}, 307 {Tag: "machine-0", Tools: ¶ms.Version{Version: vers}}, 308 }, 309 }) 310 c.Assert(err, jc.ErrorIsNil) 311 c.Assert(results, jc.DeepEquals, params.ErrorResults{ 312 Results: []params.ErrorResult{ 313 {}, 314 { 315 Error: ¶ms.Error{ 316 Code: "unauthorized access", 317 Message: "permission denied", 318 }, 319 }}, 320 }) 321 s.st.app.CheckCall(c, 0, "SetAgentVersion", vers) 322 } 323 324 func (s *CAASOperatorSuite) TestAddresses(c *gc.C) { 325 _, err := s.facade.APIAddresses() 326 c.Assert(err, jc.ErrorIsNil) 327 s.st.CheckCallNames(c, "Model", "APIHostPortsForAgents") 328 } 329 330 func (s *CAASOperatorSuite) TestWatchAPIHostPorts(c *gc.C) { 331 _, err := s.facade.WatchAPIHostPorts() 332 c.Assert(err, jc.ErrorIsNil) 333 s.st.CheckCallNames(c, "Model", "WatchAPIHostPortsForAgents") 334 }