github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/migrationflag/util_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the LGPLv3, see LICENCE file for details. 3 4 package migrationflag_test 5 6 import ( 7 "github.com/juju/testing" 8 9 "github.com/juju/juju/apiserver/common" 10 "github.com/juju/juju/apiserver/params" 11 "github.com/juju/juju/core/migration" 12 "github.com/juju/juju/state" 13 coretesting "github.com/juju/juju/testing" 14 ) 15 16 // agentAuth implements common.Authorizer for use in the tests. 17 type agentAuth struct { 18 common.Authorizer 19 machine bool 20 unit bool 21 } 22 23 // AuthMachineAgent is part of the common.Authorizer interface. 24 func (auth agentAuth) AuthMachineAgent() bool { 25 return auth.machine 26 } 27 28 // AuthUnitAgent is part of the common.Authorizer interface. 29 func (auth agentAuth) AuthUnitAgent() bool { 30 return auth.unit 31 } 32 33 // newMockBackend returns a mock Backend that will add calls to the 34 // supplied testing.Stub, and return errors in the sequence it 35 // specifies. 36 func newMockBackend(stub *testing.Stub) *mockBackend { 37 return &mockBackend{ 38 stub: stub, 39 } 40 } 41 42 // mockBackend implements migrationflag.Backend for use in the tests. 43 type mockBackend struct { 44 stub *testing.Stub 45 } 46 47 // ModelUUID is part of the migrationflag.Backend interface. 48 func (mock *mockBackend) ModelUUID() string { 49 return coretesting.ModelTag.Id() 50 } 51 52 // MigrationPhase is part of the migrationflag.Backend interface. 53 func (mock *mockBackend) MigrationPhase() (migration.Phase, error) { 54 mock.stub.AddCall("MigrationPhase") 55 if err := mock.stub.NextErr(); err != nil { 56 return migration.UNKNOWN, err 57 } 58 return migration.REAP, nil 59 } 60 61 // WatchMigrationPhase is part of the migrationflag.Backend interface. 62 func (mock *mockBackend) WatchMigrationPhase() (state.NotifyWatcher, error) { 63 mock.stub.AddCall("WatchMigrationPhase") 64 if err := mock.stub.NextErr(); err != nil { 65 return nil, err 66 } 67 return newMockWatcher(mock.stub), nil 68 } 69 70 // newMockWatcher consumes an error from the supplied testing.Stub, and 71 // returns a state.NotifyWatcher that either works or doesn't depending 72 // on whether the error was nil. 73 func newMockWatcher(stub *testing.Stub) *mockWatcher { 74 changes := make(chan struct{}, 1) 75 err := stub.NextErr() 76 if err == nil { 77 changes <- struct{}{} 78 } else { 79 close(changes) 80 } 81 return &mockWatcher{ 82 err: err, 83 changes: changes, 84 } 85 } 86 87 // mockWatcher implements state.NotifyWatcher for use in the tests. 88 type mockWatcher struct { 89 state.NotifyWatcher 90 changes chan struct{} 91 err error 92 } 93 94 // Changes is part of the state.NotifyWatcher interface. 95 func (mock *mockWatcher) Changes() <-chan struct{} { 96 return mock.changes 97 } 98 99 // Err is part of the state.NotifyWatcher interface. 100 func (mock *mockWatcher) Err() error { 101 return mock.err 102 } 103 104 // entities is a convenience constructor for params.Entities. 105 func entities(tags ...string) params.Entities { 106 entities := params.Entities{ 107 Entities: make([]params.Entity, len(tags)), 108 } 109 for i, tag := range tags { 110 entities.Entities[i].Tag = tag 111 } 112 return entities 113 } 114 115 // authOK will always authenticate successfully. 116 var authOK = agentAuth{machine: true} 117 118 // unknownModel is expected to induce a permissions error. 119 const unknownModel = "model-01234567-89ab-cdef-0123-456789abcdef"