gitlab_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 gitlab
  13. import (
  14. "context"
  15. "fmt"
  16. "reflect"
  17. "strings"
  18. "testing"
  19. gitlab "github.com/xanzy/go-gitlab"
  20. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  21. fakegitlab "github.com/external-secrets/external-secrets/pkg/provider/gitlab/fake"
  22. )
  23. type secretManagerTestCase struct {
  24. mockClient *fakegitlab.GitlabMockClient
  25. apiInputProjectID string
  26. apiInputKey string
  27. apiOutput *gitlab.ProjectVariable
  28. ref *esv1alpha1.ExternalSecretDataRemoteRef
  29. projectID *string
  30. apiErr error
  31. expectError string
  32. expectedSecret string
  33. // for testing secretmap
  34. expectedData map[string][]byte
  35. }
  36. func makeValidSecretManagerTestCase() *secretManagerTestCase {
  37. smtc := secretManagerTestCase{
  38. mockClient: &fakegitlab.GitlabMockClient{},
  39. apiInputProjectID: makeValidAPIInputProjectID(),
  40. apiInputKey: makeValidAPIInputKey(),
  41. ref: makeValidRef(),
  42. projectID: nil,
  43. apiOutput: makeValidAPIOutput(),
  44. apiErr: nil,
  45. expectError: "",
  46. expectedSecret: "",
  47. expectedData: map[string][]byte{},
  48. }
  49. smtc.mockClient.WithValue(smtc.apiInputProjectID, smtc.apiInputKey, smtc.apiOutput, smtc.apiErr)
  50. return &smtc
  51. }
  52. func makeValidRef() *esv1alpha1.ExternalSecretDataRemoteRef {
  53. return &esv1alpha1.ExternalSecretDataRemoteRef{
  54. Extract: esv1alpha1.ExternalSecretExtract{
  55. Key: "test-secret",
  56. Version: "default",
  57. },
  58. }
  59. }
  60. func makeValidAPIInputProjectID() string {
  61. return "testID"
  62. }
  63. func makeValidAPIInputKey() string {
  64. return "testKey"
  65. }
  66. func makeValidAPIOutput() *gitlab.ProjectVariable {
  67. return &gitlab.ProjectVariable{
  68. Key: "testKey",
  69. Value: "",
  70. }
  71. }
  72. func makeValidSecretManagerTestCaseCustom(tweaks ...func(smtc *secretManagerTestCase)) *secretManagerTestCase {
  73. smtc := makeValidSecretManagerTestCase()
  74. for _, fn := range tweaks {
  75. fn(smtc)
  76. }
  77. smtc.mockClient.WithValue(smtc.apiInputProjectID, smtc.apiInputKey, smtc.apiOutput, smtc.apiErr)
  78. return smtc
  79. }
  80. // This case can be shared by both GetSecret and GetSecretMap tests.
  81. // bad case: set apiErr.
  82. var setAPIErr = func(smtc *secretManagerTestCase) {
  83. smtc.apiErr = fmt.Errorf("oh no")
  84. smtc.expectError = "oh no"
  85. }
  86. var setNilMockClient = func(smtc *secretManagerTestCase) {
  87. smtc.mockClient = nil
  88. smtc.expectError = errUninitalizedGitlabProvider
  89. }
  90. // test the sm<->gcp interface
  91. // make sure correct values are passed and errors are handled accordingly.
  92. func TestGitlabSecretManagerGetSecret(t *testing.T) {
  93. secretValue := "changedvalue"
  94. // good case: default version is set
  95. // key is passed in, output is sent back
  96. setSecretString := func(smtc *secretManagerTestCase) {
  97. smtc.apiOutput = &gitlab.ProjectVariable{
  98. Key: "testkey",
  99. Value: "changedvalue",
  100. }
  101. smtc.expectedSecret = secretValue
  102. }
  103. successCases := []*secretManagerTestCase{
  104. makeValidSecretManagerTestCaseCustom(setSecretString),
  105. makeValidSecretManagerTestCaseCustom(setAPIErr),
  106. makeValidSecretManagerTestCaseCustom(setNilMockClient),
  107. }
  108. sm := Gitlab{}
  109. for k, v := range successCases {
  110. sm.client = v.mockClient
  111. out, err := sm.GetSecret(context.Background(), *v.ref)
  112. if !ErrorContains(err, v.expectError) {
  113. t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  114. }
  115. if string(out) != v.expectedSecret {
  116. t.Errorf("[%d] unexpected secret: expected %s, got %s", k, v.expectedSecret, string(out))
  117. }
  118. }
  119. }
  120. func TestGetSecretMap(t *testing.T) {
  121. // good case: default version & deserialization
  122. setDeserialization := func(smtc *secretManagerTestCase) {
  123. smtc.apiOutput.Value = `{"foo":"bar"}`
  124. smtc.expectedData["foo"] = []byte("bar")
  125. }
  126. // bad case: invalid json
  127. setInvalidJSON := func(smtc *secretManagerTestCase) {
  128. smtc.apiOutput.Value = `-----------------`
  129. smtc.expectError = "unable to unmarshal secret"
  130. }
  131. successCases := []*secretManagerTestCase{
  132. makeValidSecretManagerTestCaseCustom(setDeserialization),
  133. makeValidSecretManagerTestCaseCustom(setInvalidJSON),
  134. makeValidSecretManagerTestCaseCustom(setNilMockClient),
  135. makeValidSecretManagerTestCaseCustom(setAPIErr),
  136. }
  137. sm := Gitlab{}
  138. for k, v := range successCases {
  139. sm.client = v.mockClient
  140. out, err := sm.GetSecretMap(context.Background(), *v.ref)
  141. if !ErrorContains(err, v.expectError) {
  142. t.Errorf("[%d] unexpected error: %s, expected: '%s'", k, err.Error(), v.expectError)
  143. }
  144. if err == nil && !reflect.DeepEqual(out, v.expectedData) {
  145. t.Errorf("[%d] unexpected secret data: expected %#v, got %#v", k, v.expectedData, out)
  146. }
  147. }
  148. }
  149. func ErrorContains(out error, want string) bool {
  150. if out == nil {
  151. return want == ""
  152. }
  153. if want == "" {
  154. return false
  155. }
  156. return strings.Contains(out.Error(), want)
  157. }