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