1212local .cwd .chdir (os .path .dirname (__file__ ))
1313certgen = local ["./certgen" ]
1414
15+ # Helpers
16+ CONF_EXTRA = """-eCONF_EXTRA=
17+ log_connections = on
18+ log_min_messages = log
19+ """
20+
1521
1622class PostgresAutoconfCase (unittest .TestCase ):
1723 """Test behavior for this docker image"""
1824
19- def setUp (self ):
25+ @classmethod
26+ def setUpClass (cls ):
2027 with local .cwd (local .cwd / ".." ):
2128 print ("Building image" )
2229 local ["./hooks/build" ] & FG
30+ cls .image = "tecnativa/postgres-autoconf:{}" .format (local .env ["DOCKER_TAG" ])
31+ cls .cert_files = {"client.ca.cert.pem" , "server.cert.pem" , "server.key.pem" }
32+ return super ().setUpClass ()
33+
34+ def setUp (self ):
2335 docker ("network" , "create" , "lan" )
2436 docker ("network" , "create" , "wan" )
25- self .version = os .environ ["DOCKER_TAG" ]
26- self .image = ("tecnativa/postgres-autoconf:{}" .format (self .version ),)
27- self .cert_files = {"client.ca.cert.pem" , "server.cert.pem" , "server.key.pem" }
2837 return super ().setUp ()
2938
3039 def tearDown (self ):
3140 try :
3241 print ("Postgres container logs:" )
3342 docker ["container" , "logs" , self .postgres_container ] & FG
34- docker [ "container" , "stop" , self .postgres_container ] & FG
35- docker [ "container" , "rm" , self .postgres_container ] & FG
43+ docker ( "container" , "stop" , self .postgres_container )
44+ docker ( "container" , "rm" , self .postgres_container )
3645 except AttributeError :
3746 pass # No postgres daemon
3847 docker ("network" , "rm" , "lan" , "wan" )
3948 return super ().tearDown ()
4049
4150 def _generate_certs (self ):
4251 """Generate certificates for testing the image."""
43- certgen ("example.com " , "test_user" )
52+ certgen ("example.localdomain " , "test_user" )
4453
4554 def _check_local_connection (self ):
4655 """Check that local connection works fine."""
@@ -105,7 +114,7 @@ def _check_password_auth(self, host=None):
105114 ),
106115 )
107116
108- def _connect_wan_network (self , alias = "example.com " ):
117+ def _connect_wan_network (self , alias = "example.localdomain " ):
109118 """Bind a new network, to imitate WAN connections."""
110119 docker ("network" , "connect" , "--alias" , alias , "wan" , self .postgres_container )
111120
@@ -131,12 +140,13 @@ def _check_cert_auth(self):
131140 "PGSSLROOTCERT=/certs/server.ca.cert.pem" ,
132141 "-e" ,
133142 "PGUSER=test_user" ,
143+ CONF_EXTRA ,
134144 "-v" ,
135145 "{}:/certs" .format (local .cwd ),
136146 self .image ,
137147 "psql" ,
138148 "--host" ,
139- "example.com " ,
149+ "example.localdomain " ,
140150 "--command" ,
141151 "SELECT 1" ,
142152 "--no-align" ,
@@ -164,6 +174,7 @@ def test_server_certs_var(self):
164174 "POSTGRES_PASSWORD=test_password" ,
165175 "-e" ,
166176 "POSTGRES_USER=test_user" ,
177+ CONF_EXTRA ,
167178 self .image ,
168179 ).strip ()
169180 self ._check_local_connection ()
@@ -196,6 +207,7 @@ def test_server_certs_mount(self):
196207 "POSTGRES_PASSWORD=test_password" ,
197208 "-e" ,
198209 "POSTGRES_USER=test_user" ,
210+ CONF_EXTRA ,
199211 * cert_vols ,
200212 self .image ,
201213 ).strip ()
@@ -218,13 +230,14 @@ def test_no_certs_lan(self):
218230 "POSTGRES_PASSWORD=test_password" ,
219231 "-e" ,
220232 "POSTGRES_USER=test_user" ,
233+ CONF_EXTRA ,
221234 self .image ,
222235 ).strip ()
223236 self ._check_local_connection ()
224237 self ._check_password_auth ()
225238 self ._connect_wan_network ()
226239 with self .assertRaises (ProcessExecutionError ):
227- self ._check_password_auth ("example.com " )
240+ self ._check_password_auth ("example.localdomain " )
228241
229242 def test_no_certs_wan (self ):
230243 """Unencrypted WAN access works (although this is dangerous)."""
@@ -244,13 +257,14 @@ def test_no_certs_wan(self):
244257 "WAN_AUTH_METHOD=md5" ,
245258 "-e" ,
246259 "WAN_CONNECTION=host" ,
260+ CONF_EXTRA ,
247261 self .image ,
248262 ).strip ()
249263 self ._check_local_connection ()
250264 self ._check_password_auth ()
251265 self ._connect_wan_network ()
252266 with self .assertRaises (ProcessExecutionError ):
253- self ._check_password_auth ("example.com " )
267+ self ._check_password_auth ("example.localdomain " )
254268
255269 def test_certs_falsy_lan (self ):
256270 """Configuration with falsy values for certs works fine."""
@@ -266,6 +280,7 @@ def test_certs_falsy_lan(self):
266280 "POSTGRES_PASSWORD=test_password" ,
267281 "-e" ,
268282 "POSTGRES_USER=test_user" ,
283+ CONF_EXTRA ,
269284 "-e" ,
270285 "CERTS={}" .format (
271286 json .dumps (
@@ -282,7 +297,7 @@ def test_certs_falsy_lan(self):
282297 self ._check_password_auth ()
283298 self ._connect_wan_network ()
284299 with self .assertRaises (ProcessExecutionError ):
285- self ._check_password_auth ("example.com " )
300+ self ._check_password_auth ("example.localdomain " )
286301
287302
288303if __name__ == "__main__" :
0 commit comments