fake.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. "net/http"
  16. gitlab "gitlab.com/gitlab-org/api/client-go"
  17. )
  18. type APIResponse[O any] struct {
  19. Output O
  20. Response *gitlab.Response
  21. Error error
  22. }
  23. type GitVariable interface {
  24. gitlab.ProjectVariable |
  25. gitlab.GroupVariable
  26. }
  27. type extractKey[V GitVariable] func(gv V) string
  28. func keyFromProjectVariable(pv gitlab.ProjectVariable) string {
  29. return pv.Key
  30. }
  31. func keyFromGroupVariable(gv gitlab.GroupVariable) string {
  32. return gv.Key
  33. }
  34. type GitlabMockProjectsClient struct {
  35. listProjectsGroups func(pid any, opt *gitlab.ListProjectGroupOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.ProjectGroup, *gitlab.Response, error)
  36. }
  37. func (mc *GitlabMockProjectsClient) ListProjectsGroups(pid any, opt *gitlab.ListProjectGroupOptions, _ ...gitlab.RequestOptionFunc) ([]*gitlab.ProjectGroup, *gitlab.Response, error) {
  38. return mc.listProjectsGroups(pid, opt, nil)
  39. }
  40. func (mc *GitlabMockProjectsClient) WithValue(output []*gitlab.ProjectGroup, response *gitlab.Response, err error) {
  41. if mc != nil {
  42. mc.listProjectsGroups = func(pid any, opt *gitlab.ListProjectGroupOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.ProjectGroup, *gitlab.Response, error) {
  43. return output, response, err
  44. }
  45. }
  46. }
  47. type GitlabMockProjectVariablesClient struct {
  48. getVariable func(pid any, key string, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, *gitlab.Response, error)
  49. listVariables func(pid any, options ...gitlab.RequestOptionFunc) ([]*gitlab.ProjectVariable, *gitlab.Response, error)
  50. }
  51. func (mc *GitlabMockProjectVariablesClient) GetVariable(pid any, key string, _ *gitlab.GetProjectVariableOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, *gitlab.Response, error) {
  52. return mc.getVariable(pid, key, nil)
  53. }
  54. func (mc *GitlabMockProjectVariablesClient) ListVariables(pid any, _ *gitlab.ListProjectVariablesOptions, _ ...gitlab.RequestOptionFunc) ([]*gitlab.ProjectVariable, *gitlab.Response, error) {
  55. return mc.listVariables(pid)
  56. }
  57. func (mc *GitlabMockProjectVariablesClient) WithValue(response APIResponse[[]*gitlab.ProjectVariable]) {
  58. mc.WithValues([]APIResponse[[]*gitlab.ProjectVariable]{response})
  59. }
  60. func (mc *GitlabMockProjectVariablesClient) WithValues(responses []APIResponse[[]*gitlab.ProjectVariable]) {
  61. if mc != nil {
  62. mc.getVariable = mockGetVariable(keyFromProjectVariable, responses)
  63. mc.listVariables = mockListVariable(responses)
  64. }
  65. }
  66. func mockGetVariable[V GitVariable](keyExtractor extractKey[V], responses []APIResponse[[]*V]) func(any, string, ...gitlab.RequestOptionFunc) (*V, *gitlab.Response, error) {
  67. getCount := -1
  68. return func(pid any, key string, options ...gitlab.RequestOptionFunc) (*V, *gitlab.Response, error) {
  69. getCount++
  70. if getCount > len(responses)-1 {
  71. return nil, make404APIResponse(), nil
  72. }
  73. var match *V
  74. for _, v := range responses[getCount].Output {
  75. if keyExtractor(*v) == key {
  76. match = v
  77. }
  78. }
  79. if match == nil {
  80. return nil, make404APIResponse(), nil
  81. }
  82. return match, responses[getCount].Response, responses[getCount].Error
  83. }
  84. }
  85. func mockListVariable[V GitVariable](responses []APIResponse[[]*V]) func(any, ...gitlab.RequestOptionFunc) ([]*V, *gitlab.Response, error) {
  86. listCount := -1
  87. return func(pid any, options ...gitlab.RequestOptionFunc) ([]*V, *gitlab.Response, error) {
  88. listCount++
  89. if listCount > len(responses)-1 {
  90. return nil, makeAPIResponse(listCount, len(responses)), nil
  91. }
  92. return responses[listCount].Output, responses[listCount].Response, responses[listCount].Error
  93. }
  94. }
  95. func make404APIResponse() *gitlab.Response {
  96. return &gitlab.Response{
  97. Response: &http.Response{
  98. StatusCode: http.StatusNotFound,
  99. },
  100. }
  101. }
  102. func makeAPIResponse(page, pages int) *gitlab.Response {
  103. return &gitlab.Response{
  104. Response: &http.Response{
  105. StatusCode: http.StatusOK,
  106. },
  107. CurrentPage: page,
  108. TotalPages: pages,
  109. }
  110. }
  111. type GitlabMockGroupVariablesClient struct {
  112. getVariable func(gid any, key string, options ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error)
  113. listVariables func(gid any, options ...gitlab.RequestOptionFunc) ([]*gitlab.GroupVariable, *gitlab.Response, error)
  114. }
  115. func (mc *GitlabMockGroupVariablesClient) GetVariable(gid any, key string, _ *gitlab.GetGroupVariableOptions, _ ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error) {
  116. return mc.getVariable(gid, key, nil)
  117. }
  118. func (mc *GitlabMockGroupVariablesClient) ListVariables(gid any, _ *gitlab.ListGroupVariablesOptions, _ ...gitlab.RequestOptionFunc) ([]*gitlab.GroupVariable, *gitlab.Response, error) {
  119. return mc.listVariables(gid)
  120. }
  121. func (mc *GitlabMockGroupVariablesClient) WithValue(output *gitlab.GroupVariable, response *gitlab.Response, err error) {
  122. if mc != nil {
  123. mc.getVariable = func(gid any, key string, options ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error) {
  124. return output, response, err
  125. }
  126. mc.listVariables = func(gid any, options ...gitlab.RequestOptionFunc) ([]*gitlab.GroupVariable, *gitlab.Response, error) {
  127. return []*gitlab.GroupVariable{output}, response, err
  128. }
  129. }
  130. }
  131. func (mc *GitlabMockGroupVariablesClient) WithValues(responses []APIResponse[[]*gitlab.GroupVariable]) {
  132. if mc != nil {
  133. mc.getVariable = mockGetVariable(keyFromGroupVariable, responses)
  134. mc.listVariables = mockListVariable(responses)
  135. }
  136. }