parser_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 templating
  14. import (
  15. "context"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. "github.com/stretchr/testify/require"
  19. corev1 "k8s.io/api/core/v1"
  20. "sigs.k8s.io/controller-runtime/pkg/client"
  21. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  22. )
  23. func TestParserMergeLiteralPassesTemplateFromValuesDecodingStrategy(t *testing.T) {
  24. literal := "decoded: SGVsbG8="
  25. var got esv1.ExternalSecretDecodingStrategy
  26. p := &Parser{
  27. Exec: func(_ map[string][]byte, _ map[string][]byte, _ esv1.TemplateScope, _ string, _ client.Object, decodingStrategy esv1.ExternalSecretDecodingStrategy) error {
  28. got = decodingStrategy
  29. return nil
  30. },
  31. DataMap: map[string][]byte{},
  32. TargetSecret: &corev1.Secret{},
  33. }
  34. err := p.MergeLiteral(context.Background(), esv1.TemplateFrom{
  35. Literal: &literal,
  36. ValuesDecodingStrategy: esv1.ExternalSecretDecodeBase64,
  37. })
  38. require.NoError(t, err)
  39. assert.Equal(t, esv1.ExternalSecretDecodeBase64, got)
  40. }
  41. func TestParserMergeMapKeepsTemplateDataUndecoded(t *testing.T) {
  42. var got esv1.ExternalSecretDecodingStrategy
  43. p := &Parser{
  44. Exec: func(_ map[string][]byte, _ map[string][]byte, _ esv1.TemplateScope, _ string, _ client.Object, decodingStrategy esv1.ExternalSecretDecodingStrategy) error {
  45. got = decodingStrategy
  46. return nil
  47. },
  48. DataMap: map[string][]byte{},
  49. TargetSecret: &corev1.Secret{},
  50. }
  51. err := p.MergeMap(map[string]string{"encoded": "SGVsbG8="}, esv1.TemplateTargetData)
  52. require.NoError(t, err)
  53. assert.Equal(t, esv1.ExternalSecretDecodeNone, got)
  54. }