secretstore_conversion_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. esmeta "github.com/external-secrets/external-secrets/apis/meta/v1"
  20. )
  21. const (
  22. storeName = "secret-store"
  23. storeNamespace = "my-namespace"
  24. storeReason = "it's a mock, it's always ready"
  25. storeMessage = "...why wouldn't it be?"
  26. storeAWSRegion = "us-east-1"
  27. storeAWSRole = "arn:aws:iam::123456789012:role/my-role"
  28. storeAccessName = "my-access"
  29. storeKey = "my-key"
  30. storeSecretName = "my-secret"
  31. defaultErrorMessage = "test failed with error: %v"
  32. defaultComparisonMessage = "test failed, expected: %v, got: %v"
  33. )
  34. func newSecretStoreV1Alpha1() *SecretStore {
  35. return &SecretStore{
  36. ObjectMeta: metav1.ObjectMeta{
  37. Name: storeName,
  38. Namespace: storeNamespace,
  39. },
  40. Status: SecretStoreStatus{
  41. Conditions: []SecretStoreStatusCondition{
  42. {
  43. Type: SecretStoreReady,
  44. Status: corev1.ConditionTrue,
  45. Reason: storeReason,
  46. Message: storeMessage,
  47. },
  48. },
  49. },
  50. Spec: SecretStoreSpec{
  51. Controller: "dev",
  52. Provider: &SecretStoreProvider{
  53. AWS: &AWSProvider{
  54. Service: AWSServiceSecretsManager,
  55. Region: storeAWSRegion,
  56. Role: storeAWSRole,
  57. Auth: AWSAuth{
  58. SecretRef: &AWSAuthSecretRef{
  59. AccessKeyID: esmeta.SecretKeySelector{
  60. Name: storeAccessName,
  61. Key: storeKey,
  62. },
  63. SecretAccessKey: esmeta.SecretKeySelector{
  64. Name: storeSecretName,
  65. Key: storeKey,
  66. },
  67. },
  68. },
  69. },
  70. },
  71. },
  72. }
  73. }
  74. func newSecretStoreV1Beta1() *esv1beta1.SecretStore {
  75. return &esv1beta1.SecretStore{
  76. ObjectMeta: metav1.ObjectMeta{
  77. Name: storeName,
  78. Namespace: storeNamespace,
  79. },
  80. Status: esv1beta1.SecretStoreStatus{
  81. Conditions: []esv1beta1.SecretStoreStatusCondition{
  82. {
  83. Type: esv1beta1.SecretStoreReady,
  84. Status: corev1.ConditionTrue,
  85. Reason: storeReason,
  86. Message: storeMessage,
  87. },
  88. },
  89. },
  90. Spec: esv1beta1.SecretStoreSpec{
  91. Controller: "dev",
  92. Provider: &esv1beta1.SecretStoreProvider{
  93. AWS: &esv1beta1.AWSProvider{
  94. Service: esv1beta1.AWSServiceSecretsManager,
  95. Region: storeAWSRegion,
  96. Role: storeAWSRole,
  97. Auth: esv1beta1.AWSAuth{
  98. SecretRef: &esv1beta1.AWSAuthSecretRef{
  99. AccessKeyID: esmeta.SecretKeySelector{
  100. Name: storeAccessName,
  101. Key: storeKey,
  102. },
  103. SecretAccessKey: esmeta.SecretKeySelector{
  104. Name: storeSecretName,
  105. Key: storeKey,
  106. },
  107. },
  108. },
  109. },
  110. },
  111. },
  112. }
  113. }
  114. func newClusterSecretStoreV1Alpha1() *ClusterSecretStore {
  115. ns := storeNamespace
  116. return &ClusterSecretStore{
  117. ObjectMeta: metav1.ObjectMeta{
  118. Name: storeName,
  119. },
  120. Status: SecretStoreStatus{
  121. Conditions: []SecretStoreStatusCondition{
  122. {
  123. Type: SecretStoreReady,
  124. Status: corev1.ConditionTrue,
  125. Reason: storeReason,
  126. Message: storeMessage,
  127. },
  128. },
  129. },
  130. Spec: SecretStoreSpec{
  131. Controller: "dev",
  132. Provider: &SecretStoreProvider{
  133. AWS: &AWSProvider{
  134. Service: AWSServiceSecretsManager,
  135. Region: storeAWSRegion,
  136. Role: storeAWSRole,
  137. Auth: AWSAuth{
  138. SecretRef: &AWSAuthSecretRef{
  139. AccessKeyID: esmeta.SecretKeySelector{
  140. Name: storeAccessName,
  141. Key: storeKey,
  142. Namespace: &ns,
  143. },
  144. SecretAccessKey: esmeta.SecretKeySelector{
  145. Name: storeSecretName,
  146. Key: storeKey,
  147. Namespace: &ns,
  148. },
  149. },
  150. },
  151. },
  152. },
  153. },
  154. }
  155. }
  156. func newClusterSecretStoreV1Beta1() *esv1beta1.ClusterSecretStore {
  157. ns := storeNamespace
  158. return &esv1beta1.ClusterSecretStore{
  159. ObjectMeta: metav1.ObjectMeta{
  160. Name: storeName,
  161. },
  162. Status: esv1beta1.SecretStoreStatus{
  163. Conditions: []esv1beta1.SecretStoreStatusCondition{
  164. {
  165. Type: esv1beta1.SecretStoreReady,
  166. Status: corev1.ConditionTrue,
  167. Reason: storeReason,
  168. Message: storeMessage,
  169. },
  170. },
  171. },
  172. Spec: esv1beta1.SecretStoreSpec{
  173. Controller: "dev",
  174. Provider: &esv1beta1.SecretStoreProvider{
  175. AWS: &esv1beta1.AWSProvider{
  176. Service: esv1beta1.AWSServiceSecretsManager,
  177. Region: storeAWSRegion,
  178. Role: storeAWSRole,
  179. Auth: esv1beta1.AWSAuth{
  180. SecretRef: &esv1beta1.AWSAuthSecretRef{
  181. AccessKeyID: esmeta.SecretKeySelector{
  182. Name: storeAccessName,
  183. Key: storeKey,
  184. Namespace: &ns,
  185. },
  186. SecretAccessKey: esmeta.SecretKeySelector{
  187. Name: storeSecretName,
  188. Key: storeKey,
  189. Namespace: &ns,
  190. },
  191. },
  192. },
  193. },
  194. },
  195. },
  196. }
  197. }
  198. func TestSecretStoreConvertFrom(t *testing.T) {
  199. given := newSecretStoreV1Beta1()
  200. want := newSecretStoreV1Alpha1()
  201. got := &SecretStore{}
  202. err := got.ConvertFrom(given)
  203. if err != nil {
  204. t.Errorf(defaultErrorMessage, err)
  205. }
  206. if !assert.Equal(t, want, got) {
  207. t.Errorf("test failed, expected: %v, got: %v", want, got)
  208. }
  209. }
  210. func TestSecretStoreConvertTo(t *testing.T) {
  211. want := newSecretStoreV1Beta1()
  212. given := newSecretStoreV1Alpha1()
  213. got := &esv1beta1.SecretStore{}
  214. err := given.ConvertTo(got)
  215. if err != nil {
  216. t.Errorf(defaultErrorMessage, err)
  217. }
  218. if !assert.Equal(t, want, got) {
  219. t.Errorf(defaultComparisonMessage, want, got)
  220. }
  221. }
  222. func TestClusterSecretStoreConvertFrom(t *testing.T) {
  223. given := newClusterSecretStoreV1Beta1()
  224. want := newClusterSecretStoreV1Alpha1()
  225. got := &ClusterSecretStore{}
  226. err := got.ConvertFrom(given)
  227. if err != nil {
  228. t.Errorf(defaultErrorMessage, err)
  229. }
  230. if !assert.Equal(t, want, got) {
  231. t.Errorf(defaultComparisonMessage, want, got)
  232. }
  233. }
  234. func TestClusterSecretStoreConvertTo(t *testing.T) {
  235. want := newClusterSecretStoreV1Beta1()
  236. given := newClusterSecretStoreV1Alpha1()
  237. got := &esv1beta1.ClusterSecretStore{}
  238. err := given.ConvertTo(got)
  239. if err != nil {
  240. t.Errorf(defaultErrorMessage, err)
  241. }
  242. if !assert.Equal(t, want, got) {
  243. t.Errorf(defaultComparisonMessage, want, got)
  244. }
  245. }