gitlab_test.go 5.1 KB

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