pipeline_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. package core
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "gopkg.in/src-d/go-git.v4"
  12. "gopkg.in/src-d/go-git.v4/plumbing"
  13. "gopkg.in/src-d/go-git.v4/plumbing/object"
  14. "gopkg.in/src-d/hercules.v4/internal/pb"
  15. "gopkg.in/src-d/hercules.v4/internal/test"
  16. )
  17. type testPipelineItem struct {
  18. Initialized bool
  19. DepsConsumed bool
  20. Forked bool
  21. Merged *bool
  22. CommitMatches bool
  23. IndexMatches bool
  24. TestError bool
  25. }
  26. func (item *testPipelineItem) Name() string {
  27. return "Test"
  28. }
  29. func (item *testPipelineItem) Provides() []string {
  30. arr := [...]string{"test"}
  31. return arr[:]
  32. }
  33. func (item *testPipelineItem) Requires() []string {
  34. return []string{}
  35. }
  36. func (item *testPipelineItem) Configure(facts map[string]interface{}) {
  37. }
  38. func (item *testPipelineItem) ListConfigurationOptions() []ConfigurationOption {
  39. options := [...]ConfigurationOption{{
  40. Name: "TestOption",
  41. Description: "The option description.",
  42. Flag: "test-option",
  43. Type: IntConfigurationOption,
  44. Default: 10,
  45. }}
  46. return options[:]
  47. }
  48. func (item *testPipelineItem) Flag() string {
  49. return "mytest"
  50. }
  51. func (item *testPipelineItem) Features() []string {
  52. f := [...]string{"power"}
  53. return f[:]
  54. }
  55. func (item *testPipelineItem) Initialize(repository *git.Repository) {
  56. item.Initialized = repository != nil
  57. }
  58. func (item *testPipelineItem) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  59. if item.TestError {
  60. return nil, errors.New("error")
  61. }
  62. obj, exists := deps[DependencyCommit]
  63. item.DepsConsumed = exists
  64. if item.DepsConsumed {
  65. commit := obj.(*object.Commit)
  66. item.CommitMatches = commit.Hash == plumbing.NewHash(
  67. "af9ddc0db70f09f3f27b4b98e415592a7485171c")
  68. obj, item.DepsConsumed = deps[DependencyIndex]
  69. if item.DepsConsumed {
  70. item.IndexMatches = obj.(int) == 0
  71. }
  72. }
  73. return map[string]interface{}{"test": item}, nil
  74. }
  75. func (item *testPipelineItem) Fork(n int) []PipelineItem {
  76. result := make([]PipelineItem, n)
  77. for i := 0; i < n; i++ {
  78. result[i] = &testPipelineItem{Merged: item.Merged}
  79. }
  80. item.Forked = true
  81. return result
  82. }
  83. func (item *testPipelineItem) Merge(branches []PipelineItem) {
  84. *item.Merged = true
  85. }
  86. func (item *testPipelineItem) Finalize() interface{} {
  87. return item
  88. }
  89. func (item *testPipelineItem) Serialize(result interface{}, binary bool, writer io.Writer) error {
  90. return nil
  91. }
  92. type dependingTestPipelineItem struct {
  93. DependencySatisfied bool
  94. TestNilConsumeReturn bool
  95. }
  96. func (item *dependingTestPipelineItem) Name() string {
  97. return "Test2"
  98. }
  99. func (item *dependingTestPipelineItem) Provides() []string {
  100. arr := [...]string{"test2"}
  101. return arr[:]
  102. }
  103. func (item *dependingTestPipelineItem) Requires() []string {
  104. arr := [...]string{"test"}
  105. return arr[:]
  106. }
  107. func (item *dependingTestPipelineItem) ListConfigurationOptions() []ConfigurationOption {
  108. options := [...]ConfigurationOption{{
  109. Name: "TestOption2",
  110. Description: "The option description.",
  111. Flag: "test-option2",
  112. Type: IntConfigurationOption,
  113. Default: 10,
  114. }}
  115. return options[:]
  116. }
  117. func (item *dependingTestPipelineItem) Configure(facts map[string]interface{}) {
  118. }
  119. func (item *dependingTestPipelineItem) Initialize(repository *git.Repository) {
  120. }
  121. func (item *dependingTestPipelineItem) Flag() string {
  122. return "depflag"
  123. }
  124. func (item *dependingTestPipelineItem) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  125. _, exists := deps["test"]
  126. item.DependencySatisfied = exists
  127. if !item.TestNilConsumeReturn {
  128. return map[string]interface{}{"test2": item}, nil
  129. }
  130. return nil, nil
  131. }
  132. func (item *dependingTestPipelineItem) Fork(n int) []PipelineItem {
  133. return nil
  134. }
  135. func (item *dependingTestPipelineItem) Merge(branches []PipelineItem) {
  136. }
  137. func (item *dependingTestPipelineItem) Finalize() interface{} {
  138. return true
  139. }
  140. func (item *dependingTestPipelineItem) Serialize(result interface{}, binary bool, writer io.Writer) error {
  141. return nil
  142. }
  143. func TestPipelineFacts(t *testing.T) {
  144. pipeline := NewPipeline(test.Repository)
  145. pipeline.SetFact("fact", "value")
  146. assert.Equal(t, pipeline.GetFact("fact"), "value")
  147. }
  148. func TestPipelineFeatures(t *testing.T) {
  149. pipeline := NewPipeline(test.Repository)
  150. pipeline.SetFeature("feat")
  151. val, _ := pipeline.GetFeature("feat")
  152. assert.True(t, val)
  153. _, exists := pipeline.GetFeature("!")
  154. assert.False(t, exists)
  155. Registry.featureFlags.Set("777")
  156. defer func() {
  157. Registry.featureFlags = arrayFeatureFlags{Flags: []string{}, Choices: map[string]bool{}}
  158. }()
  159. pipeline.SetFeaturesFromFlags()
  160. _, exists = pipeline.GetFeature("777")
  161. assert.False(t, exists)
  162. assert.Panics(t, func() {
  163. pipeline.SetFeaturesFromFlags(
  164. &PipelineItemRegistry{}, &PipelineItemRegistry{})
  165. })
  166. }
  167. func TestPipelineRun(t *testing.T) {
  168. pipeline := NewPipeline(test.Repository)
  169. item := &testPipelineItem{Merged: new(bool)}
  170. pipeline.AddItem(item)
  171. pipeline.Initialize(map[string]interface{}{})
  172. assert.True(t, item.Initialized)
  173. commits := make([]*object.Commit, 1)
  174. commits[0], _ = test.Repository.CommitObject(plumbing.NewHash(
  175. "af9ddc0db70f09f3f27b4b98e415592a7485171c"))
  176. result, err := pipeline.Run(commits)
  177. assert.Nil(t, err)
  178. assert.Equal(t, 2, len(result))
  179. assert.Equal(t, item, result[item].(*testPipelineItem))
  180. common := result[nil].(*CommonAnalysisResult)
  181. assert.Equal(t, common.BeginTime, int64(1481719092))
  182. assert.Equal(t, common.EndTime, int64(1481719092))
  183. assert.Equal(t, common.CommitsNumber, 1)
  184. assert.True(t, common.RunTime.Nanoseconds()/1e6 < 100)
  185. assert.True(t, item.DepsConsumed)
  186. assert.True(t, item.CommitMatches)
  187. assert.True(t, item.IndexMatches)
  188. assert.False(t, item.Forked)
  189. assert.False(t, *item.Merged)
  190. pipeline.RemoveItem(item)
  191. result, err = pipeline.Run(commits)
  192. assert.Nil(t, err)
  193. assert.Equal(t, 1, len(result))
  194. }
  195. func TestPipelineRunBranches(t *testing.T) {
  196. pipeline := NewPipeline(test.Repository)
  197. item := &testPipelineItem{Merged: new(bool)}
  198. pipeline.AddItem(item)
  199. pipeline.Initialize(map[string]interface{}{})
  200. assert.True(t, item.Initialized)
  201. commits := make([]*object.Commit, 5)
  202. hashes := []string {
  203. "6db8065cdb9bb0758f36a7e75fc72ab95f9e8145",
  204. "f30daba81ff2bf0b3ba02a1e1441e74f8a4f6fee",
  205. "8a03b5620b1caa72ec9cb847ea88332621e2950a",
  206. "dd9dd084d5851d7dc4399fc7dbf3d8292831ebc5",
  207. "f4ed0405b14f006c0744029d87ddb3245607587a",
  208. }
  209. for i, h := range hashes {
  210. var err error
  211. commits[i], err = test.Repository.CommitObject(plumbing.NewHash(h))
  212. if err != nil {
  213. t.Fatal(err)
  214. }
  215. }
  216. result, err := pipeline.Run(commits)
  217. assert.Nil(t, err)
  218. assert.True(t, item.Forked)
  219. assert.True(t, *item.Merged)
  220. assert.Equal(t, 2, len(result))
  221. assert.Equal(t, item, result[item].(*testPipelineItem))
  222. common := result[nil].(*CommonAnalysisResult)
  223. assert.Equal(t, common.CommitsNumber, 5)
  224. }
  225. func TestPipelineOnProgress(t *testing.T) {
  226. pipeline := NewPipeline(test.Repository)
  227. progressOk := 0
  228. onProgress := func(step int, total int) {
  229. if step == 1 && total == 3 {
  230. progressOk++
  231. }
  232. if step == 2 && total == 3 {
  233. progressOk++
  234. }
  235. if step == 3 && total == 3 {
  236. progressOk++
  237. }
  238. }
  239. pipeline.OnProgress = onProgress
  240. commits := make([]*object.Commit, 1)
  241. commits[0], _ = test.Repository.CommitObject(plumbing.NewHash(
  242. "af9ddc0db70f09f3f27b4b98e415592a7485171c"))
  243. result, err := pipeline.Run(commits)
  244. assert.Nil(t, err)
  245. assert.Equal(t, 1, len(result))
  246. assert.Equal(t, 3, progressOk)
  247. }
  248. func TestPipelineCommits(t *testing.T) {
  249. pipeline := NewPipeline(test.Repository)
  250. commits := pipeline.Commits()
  251. assert.True(t, len(commits) >= 100)
  252. hashMap := map[plumbing.Hash]bool{}
  253. for _, c := range commits {
  254. hashMap[c.Hash] = true
  255. }
  256. assert.Equal(t, len(commits), len(hashMap))
  257. assert.Contains(t, hashMap, plumbing.NewHash(
  258. "cce947b98a050c6d356bc6ba95030254914027b1"))
  259. }
  260. func TestLoadCommitsFromFile(t *testing.T) {
  261. tmp, err := ioutil.TempFile("", "hercules-test-")
  262. assert.Nil(t, err)
  263. tmp.WriteString("cce947b98a050c6d356bc6ba95030254914027b1\n6db8065cdb9bb0758f36a7e75fc72ab95f9e8145")
  264. tmp.Close()
  265. defer os.Remove(tmp.Name())
  266. commits, err := LoadCommitsFromFile(tmp.Name(), test.Repository)
  267. assert.Nil(t, err)
  268. assert.Equal(t, len(commits), 2)
  269. assert.Equal(t, commits[0].Hash, plumbing.NewHash(
  270. "cce947b98a050c6d356bc6ba95030254914027b1"))
  271. assert.Equal(t, commits[1].Hash, plumbing.NewHash(
  272. "6db8065cdb9bb0758f36a7e75fc72ab95f9e8145"))
  273. commits, err = LoadCommitsFromFile("/WAT?xxx!", test.Repository)
  274. assert.Nil(t, commits)
  275. assert.NotNil(t, err)
  276. tmp, err = ioutil.TempFile("", "hercules-test-")
  277. assert.Nil(t, err)
  278. tmp.WriteString("WAT")
  279. tmp.Close()
  280. defer os.Remove(tmp.Name())
  281. commits, err = LoadCommitsFromFile(tmp.Name(), test.Repository)
  282. assert.Nil(t, commits)
  283. assert.NotNil(t, err)
  284. tmp, err = ioutil.TempFile("", "hercules-test-")
  285. assert.Nil(t, err)
  286. tmp.WriteString("ffffffffffffffffffffffffffffffffffffffff")
  287. tmp.Close()
  288. defer os.Remove(tmp.Name())
  289. commits, err = LoadCommitsFromFile(tmp.Name(), test.Repository)
  290. assert.Nil(t, commits)
  291. assert.NotNil(t, err)
  292. }
  293. func TestPipelineDeps(t *testing.T) {
  294. pipeline := NewPipeline(test.Repository)
  295. item1 := &dependingTestPipelineItem{}
  296. item2 := &testPipelineItem{}
  297. pipeline.AddItem(item1)
  298. pipeline.AddItem(item2)
  299. assert.Equal(t, pipeline.Len(), 2)
  300. pipeline.Initialize(map[string]interface{}{})
  301. commits := make([]*object.Commit, 1)
  302. commits[0], _ = test.Repository.CommitObject(plumbing.NewHash(
  303. "af9ddc0db70f09f3f27b4b98e415592a7485171c"))
  304. result, err := pipeline.Run(commits)
  305. assert.Nil(t, err)
  306. assert.True(t, result[item1].(bool))
  307. assert.Equal(t, result[item2], item2)
  308. item1.TestNilConsumeReturn = true
  309. assert.Panics(t, func() { pipeline.Run(commits) })
  310. }
  311. func TestPipelineDeployFeatures(t *testing.T) {
  312. pipeline := NewPipeline(test.Repository)
  313. pipeline.DeployItem(&testPipelineItem{})
  314. f, _ := pipeline.GetFeature("power")
  315. assert.True(t, f)
  316. }
  317. func TestPipelineError(t *testing.T) {
  318. pipeline := NewPipeline(test.Repository)
  319. item := &testPipelineItem{}
  320. item.TestError = true
  321. pipeline.AddItem(item)
  322. pipeline.Initialize(map[string]interface{}{})
  323. commits := make([]*object.Commit, 1)
  324. commits[0], _ = test.Repository.CommitObject(plumbing.NewHash(
  325. "af9ddc0db70f09f3f27b4b98e415592a7485171c"))
  326. result, err := pipeline.Run(commits)
  327. assert.Nil(t, result)
  328. assert.NotNil(t, err)
  329. }
  330. func TestCommonAnalysisResultMerge(t *testing.T) {
  331. c1 := CommonAnalysisResult{
  332. BeginTime: 1513620635, EndTime: 1513720635, CommitsNumber: 1, RunTime: 100}
  333. assert.Equal(t, c1.BeginTimeAsTime().Unix(), int64(1513620635))
  334. assert.Equal(t, c1.EndTimeAsTime().Unix(), int64(1513720635))
  335. c2 := CommonAnalysisResult{
  336. BeginTime: 1513620535, EndTime: 1513730635, CommitsNumber: 2, RunTime: 200}
  337. c1.Merge(&c2)
  338. assert.Equal(t, c1.BeginTime, int64(1513620535))
  339. assert.Equal(t, c1.EndTime, int64(1513730635))
  340. assert.Equal(t, c1.CommitsNumber, 3)
  341. assert.Equal(t, c1.RunTime.Nanoseconds(), int64(300))
  342. }
  343. func TestCommonAnalysisResultMetadata(t *testing.T) {
  344. c1 := &CommonAnalysisResult{
  345. BeginTime: 1513620635, EndTime: 1513720635, CommitsNumber: 1, RunTime: 100 * 1e6}
  346. meta := &pb.Metadata{}
  347. c1 = MetadataToCommonAnalysisResult(c1.FillMetadata(meta))
  348. assert.Equal(t, c1.BeginTimeAsTime().Unix(), int64(1513620635))
  349. assert.Equal(t, c1.EndTimeAsTime().Unix(), int64(1513720635))
  350. assert.Equal(t, c1.CommitsNumber, 1)
  351. assert.Equal(t, c1.RunTime.Nanoseconds(), int64(100*1e6))
  352. }
  353. func TestConfigurationOptionTypeString(t *testing.T) {
  354. opt := ConfigurationOptionType(0)
  355. assert.Equal(t, opt.String(), "")
  356. opt = ConfigurationOptionType(1)
  357. assert.Equal(t, opt.String(), "int")
  358. opt = ConfigurationOptionType(2)
  359. assert.Equal(t, opt.String(), "string")
  360. opt = ConfigurationOptionType(3)
  361. assert.Equal(t, opt.String(), "float")
  362. opt = ConfigurationOptionType(4)
  363. assert.Equal(t, opt.String(), "string")
  364. opt = ConfigurationOptionType(5)
  365. assert.Panics(t, func() { _ = opt.String() })
  366. }
  367. func TestConfigurationOptionFormatDefault(t *testing.T) {
  368. opt := ConfigurationOption{Type: StringConfigurationOption, Default: "ololo"}
  369. assert.Equal(t, opt.FormatDefault(), "\"ololo\"")
  370. opt = ConfigurationOption{Type: IntConfigurationOption, Default: 7}
  371. assert.Equal(t, opt.FormatDefault(), "7")
  372. opt = ConfigurationOption{Type: BoolConfigurationOption, Default: false}
  373. assert.Equal(t, opt.FormatDefault(), "false")
  374. opt = ConfigurationOption{Type: FloatConfigurationOption, Default: 0.5}
  375. assert.Equal(t, opt.FormatDefault(), "0.5")
  376. }
  377. func TestPrepareRunPlanTiny(t *testing.T) {
  378. rootCommit, err := test.Repository.CommitObject(plumbing.NewHash(
  379. "cce947b98a050c6d356bc6ba95030254914027b1"))
  380. if err != nil {
  381. t.Fatal(err)
  382. }
  383. plan := prepareRunPlan([]*object.Commit{rootCommit})
  384. assert.Len(t, plan, 1)
  385. assert.Equal(t, runActionCommit, plan[0].Action)
  386. assert.Equal(t, 0, plan[0].Items[0])
  387. assert.Equal(t, "cce947b98a050c6d356bc6ba95030254914027b1", plan[0].Commit.Hash.String())
  388. }
  389. func TestPrepareRunPlanSmall(t *testing.T) {
  390. cit, err := test.Repository.Log(&git.LogOptions{From: plumbing.ZeroHash})
  391. if err != nil {
  392. panic(err)
  393. }
  394. defer cit.Close()
  395. var commits []*object.Commit
  396. timeCutoff := time.Date(2016, 12, 15, 0, 0, 0, 0, time.FixedZone("CET", 7200))
  397. cit.ForEach(func(commit *object.Commit) error {
  398. reliableTime := time.Date(commit.Author.When.Year(), commit.Author.When.Month(),
  399. commit.Author.When.Day(), commit.Author.When.Hour(), commit.Author.When.Minute(),
  400. commit.Author.When.Second(), 0, time.FixedZone("CET", 7200))
  401. if reliableTime.Before(timeCutoff) {
  402. commits = append(commits, commit)
  403. }
  404. return nil
  405. })
  406. plan := prepareRunPlan(commits)
  407. /*for _, p := range plan {
  408. if p.Commit != nil {
  409. fmt.Println(p.Action, p.Commit.Hash.String(), p.Items)
  410. } else {
  411. fmt.Println(p.Action, strings.Repeat(" ", 40), p.Items)
  412. }
  413. }*/
  414. // fork, merge and one artificial commit per branch
  415. assert.Len(t, plan, len(commits))
  416. assert.Equal(t, runActionCommit, plan[0].Action)
  417. assert.Equal(t, 0, plan[0].Items[0])
  418. assert.Equal(t, "cce947b98a050c6d356bc6ba95030254914027b1", plan[0].Commit.Hash.String())
  419. assert.Equal(t, runActionCommit, plan[1].Action)
  420. assert.Equal(t, 0, plan[1].Items[0])
  421. assert.Equal(t, "a3ee37f91f0d705ec9c41ae88426f0ae44b2fbc3", plan[1].Commit.Hash.String())
  422. assert.Equal(t, runActionCommit, plan[9].Action)
  423. assert.Equal(t, 0, plan[9].Items[0])
  424. assert.Equal(t, "a28e9064c70618dc9d68e1401b889975e0680d11", plan[9].Commit.Hash.String())
  425. }
  426. func TestMergeDag(t *testing.T) {
  427. cit, err := test.Repository.Log(&git.LogOptions{From: plumbing.ZeroHash})
  428. if err != nil {
  429. panic(err)
  430. }
  431. defer cit.Close()
  432. var commits []*object.Commit
  433. timeCutoff := time.Date(2017, 8, 12, 0, 0, 0, 0, time.FixedZone("CET", 7200))
  434. cit.ForEach(func(commit *object.Commit) error {
  435. reliableTime := time.Date(commit.Author.When.Year(), commit.Author.When.Month(),
  436. commit.Author.When.Day(), commit.Author.When.Hour(), commit.Author.When.Minute(),
  437. commit.Author.When.Second(), 0, time.FixedZone("CET", 7200))
  438. if reliableTime.Before(timeCutoff) {
  439. commits = append(commits, commit)
  440. }
  441. return nil
  442. })
  443. hashes, dag := buildDag(commits)
  444. leaveRootComponent(hashes, dag)
  445. mergedDag, _ := mergeDag(hashes, dag)
  446. for key, vals := range mergedDag {
  447. if key != plumbing.NewHash("a28e9064c70618dc9d68e1401b889975e0680d11") &&
  448. key != plumbing.NewHash("db325a212d0bc99b470e000641d814745024bbd5") {
  449. assert.Len(t, vals, len(dag[key]), key.String())
  450. } else {
  451. mvals := map[string]bool{}
  452. for _, val := range vals {
  453. mvals[val.Hash.String()] = true
  454. }
  455. if key == plumbing.NewHash("a28e9064c70618dc9d68e1401b889975e0680d11") {
  456. assert.Contains(t, mvals, "db325a212d0bc99b470e000641d814745024bbd5")
  457. assert.Contains(t, mvals, "be9b61e09b08b98e64ed461a4004c9e2412f78ee")
  458. }
  459. if key == plumbing.NewHash("db325a212d0bc99b470e000641d814745024bbd5") {
  460. assert.Contains(t, mvals, "f30daba81ff2bf0b3ba02a1e1441e74f8a4f6fee")
  461. assert.Contains(t, mvals, "8a03b5620b1caa72ec9cb847ea88332621e2950a")
  462. }
  463. }
  464. }
  465. assert.Len(t, mergedDag, 8)
  466. assert.Contains(t, mergedDag, plumbing.NewHash("cce947b98a050c6d356bc6ba95030254914027b1"))
  467. assert.Contains(t, mergedDag, plumbing.NewHash("a3ee37f91f0d705ec9c41ae88426f0ae44b2fbc3"))
  468. assert.Contains(t, mergedDag, plumbing.NewHash("a28e9064c70618dc9d68e1401b889975e0680d11"))
  469. assert.Contains(t, mergedDag, plumbing.NewHash("be9b61e09b08b98e64ed461a4004c9e2412f78ee"))
  470. assert.Contains(t, mergedDag, plumbing.NewHash("db325a212d0bc99b470e000641d814745024bbd5"))
  471. assert.Contains(t, mergedDag, plumbing.NewHash("f30daba81ff2bf0b3ba02a1e1441e74f8a4f6fee"))
  472. assert.Contains(t, mergedDag, plumbing.NewHash("8a03b5620b1caa72ec9cb847ea88332621e2950a"))
  473. assert.Contains(t, mergedDag, plumbing.NewHash("dd9dd084d5851d7dc4399fc7dbf3d8292831ebc5"))
  474. queue := []plumbing.Hash{plumbing.NewHash("cce947b98a050c6d356bc6ba95030254914027b1")}
  475. visited := map[plumbing.Hash]bool{}
  476. for len(queue) > 0 {
  477. head := queue[len(queue)-1]
  478. queue = queue[:len(queue)-1]
  479. if visited[head] {
  480. continue
  481. }
  482. visited[head] = true
  483. for _, child := range mergedDag[head] {
  484. queue = append(queue, child.Hash)
  485. }
  486. }
  487. assert.Len(t, visited, 8)
  488. }
  489. func TestPrepareRunPlanBig(t *testing.T) {
  490. cases := [][7]int {
  491. {2017, 8, 9, 0, 0, 0, 0},
  492. {2017, 8, 10, 0, 0, 0, 0},
  493. {2017, 8, 24, 1, 1, 1, 1},
  494. {2017, 9, 19, 1-2, 1, 1, 1},
  495. {2017, 9, 23, 1-2, 1, 1, 1},
  496. {2017, 12, 8, 1, 1, 1, 1},
  497. {2017, 12, 9, 1, 1, 1, 1},
  498. {2017, 12, 10, 1, 1, 1, 1},
  499. {2017, 12, 11, 2, 2, 2, 2},
  500. {2017, 12, 19, 3, 3, 3, 3},
  501. {2017, 12, 27, 3, 3, 3, 3},
  502. {2018, 1, 10, 3, 3, 3, 3},
  503. {2018, 1, 16, 3, 3, 3, 3},
  504. {2018, 1, 18, 4, 5, 4, 4},
  505. {2018, 1, 23, 5, 5, 5, 5},
  506. {2018, 3, 12, 6, 6, 6, 6},
  507. {2018, 5, 13, 6, 6, 6, 6},
  508. {2018, 5, 16, 7, 7, 7, 7},
  509. }
  510. for _, testCase := range cases {
  511. func() {
  512. cit, err := test.Repository.Log(&git.LogOptions{From: plumbing.ZeroHash})
  513. if err != nil {
  514. panic(err)
  515. }
  516. defer cit.Close()
  517. var commits []*object.Commit
  518. timeCutoff := time.Date(
  519. testCase[0], time.Month(testCase[1]), testCase[2], 0, 0, 0, 0, time.FixedZone("CET", 7200))
  520. cit.ForEach(func(commit *object.Commit) error {
  521. reliableTime := time.Date(commit.Author.When.Year(), commit.Author.When.Month(),
  522. commit.Author.When.Day(), commit.Author.When.Hour(), commit.Author.When.Minute(),
  523. commit.Author.When.Second(), 0, time.FixedZone("CET", 7200))
  524. if reliableTime.Before(timeCutoff) {
  525. commits = append(commits, commit)
  526. }
  527. return nil
  528. })
  529. plan := prepareRunPlan(commits)
  530. /*for _, p := range plan {
  531. if p.Commit != nil {
  532. fmt.Println(p.Action, p.Commit.Hash.String(), p.Items)
  533. } else {
  534. fmt.Println(p.Action, strings.Repeat(" ", 40), p.Items)
  535. }
  536. }*/
  537. numCommits := 0
  538. numForks := 0
  539. numMerges := 0
  540. numDeletes := 0
  541. processed := map[plumbing.Hash]map[int]int{}
  542. for _, p := range plan {
  543. switch p.Action {
  544. case runActionCommit:
  545. branches := processed[p.Commit.Hash]
  546. if branches == nil {
  547. branches = map[int]int{}
  548. processed[p.Commit.Hash] = branches
  549. }
  550. branches[p.Items[0]]++
  551. for _, parent := range p.Commit.ParentHashes {
  552. assert.Contains(t, processed, parent)
  553. }
  554. numCommits++
  555. case runActionFork:
  556. numForks++
  557. case runActionMerge:
  558. counts := map[int]int{}
  559. for _, i := range p.Items {
  560. counts[i]++
  561. }
  562. for x, v := range counts {
  563. assert.Equal(t, 1, v, x)
  564. }
  565. numMerges++
  566. case runActionDelete:
  567. numDeletes++
  568. }
  569. }
  570. for c, branches := range processed {
  571. for b, v := range branches {
  572. assert.Equal(t, 1, v, fmt.Sprint(c.String(), b))
  573. }
  574. }
  575. assert.Equal(t, numCommits, len(commits)+testCase[3], fmt.Sprintf("commits %v", testCase))
  576. assert.Equal(t, numForks, testCase[4], fmt.Sprintf("forks %v", testCase))
  577. assert.Equal(t, numMerges, testCase[5], fmt.Sprintf("merges %v", testCase))
  578. assert.Equal(t, numDeletes, testCase[6], fmt.Sprintf("deletes %v", testCase))
  579. }()
  580. }
  581. }