pipeline_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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.True(t, item.DepsConsumed)
  196. assert.True(t, item.CommitMatches)
  197. assert.True(t, item.IndexMatches)
  198. assert.Equal(t, 1, *item.MergeState)
  199. assert.True(t, item.Forked)
  200. assert.False(t, *item.Merged)
  201. pipeline.RemoveItem(item)
  202. result, err = pipeline.Run(commits)
  203. assert.Nil(t, err)
  204. assert.Equal(t, 1, len(result))
  205. }
  206. func TestPipelineRunBranches(t *testing.T) {
  207. pipeline := NewPipeline(test.Repository)
  208. item := &testPipelineItem{}
  209. pipeline.AddItem(item)
  210. pipeline.Initialize(map[string]interface{}{})
  211. assert.True(t, item.Initialized)
  212. commits := make([]*object.Commit, 5)
  213. hashes := []string {
  214. "6db8065cdb9bb0758f36a7e75fc72ab95f9e8145",
  215. "f30daba81ff2bf0b3ba02a1e1441e74f8a4f6fee",
  216. "8a03b5620b1caa72ec9cb847ea88332621e2950a",
  217. "dd9dd084d5851d7dc4399fc7dbf3d8292831ebc5",
  218. "f4ed0405b14f006c0744029d87ddb3245607587a",
  219. }
  220. for i, h := range hashes {
  221. var err error
  222. commits[i], err = test.Repository.CommitObject(plumbing.NewHash(h))
  223. if err != nil {
  224. t.Fatal(err)
  225. }
  226. }
  227. result, err := pipeline.Run(commits)
  228. assert.Nil(t, err)
  229. assert.True(t, item.Forked)
  230. assert.True(t, *item.Merged)
  231. assert.Equal(t, 2, len(result))
  232. assert.Equal(t, item, result[item].(*testPipelineItem))
  233. common := result[nil].(*CommonAnalysisResult)
  234. assert.Equal(t, common.CommitsNumber, 5)
  235. assert.Equal(t, *item.MergeState, 8)
  236. }
  237. func TestPipelineOnProgress(t *testing.T) {
  238. pipeline := NewPipeline(test.Repository)
  239. progressOk := 0
  240. onProgress := func(step int, total int) {
  241. if step == 1 && total == 4 {
  242. progressOk++
  243. }
  244. if step == 2 && total == 4 {
  245. progressOk++
  246. }
  247. if step == 3 && total == 4 {
  248. progressOk++
  249. }
  250. if step == 4 && total == 4 {
  251. progressOk++
  252. }
  253. }
  254. pipeline.OnProgress = onProgress
  255. commits := make([]*object.Commit, 1)
  256. commits[0], _ = test.Repository.CommitObject(plumbing.NewHash(
  257. "af9ddc0db70f09f3f27b4b98e415592a7485171c"))
  258. result, err := pipeline.Run(commits)
  259. assert.Nil(t, err)
  260. assert.Equal(t, 1, len(result))
  261. assert.Equal(t, 4, progressOk)
  262. }
  263. func TestPipelineCommitsFull(t *testing.T) {
  264. pipeline := NewPipeline(test.Repository)
  265. commits, err := pipeline.Commits(false)
  266. assert.Nil(t, err)
  267. assert.True(t, len(commits) >= 100)
  268. hashMap := map[plumbing.Hash]bool{}
  269. for _, c := range commits {
  270. hashMap[c.Hash] = true
  271. }
  272. assert.Equal(t, len(commits), len(hashMap))
  273. assert.Contains(t, hashMap, plumbing.NewHash(
  274. "cce947b98a050c6d356bc6ba95030254914027b1"))
  275. assert.Contains(t, hashMap, plumbing.NewHash(
  276. "a3ee37f91f0d705ec9c41ae88426f0ae44b2fbc3"))
  277. }
  278. func TestPipelineCommitsFirstParent(t *testing.T) {
  279. pipeline := NewPipeline(test.Repository)
  280. commits, err := pipeline.Commits(true)
  281. assert.Nil(t, err)
  282. assert.True(t, len(commits) >= 100)
  283. hashMap := map[plumbing.Hash]bool{}
  284. for _, c := range commits {
  285. hashMap[c.Hash] = true
  286. }
  287. assert.Equal(t, len(commits), len(hashMap))
  288. assert.Contains(t, hashMap, plumbing.NewHash(
  289. "cce947b98a050c6d356bc6ba95030254914027b1"))
  290. assert.NotContains(t, hashMap, plumbing.NewHash(
  291. "a3ee37f91f0d705ec9c41ae88426f0ae44b2fbc3"))
  292. }
  293. func TestLoadCommitsFromFile(t *testing.T) {
  294. tmp, err := ioutil.TempFile("", "hercules-test-")
  295. assert.Nil(t, err)
  296. tmp.WriteString("cce947b98a050c6d356bc6ba95030254914027b1\n6db8065cdb9bb0758f36a7e75fc72ab95f9e8145")
  297. tmp.Close()
  298. defer os.Remove(tmp.Name())
  299. commits, err := LoadCommitsFromFile(tmp.Name(), test.Repository)
  300. assert.Nil(t, err)
  301. assert.Equal(t, len(commits), 2)
  302. assert.Equal(t, commits[0].Hash, plumbing.NewHash(
  303. "cce947b98a050c6d356bc6ba95030254914027b1"))
  304. assert.Equal(t, commits[1].Hash, plumbing.NewHash(
  305. "6db8065cdb9bb0758f36a7e75fc72ab95f9e8145"))
  306. commits, err = LoadCommitsFromFile("/WAT?xxx!", test.Repository)
  307. assert.Nil(t, commits)
  308. assert.NotNil(t, err)
  309. tmp, err = ioutil.TempFile("", "hercules-test-")
  310. assert.Nil(t, err)
  311. tmp.WriteString("WAT")
  312. tmp.Close()
  313. defer os.Remove(tmp.Name())
  314. commits, err = LoadCommitsFromFile(tmp.Name(), test.Repository)
  315. assert.Nil(t, commits)
  316. assert.NotNil(t, err)
  317. tmp, err = ioutil.TempFile("", "hercules-test-")
  318. assert.Nil(t, err)
  319. tmp.WriteString("ffffffffffffffffffffffffffffffffffffffff")
  320. tmp.Close()
  321. defer os.Remove(tmp.Name())
  322. commits, err = LoadCommitsFromFile(tmp.Name(), test.Repository)
  323. assert.Nil(t, commits)
  324. assert.NotNil(t, err)
  325. }
  326. func TestPipelineDeps(t *testing.T) {
  327. pipeline := NewPipeline(test.Repository)
  328. item1 := &dependingTestPipelineItem{}
  329. item2 := &testPipelineItem{}
  330. pipeline.AddItem(item1)
  331. pipeline.AddItem(item2)
  332. assert.Equal(t, pipeline.Len(), 2)
  333. pipeline.Initialize(map[string]interface{}{})
  334. commits := make([]*object.Commit, 1)
  335. commits[0], _ = test.Repository.CommitObject(plumbing.NewHash(
  336. "af9ddc0db70f09f3f27b4b98e415592a7485171c"))
  337. result, err := pipeline.Run(commits)
  338. assert.Nil(t, err)
  339. assert.True(t, result[item1].(bool))
  340. assert.Equal(t, result[item2], item2)
  341. item1.TestNilConsumeReturn = true
  342. assert.Panics(t, func() { pipeline.Run(commits) })
  343. }
  344. func TestPipelineDeployFeatures(t *testing.T) {
  345. pipeline := NewPipeline(test.Repository)
  346. pipeline.DeployItem(&testPipelineItem{})
  347. f, _ := pipeline.GetFeature("power")
  348. assert.True(t, f)
  349. }
  350. func TestPipelineError(t *testing.T) {
  351. pipeline := NewPipeline(test.Repository)
  352. item := &testPipelineItem{}
  353. item.TestError = true
  354. pipeline.AddItem(item)
  355. pipeline.Initialize(map[string]interface{}{})
  356. commits := make([]*object.Commit, 1)
  357. commits[0], _ = test.Repository.CommitObject(plumbing.NewHash(
  358. "af9ddc0db70f09f3f27b4b98e415592a7485171c"))
  359. result, err := pipeline.Run(commits)
  360. assert.Nil(t, result)
  361. assert.NotNil(t, err)
  362. }
  363. func TestCommonAnalysisResultMerge(t *testing.T) {
  364. c1 := CommonAnalysisResult{
  365. BeginTime: 1513620635, EndTime: 1513720635, CommitsNumber: 1, RunTime: 100}
  366. assert.Equal(t, c1.BeginTimeAsTime().Unix(), int64(1513620635))
  367. assert.Equal(t, c1.EndTimeAsTime().Unix(), int64(1513720635))
  368. c2 := CommonAnalysisResult{
  369. BeginTime: 1513620535, EndTime: 1513730635, CommitsNumber: 2, RunTime: 200}
  370. c1.Merge(&c2)
  371. assert.Equal(t, c1.BeginTime, int64(1513620535))
  372. assert.Equal(t, c1.EndTime, int64(1513730635))
  373. assert.Equal(t, c1.CommitsNumber, 3)
  374. assert.Equal(t, c1.RunTime.Nanoseconds(), int64(300))
  375. }
  376. func TestCommonAnalysisResultMetadata(t *testing.T) {
  377. c1 := &CommonAnalysisResult{
  378. BeginTime: 1513620635, EndTime: 1513720635, CommitsNumber: 1, RunTime: 100 * 1e6}
  379. meta := &pb.Metadata{}
  380. c1 = MetadataToCommonAnalysisResult(c1.FillMetadata(meta))
  381. assert.Equal(t, c1.BeginTimeAsTime().Unix(), int64(1513620635))
  382. assert.Equal(t, c1.EndTimeAsTime().Unix(), int64(1513720635))
  383. assert.Equal(t, c1.CommitsNumber, 1)
  384. assert.Equal(t, c1.RunTime.Nanoseconds(), int64(100*1e6))
  385. }
  386. func TestConfigurationOptionTypeString(t *testing.T) {
  387. opt := ConfigurationOptionType(0)
  388. assert.Equal(t, opt.String(), "")
  389. opt = ConfigurationOptionType(1)
  390. assert.Equal(t, opt.String(), "int")
  391. opt = ConfigurationOptionType(2)
  392. assert.Equal(t, opt.String(), "string")
  393. opt = ConfigurationOptionType(3)
  394. assert.Equal(t, opt.String(), "float")
  395. opt = ConfigurationOptionType(4)
  396. assert.Equal(t, opt.String(), "string")
  397. opt = ConfigurationOptionType(5)
  398. assert.Panics(t, func() { _ = opt.String() })
  399. }
  400. func TestConfigurationOptionFormatDefault(t *testing.T) {
  401. opt := ConfigurationOption{Type: StringConfigurationOption, Default: "ololo"}
  402. assert.Equal(t, opt.FormatDefault(), "\"ololo\"")
  403. opt = ConfigurationOption{Type: IntConfigurationOption, Default: 7}
  404. assert.Equal(t, opt.FormatDefault(), "7")
  405. opt = ConfigurationOption{Type: BoolConfigurationOption, Default: false}
  406. assert.Equal(t, opt.FormatDefault(), "false")
  407. opt = ConfigurationOption{Type: FloatConfigurationOption, Default: 0.5}
  408. assert.Equal(t, opt.FormatDefault(), "0.5")
  409. }
  410. func TestPrepareRunPlanTiny(t *testing.T) {
  411. rootCommit, err := test.Repository.CommitObject(plumbing.NewHash(
  412. "cce947b98a050c6d356bc6ba95030254914027b1"))
  413. if err != nil {
  414. t.Fatal(err)
  415. }
  416. plan := prepareRunPlan([]*object.Commit{rootCommit})
  417. assert.Len(t, plan, 2)
  418. assert.Equal(t, runActionEmerge, plan[0].Action)
  419. assert.Equal(t, rootBranchIndex, plan[0].Items[0])
  420. assert.Equal(t, "cce947b98a050c6d356bc6ba95030254914027b1", plan[0].Commit.Hash.String())
  421. assert.Equal(t, runActionCommit, plan[1].Action)
  422. assert.Equal(t, rootBranchIndex, plan[1].Items[0])
  423. assert.Equal(t, "cce947b98a050c6d356bc6ba95030254914027b1", plan[1].Commit.Hash.String())
  424. }
  425. func TestPrepareRunPlanSmall(t *testing.T) {
  426. cit, err := test.Repository.Log(&git.LogOptions{From: plumbing.ZeroHash})
  427. if err != nil {
  428. panic(err)
  429. }
  430. defer cit.Close()
  431. var commits []*object.Commit
  432. timeCutoff := time.Date(2016, 12, 15, 0, 0, 0, 0, time.FixedZone("CET", 7200))
  433. cit.ForEach(func(commit *object.Commit) error {
  434. reliableTime := time.Date(commit.Author.When.Year(), commit.Author.When.Month(),
  435. commit.Author.When.Day(), commit.Author.When.Hour(), commit.Author.When.Minute(),
  436. commit.Author.When.Second(), 0, time.FixedZone("CET", 7200))
  437. if reliableTime.Before(timeCutoff) {
  438. commits = append(commits, commit)
  439. }
  440. return nil
  441. })
  442. plan := prepareRunPlan(commits)
  443. /*for _, p := range plan {
  444. if p.Commit != nil {
  445. fmt.Println(p.Action, p.Commit.Hash.String(), p.Items)
  446. } else {
  447. fmt.Println(p.Action, strings.Repeat(" ", 40), p.Items)
  448. }
  449. }*/
  450. // fork, merge and one artificial commit per branch
  451. assert.Len(t, plan, len(commits) + 1)
  452. assert.Equal(t, runActionEmerge, plan[0].Action)
  453. assert.Equal(t, "cce947b98a050c6d356bc6ba95030254914027b1", plan[0].Commit.Hash.String())
  454. assert.Equal(t, rootBranchIndex, plan[0].Items[0])
  455. assert.Equal(t, runActionCommit, plan[1].Action)
  456. assert.Equal(t, rootBranchIndex, plan[1].Items[0])
  457. assert.Equal(t, "cce947b98a050c6d356bc6ba95030254914027b1", plan[1].Commit.Hash.String())
  458. assert.Equal(t, runActionCommit, plan[2].Action)
  459. assert.Equal(t, rootBranchIndex, plan[2].Items[0])
  460. assert.Equal(t, "a3ee37f91f0d705ec9c41ae88426f0ae44b2fbc3", plan[2].Commit.Hash.String())
  461. assert.Equal(t, runActionCommit, plan[10].Action)
  462. assert.Equal(t, rootBranchIndex, plan[10].Items[0])
  463. assert.Equal(t, "a28e9064c70618dc9d68e1401b889975e0680d11", plan[10].Commit.Hash.String())
  464. }
  465. func TestMergeDag(t *testing.T) {
  466. cit, err := test.Repository.Log(&git.LogOptions{From: plumbing.ZeroHash})
  467. if err != nil {
  468. panic(err)
  469. }
  470. defer cit.Close()
  471. var commits []*object.Commit
  472. timeCutoff := time.Date(2017, 8, 12, 0, 0, 0, 0, time.FixedZone("CET", 7200))
  473. cit.ForEach(func(commit *object.Commit) error {
  474. reliableTime := time.Date(commit.Author.When.Year(), commit.Author.When.Month(),
  475. commit.Author.When.Day(), commit.Author.When.Hour(), commit.Author.When.Minute(),
  476. commit.Author.When.Second(), 0, time.FixedZone("CET", 7200))
  477. if reliableTime.Before(timeCutoff) {
  478. commits = append(commits, commit)
  479. }
  480. return nil
  481. })
  482. hashes, dag := buildDag(commits)
  483. leaveRootComponent(hashes, dag)
  484. mergedDag, _ := mergeDag(hashes, dag)
  485. for key, vals := range mergedDag {
  486. if key != plumbing.NewHash("a28e9064c70618dc9d68e1401b889975e0680d11") &&
  487. key != plumbing.NewHash("db325a212d0bc99b470e000641d814745024bbd5") {
  488. assert.Len(t, vals, len(dag[key]), key.String())
  489. } else {
  490. mvals := map[string]bool{}
  491. for _, val := range vals {
  492. mvals[val.Hash.String()] = true
  493. }
  494. if key == plumbing.NewHash("a28e9064c70618dc9d68e1401b889975e0680d11") {
  495. assert.Contains(t, mvals, "db325a212d0bc99b470e000641d814745024bbd5")
  496. assert.Contains(t, mvals, "be9b61e09b08b98e64ed461a4004c9e2412f78ee")
  497. }
  498. if key == plumbing.NewHash("db325a212d0bc99b470e000641d814745024bbd5") {
  499. assert.Contains(t, mvals, "f30daba81ff2bf0b3ba02a1e1441e74f8a4f6fee")
  500. assert.Contains(t, mvals, "8a03b5620b1caa72ec9cb847ea88332621e2950a")
  501. }
  502. }
  503. }
  504. assert.Len(t, mergedDag, 8)
  505. assert.Contains(t, mergedDag, plumbing.NewHash("cce947b98a050c6d356bc6ba95030254914027b1"))
  506. assert.Contains(t, mergedDag, plumbing.NewHash("a3ee37f91f0d705ec9c41ae88426f0ae44b2fbc3"))
  507. assert.Contains(t, mergedDag, plumbing.NewHash("a28e9064c70618dc9d68e1401b889975e0680d11"))
  508. assert.Contains(t, mergedDag, plumbing.NewHash("be9b61e09b08b98e64ed461a4004c9e2412f78ee"))
  509. assert.Contains(t, mergedDag, plumbing.NewHash("db325a212d0bc99b470e000641d814745024bbd5"))
  510. assert.Contains(t, mergedDag, plumbing.NewHash("f30daba81ff2bf0b3ba02a1e1441e74f8a4f6fee"))
  511. assert.Contains(t, mergedDag, plumbing.NewHash("8a03b5620b1caa72ec9cb847ea88332621e2950a"))
  512. assert.Contains(t, mergedDag, plumbing.NewHash("dd9dd084d5851d7dc4399fc7dbf3d8292831ebc5"))
  513. queue := []plumbing.Hash{plumbing.NewHash("cce947b98a050c6d356bc6ba95030254914027b1")}
  514. visited := map[plumbing.Hash]bool{}
  515. for len(queue) > 0 {
  516. head := queue[len(queue)-1]
  517. queue = queue[:len(queue)-1]
  518. if visited[head] {
  519. continue
  520. }
  521. visited[head] = true
  522. for _, child := range mergedDag[head] {
  523. queue = append(queue, child.Hash)
  524. }
  525. }
  526. assert.Len(t, visited, 8)
  527. }
  528. func TestPrepareRunPlanBig(t *testing.T) {
  529. cases := [][7]int {
  530. {2017, 8, 9, 0, 0, 0, 0},
  531. {2017, 8, 10, 0, 0, 0, 0},
  532. {2017, 8, 24, 1, 1, 1, 1},
  533. {2017, 9, 19, 1-2, 1, 1, 1},
  534. {2017, 9, 23, 1-2, 1, 1, 1},
  535. {2017, 12, 8, 1, 1, 1, 1},
  536. {2017, 12, 9, 1, 1, 1, 1},
  537. {2017, 12, 10, 1, 1, 1, 1},
  538. {2017, 12, 11, 2, 2, 2, 2},
  539. {2017, 12, 19, 3, 3, 3, 3},
  540. {2017, 12, 27, 3, 3, 3, 3},
  541. {2018, 1, 10, 3, 3, 3, 3},
  542. {2018, 1, 16, 3, 3, 3, 3},
  543. {2018, 1, 18, 4, 5, 4, 4},
  544. {2018, 1, 23, 5, 5, 5, 5},
  545. {2018, 3, 12, 6, 6, 6, 6},
  546. {2018, 5, 13, 6, 6, 6, 6},
  547. {2018, 5, 16, 7, 7, 7, 7},
  548. }
  549. for _, testCase := range cases {
  550. func() {
  551. cit, err := test.Repository.Log(&git.LogOptions{From: plumbing.ZeroHash})
  552. if err != nil {
  553. panic(err)
  554. }
  555. defer cit.Close()
  556. var commits []*object.Commit
  557. timeCutoff := time.Date(
  558. testCase[0], time.Month(testCase[1]), testCase[2], 0, 0, 0, 0, time.FixedZone("CET", 7200))
  559. cit.ForEach(func(commit *object.Commit) error {
  560. reliableTime := time.Date(commit.Author.When.Year(), commit.Author.When.Month(),
  561. commit.Author.When.Day(), commit.Author.When.Hour(), commit.Author.When.Minute(),
  562. commit.Author.When.Second(), 0, time.FixedZone("CET", 7200))
  563. if reliableTime.Before(timeCutoff) {
  564. commits = append(commits, commit)
  565. }
  566. return nil
  567. })
  568. plan := prepareRunPlan(commits)
  569. /*for _, p := range plan {
  570. if p.Commit != nil {
  571. fmt.Println(p.Action, p.Commit.Hash.String(), p.Items)
  572. } else {
  573. fmt.Println(p.Action, strings.Repeat(" ", 40), p.Items)
  574. }
  575. }*/
  576. numCommits := 0
  577. numForks := 0
  578. numMerges := 0
  579. numDeletes := 0
  580. numEmerges := 0
  581. processed := map[plumbing.Hash]map[int]int{}
  582. for _, p := range plan {
  583. switch p.Action {
  584. case runActionCommit:
  585. branches := processed[p.Commit.Hash]
  586. if branches == nil {
  587. branches = map[int]int{}
  588. processed[p.Commit.Hash] = branches
  589. }
  590. branches[p.Items[0]]++
  591. for _, parent := range p.Commit.ParentHashes {
  592. assert.Contains(t, processed, parent)
  593. }
  594. numCommits++
  595. case runActionFork:
  596. numForks++
  597. case runActionMerge:
  598. counts := map[int]int{}
  599. for _, i := range p.Items {
  600. counts[i]++
  601. }
  602. for x, v := range counts {
  603. assert.Equal(t, 1, v, x)
  604. }
  605. numMerges++
  606. case runActionDelete:
  607. numDeletes++
  608. case runActionEmerge:
  609. numEmerges++
  610. }
  611. }
  612. for c, branches := range processed {
  613. for b, v := range branches {
  614. assert.Equal(t, 1, v, fmt.Sprint(c.String(), b))
  615. }
  616. }
  617. assert.Equal(t, numCommits, len(commits)+testCase[3], fmt.Sprintf("commits %v", testCase))
  618. assert.Equal(t, numForks, testCase[4], fmt.Sprintf("forks %v", testCase))
  619. assert.Equal(t, numMerges, testCase[5], fmt.Sprintf("merges %v", testCase))
  620. assert.Equal(t, numDeletes, testCase[6], fmt.Sprintf("deletes %v", testCase))
  621. assert.Equal(t, numEmerges, 1, fmt.Sprintf("emerges %v", testCase))
  622. }()
  623. }
  624. }