12
12
local .cwd .chdir (os .path .dirname (__file__ ))
13
13
certgen = local ["./certgen" ]
14
14
15
+ # Helpers
16
+ CONF_EXTRA = """-eCONF_EXTRA=
17
+ log_connections = on
18
+ log_min_messages = log
19
+ """
20
+
15
21
16
22
class PostgresAutoconfCase (unittest .TestCase ):
17
23
"""Test behavior for this docker image"""
18
24
19
- def setUp (self ):
25
+ @classmethod
26
+ def setUpClass (cls ):
20
27
with local .cwd (local .cwd / ".." ):
21
28
print ("Building image" )
22
29
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 ):
23
35
docker ("network" , "create" , "lan" )
24
36
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" }
28
37
return super ().setUp ()
29
38
30
39
def tearDown (self ):
31
40
try :
32
41
print ("Postgres container logs:" )
33
42
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 )
36
45
except AttributeError :
37
46
pass # No postgres daemon
38
47
docker ("network" , "rm" , "lan" , "wan" )
39
48
return super ().tearDown ()
40
49
41
50
def _generate_certs (self ):
42
51
"""Generate certificates for testing the image."""
43
- certgen ("example.com " , "test_user" )
52
+ certgen ("example.localdomain " , "test_user" )
44
53
45
54
def _check_local_connection (self ):
46
55
"""Check that local connection works fine."""
@@ -105,7 +114,7 @@ def _check_password_auth(self, host=None):
105
114
),
106
115
)
107
116
108
- def _connect_wan_network (self , alias = "example.com " ):
117
+ def _connect_wan_network (self , alias = "example.localdomain " ):
109
118
"""Bind a new network, to imitate WAN connections."""
110
119
docker ("network" , "connect" , "--alias" , alias , "wan" , self .postgres_container )
111
120
@@ -131,12 +140,13 @@ def _check_cert_auth(self):
131
140
"PGSSLROOTCERT=/certs/server.ca.cert.pem" ,
132
141
"-e" ,
133
142
"PGUSER=test_user" ,
143
+ CONF_EXTRA ,
134
144
"-v" ,
135
145
"{}:/certs" .format (local .cwd ),
136
146
self .image ,
137
147
"psql" ,
138
148
"--host" ,
139
- "example.com " ,
149
+ "example.localdomain " ,
140
150
"--command" ,
141
151
"SELECT 1" ,
142
152
"--no-align" ,
@@ -164,6 +174,7 @@ def test_server_certs_var(self):
164
174
"POSTGRES_PASSWORD=test_password" ,
165
175
"-e" ,
166
176
"POSTGRES_USER=test_user" ,
177
+ CONF_EXTRA ,
167
178
self .image ,
168
179
).strip ()
169
180
self ._check_local_connection ()
@@ -196,6 +207,7 @@ def test_server_certs_mount(self):
196
207
"POSTGRES_PASSWORD=test_password" ,
197
208
"-e" ,
198
209
"POSTGRES_USER=test_user" ,
210
+ CONF_EXTRA ,
199
211
* cert_vols ,
200
212
self .image ,
201
213
).strip ()
@@ -218,13 +230,14 @@ def test_no_certs_lan(self):
218
230
"POSTGRES_PASSWORD=test_password" ,
219
231
"-e" ,
220
232
"POSTGRES_USER=test_user" ,
233
+ CONF_EXTRA ,
221
234
self .image ,
222
235
).strip ()
223
236
self ._check_local_connection ()
224
237
self ._check_password_auth ()
225
238
self ._connect_wan_network ()
226
239
with self .assertRaises (ProcessExecutionError ):
227
- self ._check_password_auth ("example.com " )
240
+ self ._check_password_auth ("example.localdomain " )
228
241
229
242
def test_no_certs_wan (self ):
230
243
"""Unencrypted WAN access works (although this is dangerous)."""
@@ -244,13 +257,14 @@ def test_no_certs_wan(self):
244
257
"WAN_AUTH_METHOD=md5" ,
245
258
"-e" ,
246
259
"WAN_CONNECTION=host" ,
260
+ CONF_EXTRA ,
247
261
self .image ,
248
262
).strip ()
249
263
self ._check_local_connection ()
250
264
self ._check_password_auth ()
251
265
self ._connect_wan_network ()
252
266
with self .assertRaises (ProcessExecutionError ):
253
- self ._check_password_auth ("example.com " )
267
+ self ._check_password_auth ("example.localdomain " )
254
268
255
269
def test_certs_falsy_lan (self ):
256
270
"""Configuration with falsy values for certs works fine."""
@@ -267,6 +281,7 @@ def test_certs_falsy_lan(self):
267
281
"-e" ,
268
282
"POSTGRES_USER=test_user" ,
269
283
"-e" ,
284
+ CONF_EXTRA ,
270
285
"CERTS={}" .format (
271
286
json .dumps (
272
287
{
@@ -282,7 +297,7 @@ def test_certs_falsy_lan(self):
282
297
self ._check_password_auth ()
283
298
self ._connect_wan_network ()
284
299
with self .assertRaises (ProcessExecutionError ):
285
- self ._check_password_auth ("example.com " )
300
+ self ._check_password_auth ("example.localdomain " )
286
301
287
302
288
303
if __name__ == "__main__" :
0 commit comments