gitlab_test.go 5.1 KB

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