github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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/facade" 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 facade.Authorizer for use in the tests. 17 type agentAuth struct { 18 facade.Authorizer 19 machine bool 20 unit bool 21 } 22 23 // AuthMachineAgent is part of the facade.Authorizer interface. 24 func (auth agentAuth) AuthMachineAgent() bool { 25 return auth.machine 26 } 27 28 // AuthUnitAgent is part of the facade.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 { 63 mock.stub.AddCall("WatchMigrationPhase") 64 return newMockWatcher(mock.stub) 65 } 66 67 // newMockWatcher consumes an error from the supplied testing.Stub, and 68 // returns a state.NotifyWatcher that either works or doesn't depending 69 // on whether the error was nil. 70 func newMockWatcher(stub *testing.Stub) *mockWatcher { 71 changes := make(chan struct{}, 1) 72 err := stub.NextErr() 73 if err == nil { 74 changes <- struct{}{} 75 } else { 76 close(changes) 77 } 78 return &mockWatcher{ 79 err: err, 80 changes: changes, 81 } 82 } 83 84 // mockWatcher implements state.NotifyWatcher for use in the tests. 85 type mockWatcher struct { 86 state.NotifyWatcher 87 changes chan struct{} 88 err error 89 } 90 91 // Changes is part of the state.NotifyWatcher interface. 92 func (mock *mockWatcher) Changes() <-chan struct{} { 93 return mock.changes 94 } 95 96 // Err is part of the state.NotifyWatcher interface. 97 func (mock *mockWatcher) Err() error { 98 return mock.err 99 } 100 101 // entities is a convenience constructor for params.Entities. 102 func entities(tags ...string) params.Entities { 103 entities := params.Entities{ 104 Entities: make([]params.Entity, len(tags)), 105 } 106 for i, tag := range tags { 107 entities.Entities[i].Tag = tag 108 } 109 return entities 110 } 111 112 // authOK will always authenticate successfully. 113 var authOK = agentAuth{machine: true} 114 115 // unknownModel is expected to induce a permissions error. 116 const unknownModel = "model-01234567-89ab-cdef-0123-456789abcdef"