pool_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package grpc
  14. import (
  15. "context"
  16. "crypto/tls"
  17. "crypto/x509"
  18. "net"
  19. "testing"
  20. "time"
  21. "google.golang.org/grpc"
  22. "google.golang.org/grpc/credentials"
  23. pb "github.com/external-secrets/external-secrets/proto/provider"
  24. )
  25. func TestConnectionPoolGetReleaseReuse(t *testing.T) {
  26. address, tlsConfig := newPoolTestServer(t)
  27. pool := NewConnectionPool(PoolConfig{
  28. MaxIdleTime: time.Minute,
  29. MaxLifetime: time.Minute,
  30. HealthCheckInterval: time.Hour,
  31. })
  32. defer func() {
  33. _ = pool.Close()
  34. }()
  35. client1, err := pool.Get(context.Background(), address, tlsConfig)
  36. if err != nil {
  37. t.Fatalf("Get() error = %v", err)
  38. }
  39. client2, err := pool.Get(context.Background(), address, tlsConfig)
  40. if err != nil {
  41. t.Fatalf("second Get() error = %v", err)
  42. }
  43. if client1 != client2 {
  44. t.Fatal("expected pooled client to be reused")
  45. }
  46. key := pool.connectionKey(address, tlsConfig)
  47. pooled := pool.connections[key]
  48. if pooled == nil {
  49. t.Fatalf("expected pooled connection for key %q", key)
  50. }
  51. if pooled.references != 2 {
  52. t.Fatalf("expected references=2, got %d", pooled.references)
  53. }
  54. pool.Release(address, tlsConfig)
  55. if pooled.references != 1 {
  56. t.Fatalf("expected references=1 after release, got %d", pooled.references)
  57. }
  58. pool.Release(address, tlsConfig)
  59. if pooled.references != 0 {
  60. t.Fatalf("expected references=0 after second release, got %d", pooled.references)
  61. }
  62. }
  63. func TestConnectionPoolGetReplacesExpiredConnection(t *testing.T) {
  64. address, tlsConfig := newPoolTestServer(t)
  65. pool := NewConnectionPool(PoolConfig{
  66. MaxIdleTime: time.Minute,
  67. MaxLifetime: time.Minute,
  68. HealthCheckInterval: time.Hour,
  69. })
  70. defer func() {
  71. _ = pool.Close()
  72. }()
  73. client1, err := pool.Get(context.Background(), address, tlsConfig)
  74. if err != nil {
  75. t.Fatalf("Get() error = %v", err)
  76. }
  77. pool.Release(address, tlsConfig)
  78. key := pool.connectionKey(address, tlsConfig)
  79. pooled := pool.connections[key]
  80. if pooled == nil {
  81. t.Fatalf("expected pooled connection for key %q", key)
  82. }
  83. pooled.mu.Lock()
  84. pooled.created = time.Now().Add(-2 * time.Hour)
  85. pooled.mu.Unlock()
  86. client2, err := pool.Get(context.Background(), address, tlsConfig)
  87. if err != nil {
  88. t.Fatalf("second Get() error = %v", err)
  89. }
  90. if client1 == client2 {
  91. t.Fatal("expected expired pooled client to be replaced")
  92. }
  93. }
  94. func TestConnectionPoolCleanupIdleConnectionsRemovesReleasedConnection(t *testing.T) {
  95. address, tlsConfig := newPoolTestServer(t)
  96. pool := NewConnectionPool(PoolConfig{
  97. MaxIdleTime: time.Second,
  98. MaxLifetime: time.Minute,
  99. HealthCheckInterval: time.Hour,
  100. })
  101. defer func() {
  102. _ = pool.Close()
  103. }()
  104. _, err := pool.Get(context.Background(), address, tlsConfig)
  105. if err != nil {
  106. t.Fatalf("Get() error = %v", err)
  107. }
  108. pool.Release(address, tlsConfig)
  109. key := pool.connectionKey(address, tlsConfig)
  110. pooled := pool.connections[key]
  111. if pooled == nil {
  112. t.Fatalf("expected pooled connection for key %q", key)
  113. }
  114. pooled.mu.Lock()
  115. pooled.lastUsed = time.Now().Add(-2 * time.Second)
  116. pooled.mu.Unlock()
  117. pool.cleanupIdleConnections()
  118. if _, ok := pool.connections[key]; ok {
  119. t.Fatalf("expected idle pooled connection %q to be removed", key)
  120. }
  121. }
  122. func TestConnectionPoolCheckConnectionHealthRemovesShutdownConnection(t *testing.T) {
  123. address, tlsConfig := newPoolTestServer(t)
  124. pool := NewConnectionPool(PoolConfig{
  125. MaxIdleTime: time.Minute,
  126. MaxLifetime: time.Minute,
  127. HealthCheckInterval: time.Hour,
  128. })
  129. defer func() {
  130. _ = pool.Close()
  131. }()
  132. _, err := pool.Get(context.Background(), address, tlsConfig)
  133. if err != nil {
  134. t.Fatalf("Get() error = %v", err)
  135. }
  136. pool.Release(address, tlsConfig)
  137. key := pool.connectionKey(address, tlsConfig)
  138. pooled := pool.connections[key]
  139. if pooled == nil {
  140. t.Fatalf("expected pooled connection for key %q", key)
  141. }
  142. if err := pooled.conn.Close(); err != nil {
  143. t.Fatalf("Close() error = %v", err)
  144. }
  145. pool.checkConnectionHealth()
  146. if _, ok := pool.connections[key]; ok {
  147. t.Fatalf("expected unhealthy pooled connection %q to be removed", key)
  148. }
  149. }
  150. func TestConnectionPoolCloseClearsTrackedConnections(t *testing.T) {
  151. address, tlsConfig := newPoolTestServer(t)
  152. pool := NewConnectionPool(PoolConfig{
  153. MaxIdleTime: time.Minute,
  154. MaxLifetime: time.Minute,
  155. HealthCheckInterval: time.Hour,
  156. })
  157. _, err := pool.Get(context.Background(), address, tlsConfig)
  158. if err != nil {
  159. t.Fatalf("Get() error = %v", err)
  160. }
  161. pool.Release(address, tlsConfig)
  162. if len(pool.connections) != 1 {
  163. t.Fatalf("expected one tracked connection, got %d", len(pool.connections))
  164. }
  165. if err := pool.Close(); err != nil {
  166. t.Fatalf("Close() error = %v", err)
  167. }
  168. if len(pool.connections) != 0 {
  169. t.Fatalf("expected no tracked connections after close, got %d", len(pool.connections))
  170. }
  171. }
  172. func newPoolTestServer(t *testing.T) (string, *TLSConfig) {
  173. t.Helper()
  174. serverCert, serverKey, clientCert, clientKey, caCert := newTLSArtifactsForTest(t, "127.0.0.1")
  175. caPool := x509.NewCertPool()
  176. if !caPool.AppendCertsFromPEM(caCert) {
  177. t.Fatal("failed to append CA cert")
  178. }
  179. tlsCert, err := tls.X509KeyPair(serverCert, serverKey)
  180. if err != nil {
  181. t.Fatalf("X509KeyPair() error = %v", err)
  182. }
  183. lis, err := net.Listen("tcp", "127.0.0.1:0")
  184. if err != nil {
  185. t.Fatalf("Listen() error = %v", err)
  186. }
  187. server := grpc.NewServer(grpc.Creds(credentials.NewTLS(&tls.Config{
  188. MinVersion: tls.VersionTLS12,
  189. Certificates: []tls.Certificate{tlsCert},
  190. ClientCAs: caPool,
  191. ClientAuth: tls.RequireAndVerifyClientCert,
  192. })))
  193. pb.RegisterSecretStoreProviderServer(server, &mockServer{})
  194. go func() {
  195. _ = server.Serve(lis)
  196. }()
  197. t.Cleanup(func() {
  198. server.Stop()
  199. _ = lis.Close()
  200. })
  201. return lis.Addr().String(), &TLSConfig{
  202. CACert: caCert,
  203. ClientCert: clientCert,
  204. ClientKey: clientKey,
  205. ServerName: "127.0.0.1",
  206. }
  207. }