client_secret_exists_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 ovh
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "testing"
  19. kclient "sigs.k8s.io/controller-runtime/pkg/client"
  20. "github.com/external-secrets/external-secrets/providers/v1/ovh/fake"
  21. testingfake "github.com/external-secrets/external-secrets/runtime/testing/fake"
  22. )
  23. func TestSecretExists(t *testing.T) {
  24. mysecretRef := testingfake.PushSecretData{
  25. RemoteKey: "mysecret",
  26. }
  27. nonExistentSecretRef := testingfake.PushSecretData{
  28. RemoteKey: "non-existent-secret",
  29. }
  30. testCases := map[string]struct {
  31. should bool
  32. errshould string
  33. remoteRef testingfake.PushSecretData
  34. okmsClient fake.FakeOkmsClient
  35. kube kclient.Client
  36. }{
  37. "Valid Secret": {
  38. should: true,
  39. remoteRef: mysecretRef,
  40. },
  41. "Non-existent Secret": {
  42. should: false,
  43. remoteRef: nonExistentSecretRef,
  44. },
  45. "Error case": {
  46. errshould: fmt.Sprintf("failed to check existence of secret %q: failed to parse the following okms error: custom error", mysecretRef.RemoteKey),
  47. remoteRef: mysecretRef,
  48. okmsClient: fake.FakeOkmsClient{
  49. GetSecretV2Fn: fake.NewGetSecretV2Fn(mysecretRef.RemoteKey, errors.New("custom error")),
  50. },
  51. },
  52. }
  53. for name, testCase := range testCases {
  54. t.Run(name, func(t *testing.T) {
  55. cl := &ovhClient{
  56. kube: testCase.kube,
  57. okmsClient: testCase.okmsClient,
  58. }
  59. ctx := context.Background()
  60. exists, err := cl.SecretExists(ctx, testCase.remoteRef)
  61. if testCase.errshould != "" {
  62. if err == nil {
  63. t.Errorf("\nexpected error: %s\nactual error: <nil>\n\n", testCase.errshould)
  64. } else if err.Error() != testCase.errshould {
  65. t.Errorf("\nexpected error: %s\nactual error: %v\n\n", testCase.errshould, err)
  66. }
  67. return
  68. } else if err != nil {
  69. t.Errorf("\nunexpected error: %v\n\n", err)
  70. return
  71. }
  72. if exists != testCase.should {
  73. t.Errorf("\nexpected value: %t\nactual value: %t\n\n", testCase.should, exists)
  74. }
  75. })
  76. }
  77. }