pipeline_test.go 20 KB

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