pipeline_test.go 22 KB

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