github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/db/pipeline_lifecycle_test.go (about) 1 package db_test 2 3 import ( 4 "fmt" 5 6 "github.com/pf-qiu/concourse/v6/atc" 7 "github.com/pf-qiu/concourse/v6/atc/db" 8 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 ) 12 13 var _ = Describe("PipelineLifecycle", func() { 14 var ( 15 pl db.PipelineLifecycle 16 err error 17 ) 18 19 BeforeEach(func() { 20 pl = db.NewPipelineLifecycle(dbConn, lockFactory) 21 }) 22 23 Describe("ArchiveAbandonedPipelines", func() { 24 JustBeforeEach(func() { 25 err = pl.ArchiveAbandonedPipelines() 26 Expect(err).NotTo(HaveOccurred()) 27 }) 28 29 Context("child pipeline is set by a job in a pipeline", func() { 30 var ( 31 childPipeline db.Pipeline 32 ) 33 34 BeforeEach(func() { 35 build, _ := defaultJob.CreateBuild() 36 childPipeline, _, _ = build.SavePipeline(atc.PipelineRef{Name: "child-pipeline"}, defaultTeam.ID(), defaultPipelineConfig, db.ConfigVersion(0), false) 37 build.Finish(db.BuildStatusSucceeded) 38 }) 39 40 Context("parent pipeline is destroyed", func() { 41 BeforeEach(func() { 42 defaultPipeline.Destroy() 43 }) 44 45 It("should archive all child pipelines", func() { 46 childPipeline.Reload() 47 Expect(childPipeline.Archived()).To(BeTrue()) 48 }) 49 }) 50 51 Context("parent pipeline is archived", func() { 52 BeforeEach(func() { 53 defaultPipeline.Archive() 54 }) 55 56 It("should archive all child pipelines", func() { 57 childPipeline.Reload() 58 Expect(childPipeline.Archived()).To(BeTrue()) 59 }) 60 }) 61 62 Context("job is removed from the parent pipeline", func() { 63 BeforeEach(func() { 64 defaultPipelineConfig.Jobs = atc.JobConfigs{ 65 { 66 Name: "a-different-job", 67 }, 68 } 69 defaultTeam.SavePipeline(defaultPipelineRef, defaultPipelineConfig, defaultPipeline.ConfigVersion(), false) 70 }) 71 72 It("archives all child pipelines set by the deleted job", func() { 73 childPipeline.Reload() 74 Expect(childPipeline.Archived()).To(BeTrue()) 75 }) 76 }) 77 }) 78 79 Context("pipeline does not have a parent job and build ID", func() { 80 It("Should not archive the pipeline", func() { 81 defaultPipeline.Reload() 82 Expect(defaultPipeline.Archived()).To(BeFalse()) 83 }) 84 }) 85 }) 86 87 Describe("RemoveBuildEventsForDeletedPipelines", func() { 88 var ( 89 pipeline1 db.Pipeline 90 pipeline2 db.Pipeline 91 ) 92 93 BeforeEach(func() { 94 pipeline1, _, err = defaultTeam.SavePipeline(atc.PipelineRef{Name: "pipeline1"}, defaultPipelineConfig, 0, false) 95 Expect(err).ToNot(HaveOccurred()) 96 pipeline2, _, err = defaultTeam.SavePipeline(atc.PipelineRef{Name: "pipeline2"}, defaultPipelineConfig, 0, false) 97 Expect(err).ToNot(HaveOccurred()) 98 }) 99 100 pipelineBuildEventsExists := func(id int) bool { 101 var exists bool 102 err = dbConn.QueryRow(fmt.Sprintf("SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'pipeline_build_events_%d')", id)).Scan(&exists) 103 Expect(err).ToNot(HaveOccurred()) 104 105 return exists 106 } 107 108 It("drops the pipeline_build_events_x table for each deleted pipeline", func() { 109 destroy(pipeline1) 110 destroy(pipeline2) 111 112 err := pl.RemoveBuildEventsForDeletedPipelines() 113 Expect(err).ToNot(HaveOccurred()) 114 115 Expect(pipelineBuildEventsExists(pipeline1.ID())).To(BeFalse()) 116 Expect(pipelineBuildEventsExists(pipeline2.ID())).To(BeFalse()) 117 }) 118 119 It("clears the deleted_pipelines table", func() { 120 destroy(pipeline1) 121 err := pl.RemoveBuildEventsForDeletedPipelines() 122 Expect(err).ToNot(HaveOccurred()) 123 124 var count int 125 err = dbConn.QueryRow("SELECT COUNT(*) FROM deleted_pipelines").Scan(&count) 126 Expect(err).ToNot(HaveOccurred()) 127 Expect(count).To(Equal(0)) 128 }) 129 130 It("is resilient to pipeline_build_events_x tables not existing", func() { 131 destroy(pipeline1) 132 _, err := dbConn.Exec(fmt.Sprintf("DROP TABLE pipeline_build_events_%d", pipeline1.ID())) 133 Expect(err).ToNot(HaveOccurred()) 134 135 err = pl.RemoveBuildEventsForDeletedPipelines() 136 Expect(err).ToNot(HaveOccurred()) 137 }) 138 }) 139 })