mock.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package fake
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/gophercloud/gophercloud/v2"
  18. "github.com/gophercloud/gophercloud/v2/openstack/keymanager/v1/secrets"
  19. "github.com/gophercloud/gophercloud/v2/pagination"
  20. )
  21. // MockKeyManagerClient is a mock implementation of the Gophercloud ServiceClient
  22. // for testing purposes without requiring actual OpenStack services.
  23. type MockKeyManagerClient struct {
  24. // Mock data storage
  25. secrets map[string][]byte
  26. secretsInfo map[string]secrets.Secret
  27. shouldError bool
  28. errorMessage string
  29. }
  30. // NewMockKeyManagerClient creates a new mock client.
  31. func NewMockKeyManagerClient() *MockKeyManagerClient {
  32. return &MockKeyManagerClient{
  33. secrets: make(map[string][]byte),
  34. secretsInfo: make(map[string]secrets.Secret),
  35. shouldError: false,
  36. }
  37. }
  38. // WithSecret adds a secret to the mock client.
  39. func (m *MockKeyManagerClient) WithSecret(uuid, name string, payload []byte) *MockKeyManagerClient {
  40. m.secrets[uuid] = payload
  41. m.secretsInfo[uuid] = secrets.Secret{
  42. SecretRef: fmt.Sprintf("https://barbican.example.com/v1/secrets/%s", uuid),
  43. Name: name,
  44. }
  45. return m
  46. }
  47. // WithError configures the mock to return an error.
  48. func (m *MockKeyManagerClient) WithError(errorMessage string) *MockKeyManagerClient {
  49. m.shouldError = true
  50. m.errorMessage = errorMessage
  51. return m
  52. }
  53. // Reset clears all mock data and error states.
  54. func (m *MockKeyManagerClient) Reset() {
  55. m.secrets = make(map[string][]byte)
  56. m.secretsInfo = make(map[string]secrets.Secret)
  57. m.shouldError = false
  58. m.errorMessage = ""
  59. }
  60. // GetPayload mocks the secrets.GetPayload function.
  61. func (m *MockKeyManagerClient) GetPayload(_ context.Context, _ *gophercloud.ServiceClient, uuid string, _ secrets.GetPayloadOptsBuilder) ([]byte, error) {
  62. if m.shouldError {
  63. return nil, fmt.Errorf("%s", m.errorMessage)
  64. }
  65. payload, exists := m.secrets[uuid]
  66. if !exists {
  67. return nil, fmt.Errorf("secret with UUID %s not found", uuid)
  68. }
  69. return payload, nil
  70. }
  71. // ListSecrets mocks the secrets.List function.
  72. func (m *MockKeyManagerClient) ListSecrets(_ context.Context, _ *gophercloud.ServiceClient, opts secrets.ListOptsBuilder) ([]secrets.Secret, error) {
  73. if m.shouldError {
  74. return nil, fmt.Errorf("%s", m.errorMessage)
  75. }
  76. var result = make([]secrets.Secret, 10)
  77. for _, secret := range m.secretsInfo {
  78. // Apply name filter if provided
  79. if opts != nil {
  80. if listOpts, ok := opts.(secrets.ListOpts); ok && listOpts.Name != "" {
  81. if secret.Name != listOpts.Name {
  82. continue
  83. }
  84. }
  85. }
  86. result = append(result, secret)
  87. }
  88. return result, nil
  89. }
  90. // MockPagination implements pagination.Page for testing.
  91. type MockPagination struct {
  92. secrets []secrets.Secret
  93. }
  94. func (p MockPagination) NextPageURL() (string, error) {
  95. return "", nil
  96. }
  97. func (p MockPagination) IsEmpty() (bool, error) {
  98. return len(p.secrets) == 0, nil
  99. }
  100. func (p MockPagination) LastMarker() (string, error) {
  101. return "", nil
  102. }
  103. func (p MockPagination) GetBody() interface{} {
  104. return map[string]interface{}{
  105. "secrets": p.secrets,
  106. }
  107. }
  108. // MockPager implements pagination.Pager for testing.
  109. type MockPager struct {
  110. page MockPagination
  111. }
  112. func (p MockPager) AllPages(_ context.Context) (pagination.Page, error) {
  113. return p.page, nil
  114. }
  115. func (p MockPager) EachPage(_ context.Context, fn func(pagination.Page) (bool, error)) error {
  116. cont, err := fn(p.page)
  117. if err != nil {
  118. return err
  119. }
  120. _ = cont
  121. return nil
  122. }
  123. // NewMockPager creates a new mock pager with the provided secrets.
  124. func NewMockPager(secrets []secrets.Secret) MockPager {
  125. return MockPager{
  126. page: MockPagination{secrets: secrets},
  127. }
  128. }