From 7ef33622a82436cd78ca784766fc60fb160b86cc Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Thu, 15 Aug 2019 08:54:04 +1000 Subject: [PATCH] DCD-545: Use f-strings and constants throughout. --- entrypoint.py | 16 +++++++------- tests/test_image.py | 51 ++++++++++++++++++++++++--------------------- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/entrypoint.py b/entrypoint.py index 948f90b..e62bc66 100755 --- a/entrypoint.py +++ b/entrypoint.py @@ -22,7 +22,7 @@ jenv = j2.Environment( autoescape=j2.select_autoescape(['xml'])) def gen_cfg(tmpl, target, env, user='root', group='root', mode=0o644): - logging.info("Generating {} from template {}".format(target, tmpl)) + logging.info(f"Generating {target} from template {tmpl}") cfg = jenv.get_template(tmpl).render(env) with open(target, 'w') as fd: fd.write(cfg) @@ -49,25 +49,25 @@ env['atl_product_home_shared'] = env.get('confluence_shared_home') # Generate all configuration files for Confluence gen_cfg('server.xml.j2', - env['confluence_install_dir']+'/conf/server.xml', env) + f"{env['confluence_install_dir']}/conf/server.xml", env) gen_cfg('seraph-config.xml.j2', - env['confluence_install_dir']+'/confluence/WEB-INF/classes/seraph-config.xml', env) + f"{env['confluence_install_dir']}/confluence/WEB-INF/classes/seraph-config.xml", env) gen_cfg('confluence-init.properties.j2', - env['confluence_install_dir']+'/confluence/WEB-INF/classes/confluence-init.properties', env) + f"{env['confluence_install_dir']}/confluence/WEB-INF/classes/confluence-init.properties", env) gen_cfg('confluence.cfg.xml.j2', - env['confluence_home']+'/confluence.cfg.xml', env, + f"{env['confluence_home']}/confluence.cfg.xml", env, user=env['run_user'], group=env['run_group'], mode=0o640) ###################################################################### # Start Confluence as the correct user -start_cmd = "{}/bin/start-confluence.sh".format(env['confluence_install_dir']) +start_cmd = f"{env['confluence_install_dir']}/bin/start-confluence.sh" if os.getuid() == 0: - logging.info("User is currently root. Will change directory ownership to {} then downgrade permissions".format(env['run_user'])) + logging.info(f"User is currently root. Will change directory ownership to {env['run_user']} then downgrade permissions") set_perms(env['confluence_home'], env['run_user'], env['run_group'], 0o700) cmd = '/bin/su' @@ -77,5 +77,5 @@ else: cmd = start_cmd args = [start_cmd] + sys.argv[1:] -logging.info("Running Confluence with command '{}', arguments {}".format(cmd, args)) +logging.info(f"Running Confluence with command '{cmd}', arguments {args}") os.execv(cmd, args) diff --git a/tests/test_image.py b/tests/test_image.py index 765ed33..7da3178 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -5,10 +5,13 @@ import tarfile import testinfra import time import xml.etree.ElementTree as etree - import requests +CONF_INSTALL = '/opt/atlassian/confluence' +CONF_HOME = '/var/atlassian/application-data/confluence' +CONF_SHARED_HOME = '/media/atl/confluence/shared-home' + # Run an image and wrap it in a TestInfra host for convenience. # FIXME: There's probably a way to turn this into a fixture with parameters. def run_image(docker_cli, image, environment={}, ports={}): @@ -62,10 +65,10 @@ def test_jvm_args(docker_cli, image): def test_install_permissions(docker_cli, image): container = run_image(docker_cli, image) - assert container.file('/opt/atlassian/confluence/conf/server.xml').user == 'root' + assert container.file(f'{CONF_INSTALL}/conf/server.xml').user == 'root' for d in ['logs', 'work', 'temp']: - path = '/opt/atlassian/confluence/{}/'.format(d) + path = f'{CONF_INSTALL}/{d}/' assert container.file(path).user == 'confluence' @@ -92,7 +95,7 @@ def test_server_xml_defaults(docker_cli, image): container = run_image(docker_cli, image) _jvm = wait_for_proc(container, "org.apache.catalina.startup.Bootstrap") - xml = etree.fromstring(container.file('/opt/atlassian/confluence/conf/server.xml').content) + xml = etree.fromstring(container.file(f'{CONF_INSTALL}/conf/server.xml').content) connector = xml.find('.//Connector') context = xml.find('.//Context') @@ -119,7 +122,7 @@ def test_server_xml_catalina_fallback(docker_cli, image): container = run_image(docker_cli, image, environment=environment) _jvm = wait_for_proc(container, "org.apache.catalina.startup.Bootstrap") - xml = etree.fromstring(container.file('/opt/atlassian/confluence/conf/server.xml').content) + xml = etree.fromstring(container.file(f'{CONF_INSTALL}/conf/server.xml').content) connector = xml.find('.//Connector') context = xml.find('.//Context') @@ -148,7 +151,7 @@ def test_server_xml_params(docker_cli, image): container = run_image(docker_cli, image, environment=environment) _jvm = wait_for_proc(container, "org.apache.catalina.startup.Bootstrap") - xml = etree.fromstring(container.file('/opt/atlassian/confluence/conf/server.xml').content) + xml = etree.fromstring(container.file(f'{CONF_INSTALL}/conf/server.xml').content) connector = xml.find('.//Connector') context = xml.find('.//Context') @@ -171,34 +174,34 @@ def test_server_xml_params(docker_cli, image): def test_seraph_defaults(docker_cli, image): container = run_image(docker_cli, image) - wait_for_file(container, "/opt/atlassian/confluence/confluence/WEB-INF/classes/seraph-config.xml") + wait_for_file(container, f"{CONF_INSTALL}/confluence/WEB-INF/classes/seraph-config.xml") - xml = etree.fromstring(container.file('/opt/atlassian/confluence/confluence/WEB-INF/classes/seraph-config.xml').content) + xml = etree.fromstring(container.file(f'{CONF_INSTALL}/confluence/WEB-INF/classes/seraph-config.xml').content) #param = xml.findall('//param-name[text()="autologin.cookie.age"]') == [] param = xml.findall('.//param-name[.="autologin.cookie.age"]') == [] def test_seraph_login_set(docker_cli, image): container = run_image(docker_cli, image, environment={"ATL_AUTOLOGIN_COOKIE_AGE": "TEST_VAL"}) - wait_for_file(container, "/opt/atlassian/confluence/confluence/WEB-INF/classes/seraph-config.xml") + wait_for_file(container, f"{CONF_INSTALL}/confluence/WEB-INF/classes/seraph-config.xml") - xml = etree.fromstring(container.file('/opt/atlassian/confluence/confluence/WEB-INF/classes/seraph-config.xml').content) + xml = etree.fromstring(container.file(f'{CONF_INSTALL}/confluence/WEB-INF/classes/seraph-config.xml').content) assert xml.findall('.//param-value[.="TEST_VAL"]')[0].text == "TEST_VAL" def test_conf_init_set(docker_cli, image): container = run_image(docker_cli, image, environment={"CONFLUENCE_HOME": "/tmp/"}) - wait_for_file(container, "/opt/atlassian/confluence/confluence/WEB-INF/classes/confluence-init.properties") + wait_for_file(container, f"{CONF_INSTALL}/confluence/WEB-INF/classes/confluence-init.properties") - init = container.file('/opt/atlassian/confluence/confluence/WEB-INF/classes/confluence-init.properties') + init = container.file(f'{CONF_INSTALL}/confluence/WEB-INF/classes/confluence-init.properties') assert init.contains("confluence.home = /tmp/") def test_confluence_xml_default(docker_cli, image): container = run_image(docker_cli, image) - wait_for_file(container, "/opt/atlassian/confluence/confluence/WEB-INF/classes/confluence-init.properties") + wait_for_file(container, f"{CONF_INSTALL}/confluence/WEB-INF/classes/confluence-init.properties") #_jvm = wait_for_proc(container, "org.apache.catalina.startup.Bootstrap") - xml = etree.fromstring(container.file('/var/atlassian/application-data/confluence/confluence.cfg.xml').content) + xml = etree.fromstring(container.file(f'{CONF_HOME}/confluence.cfg.xml').content) assert xml.findall('.//buildNumber')[0].text == "0" assert xml.findall('.//property[@name="hibernate.connection.url"]') == [] assert xml.findall('.//property[@name="confluence.cluster.home"]') == [] @@ -211,9 +214,9 @@ def test_confluence_xml_postgres(docker_cli, image): 'ATL_JDBC_PASSWORD': 'atl_jdbc_password' } container = run_image(docker_cli, image, environment=environment) - wait_for_file(container, "/opt/atlassian/confluence/confluence/WEB-INF/classes/confluence-init.properties") + wait_for_file(container, f"{CONF_INSTALL}/confluence/WEB-INF/classes/confluence-init.properties") - xml = etree.fromstring(container.file('/var/atlassian/application-data/confluence/confluence.cfg.xml').content) + xml = etree.fromstring(container.file(f'{CONF_HOME}/confluence.cfg.xml').content) assert xml.findall('.//property[@name="hibernate.connection.url"]')[0].text == "atl_jdbc_url" assert xml.findall('.//property[@name="hibernate.connection.username"]')[0].text == "atl_jdbc_user" assert xml.findall('.//property[@name="hibernate.connection.password"]')[0].text == "atl_jdbc_password" @@ -246,9 +249,9 @@ def test_confluence_xml_postgres_all_set(docker_cli, image): 'ATL_DB_VALIDATIONQUERY': 'xselect 1' } container = run_image(docker_cli, image, environment=environment) - wait_for_file(container, "/var/atlassian/application-data/confluence/confluence.cfg.xml") + wait_for_file(container, f"{CONF_HOME}/confluence.cfg.xml") - xml = etree.fromstring(container.file('/var/atlassian/application-data/confluence/confluence.cfg.xml').content) + xml = etree.fromstring(container.file(f'{CONF_HOME}/confluence.cfg.xml').content) assert xml.findall('.//property[@name="hibernate.connection.driver_class"]')[0].text == "org.postgresql.Driver" assert xml.findall('.//property[@name="hibernate.dialect"]')[0].text == "com.atlassian.confluence.impl.hibernate.dialect.PostgreSQLDialect" assert xml.findall('.//property[@name="hibernate.c3p0.min_size"]')[0].text == "x20" @@ -273,8 +276,8 @@ def test_confluence_xml_cluster_aws(docker_cli, image): 'ATL_CLUSTER_TTL': 'atl_cluster_ttl' } container = run_image(docker_cli, image, environment=environment) - wait_for_file(container, "/var/atlassian/application-data/confluence/confluence.cfg.xml") - xml = etree.fromstring(container.file('/var/atlassian/application-data/confluence/confluence.cfg.xml').content) + wait_for_file(container, f"{CONF_HOME}/confluence.cfg.xml") + xml = etree.fromstring(container.file(f'{CONF_HOME}/confluence.cfg.xml').content) assert xml.findall('.//property[@name="confluence.cluster"]')[0].text == "true" assert xml.findall('.//property[@name="confluence.cluster.join.type"]')[0].text == "aws" @@ -295,8 +298,8 @@ def test_confluence_xml_cluster_multicast(docker_cli, image): 'ATL_CLUSTER_ADDRESS': '99.99.99.99' } container = run_image(docker_cli, image, environment=environment) - wait_for_file(container, "/var/atlassian/application-data/confluence/confluence.cfg.xml") - xml = etree.fromstring(container.file('/var/atlassian/application-data/confluence/confluence.cfg.xml').content) + wait_for_file(container, f"{CONF_HOME}/confluence.cfg.xml") + xml = etree.fromstring(container.file(f'{CONF_HOME}/confluence.cfg.xml').content) assert xml.findall('.//property[@name="confluence.cluster"]')[0].text == "true" assert xml.findall('.//property[@name="confluence.cluster.join.type"]')[0].text == "multicast" @@ -311,8 +314,8 @@ def test_confluence_xml_cluster_tcp(docker_cli, image): 'ATL_CLUSTER_NAME': 'atl_cluster_name', } container = run_image(docker_cli, image, environment=environment) - wait_for_file(container, "/var/atlassian/application-data/confluence/confluence.cfg.xml") - xml = etree.fromstring(container.file('/var/atlassian/application-data/confluence/confluence.cfg.xml').content) + wait_for_file(container, f"{CONF_HOME}/confluence.cfg.xml") + xml = etree.fromstring(container.file(f'{CONF_HOME}/confluence.cfg.xml').content) assert xml.findall('.//property[@name="confluence.cluster"]')[0].text == "true" assert xml.findall('.//property[@name="confluence.cluster.join.type"]')[0].text == "tcp_ip"