push_secret_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 secretmanager
  13. import (
  14. "testing"
  15. "github.com/stretchr/testify/assert"
  16. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  17. testingfake "github.com/external-secrets/external-secrets/pkg/provider/testing/fake"
  18. )
  19. func TestBuildMetadata(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. labels map[string]string
  23. metadata *apiextensionsv1.JSON
  24. expectedError bool
  25. expectedLabels map[string]string
  26. expectedAnnotations map[string]string
  27. expectedTopics []string
  28. }{
  29. {
  30. name: "secret not managed by external secrets",
  31. labels: map[string]string{
  32. "someKey": "someValue",
  33. },
  34. expectedError: true,
  35. },
  36. {
  37. name: "metadata with default MergePolicy of Replace",
  38. labels: map[string]string{
  39. managedByKey: managedByValue,
  40. "someOtherKey": "someOtherValue",
  41. },
  42. metadata: &apiextensionsv1.JSON{
  43. Raw: []byte(`{
  44. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  45. "kind": "PushSecretMetadata",
  46. "spec": {
  47. "annotations": {"key1":"value1"},
  48. "labels": {"key2":"value2"}
  49. }
  50. }`),
  51. },
  52. expectedError: false,
  53. expectedLabels: map[string]string{
  54. managedByKey: managedByValue,
  55. "key2": "value2",
  56. },
  57. expectedAnnotations: map[string]string{
  58. "key1": "value1",
  59. },
  60. expectedTopics: nil,
  61. },
  62. {
  63. name: "metadata with merge policy",
  64. labels: map[string]string{
  65. managedByKey: managedByValue,
  66. "existingKey": "existingValue",
  67. },
  68. metadata: &apiextensionsv1.JSON{
  69. Raw: []byte(`{
  70. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  71. "kind": "PushSecretMetadata",
  72. "spec": {
  73. "annotations": {"key1":"value1"},
  74. "labels": {"key2":"value2"},
  75. "mergePolicy": "Merge"
  76. }
  77. }`),
  78. },
  79. expectedError: false,
  80. expectedLabels: map[string]string{
  81. managedByKey: managedByValue,
  82. "existingKey": "existingValue",
  83. "key2": "value2",
  84. },
  85. expectedAnnotations: map[string]string{
  86. "key1": "value1",
  87. },
  88. expectedTopics: nil,
  89. },
  90. {
  91. name: "metadata with CMEK key name",
  92. labels: map[string]string{
  93. managedByKey: managedByValue,
  94. },
  95. metadata: &apiextensionsv1.JSON{
  96. Raw: []byte(`{
  97. "apiVersion": "kubernetes.external-secrets.io/v1alpha1",
  98. "kind": "PushSecretMetadata",
  99. "spec": {
  100. "annotations": {"key1":"value1"},
  101. "labels": {"key2":"value2"},
  102. "cmekKeyName": "projects/my-project/locations/us-east1/keyRings/my-keyring/cryptoKeys/my-key"
  103. }
  104. }`),
  105. },
  106. expectedError: false,
  107. expectedLabels: map[string]string{
  108. managedByKey: managedByValue,
  109. "key2": "value2",
  110. },
  111. expectedAnnotations: map[string]string{
  112. "key1": "value1",
  113. },
  114. },
  115. }
  116. for _, tt := range tests {
  117. t.Run(tt.name, func(t *testing.T) {
  118. psData := testingfake.PushSecretData{
  119. Metadata: tt.metadata,
  120. }
  121. builder := &psBuilder{
  122. pushSecretData: psData,
  123. }
  124. annotations, labels, topics, err := builder.buildMetadata(nil, tt.labels, nil)
  125. if tt.expectedError {
  126. assert.Error(t, err)
  127. } else {
  128. assert.NoError(t, err)
  129. assert.Equal(t, tt.expectedLabels, labels)
  130. assert.Equal(t, tt.expectedAnnotations, annotations)
  131. assert.Equal(t, tt.expectedTopics, topics)
  132. }
  133. })
  134. }
  135. }