Commit c6c948e1 authored by Jérome Perrin's avatar Jérome Perrin

software/caddy-frontend/test: open files in context manager to silent ResourceWarning

Now that this test is python 3, test output has lots of warnings like this:

    software/caddy-frontend/test/test.py:305: ResourceWarning: unclosed file <_io.TextIOWrapper name='software/caddy-frontend/test/test_data/test.TestEnableHttp2ByDefaultFalseSlave.test00cluster_request_instance_parameter_dict.txt' mode='r' encoding='UTF-8'>
      test_data = open(test_data_file).read().strip()
    ResourceWarning: Enable tracemalloc to get the object allocation traceback
parent 86df2ac5
...@@ -302,7 +302,8 @@ class TestDataMixin(object): ...@@ -302,7 +302,8 @@ class TestDataMixin(object):
os.path.dirname(os.path.realpath(__file__)), 'test_data', filename) os.path.dirname(os.path.realpath(__file__)), 'test_data', filename)
try: try:
test_data = open(test_data_file).read().strip() with open(test_data_file) as fh:
test_data = fh.read().strip()
except IOError: except IOError:
test_data = '' test_data = ''
...@@ -840,8 +841,8 @@ class HttpFrontendTestCase(SlapOSInstanceTestCase): ...@@ -840,8 +841,8 @@ class HttpFrontendTestCase(SlapOSInstanceTestCase):
assert ca_certificate.status_code == http.client.OK assert ca_certificate.status_code == http.client.OK
cls.kedifa_caucase_ca_certificate_file = os.path.join( cls.kedifa_caucase_ca_certificate_file = os.path.join(
cls.working_directory, 'kedifa-caucase.ca.crt.pem') cls.working_directory, 'kedifa-caucase.ca.crt.pem')
open(cls.kedifa_caucase_ca_certificate_file, 'w').write( with open(cls.kedifa_caucase_ca_certificate_file, 'w') as fh:
ca_certificate.text) fh.write(ca_certificate.text)
@classmethod @classmethod
def _fetchBackendClientCaCertificateFile(cls, parameter_dict): def _fetchBackendClientCaCertificateFile(cls, parameter_dict):
...@@ -850,8 +851,8 @@ class HttpFrontendTestCase(SlapOSInstanceTestCase): ...@@ -850,8 +851,8 @@ class HttpFrontendTestCase(SlapOSInstanceTestCase):
assert ca_certificate.status_code == http.client.OK assert ca_certificate.status_code == http.client.OK
cls.backend_client_caucase_ca_certificate_file = os.path.join( cls.backend_client_caucase_ca_certificate_file = os.path.join(
cls.working_directory, 'backend-client-caucase.ca.crt.pem') cls.working_directory, 'backend-client-caucase.ca.crt.pem')
open(cls.backend_client_caucase_ca_certificate_file, 'w').write( with open(cls.backend_client_caucase_ca_certificate_file, 'w') as fh:
ca_certificate.text) fh.write(ca_certificate.text)
@classmethod @classmethod
def setUpMaster(cls): def setUpMaster(cls):
...@@ -1391,9 +1392,10 @@ class SlaveHttpFrontendTestCase(HttpFrontendTestCase): ...@@ -1391,9 +1392,10 @@ class SlaveHttpFrontendTestCase(HttpFrontendTestCase):
self.instance_path, '*', 'var', 'log', 'httpd', log_name self.instance_path, '*', 'var', 'log', 'httpd', log_name
))[0] ))[0]
self.assertRegex( with open(log_file) as fh:
open(log_file, 'r').readlines()[-1], self.assertRegex(
log_regexp) fh.readlines()[-1],
log_regexp)
class TestMasterRequestDomain(HttpFrontendTestCase, TestDataMixin): class TestMasterRequestDomain(HttpFrontendTestCase, TestDataMixin):
...@@ -2050,10 +2052,9 @@ class TestSlave(SlaveHttpFrontendTestCase, TestDataMixin): ...@@ -2050,10 +2052,9 @@ class TestSlave(SlaveHttpFrontendTestCase, TestDataMixin):
# check that monitor cors domains are correctly setup by file presence, as # check that monitor cors domains are correctly setup by file presence, as
# we trust monitor stack being tested in proper place and it is too hard # we trust monitor stack being tested in proper place and it is too hard
# to have working monitor with local proxy # to have working monitor with local proxy
self.assertTestData( with open(os.path.join(
open( partition_path, 'etc', 'httpd-cors.cfg')) as fh:
os.path.join( self.assertTestData(fh.read().strip())
partition_path, 'etc', 'httpd-cors.cfg'), 'r').read().strip())
def test_node_information_json(self): def test_node_information_json(self):
node_information_file_path = glob.glob(os.path.join( node_information_file_path = glob.glob(os.path.join(
...@@ -2100,10 +2101,8 @@ class TestSlave(SlaveHttpFrontendTestCase, TestDataMixin): ...@@ -2100,10 +2101,8 @@ class TestSlave(SlaveHttpFrontendTestCase, TestDataMixin):
def test_slave_partition_state(self): def test_slave_partition_state(self):
partition_path = self.getSlavePartitionPath() partition_path = self.getSlavePartitionPath()
self.assertTrue( with open(os.path.join(partition_path, 'bin', 'caddy-wrapper')) as fh:
'-grace 2s' in self.assertIn('-grace 2s', fh.read())
open(os.path.join(partition_path, 'bin', 'caddy-wrapper'), 'r').read()
)
def test_monitor_conf(self): def test_monitor_conf(self):
monitor_conf_list = glob.glob( monitor_conf_list = glob.glob(
...@@ -2112,8 +2111,11 @@ class TestSlave(SlaveHttpFrontendTestCase, TestDataMixin): ...@@ -2112,8 +2111,11 @@ class TestSlave(SlaveHttpFrontendTestCase, TestDataMixin):
)) ))
self.assertEqual(3, len(monitor_conf_list)) self.assertEqual(3, len(monitor_conf_list))
expected = [(False, q) for q in monitor_conf_list] expected = [(False, q) for q in monitor_conf_list]
got = [('!py!' in open(q).read(), q) for q in monitor_conf_list] got = []
# check that no monitor.conf in generated configuratio has magic !py! for q in monitor_conf_list:
with open(q) as fh:
got.append(('!py!' in fh.read(), q))
# check that no monitor.conf in generated configuration has magic !py!
self.assertEqual( self.assertEqual(
expected, expected,
got got
...@@ -2173,14 +2175,15 @@ class TestSlave(SlaveHttpFrontendTestCase, TestDataMixin): ...@@ -2173,14 +2175,15 @@ class TestSlave(SlaveHttpFrontendTestCase, TestDataMixin):
def test_server_polluted_keys_removed(self): def test_server_polluted_keys_removed(self):
buildout_file = os.path.join( buildout_file = os.path.join(
self.getMasterPartitionPath(), 'instance-caddy-replicate.cfg') self.getMasterPartitionPath(), 'instance-caddy-replicate.cfg')
for line in [ with open(buildout_file) as fh:
q for q in open(buildout_file).readlines() for line in [
if q.startswith('config-slave-list') or q.startswith( q for q in fh.readlines()
'config-extra_slave_instance_list')]: if q.startswith('config-slave-list') or q.startswith(
self.assertFalse('slave_title' in line) 'config-extra_slave_instance_list')]:
self.assertFalse('slap_software_type' in line) self.assertFalse('slave_title' in line)
self.assertFalse('connection-parameter-hash' in line) self.assertFalse('slap_software_type' in line)
self.assertFalse('timestamp' in line) self.assertFalse('connection-parameter-hash' in line)
self.assertFalse('timestamp' in line)
def assertBackendHeaders( def assertBackendHeaders(
self, backend_header_dict, domain, source_ip=SOURCE_IP, port=HTTPS_PORT, self, backend_header_dict, domain, source_ip=SOURCE_IP, port=HTTPS_PORT,
...@@ -3680,10 +3683,10 @@ class TestSlave(SlaveHttpFrontendTestCase, TestDataMixin): ...@@ -3680,10 +3683,10 @@ class TestSlave(SlaveHttpFrontendTestCase, TestDataMixin):
os.path.join( os.path.join(
self.instance_path, '*', 'etc', 'caddy-slave-conf.d', '_ciphers.conf' self.instance_path, '*', 'etc', 'caddy-slave-conf.d', '_ciphers.conf'
))[0] ))[0]
self.assertTrue( with open(configuration_file) as fh:
'ciphers RSA-3DES-EDE-CBC-SHA RSA-AES128-CBC-SHA' self.assertIn(
in open(configuration_file).read() 'ciphers RSA-3DES-EDE-CBC-SHA RSA-AES128-CBC-SHA',
) fh.read())
def test_enable_cache_custom_domain(self): def test_enable_cache_custom_domain(self):
parameter_dict = self.assertSlaveBase( parameter_dict = self.assertSlaveBase(
...@@ -5867,10 +5870,10 @@ class TestSlaveCiphers(SlaveHttpFrontendTestCase, TestDataMixin): ...@@ -5867,10 +5870,10 @@ class TestSlaveCiphers(SlaveHttpFrontendTestCase, TestDataMixin):
self.instance_path, '*', 'etc', 'caddy-slave-conf.d', self.instance_path, '*', 'etc', 'caddy-slave-conf.d',
'_default_ciphers.conf' '_default_ciphers.conf'
))[0] ))[0]
self.assertTrue( with open(configuration_file) as fh:
'ciphers ECDHE-ECDSA-AES256-GCM-SHA384 ECDHE-RSA-AES256-GCM-SHA384' self.assertIn(
in open(configuration_file).read() 'ciphers ECDHE-ECDSA-AES256-GCM-SHA384 ECDHE-RSA-AES256-GCM-SHA384',
) fh.read())
def test_own_ciphers(self): def test_own_ciphers(self):
parameter_dict = self.assertSlaveBase('own_ciphers') parameter_dict = self.assertSlaveBase('own_ciphers')
...@@ -5893,10 +5896,10 @@ class TestSlaveCiphers(SlaveHttpFrontendTestCase, TestDataMixin): ...@@ -5893,10 +5896,10 @@ class TestSlaveCiphers(SlaveHttpFrontendTestCase, TestDataMixin):
self.instance_path, '*', 'etc', 'caddy-slave-conf.d', self.instance_path, '*', 'etc', 'caddy-slave-conf.d',
'_own_ciphers.conf' '_own_ciphers.conf'
))[0] ))[0]
self.assertTrue( with open(configuration_file) as fh:
'ciphers ECDHE-ECDSA-AES128-GCM-SHA256 ECDHE-RSA-AES128-GCM-SHA256' self.assertIn(
in open(configuration_file).read() 'ciphers ECDHE-ECDSA-AES128-GCM-SHA256 ECDHE-RSA-AES128-GCM-SHA256',
) fh.read())
class TestSlaveRejectReportUnsafeDamaged(SlaveHttpFrontendTestCase): class TestSlaveRejectReportUnsafeDamaged(SlaveHttpFrontendTestCase):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment