github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/creds/pool_test.go (about) 1 package creds_test 2 3 import ( 4 "time" 5 6 "code.cloudfoundry.org/clock/fakeclock" 7 "code.cloudfoundry.org/lager" 8 "code.cloudfoundry.org/lager/lagertest" 9 "github.com/pf-qiu/concourse/v6/atc/creds" 10 11 // load dummy credential manager 12 _ "github.com/pf-qiu/concourse/v6/atc/creds/dummy" 13 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = Context("pool", func() { 19 var ( 20 logger lager.Logger 21 factory creds.ManagerFactory 22 credentialManagement creds.CredentialManagementConfig 23 varSourcePool creds.VarSourcePool 24 config1, config2 map[string]interface{} 25 fakeClock *fakeclock.FakeClock 26 ) 27 28 BeforeEach(func() { 29 logger = lagertest.NewTestLogger("pool-test") 30 factory = creds.ManagerFactories()["dummy"] 31 32 config1 = map[string]interface{}{ 33 "vars": map[string]interface{}{"k1": "v1"}, 34 } 35 36 config2 = map[string]interface{}{ 37 "vars": map[string]interface{}{"k2": "v2"}, 38 } 39 40 fakeClock = fakeclock.NewFakeClock(time.Now()) 41 42 credentialManagement = creds.CredentialManagementConfig{ 43 RetryConfig: creds.SecretRetryConfig{ 44 Attempts: 5, 45 Interval: time.Second, 46 }, 47 CacheConfig: creds.SecretCacheConfig{ 48 Enabled: true, 49 Duration: time.Minute, 50 DurationNotFound: time.Minute, 51 PurgeInterval: time.Minute * 10, 52 }, 53 } 54 }) 55 56 Context("FindOrCreate", func() { 57 BeforeEach(func() { 58 varSourcePool = creds.NewVarSourcePool(logger, credentialManagement, 5*time.Minute, time.Minute, fakeClock) 59 }) 60 61 AfterEach(func() { 62 varSourcePool.Close() 63 }) 64 65 Context("add 1 config", func() { 66 var ( 67 secrets creds.Secrets 68 err error 69 ) 70 71 JustBeforeEach(func() { 72 secrets, err = varSourcePool.FindOrCreate(logger, config1, factory) 73 Expect(err).ToNot(HaveOccurred()) 74 }) 75 76 It("should get k1", func() { 77 v, _, found, err := secrets.Get("k1") 78 Expect(err).ToNot(HaveOccurred()) 79 Expect(found).To(BeTrue()) 80 Expect(v.(string)).To(Equal("v1")) 81 }) 82 83 It("should not get foo", func() { 84 _, _, found, err := secrets.Get("foo") 85 Expect(err).ToNot(HaveOccurred()) 86 Expect(found).To(BeFalse()) 87 }) 88 89 It("pool size should be 1", func() { 90 Expect(varSourcePool.Size()).To(Equal(1)) 91 }) 92 }) 93 94 Context("add 2 configs", func() { 95 var ( 96 secrets1, secrets2 creds.Secrets 97 err error 98 ) 99 JustBeforeEach(func() { 100 secrets1, err = varSourcePool.FindOrCreate(logger, config1, factory) 101 Expect(err).ToNot(HaveOccurred()) 102 secrets2, err = varSourcePool.FindOrCreate(logger, config2, factory) 103 Expect(err).ToNot(HaveOccurred()) 104 }) 105 106 It("should get k1", func() { 107 v, _, found, err := secrets1.Get("k1") 108 Expect(err).ToNot(HaveOccurred()) 109 Expect(found).To(BeTrue()) 110 Expect(v.(string)).To(Equal("v1")) 111 }) 112 113 It("should get k2", func() { 114 v, _, found, err := secrets2.Get("k2") 115 Expect(err).ToNot(HaveOccurred()) 116 Expect(found).To(BeTrue()) 117 Expect(v.(string)).To(Equal("v2")) 118 }) 119 120 It("should not get foo", func() { 121 _, _, found, err := secrets1.Get("foo") 122 Expect(err).ToNot(HaveOccurred()) 123 Expect(found).To(BeFalse()) 124 125 _, _, found, err = secrets2.Get("foo") 126 Expect(err).ToNot(HaveOccurred()) 127 Expect(found).To(BeFalse()) 128 }) 129 130 It("pool size should be 2", func() { 131 Expect(varSourcePool.Size()).To(Equal(2)) 132 }) 133 }) 134 135 Context("add same config for multiple times", func() { 136 var ( 137 secrets1, secrets2 creds.Secrets 138 err error 139 ) 140 JustBeforeEach(func() { 141 secrets1, err = varSourcePool.FindOrCreate(logger, config1, factory) 142 Expect(err).ToNot(HaveOccurred()) 143 secrets1, err = varSourcePool.FindOrCreate(logger, config1, factory) 144 Expect(err).ToNot(HaveOccurred()) 145 secrets1, err = varSourcePool.FindOrCreate(logger, config1, factory) 146 Expect(err).ToNot(HaveOccurred()) 147 secrets2, err = varSourcePool.FindOrCreate(logger, config2, factory) 148 Expect(err).ToNot(HaveOccurred()) 149 secrets2, err = varSourcePool.FindOrCreate(logger, config2, factory) 150 Expect(err).ToNot(HaveOccurred()) 151 secrets2, err = varSourcePool.FindOrCreate(logger, config2, factory) 152 Expect(err).ToNot(HaveOccurred()) 153 }) 154 155 It("should get k1", func() { 156 v, _, found, err := secrets1.Get("k1") 157 Expect(err).ToNot(HaveOccurred()) 158 Expect(found).To(BeTrue()) 159 Expect(v.(string)).To(Equal("v1")) 160 }) 161 162 It("should get k2", func() { 163 v, _, found, err := secrets2.Get("k2") 164 Expect(err).ToNot(HaveOccurred()) 165 Expect(found).To(BeTrue()) 166 Expect(v.(string)).To(Equal("v2")) 167 }) 168 169 It("should not get foo", func() { 170 _, _, found, err := secrets1.Get("foo") 171 Expect(err).ToNot(HaveOccurred()) 172 Expect(found).To(BeFalse()) 173 174 _, _, found, err = secrets2.Get("foo") 175 Expect(err).ToNot(HaveOccurred()) 176 Expect(found).To(BeFalse()) 177 }) 178 179 It("pool size should be 2", func() { 180 Expect(varSourcePool.Size()).To(Equal(2)) 181 }) 182 }) 183 }) 184 185 Describe("Close", func() { 186 var err error 187 188 BeforeEach(func() { 189 varSourcePool = creds.NewVarSourcePool(logger, credentialManagement, 7*time.Second, 1*time.Second, fakeClock) 190 }) 191 192 It("cleans up all var sources", func() { 193 _, err = varSourcePool.FindOrCreate(logger, config1, factory) 194 Expect(err).ToNot(HaveOccurred()) 195 Expect(varSourcePool.Size()).To(Equal(1)) 196 197 fakeClock.WaitForWatcherAndIncrement(4 * time.Second) 198 _, err = varSourcePool.FindOrCreate(logger, config2, factory) 199 Expect(err).ToNot(HaveOccurred()) 200 Expect(varSourcePool.Size()).To(Equal(2)) 201 202 varSourcePool.Close() 203 Eventually(varSourcePool.Size).Should(Equal(0)) 204 }) 205 }) 206 207 Describe("Garbage Collection", func() { 208 var err error 209 210 BeforeEach(func() { 211 varSourcePool = creds.NewVarSourcePool(logger, credentialManagement, 7*time.Second, 1*time.Second, fakeClock) 212 }) 213 214 AfterEach(func() { 215 varSourcePool.Close() 216 }) 217 218 It("should clean up once ttl expires", func() { 219 _, err = varSourcePool.FindOrCreate(logger, config1, factory) 220 Expect(err).ToNot(HaveOccurred()) 221 Expect(varSourcePool.Size()).To(Equal(1)) 222 223 fakeClock.WaitForWatcherAndIncrement(4 * time.Second) 224 _, err = varSourcePool.FindOrCreate(logger, config2, factory) 225 Expect(err).ToNot(HaveOccurred()) 226 Expect(varSourcePool.Size()).To(Equal(2)) 227 228 fakeClock.WaitForWatcherAndIncrement(4 * time.Second) 229 Eventually(varSourcePool.Size).Should(Equal(1)) 230 231 fakeClock.WaitForWatcherAndIncrement(4 * time.Second) 232 Eventually(varSourcePool.Size).Should(Equal(0)) 233 }) 234 }) 235 })