fake.go 5.8 KB

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