push_secret_test.go 3.8 KB

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