github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/undertaker/mock_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package undertaker_test 5 6 import ( 7 "github.com/juju/testing" 8 jc "github.com/juju/testing/checkers" 9 gc "gopkg.in/check.v1" 10 "gopkg.in/juju/worker.v1" 11 "gopkg.in/juju/worker.v1/workertest" 12 13 "github.com/juju/juju/apiserver/params" 14 "github.com/juju/juju/core/status" 15 "github.com/juju/juju/core/watcher" 16 "github.com/juju/juju/environs/context" 17 "github.com/juju/juju/worker/undertaker" 18 ) 19 20 type mockFacade struct { 21 stub *testing.Stub 22 info params.UndertakerModelInfoResult 23 } 24 25 func (mock *mockFacade) ModelInfo() (params.UndertakerModelInfoResult, error) { 26 mock.stub.AddCall("ModelInfo") 27 if err := mock.stub.NextErr(); err != nil { 28 return params.UndertakerModelInfoResult{}, err 29 } 30 return mock.info, nil 31 } 32 33 func (mock *mockFacade) WatchModelResources() (watcher.NotifyWatcher, error) { 34 mock.stub.AddCall("WatchModelResources") 35 if err := mock.stub.NextErr(); err != nil { 36 return nil, err 37 } 38 const count = 5 39 changes := make(chan struct{}, count) 40 for i := 0; i < count; i++ { 41 changes <- struct{}{} 42 } 43 return &mockWatcher{ 44 Worker: workertest.NewErrorWorker(nil), 45 changes: changes, 46 }, nil 47 } 48 49 func (mock *mockFacade) ProcessDyingModel() error { 50 mock.stub.AddCall("ProcessDyingModel") 51 return mock.stub.NextErr() 52 } 53 54 func (mock *mockFacade) SetStatus(status status.Status, info string, data map[string]interface{}) error { 55 mock.stub.MethodCall(mock, "SetStatus", status, info, data) 56 return mock.stub.NextErr() 57 } 58 59 func (mock *mockFacade) RemoveModel() error { 60 mock.stub.AddCall("RemoveModel") 61 return mock.stub.NextErr() 62 } 63 64 type cloudDestroyer interface { 65 Destroy(context.ProviderCallContext) error 66 } 67 68 type mockDestroyer struct { 69 cloudDestroyer 70 stub *testing.Stub 71 } 72 73 func (mock *mockDestroyer) Destroy(ctx context.ProviderCallContext) error { 74 mock.stub.AddCall("Destroy", ctx) 75 return mock.stub.NextErr() 76 } 77 78 type mockWatcher struct { 79 worker.Worker 80 changes chan struct{} 81 } 82 83 func (mock *mockWatcher) Changes() watcher.NotifyChannel { 84 return mock.changes 85 } 86 87 type fixture struct { 88 info params.UndertakerModelInfoResult 89 errors []error 90 dirty bool 91 } 92 93 func (fix fixture) cleanup(c *gc.C, w worker.Worker) { 94 if fix.dirty { 95 workertest.DirtyKill(c, w) 96 } else { 97 workertest.CleanKill(c, w) 98 } 99 } 100 101 func (fix fixture) run(c *gc.C, test func(worker.Worker)) *testing.Stub { 102 stub := &testing.Stub{} 103 environOrBroker := &mockDestroyer{ 104 stub: stub, 105 } 106 facade := &mockFacade{ 107 stub: stub, 108 info: fix.info, 109 } 110 stub.SetErrors(fix.errors...) 111 w, err := undertaker.NewUndertaker(undertaker.Config{ 112 Facade: facade, 113 Destroyer: environOrBroker, 114 CredentialAPI: &fakeCredentialAPI{}, 115 }) 116 c.Assert(err, jc.ErrorIsNil) 117 defer fix.cleanup(c, w) 118 test(w) 119 return stub 120 }