workflow_engine_test.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. package workflow
  2. import (
  3. "context"
  4. "reflect"
  5. "testing"
  6. "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  7. "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  8. "github.com/go-logr/logr"
  9. corev1 "k8s.io/api/core/v1"
  10. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  11. "k8s.io/apimachinery/pkg/runtime"
  12. "sigs.k8s.io/controller-runtime/pkg/client/fake"
  13. )
  14. func TestWorkflowRunner_Run(t *testing.T) {
  15. tests := []struct {
  16. name string
  17. workflows []v1alpha1.WorkflowItem
  18. expected map[string]interface{}
  19. }{
  20. {
  21. name: "SingleStepWorkflow",
  22. workflows: []v1alpha1.WorkflowItem{
  23. {
  24. Name: "workflow1",
  25. Steps: []v1alpha1.WorkflowStep{
  26. {
  27. Name: "step1",
  28. Pull: &v1alpha1.WorkflowStepPull{
  29. Source: v1beta1.StoreSourceRef{
  30. SecretStoreRef: v1beta1.SecretStoreRef{
  31. Name: "store1",
  32. },
  33. },
  34. Data: []v1beta1.ExternalSecretData{
  35. {
  36. SecretKey: "key1",
  37. RemoteRef: v1beta1.ExternalSecretDataRemoteRef{
  38. Key: "key1",
  39. },
  40. },
  41. },
  42. },
  43. },
  44. },
  45. },
  46. },
  47. expected: map[string]interface{}{
  48. "workflow": map[string]interface{}{
  49. "data": map[string]string{
  50. "key1": "value1",
  51. },
  52. "metadata": map[string]map[string]string{
  53. "annotations": {},
  54. "labels": {},
  55. },
  56. },
  57. "workflows": map[string]interface{}{
  58. "workflow1": map[string]interface{}{
  59. "data": map[string]string{
  60. "key1": "value1",
  61. },
  62. "metadata": map[string]map[string]string{
  63. "annotations": {},
  64. "labels": {},
  65. },
  66. },
  67. },
  68. },
  69. },
  70. {
  71. name: "MultipleStepWorkflow",
  72. workflows: []v1alpha1.WorkflowItem{
  73. {
  74. Name: "workflow1",
  75. Steps: []v1alpha1.WorkflowStep{
  76. {
  77. Name: "step1",
  78. Pull: &v1alpha1.WorkflowStepPull{
  79. Source: v1beta1.StoreSourceRef{
  80. SecretStoreRef: v1beta1.SecretStoreRef{
  81. Name: "store1",
  82. },
  83. },
  84. Data: []v1beta1.ExternalSecretData{
  85. {
  86. SecretKey: "key1",
  87. RemoteRef: v1beta1.ExternalSecretDataRemoteRef{
  88. Key: "key1",
  89. },
  90. },
  91. },
  92. },
  93. },
  94. {
  95. Name: "step2",
  96. Pull: &v1alpha1.WorkflowStepPull{
  97. Source: v1beta1.StoreSourceRef{
  98. SecretStoreRef: v1beta1.SecretStoreRef{
  99. Name: "store1",
  100. },
  101. },
  102. Data: []v1beta1.ExternalSecretData{
  103. {
  104. SecretKey: "key2",
  105. RemoteRef: v1beta1.ExternalSecretDataRemoteRef{
  106. Key: "key2",
  107. },
  108. },
  109. },
  110. },
  111. },
  112. },
  113. },
  114. },
  115. expected: map[string]interface{}{
  116. "workflow": map[string]interface{}{
  117. "data": map[string]string{
  118. "key1": "value1",
  119. "key2": "value2",
  120. },
  121. "metadata": map[string]map[string]string{
  122. "annotations": {},
  123. "labels": {},
  124. },
  125. },
  126. "workflows": map[string]interface{}{
  127. "workflow1": map[string]interface{}{
  128. "data": map[string]string{
  129. "key1": "value1",
  130. "key2": "value2",
  131. },
  132. "metadata": map[string]map[string]string{
  133. "annotations": {},
  134. "labels": {},
  135. },
  136. },
  137. },
  138. },
  139. },
  140. {
  141. name: "MultipleWorkflows",
  142. workflows: []v1alpha1.WorkflowItem{
  143. {
  144. Name: "workflow1",
  145. Steps: []v1alpha1.WorkflowStep{
  146. {
  147. Name: "step1",
  148. Pull: &v1alpha1.WorkflowStepPull{
  149. Source: v1beta1.StoreSourceRef{
  150. SecretStoreRef: v1beta1.SecretStoreRef{
  151. Name: "store1",
  152. },
  153. },
  154. Data: []v1beta1.ExternalSecretData{
  155. {
  156. SecretKey: "key1",
  157. RemoteRef: v1beta1.ExternalSecretDataRemoteRef{
  158. Key: "key1",
  159. },
  160. },
  161. },
  162. },
  163. },
  164. },
  165. },
  166. {
  167. Name: "workflow2",
  168. Steps: []v1alpha1.WorkflowStep{
  169. {
  170. Name: "step1",
  171. Pull: &v1alpha1.WorkflowStepPull{
  172. Source: v1beta1.StoreSourceRef{
  173. SecretStoreRef: v1beta1.SecretStoreRef{
  174. Name: "store1",
  175. },
  176. },
  177. Data: []v1beta1.ExternalSecretData{
  178. {
  179. SecretKey: "key2",
  180. RemoteRef: v1beta1.ExternalSecretDataRemoteRef{
  181. Key: "key2",
  182. },
  183. },
  184. },
  185. },
  186. },
  187. },
  188. },
  189. },
  190. expected: map[string]interface{}{
  191. "workflow": map[string]interface{}{
  192. "data": map[string]string{
  193. "key2": "value2",
  194. },
  195. "metadata": map[string]map[string]string{
  196. "annotations": {},
  197. "labels": {},
  198. },
  199. },
  200. "workflows": map[string]interface{}{
  201. "workflow1": map[string]interface{}{
  202. "data": map[string]string{
  203. "key1": "value1",
  204. },
  205. "metadata": map[string]map[string]string{
  206. "annotations": {},
  207. "labels": {},
  208. },
  209. },
  210. "workflow2": map[string]interface{}{
  211. "data": map[string]string{
  212. "key2": "value2",
  213. },
  214. "metadata": map[string]map[string]string{
  215. "annotations": {},
  216. "labels": {},
  217. },
  218. },
  219. },
  220. },
  221. },
  222. {
  223. name: "ChainWorkflows",
  224. workflows: []v1alpha1.WorkflowItem{
  225. {
  226. Name: "workflow1",
  227. Steps: []v1alpha1.WorkflowStep{
  228. {
  229. Name: "step1",
  230. Pull: &v1alpha1.WorkflowStepPull{
  231. Source: v1beta1.StoreSourceRef{
  232. SecretStoreRef: v1beta1.SecretStoreRef{
  233. Name: "store1",
  234. },
  235. },
  236. Data: []v1beta1.ExternalSecretData{
  237. {
  238. SecretKey: "key1",
  239. RemoteRef: v1beta1.ExternalSecretDataRemoteRef{
  240. Key: "key1",
  241. },
  242. },
  243. },
  244. },
  245. },
  246. },
  247. },
  248. {
  249. Name: "workflow2",
  250. Steps: []v1alpha1.WorkflowStep{
  251. {
  252. Name: "step1",
  253. Pull: &v1alpha1.WorkflowStepPull{
  254. Source: v1beta1.StoreSourceRef{
  255. SecretStoreRef: v1beta1.SecretStoreRef{
  256. Name: "store1",
  257. },
  258. },
  259. Data: []v1beta1.ExternalSecretData{
  260. {
  261. SecretKey: "key2",
  262. RemoteRef: v1beta1.ExternalSecretDataRemoteRef{
  263. Key: "key2",
  264. },
  265. },
  266. },
  267. },
  268. },
  269. },
  270. },
  271. {
  272. Name: "workflow3",
  273. Steps: []v1alpha1.WorkflowStep{
  274. {
  275. Name: "step1",
  276. Template: &v1alpha1.WorkflowTemplate{
  277. Data: map[string]string{
  278. "aggregated": "{{ .workflows.workflow1.data.key1 }} and {{ .workflows.workflow2.data.key2 }}",
  279. },
  280. },
  281. },
  282. },
  283. },
  284. },
  285. expected: map[string]interface{}{
  286. "workflow": map[string]interface{}{
  287. "data": map[string]string{
  288. "aggregated": "value1 and value2",
  289. },
  290. "metadata": map[string]map[string]string{
  291. "annotations": {},
  292. "labels": {},
  293. },
  294. },
  295. "workflows": map[string]interface{}{
  296. "workflow1": map[string]interface{}{
  297. "data": map[string]string{
  298. "key1": "value1",
  299. },
  300. "metadata": map[string]map[string]string{
  301. "annotations": {},
  302. "labels": {},
  303. },
  304. },
  305. "workflow2": map[string]interface{}{
  306. "data": map[string]string{
  307. "key2": "value2",
  308. },
  309. "metadata": map[string]map[string]string{
  310. "annotations": {},
  311. "labels": {},
  312. },
  313. },
  314. "workflow3": map[string]interface{}{
  315. "data": map[string]string{
  316. "aggregated": "value1 and value2",
  317. },
  318. "metadata": map[string]map[string]string{
  319. "annotations": {},
  320. "labels": {},
  321. },
  322. },
  323. },
  324. },
  325. },
  326. }
  327. ctx := context.TODO()
  328. scheme := runtime.NewScheme()
  329. v1alpha1.AddToScheme(scheme)
  330. v1beta1.AddToScheme(scheme)
  331. namespace := "test-namespace"
  332. store1 := &v1beta1.SecretStore{
  333. ObjectMeta: metav1.ObjectMeta{
  334. Name: "store1",
  335. Namespace: namespace,
  336. },
  337. Status: v1beta1.SecretStoreStatus{
  338. Conditions: []v1beta1.SecretStoreStatusCondition{
  339. {
  340. Type: v1beta1.SecretStoreReady,
  341. Status: corev1.ConditionTrue,
  342. },
  343. },
  344. },
  345. Spec: v1beta1.SecretStoreSpec{
  346. Provider: &v1beta1.SecretStoreProvider{
  347. Fake: &v1beta1.FakeProvider{
  348. Data: []v1beta1.FakeProviderData{
  349. {
  350. Key: "key1",
  351. Value: "value1",
  352. },
  353. {
  354. Key: "key2",
  355. Value: "value2",
  356. },
  357. },
  358. },
  359. },
  360. },
  361. }
  362. client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(store1).Build()
  363. log := logr.Discard()
  364. for _, tt := range tests {
  365. t.Run(tt.name, func(t *testing.T) {
  366. runner := NewWorkflowRunner(ctx, client, namespace, tt.workflows, log)
  367. err := runner.Run()
  368. if err != nil {
  369. t.Errorf("unexpected error: %v", err)
  370. }
  371. if !reflect.DeepEqual(runner.inputs.ToMap(), tt.expected) {
  372. t.Errorf("unexpected result: got %v, want %v", runner.inputs.ToMap(), tt.expected)
  373. }
  374. })
  375. }
  376. }