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