github.com/chenbh/concourse/v6@v6.4.2/atc/gc/gc_suite_test.go (about) 1 package gc_test 2 3 import ( 4 "context" 5 "os" 6 "time" 7 8 "code.cloudfoundry.org/lager" 9 10 "code.cloudfoundry.org/lager/lagertest" 11 12 sq "github.com/Masterminds/squirrel" 13 "github.com/chenbh/concourse/v6/atc" 14 "github.com/chenbh/concourse/v6/atc/db" 15 "github.com/chenbh/concourse/v6/atc/db/lock" 16 "github.com/chenbh/concourse/v6/atc/postgresrunner" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 "github.com/tedsuo/ifrit" 20 21 "testing" 22 ) 23 24 type GcCollector interface { 25 Run(context.Context) error 26 } 27 28 func TestGc(t *testing.T) { 29 RegisterFailHandler(Fail) 30 RunSpecs(t, "Gc Suite") 31 } 32 33 var psql = sq.StatementBuilder.PlaceholderFormat(sq.Dollar) 34 35 var ( 36 postgresRunner postgresrunner.Runner 37 dbProcess ifrit.Process 38 39 dbConn db.Conn 40 err error 41 resourceCacheFactory db.ResourceCacheFactory 42 resourceCacheLifecycle db.ResourceCacheLifecycle 43 resourceConfigFactory db.ResourceConfigFactory 44 buildFactory db.BuildFactory 45 lockFactory lock.LockFactory 46 47 teamFactory db.TeamFactory 48 49 defaultTeam db.Team 50 defaultPipeline db.Pipeline 51 defaultJob db.Job 52 defaultBuild db.Build 53 54 usedResource db.Resource 55 usedResourceType db.ResourceType 56 logger *lagertest.TestLogger 57 fakeLogFunc = func(logger lager.Logger, id lock.LockID) {} 58 ) 59 60 var _ = BeforeSuite(func() { 61 postgresRunner = postgresrunner.Runner{ 62 Port: 5433 + GinkgoParallelNode(), 63 } 64 65 dbProcess = ifrit.Invoke(postgresRunner) 66 67 postgresRunner.CreateTestDB() 68 }) 69 70 var _ = BeforeEach(func() { 71 postgresRunner.Truncate() 72 73 dbConn = postgresRunner.OpenConn() 74 75 lockFactory = lock.NewLockFactory(postgresRunner.OpenSingleton(), fakeLogFunc, fakeLogFunc) 76 77 teamFactory = db.NewTeamFactory(dbConn, lockFactory) 78 buildFactory = db.NewBuildFactory(dbConn, lockFactory, 0, time.Hour) 79 80 defaultTeam, err = teamFactory.CreateTeam(atc.Team{Name: "default-team"}) 81 Expect(err).NotTo(HaveOccurred()) 82 83 defaultBuild, err = defaultTeam.CreateOneOffBuild() 84 Expect(err).NotTo(HaveOccurred()) 85 86 atcConfig := atc.Config{ 87 Resources: atc.ResourceConfigs{ 88 { 89 Name: "some-resource", 90 Type: "some-base-type", 91 Source: atc.Source{"some": "source"}, 92 }, 93 }, 94 ResourceTypes: atc.ResourceTypes{ 95 { 96 Name: "some-resource-type", 97 Type: "some-base-type", 98 Source: atc.Source{"some": "source-type"}, 99 }, 100 }, 101 Jobs: atc.JobConfigs{ 102 { 103 Name: "some-job", 104 }, 105 { 106 Name: "some-other-job", 107 }, 108 }, 109 } 110 111 defaultPipeline, _, err = defaultTeam.SavePipeline("default-pipeline", atcConfig, db.ConfigVersion(0), false) 112 Expect(err).NotTo(HaveOccurred()) 113 114 var found bool 115 defaultJob, found, err = defaultPipeline.Job("some-job") 116 Expect(err).NotTo(HaveOccurred()) 117 Expect(found).To(BeTrue()) 118 119 usedResource, found, err = defaultPipeline.Resource("some-resource") 120 Expect(err).NotTo(HaveOccurred()) 121 Expect(found).To(BeTrue()) 122 123 usedResourceType, found, err = defaultPipeline.ResourceType("some-resource-type") 124 Expect(err).NotTo(HaveOccurred()) 125 Expect(found).To(BeTrue()) 126 127 setupTx, err := dbConn.Begin() 128 Expect(err).ToNot(HaveOccurred()) 129 130 baseResourceType := db.BaseResourceType{ 131 Name: "some-base-type", 132 } 133 134 _, err = baseResourceType.FindOrCreate(setupTx, false) 135 Expect(err).NotTo(HaveOccurred()) 136 137 Expect(setupTx.Commit()).To(Succeed()) 138 139 logger = lagertest.NewTestLogger("gc-test") 140 141 resourceCacheLifecycle = db.NewResourceCacheLifecycle(dbConn) 142 resourceCacheFactory = db.NewResourceCacheFactory(dbConn, lockFactory) 143 resourceConfigFactory = db.NewResourceConfigFactory(dbConn, lockFactory) 144 }) 145 146 var _ = AfterEach(func() { 147 Expect(dbConn.Close()).To(Succeed()) 148 }) 149 150 var _ = AfterSuite(func() { 151 dbProcess.Signal(os.Interrupt) 152 Eventually(dbProcess.Wait(), 10*time.Second).Should(Receive()) 153 })