vault.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package fake
  13. import (
  14. "context"
  15. vault "github.com/hashicorp/vault/api"
  16. )
  17. type MockNewRequestFn func(method, requestPath string) *vault.Request
  18. type MockRawRequestWithContextFn func(ctx context.Context, r *vault.Request) (*vault.Response, error)
  19. type MockSetTokenFn func(v string)
  20. type MockTokenFn func() string
  21. type MockClearTokenFn func()
  22. type MockSetNamespaceFn func(namespace string)
  23. type MockAddHeaderFn func(key, value string)
  24. func NewMockNewRequestFn(req *vault.Request) MockNewRequestFn {
  25. return func(method, requestPath string) *vault.Request {
  26. return req
  27. }
  28. }
  29. // An RequestFn operates on the supplied Request. You might use an RequestFn to
  30. // test or update the contents of an Request.
  31. type RequestFn func(req *vault.Request) error
  32. func NewMockRawRequestWithContextFn(res *vault.Response, err error, ofn ...RequestFn) MockRawRequestWithContextFn {
  33. return func(_ context.Context, r *vault.Request) (*vault.Response, error) {
  34. for _, fn := range ofn {
  35. if err := fn(r); err != nil {
  36. return res, err
  37. }
  38. }
  39. return res, err
  40. }
  41. }
  42. func NewSetTokenFn(ofn ...func(v string)) MockSetTokenFn {
  43. return func(v string) {
  44. for _, fn := range ofn {
  45. fn(v)
  46. }
  47. }
  48. }
  49. func NewTokenFn(v string) MockTokenFn {
  50. return func() string {
  51. return v
  52. }
  53. }
  54. func NewClearTokenFn() MockClearTokenFn {
  55. return func() {}
  56. }
  57. func NewSetNamespaceFn() MockSetNamespaceFn {
  58. return func(namespace string) {}
  59. }
  60. func NewAddHeaderFn() MockAddHeaderFn {
  61. return func(key, value string) {}
  62. }
  63. type VaultClient struct {
  64. MockNewRequest MockNewRequestFn
  65. MockRawRequestWithContext MockRawRequestWithContextFn
  66. MockSetToken MockSetTokenFn
  67. MockToken MockTokenFn
  68. MockClearToken MockClearTokenFn
  69. MockSetNamespace MockSetNamespaceFn
  70. MockAddHeader MockAddHeaderFn
  71. }
  72. func (c *VaultClient) NewRequest(method, requestPath string) *vault.Request {
  73. return c.MockNewRequest(method, requestPath)
  74. }
  75. func (c *VaultClient) RawRequestWithContext(ctx context.Context, r *vault.Request) (*vault.Response, error) {
  76. return c.MockRawRequestWithContext(ctx, r)
  77. }
  78. func (c *VaultClient) SetToken(v string) {
  79. c.MockSetToken(v)
  80. }
  81. func (c *VaultClient) Token() string {
  82. return c.MockToken()
  83. }
  84. func (c *VaultClient) ClearToken() {
  85. c.MockClearToken()
  86. }
  87. func (c *VaultClient) SetNamespace(namespace string) {
  88. c.MockSetNamespace(namespace)
  89. }
  90. func (c *VaultClient) AddHeader(key, value string) {
  91. c.MockAddHeader(key, value)
  92. }