github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/controller/applicationscaler/util_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package applicationscaler_test 5 6 import ( 7 "github.com/juju/errors" 8 jc "github.com/juju/testing/checkers" 9 gc "gopkg.in/check.v1" 10 11 "github.com/juju/juju/apiserver/common" 12 "github.com/juju/juju/apiserver/facade" 13 "github.com/juju/juju/apiserver/facades/controller/applicationscaler" 14 "github.com/juju/juju/apiserver/params" 15 "github.com/juju/juju/state" 16 ) 17 18 // mockAuth implements facade.Authorizer for the tests' convenience. 19 type mockAuth struct { 20 facade.Authorizer 21 modelManager bool 22 } 23 24 func (mock mockAuth) AuthController() bool { 25 return mock.modelManager 26 } 27 28 // auth is a convenience constructor for a mockAuth. 29 func auth(modelManager bool) facade.Authorizer { 30 return mockAuth{modelManager: modelManager} 31 } 32 33 // mockWatcher implements state.StringsWatcher for the tests' convenience. 34 type mockWatcher struct { 35 state.StringsWatcher 36 working bool 37 } 38 39 func (mock *mockWatcher) Changes() <-chan []string { 40 ch := make(chan []string, 1) 41 if mock.working { 42 ch <- []string{"pow", "zap", "kerblooie"} 43 } else { 44 close(ch) 45 } 46 return ch 47 } 48 49 func (mock *mockWatcher) Err() error { 50 return errors.New("blammo") 51 } 52 53 // watchBackend implements applicationscaler.Backend for the convenience of 54 // the tests for the Watch method. 55 type watchBackend struct { 56 applicationscaler.Backend 57 working bool 58 } 59 60 func (backend *watchBackend) WatchScaledServices() state.StringsWatcher { 61 return &mockWatcher{working: backend.working} 62 } 63 64 // watchFixture collects components needed to test the Watch method. 65 type watchFixture struct { 66 Facade *applicationscaler.Facade 67 Resources *common.Resources 68 } 69 70 func newWatchFixture(c *gc.C, working bool) *watchFixture { 71 backend := &watchBackend{working: working} 72 resources := common.NewResources() 73 facade, err := applicationscaler.NewFacade(backend, resources, auth(true)) 74 c.Assert(err, jc.ErrorIsNil) 75 return &watchFixture{facade, resources} 76 } 77 78 // rescaleBackend implements applicationscaler.Backend for the convenience of 79 // the tests for the Rescale method. 80 type rescaleBackend struct { 81 applicationscaler.Backend 82 } 83 84 func (rescaleBackend) RescaleService(name string) error { 85 switch name { 86 case "expected": 87 return nil 88 case "missing": 89 return errors.NotFoundf("application") 90 default: 91 return errors.New("blammo") 92 } 93 } 94 95 // rescaleFixture collects components needed to test the Rescale method. 96 type rescaleFixture struct { 97 Facade *applicationscaler.Facade 98 } 99 100 func newRescaleFixture(c *gc.C) *rescaleFixture { 101 facade, err := applicationscaler.NewFacade(rescaleBackend{}, nil, auth(true)) 102 c.Assert(err, jc.ErrorIsNil) 103 return &rescaleFixture{facade} 104 } 105 106 // entities is a convenience constructor for params.Entities. 107 func entities(tags ...string) params.Entities { 108 entities := params.Entities{Entities: make([]params.Entity, len(tags))} 109 for i, tag := range tags { 110 entities.Entities[i].Tag = tag 111 } 112 return entities 113 }