github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/worker/credentialvalidator/util_test.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package credentialvalidator_test 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/loggo" 9 "github.com/juju/names/v5" 10 "github.com/juju/testing" 11 jc "github.com/juju/testing/checkers" 12 "github.com/juju/worker/v3" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/api/base" 16 "github.com/juju/juju/core/watcher" 17 "github.com/juju/juju/core/watcher/watchertest" 18 coretesting "github.com/juju/juju/testing" 19 "github.com/juju/juju/worker/credentialvalidator" 20 ) 21 22 // mockFacade implements credentialvalidator.Facade for use in the tests. 23 type mockFacade struct { 24 *testing.Stub 25 credential *base.StoredCredential 26 exists bool 27 28 watcher *watchertest.MockNotifyWatcher 29 modelWatcher *watchertest.MockNotifyWatcher 30 } 31 32 func (m *mockFacade) setupModelHasNoCredential() { 33 m.credential.CloudCredential = "" 34 m.exists = false 35 m.watcher = nil 36 } 37 38 // ModelCredential is part of the credentialvalidator.Facade interface. 39 func (m *mockFacade) ModelCredential() (base.StoredCredential, bool, error) { 40 m.AddCall("ModelCredential") 41 if err := m.NextErr(); err != nil { 42 return base.StoredCredential{}, false, err 43 } 44 return *m.credential, m.exists, nil 45 } 46 47 // WatchCredential is part of the credentialvalidator.Facade interface. 48 func (mock *mockFacade) WatchCredential(id string) (watcher.NotifyWatcher, error) { 49 mock.AddCall("WatchCredential", id) 50 if err := mock.NextErr(); err != nil { 51 return nil, err 52 } 53 return mock.watcher, nil 54 } 55 56 // WatchModelCredential is part of the credentialvalidator.Facade interface. 57 func (mock *mockFacade) WatchModelCredential() (watcher.NotifyWatcher, error) { 58 mock.AddCall("WatchModelCredential") 59 if err := mock.NextErr(); err != nil { 60 return nil, err 61 } 62 return mock.modelWatcher, nil 63 } 64 65 // credentialTag is the credential tag we're using in the tests. 66 // needs to fit fmt.Sprintf("%s/%s/%s", cloudName, userName, credentialName) 67 var credentialTag = names.NewCloudCredentialTag("cloud/user/credential").String() 68 69 // panicFacade is a NewFacade that should not be called. 70 func panicFacade(base.APICaller) (credentialvalidator.Facade, error) { 71 panic("panicFacade") 72 } 73 74 // panicWorker is a NewWorker that should not be called. 75 func panicWorker(credentialvalidator.Config) (worker.Worker, error) { 76 panic("panicWorker") 77 } 78 79 // validConfig returns a minimal config stuffed with dummy objects that 80 // will explode when used. 81 func validConfig() credentialvalidator.Config { 82 return credentialvalidator.Config{ 83 Facade: struct{ credentialvalidator.Facade }{}, 84 Logger: loggo.GetLogger("test"), 85 } 86 } 87 88 // checkNotValid checks that the supplied credentialvalidator.Config fails to 89 // Validate, and cannot be used to construct a credentialvalidator.Worker. 90 func checkNotValid(c *gc.C, config credentialvalidator.Config, expect string) { 91 check := func(err error) { 92 c.Check(err, gc.ErrorMatches, expect) 93 c.Check(err, jc.Satisfies, errors.IsNotValid) 94 } 95 96 err := config.Validate() 97 check(err) 98 99 worker, err := credentialvalidator.NewWorker(config) 100 c.Check(worker, gc.IsNil) 101 check(err) 102 } 103 104 // validManifoldConfig returns a minimal config stuffed with dummy objects 105 // that will explode when used. 106 func validManifoldConfig() credentialvalidator.ManifoldConfig { 107 return credentialvalidator.ManifoldConfig{ 108 APICallerName: "api-caller", 109 NewFacade: panicFacade, 110 NewWorker: panicWorker, 111 Logger: loggo.GetLogger("test"), 112 } 113 } 114 115 // checkManifoldNotValid checks that the supplied ManifoldConfig creates 116 // a manifold that cannot be started. 117 func checkManifoldNotValid(c *gc.C, config credentialvalidator.ManifoldConfig, expect string) { 118 err := config.Validate() 119 c.Check(err, gc.ErrorMatches, expect) 120 c.Check(err, jc.Satisfies, errors.IsNotValid) 121 } 122 123 // stubCaller is a base.APICaller that only implements ModelTag. 124 type stubCaller struct { 125 base.APICaller 126 } 127 128 // ModelTag is part of the base.APICaller interface. 129 func (*stubCaller) ModelTag() (names.ModelTag, bool) { 130 return coretesting.ModelTag, true 131 }