approve_former_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package test
  2. import (
  3. _ "embed"
  4. "encoding/json"
  5. "fmt"
  6. "git.sxidc.com/go-tools/argo-api"
  7. "git.sxidc.com/go-tools/argo-api/approve_former"
  8. "git.sxidc.com/go-tools/argo-api/client"
  9. "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
  10. "github.com/pkg/errors"
  11. "sync"
  12. "testing"
  13. "time"
  14. )
  15. //go:embed approve/approve.json
  16. var approveJson []byte
  17. func TestParseWorkflowTemplate(t *testing.T) {
  18. approveJsonMap := make(map[string]any)
  19. err := json.Unmarshal(approveJson, &approveJsonMap)
  20. if err != nil {
  21. t.Fatalf("%+v\n", errors.New(err.Error()))
  22. }
  23. workflowTemplateDefinition, err := approve_former.ParseWorkflowTemplate("test", approveJsonMap)
  24. if err != nil {
  25. t.Fatalf("%+v\n", err)
  26. }
  27. fmt.Println(workflowTemplateDefinition)
  28. }
  29. func TestApproveFormer(t *testing.T) {
  30. flow := approve_former.NewFlow("flow",
  31. approve_former.NewSign("A"),
  32. approve_former.NewOr(
  33. approve_former.NewSign("B"),
  34. approve_former.NewSign("C"),
  35. ),
  36. approve_former.NewAnd(
  37. approve_former.NewSign("D"),
  38. approve_former.NewSign("E"),
  39. ),
  40. approve_former.NewSign("F"),
  41. )
  42. flowDefinition, err := flow.Render(nil)
  43. if err != nil {
  44. t.Fatalf("%+v\n", err)
  45. }
  46. fmt.Println(flowDefinition)
  47. }
  48. func TestUseApproveFormer(t *testing.T) {
  49. err := argo.Init(kubeConfig)
  50. if err != nil {
  51. t.Fatalf("%+v\n", err)
  52. }
  53. defer argo.Destroy()
  54. flowName := "flow-" + time.Now().Format("20060102150405")
  55. flow := approve_former.NewFlow(flowName,
  56. approve_former.NewSign("A"),
  57. approve_former.NewOr(
  58. approve_former.NewSign("B"),
  59. approve_former.NewSign("C"),
  60. ),
  61. approve_former.NewAnd(
  62. approve_former.NewSign("D"),
  63. approve_former.NewSign("E"),
  64. ),
  65. approve_former.NewSign("F"),
  66. )
  67. flowDefinition, err := flow.Render(nil)
  68. if err != nil {
  69. t.Fatalf("%+v\n", err)
  70. }
  71. err = argo.GetInstance().CreateWorkflowTemplate(client.CreateWorkflowTemplateParams{
  72. Namespace: namespace,
  73. TemplateDefinition: flowDefinition,
  74. })
  75. if err != nil {
  76. t.Fatalf("%+v\n", err)
  77. }
  78. defer func() {
  79. err := argo.GetInstance().DeleteWorkflowTemplate(client.DeleteWorkflowTemplateParams{
  80. Namespace: namespace,
  81. Name: flowName,
  82. })
  83. if err != nil {
  84. t.Fatalf("%+v\n", err)
  85. }
  86. }()
  87. workflowName, err := argo.GetInstance().SubmitWorkflowFromWorkflowTemplate(client.SubmitWorkflowFromWorkflowTemplateParams{
  88. Namespace: namespace,
  89. TemplateName: flowName,
  90. Parameters: []string{
  91. `object={"name": "yjp", "age": 37}`,
  92. },
  93. })
  94. if err != nil {
  95. t.Fatalf("%+v\n", err)
  96. }
  97. defer func() {
  98. err := argo.GetInstance().DeleteWorkflow(client.DeleteWorkflowParams{
  99. Namespace: namespace,
  100. Name: workflowName,
  101. })
  102. if err != nil {
  103. t.Fatalf("%+v\n", err)
  104. }
  105. }()
  106. wg := sync.WaitGroup{}
  107. wg.Add(1)
  108. resumeMap := make(map[string]bool)
  109. err = argo.GetInstance().RegisterWorkflowWatcher(namespace, workflowName, func(wf *v1alpha1.Workflow) {
  110. if wf.Status.Nodes == nil {
  111. return
  112. }
  113. for _, node := range wf.Status.Nodes {
  114. if node.DisplayName == "A" && node.Phase == "Running" && node.Outputs.Parameters[0].Value == nil {
  115. _, ok := resumeMap[node.DisplayName]
  116. if ok {
  117. return
  118. }
  119. err := argo.GetInstance().SetWorkflow(client.SetWorkflowParams{
  120. Namespace: namespace,
  121. Name: workflowName,
  122. NodeFieldSelector: "displayName=A",
  123. OutputParameters: `{"approve": "通过"}`,
  124. })
  125. if err != nil {
  126. t.Fatalf("%+v\n", err)
  127. }
  128. err = argo.GetInstance().ResumeWorkflow(client.ResumeWorkflowParams{
  129. Namespace: namespace,
  130. Name: workflowName,
  131. NodeFieldSelector: "displayName=A",
  132. })
  133. if err != nil {
  134. t.Fatalf("%+v\n", err)
  135. }
  136. resumeMap[node.DisplayName] = true
  137. }
  138. if node.DisplayName == "B" && node.Phase == "Running" && node.Outputs.Parameters[0].Value == nil {
  139. _, ok := resumeMap[node.DisplayName]
  140. if ok {
  141. return
  142. }
  143. err := argo.GetInstance().SetWorkflow(client.SetWorkflowParams{
  144. Namespace: namespace,
  145. Name: workflowName,
  146. NodeFieldSelector: "displayName=B",
  147. OutputParameters: `{"approve": "通过"}`,
  148. })
  149. if err != nil {
  150. t.Fatalf("%+v\n", err)
  151. }
  152. err = argo.GetInstance().ResumeWorkflow(client.ResumeWorkflowParams{
  153. Namespace: namespace,
  154. Name: workflowName,
  155. NodeFieldSelector: "displayName=B",
  156. })
  157. if err != nil {
  158. t.Fatalf("%+v\n", err)
  159. }
  160. resumeMap[node.DisplayName] = true
  161. }
  162. if node.DisplayName == "C" && node.Phase == "Running" && node.Outputs.Parameters[0].Value == nil {
  163. _, ok := resumeMap[node.DisplayName]
  164. if ok {
  165. return
  166. }
  167. err := argo.GetInstance().SetWorkflow(client.SetWorkflowParams{
  168. Namespace: namespace,
  169. Name: workflowName,
  170. NodeFieldSelector: "displayName=C",
  171. OutputParameters: `{"approve": "拒绝"}`,
  172. })
  173. if err != nil {
  174. t.Fatalf("%+v\n", err)
  175. }
  176. err = argo.GetInstance().ResumeWorkflow(client.ResumeWorkflowParams{
  177. Namespace: namespace,
  178. Name: workflowName,
  179. NodeFieldSelector: "displayName=C",
  180. })
  181. if err != nil {
  182. t.Fatalf("%+v\n", err)
  183. }
  184. resumeMap[node.DisplayName] = true
  185. }
  186. if node.DisplayName == "D" && node.Phase == "Running" && node.Outputs.Parameters[0].Value == nil {
  187. _, ok := resumeMap[node.DisplayName]
  188. if ok {
  189. return
  190. }
  191. err := argo.GetInstance().SetWorkflow(client.SetWorkflowParams{
  192. Namespace: namespace,
  193. Name: workflowName,
  194. NodeFieldSelector: "displayName=D",
  195. OutputParameters: `{"approve": "通过"}`,
  196. })
  197. if err != nil {
  198. t.Fatalf("%+v\n", err)
  199. }
  200. err = argo.GetInstance().ResumeWorkflow(client.ResumeWorkflowParams{
  201. Namespace: namespace,
  202. Name: workflowName,
  203. NodeFieldSelector: "displayName=D",
  204. })
  205. if err != nil {
  206. t.Fatalf("%+v\n", err)
  207. }
  208. resumeMap[node.DisplayName] = true
  209. }
  210. if node.DisplayName == "E" && node.Phase == "Running" && node.Outputs.Parameters[0].Value == nil {
  211. _, ok := resumeMap[node.DisplayName]
  212. if ok {
  213. return
  214. }
  215. err := argo.GetInstance().SetWorkflow(client.SetWorkflowParams{
  216. Namespace: namespace,
  217. Name: workflowName,
  218. NodeFieldSelector: "displayName=E",
  219. OutputParameters: `{"approve": "通过"}`,
  220. })
  221. if err != nil {
  222. t.Fatalf("%+v\n", err)
  223. }
  224. err = argo.GetInstance().ResumeWorkflow(client.ResumeWorkflowParams{
  225. Namespace: namespace,
  226. Name: workflowName,
  227. NodeFieldSelector: "displayName=E",
  228. })
  229. if err != nil {
  230. t.Fatalf("%+v\n", err)
  231. }
  232. resumeMap[node.DisplayName] = true
  233. }
  234. if node.DisplayName == "F" && node.Phase == "Running" && node.Outputs.Parameters[0].Value == nil {
  235. _, ok := resumeMap[node.DisplayName]
  236. if ok {
  237. return
  238. }
  239. err := argo.GetInstance().SetWorkflow(client.SetWorkflowParams{
  240. Namespace: namespace,
  241. Name: workflowName,
  242. NodeFieldSelector: "displayName=F",
  243. OutputParameters: `{"approve": "通过"}`,
  244. })
  245. if err != nil {
  246. t.Fatalf("%+v\n", err)
  247. }
  248. err = argo.GetInstance().ResumeWorkflow(client.ResumeWorkflowParams{
  249. Namespace: namespace,
  250. Name: workflowName,
  251. NodeFieldSelector: "displayName=F",
  252. })
  253. if err != nil {
  254. t.Fatalf("%+v\n", err)
  255. }
  256. resumeMap[node.DisplayName] = true
  257. }
  258. if node.DisplayName == "F" && node.Phase == "Succeeded" && node.Outputs.Parameters[0].Value.String() == "通过" {
  259. wg.Done()
  260. }
  261. }
  262. })
  263. if err != nil {
  264. t.Fatalf("%+v\n", err)
  265. }
  266. defer argo.GetInstance().UnregisterWorkflowWatcher(namespace, workflowName)
  267. wg.Wait()
  268. }