command_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package command
  2. import (
  3. "context"
  4. "encoding/json"
  5. "flag"
  6. "path/filepath"
  7. "testing"
  8. "go-common/app/tool/saga/conf"
  9. "go-common/app/tool/saga/dao"
  10. "go-common/app/tool/saga/model"
  11. "go-common/app/tool/saga/service/gitlab"
  12. "github.com/smartystreets/goconvey/convey"
  13. )
  14. var (
  15. c *Command
  16. gitlabHookCommentTest = []byte(`{
  17. "object_kind":"note",
  18. "event_type":"note",
  19. "user":{
  20. "name":"changhengyuan",
  21. "username":"changhengyuan",
  22. "avatar_url":"https://www.gravatar.com/avatar/d3218d34473c6fb4d18a770f14e59a89?s=80\u0026d=identicon"
  23. },
  24. "project_id":35,
  25. "project":{
  26. "id":35,
  27. "name":"test-saga",
  28. "description":"",
  29. "web_url":"http://gitlab.bilibili.co/changhengyuan/test-saga",
  30. "avatar_url":null,
  31. "git_ssh_url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  32. "git_http_url":"http://gitlab.bilibili.co/changhengyuan/test-saga.git",
  33. "namespace":"changhengyuan",
  34. "visibility_level":20,
  35. "path_with_namespace":"changhengyuan/test-saga",
  36. "default_branch":"master",
  37. "ci_config_path":null,
  38. "homepage":"http://gitlab.bilibili.co/changhengyuan/test-saga",
  39. "url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  40. "ssh_url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  41. "http_url":"http://gitlab.bilibili.co/changhengyuan/test-saga.git"},
  42. "object_attributes":{
  43. "id":3040,
  44. "note":"test",
  45. "noteable_type":"MergeRequest",
  46. "author_id":15,
  47. "created_at":"2018-09-26 06:55:13 UTC",
  48. "updated_at":"2018-09-26 06:55:13 UTC",
  49. "project_id":35,
  50. "attachment":null,
  51. "line_code":null,
  52. "commit_id":"",
  53. "noteable_id":390,
  54. "system":false,
  55. "st_diff":null,
  56. "updated_by_id":null,
  57. "type":null,
  58. "position":null,
  59. "original_position":null,
  60. "resolved_at":null,
  61. "resolved_by_id":null,
  62. "discussion_id":"450c34e4c0f9e925bdc6a24c2ae4920d7a394ebc",
  63. "change_position":null,
  64. "resolved_by_push":null,
  65. "url":"http://gitlab.bilibili.co/changhengyuan/test-saga/merge_requests/52#note_3040"
  66. },
  67. "repository":{
  68. "name":"test-saga",
  69. "url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  70. "description":"",
  71. "homepage":"http://gitlab.bilibili.co/changhengyuan/test-saga"
  72. },
  73. "merge_request":{
  74. "assignee_id":null,
  75. "author_id":15,
  76. "created_at":"2018-09-26 06:41:55 UTC",
  77. "description":"",
  78. "head_pipeline_id":4510,
  79. "id":390,
  80. "iid":52,
  81. "last_edited_at":null,
  82. "last_edited_by_id":null,
  83. "merge_commit_sha":null,
  84. "merge_error":null,
  85. "merge_params":{
  86. "force_remove_source_branch":"0"
  87. },
  88. "merge_status":"cannot_be_merged",
  89. "merge_user_id":null,
  90. "merge_when_pipeline_succeeds":false,
  91. "milestone_id":null,
  92. "source_branch":"test-branch",
  93. "source_project_id":35,
  94. "state":"opened",
  95. "target_branch":"master",
  96. "target_project_id":35,
  97. "time_estimate":0,
  98. "title":"Test branch",
  99. "updated_at":"2018-09-26 06:54:33 UTC",
  100. "updated_by_id":null,
  101. "url":"http://gitlab.bilibili.co/changhengyuan/test-saga/merge_requests/52",
  102. "source":{
  103. "id":35,
  104. "name":"test-saga",
  105. "description":"",
  106. "web_url":"http://gitlab.bilibili.co/changhengyuan/test-saga",
  107. "avatar_url":null,
  108. "git_ssh_url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  109. "git_http_url":"http://gitlab.bilibili.co/changhengyuan/test-saga.git",
  110. "namespace":"changhengyuan",
  111. "visibility_level":20,
  112. "path_with_namespace":"changhengyuan/test-saga",
  113. "default_branch":"master",
  114. "ci_config_path":null,
  115. "homepage":"http://gitlab.bilibili.co/changhengyuan/test-saga",
  116. "url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  117. "ssh_url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  118. "http_url":"http://gitlab.bilibili.co/changhengyuan/test-saga.git"
  119. },
  120. "target":{
  121. "id":35,
  122. "name":"test-saga",
  123. "description":"",
  124. "web_url":"http://gitlab.bilibili.co/changhengyuan/test-saga",
  125. "avatar_url":null,
  126. "git_ssh_url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  127. "git_http_url":"http://gitlab.bilibili.co/changhengyuan/test-saga.git",
  128. "namespace":"changhengyuan",
  129. "visibility_level":20,
  130. "path_with_namespace":"changhengyuan/test-saga",
  131. "default_branch":"master",
  132. "ci_config_path":null,
  133. "homepage":"http://gitlab.bilibili.co/changhengyuan/test-saga",
  134. "url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  135. "ssh_url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  136. "http_url":"http://gitlab.bilibili.co/changhengyuan/test-saga.git"
  137. },
  138. "last_commit":{
  139. "id":"51e9c3ba2ceac496dbaf55f0db564ab6a15e20eb",
  140. "message":"add CONTRIBUTORS.md\n",
  141. "timestamp":"2018-09-17T18:02:13+08:00",
  142. "url":"http://gitlab.bilibili.co/changhengyuan/test-saga/commit/51e9c3ba2ceac496dbaf55f0db564ab6a15e20eb",
  143. "author":{
  144. "name":"哔哩哔哩",
  145. "email":"bilibili@bilibilideMac-mini.local"
  146. }
  147. },
  148. "work_in_progress":false,
  149. "total_time_spent":0,
  150. "human_total_time_spent":null,
  151. "human_time_estimate":null}}`)
  152. )
  153. func init() {
  154. dir, _ := filepath.Abs("../../cmd/saga-test.toml")
  155. flag.Set("conf", dir)
  156. conf.Init()
  157. c = New(&dao.Dao{}, &gitlab.Gitlab{})
  158. }
  159. func TestCommandNew(t *testing.T) {
  160. convey.Convey("New", t, func(ctx convey.C) {
  161. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  162. ctx.Convey("Then c should not be nil.", func(ctx convey.C) {
  163. ctx.So(c, convey.ShouldNotBeNil)
  164. })
  165. })
  166. })
  167. }
  168. func TestCommandExec(t *testing.T) {
  169. convey.Convey("Exec", t, func(ctx convey.C) {
  170. var (
  171. ct = context.Background()
  172. cmd = "+1"
  173. event = &model.HookComment{}
  174. repo = &model.Repo{}
  175. c = &Command{}
  176. )
  177. _ = json.Unmarshal(gitlabHookCommentTest, event)
  178. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  179. err := c.Exec(ct, cmd, event, repo)
  180. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  181. ctx.So(err, convey.ShouldBeNil)
  182. })
  183. })
  184. })
  185. }
  186. func TestCommandRegister(t *testing.T) {
  187. convey.Convey("register", t, func(ctx convey.C) {
  188. var (
  189. cmd = "test_cmd"
  190. f cmdFunc
  191. )
  192. ctx.Convey("When everything goes positive", func(ctx convey.C) {
  193. c.register(cmd, f)
  194. ctx.Convey("No return values", func(ctx convey.C) {
  195. cmd, ok := c.cmds["test_cmd"]
  196. ctx.So(ok, convey.ShouldEqual, true)
  197. ctx.So(cmd, convey.ShouldEqual, f)
  198. })
  199. })
  200. })
  201. }