11
11
# License for the specific language governing permissions and limitations
12
12
# under the License.
13
13
import os
14
- import re
15
- from datetime import UTC , datetime
16
14
from time import sleep
17
15
from typing import Optional
18
16
19
17
from testcontainers .core .config import MAX_TRIES , SLEEP_TIME
20
- from testcontainers .core .generic import DbContainer
18
+ from testcontainers .core .generic import DependencyFreeDbContainer
21
19
from testcontainers .core .utils import raise_for_deprecated_parameter
22
- from testcontainers .core .waiting_utils import wait_container_is_ready
20
+ from testcontainers .core .waiting_utils import (wait_container_is_ready ,
21
+ wait_for_logs )
23
22
24
23
25
- class PostgresContainer (DbContainer ):
24
+ class PostgresContainer (DependencyFreeDbContainer ):
26
25
"""
27
26
Postgres database container.
28
27
@@ -63,33 +62,17 @@ def _configure(self) -> None:
63
62
self .with_env ("POSTGRES_PASSWORD" , self .password )
64
63
self .with_env ("POSTGRES_DB" , self .dbname )
65
64
66
- def get_connection_url (self , host = None ) -> str :
67
- return super ()._create_connection_url (
68
- dialect = f"postgresql+{ self .driver } " , username = self .username ,
69
- password = self .password , dbname = self .dbname , host = host ,
70
- port = self .port ,
71
- )
72
-
73
65
@wait_container_is_ready ()
74
- def _connect (self ) -> None :
75
- count = 0
66
+ def _verify_status (self ) -> None :
67
+ wait_for_logs ( self , ".*database system is ready to accept connections.*" , MAX_TRIES , SLEEP_TIME )
76
68
77
- # ALTERNATE IMPLEMENTATION based on comments from @HofmeisterAn
78
- # expr = re.compile(r'(?P<ts>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}Z).*')
79
- # timestamp = datetime.now()
69
+ count = 0
80
70
while count < MAX_TRIES :
81
71
status , _ = self .exec (f"pg_isready -hlocalhost -p{ self .port } -U{ self .username } " )
82
72
if status == 0 :
83
73
return
84
74
85
- # ALTERNATE IMPLEMENTATION based on comments from @HofmeisterAn
86
- # stdout = self._container.logs(stderr = False, timestamps = True, since = timestamp)
87
- # lines = stdout.decode("utf-8").split("\n")
88
- # for line in lines:
89
- # if m:= re.match(expr, line):
90
- # timestamp = datetime.fromisoformat(m.groupdict()["ts"]).replace(tzinfo=None)
91
- # if "database system is ready to accept connections" in line:
92
- # return
93
-
94
75
sleep (SLEEP_TIME )
95
- count += 1
76
+ count += 1
77
+
78
+ raise RuntimeError ("Postgres could not get into a ready state" )
0 commit comments