externalsecret_conversion_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 v1alpha1
  13. import (
  14. "testing"
  15. "github.com/stretchr/testify/assert"
  16. corev1 "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  19. )
  20. const (
  21. keyName = "my-key"
  22. )
  23. func newExternalSecretV1Alpha1() *ExternalSecret {
  24. return &ExternalSecret{
  25. ObjectMeta: metav1.ObjectMeta{
  26. Name: "full-es",
  27. Namespace: "my-ns",
  28. },
  29. Status: ExternalSecretStatus{
  30. SyncedResourceVersion: "123",
  31. Conditions: []ExternalSecretStatusCondition{
  32. {
  33. Type: ExternalSecretReady,
  34. Status: corev1.ConditionTrue,
  35. Reason: "it's a mock, it's always ready",
  36. Message: "...why wouldn't it be?",
  37. },
  38. },
  39. },
  40. Spec: ExternalSecretSpec{
  41. SecretStoreRef: SecretStoreRef{
  42. Name: "test-secret-store",
  43. Kind: "ClusterSecretStore",
  44. },
  45. Target: ExternalSecretTarget{
  46. Name: "test-target",
  47. CreationPolicy: Owner,
  48. Immutable: false,
  49. Template: &ExternalSecretTemplate{
  50. Type: corev1.SecretTypeOpaque,
  51. Metadata: ExternalSecretTemplateMetadata{
  52. Annotations: map[string]string{
  53. "foo": "bar",
  54. },
  55. Labels: map[string]string{
  56. "foolbl": "barlbl",
  57. },
  58. },
  59. Data: map[string]string{
  60. keyName: "{{.data | toString}}",
  61. },
  62. TemplateFrom: []TemplateFrom{
  63. {
  64. ConfigMap: &TemplateRef{
  65. Name: "test-configmap",
  66. Items: []TemplateRefItem{
  67. {
  68. Key: keyName,
  69. },
  70. },
  71. },
  72. Secret: &TemplateRef{
  73. Name: "test-secret",
  74. Items: []TemplateRefItem{
  75. {
  76. Key: keyName,
  77. },
  78. },
  79. },
  80. },
  81. },
  82. },
  83. },
  84. Data: []ExternalSecretData{
  85. {
  86. SecretKey: keyName,
  87. RemoteRef: ExternalSecretDataRemoteRef{
  88. Key: "datakey",
  89. Property: "dataproperty",
  90. Version: "dataversion",
  91. },
  92. },
  93. },
  94. DataFrom: []ExternalSecretDataRemoteRef{
  95. {
  96. Key: "key",
  97. Property: "property",
  98. Version: "version",
  99. },
  100. },
  101. },
  102. }
  103. }
  104. func newExternalSecretV1Beta1() *esv1beta1.ExternalSecret {
  105. return &esv1beta1.ExternalSecret{
  106. ObjectMeta: metav1.ObjectMeta{
  107. Name: "full-es",
  108. Namespace: "my-ns",
  109. },
  110. Status: esv1beta1.ExternalSecretStatus{
  111. SyncedResourceVersion: "123",
  112. Conditions: []esv1beta1.ExternalSecretStatusCondition{
  113. {
  114. Type: esv1beta1.ExternalSecretReady,
  115. Status: corev1.ConditionTrue,
  116. Reason: "it's a mock, it's always ready",
  117. Message: "...why wouldn't it be?",
  118. },
  119. },
  120. },
  121. Spec: esv1beta1.ExternalSecretSpec{
  122. SecretStoreRef: esv1beta1.SecretStoreRef{
  123. Name: "test-secret-store",
  124. Kind: "ClusterSecretStore",
  125. },
  126. Target: esv1beta1.ExternalSecretTarget{
  127. Name: "test-target",
  128. CreationPolicy: esv1beta1.CreatePolicyOwner,
  129. Immutable: false,
  130. Template: &esv1beta1.ExternalSecretTemplate{
  131. Type: corev1.SecretTypeOpaque,
  132. Metadata: esv1beta1.ExternalSecretTemplateMetadata{
  133. Annotations: map[string]string{
  134. "foo": "bar",
  135. },
  136. Labels: map[string]string{
  137. "foolbl": "barlbl",
  138. },
  139. },
  140. Data: map[string]string{
  141. keyName: "{{.data | toString}}",
  142. },
  143. TemplateFrom: []esv1beta1.TemplateFrom{
  144. {
  145. ConfigMap: &esv1beta1.TemplateRef{
  146. Name: "test-configmap",
  147. Items: []esv1beta1.TemplateRefItem{
  148. {
  149. Key: keyName,
  150. },
  151. },
  152. },
  153. Secret: &esv1beta1.TemplateRef{
  154. Name: "test-secret",
  155. Items: []esv1beta1.TemplateRefItem{
  156. {
  157. Key: keyName,
  158. },
  159. },
  160. },
  161. },
  162. },
  163. },
  164. },
  165. Data: []esv1beta1.ExternalSecretData{
  166. {
  167. SecretKey: keyName,
  168. RemoteRef: esv1beta1.ExternalSecretDataRemoteRef{
  169. Key: "datakey",
  170. Property: "dataproperty",
  171. Version: "dataversion",
  172. },
  173. },
  174. },
  175. DataFrom: []esv1beta1.ExternalSecretDataFromRemoteRef{
  176. {
  177. Extract: &esv1beta1.ExternalSecretDataRemoteRef{
  178. Key: "key",
  179. Property: "property",
  180. Version: "version",
  181. },
  182. },
  183. },
  184. },
  185. }
  186. }
  187. func TestExternalSecretConvertFrom(t *testing.T) {
  188. given := newExternalSecretV1Beta1()
  189. want := newExternalSecretV1Alpha1()
  190. got := &ExternalSecret{}
  191. err := got.ConvertFrom(given)
  192. if err != nil {
  193. t.Errorf("test failed with error: %v", err)
  194. }
  195. if !assert.Equal(t, want, got) {
  196. t.Errorf("test failed, expected: %v, got: %v", want, got)
  197. }
  198. }
  199. func TestExternalSecretConvertTo(t *testing.T) {
  200. want := newExternalSecretV1Beta1()
  201. given := newExternalSecretV1Alpha1()
  202. got := &esv1beta1.ExternalSecret{}
  203. err := given.ConvertTo(got)
  204. if err != nil {
  205. t.Errorf("test failed with error: %v", err)
  206. }
  207. if !assert.Equal(t, want, got) {
  208. t.Errorf("test failed, expected: %v, got: %v", want, got)
  209. }
  210. }