Search config files in tree 57/73257/3
authorCédric Ollivier <cedric.ollivier@orange.com>
Thu, 3 Mar 2022 14:29:16 +0000 (15:29 +0100)
committerCédric Ollivier <cedric.ollivier@orange.com>
Fri, 4 Mar 2022 14:01:06 +0000 (15:01 +0100)
It allows putting configurations in classical dirs.
It fallbacks to the python package for backward compatibility.

Change-Id: Ie33b9482fb197926c7d7d66ace815fa4ae01d02d
Signed-off-by: Cédric Ollivier <cedric.ollivier@orange.com>
xtesting/ci/run_tests.py
xtesting/core/campaign.py
xtesting/utils/config.py [new file with mode: 0644]
xtesting/utils/constants.py

index ab872dd..90409a7 100644 (file)
@@ -23,7 +23,6 @@ import sys
 import textwrap
 
 import enum
-import pkg_resources
 import prettytable
 import six
 from stevedore import driver
@@ -31,6 +30,7 @@ import yaml
 
 from xtesting.ci import tier_builder
 from xtesting.core import testcase
+from xtesting.utils import config
 from xtesting.utils import constants
 from xtesting.utils import env
 
@@ -89,8 +89,9 @@ class Runner():
         self.clean_flag = True
         self.report_flag = False
         self.push_flag = False
-        self.tiers = tier_builder.TierBuilder(
-            pkg_resources.resource_filename('xtesting', 'ci/testcases.yaml'))
+        self.tiers = tier_builder.TierBuilder(config.get_xtesting_config(
+            constants.TESTCASE_DESCRIPTION,
+            constants.TESTCASE_DESCRIPTION_DEFAULT))
 
     @staticmethod
     def source_envfile(rc_file=constants.ENV_FILE):
@@ -114,9 +115,10 @@ class Runner():
 
     @staticmethod
     def get_dict_by_test(testname):
-        # pylint: disable=bad-continuation,missing-docstring
-        with open(pkg_resources.resource_filename(
-                'xtesting', 'ci/testcases.yaml')) as tyaml:
+        # pylint: disable=missing-docstring
+        with open(config.get_xtesting_config(
+                constants.TESTCASE_DESCRIPTION,
+                constants.TESTCASE_DESCRIPTION_DEFAULT)) as tyaml:
             testcases_yaml = yaml.safe_load(tyaml)
         for dic_tier in testcases_yaml.get("tiers"):
             for dic_testcase in dic_tier['testcases']:
@@ -314,11 +316,11 @@ def main():
             six.print_("Cannot create /var/lib/xtesting/results/")
             return testcase.TestCase.EX_RUN_ERROR
     if env.get('DEBUG').lower() == 'true':
-        logging.config.fileConfig(pkg_resources.resource_filename(
-            'xtesting', 'ci/logging.debug.ini'))
+        logging.config.fileConfig(config.get_xtesting_config(
+            'logging.debug.ini', constants.DEBUG_INI_PATH_DEFAULT))
     else:
-        logging.config.fileConfig(pkg_resources.resource_filename(
-            'xtesting', 'ci/logging.ini'))
+        logging.config.fileConfig(config.get_xtesting_config(
+            'logging.ini', constants.INI_PATH_DEFAULT))
     logging.captureWarnings(True)
     parser = RunTestsParser()
     args = parser.parse_args(sys.argv[1:])
index 7c766e5..4dca304 100644 (file)
@@ -20,13 +20,15 @@ import zipfile
 import boto3
 from boto3.s3.transfer import TransferConfig
 import botocore
-import pkg_resources
 import requests
 from six.moves import urllib
 
 from xtesting.core import testcase
+from xtesting.utils import config
+from xtesting.utils import constants
 from xtesting.utils import env
 
+
 __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
 
 
@@ -118,7 +120,7 @@ class Campaign():
             dst_s3_url = os.environ["S3_DST_URL"]
             multipart_threshold = 5 * 1024 ** 5 if "google" in os.environ[
                 "S3_ENDPOINT_URL"] else 8 * 1024 * 1024
-            config = TransferConfig(multipart_threshold=multipart_threshold)
+            tconfig = TransferConfig(multipart_threshold=multipart_threshold)
             bucket_name = urllib.parse.urlparse(dst_s3_url).netloc
             s3path = re.search(
                 '^/*(.*)/*$', urllib.parse.urlparse(dst_s3_url).path).group(1)
@@ -134,7 +136,7 @@ class Campaign():
                 b3resource.Bucket(bucket_name).download_file(
                     s3_object.key,
                     re.sub('^{}/*'.format(s3path), '', s3_object.key),
-                    Config=config)
+                    Config=tconfig)
                 Campaign.__logger.info(
                     "Downloading %s",
                     re.sub('^{}/*'.format(s3path), '', s3_object.key))
@@ -181,7 +183,7 @@ class Campaign():
             dst_s3_url = os.environ["S3_DST_URL"]
             multipart_threshold = 5 * 1024 ** 5 if "google" in os.environ[
                 "S3_ENDPOINT_URL"] else 8 * 1024 * 1024
-            config = TransferConfig(multipart_threshold=multipart_threshold)
+            tconfig = TransferConfig(multipart_threshold=multipart_threshold)
             bucket_name = urllib.parse.urlparse(dst_s3_url).netloc
             mime_type = mimetypes.guess_type('{}.zip'.format(build_tag))
             path = urllib.parse.urlparse(dst_s3_url).path.strip("/")
@@ -189,7 +191,7 @@ class Campaign():
             b3resource.Bucket(bucket_name).upload_file(
                 '{}.zip'.format(build_tag),
                 os.path.join(path, '{}.zip'.format(build_tag)),
-                Config=config,
+                Config=tconfig,
                 ExtraArgs={'ContentType': mime_type[
                     0] or 'application/octet-stream'})
             dst_http_url = os.environ["HTTP_DST_URL"]
@@ -215,10 +217,10 @@ def main():
     if not os.path.exists(testcase.TestCase.dir_results):
         os.makedirs(testcase.TestCase.dir_results)
     if env.get('DEBUG').lower() == 'true':
-        logging.config.fileConfig(pkg_resources.resource_filename(
-            'xtesting', 'ci/logging.debug.ini'))
+        logging.config.fileConfig(config.get_xtesting_config(
+            'logging.debug.ini', constants.DEBUG_INI_PATH_DEFAULT))
     else:
-        logging.config.fileConfig(pkg_resources.resource_filename(
-            'xtesting', 'ci/logging.ini'))
+        logging.config.fileConfig(config.get_xtesting_config(
+            'logging.ini', constants.INI_PATH_DEFAULT))
     logging.captureWarnings(True)
     Campaign.zip_campaign_files()
diff --git a/xtesting/utils/config.py b/xtesting/utils/config.py
new file mode 100644 (file)
index 0000000..6f94fc8
--- /dev/null
@@ -0,0 +1,23 @@
+#!/usr/bin/env python
+
+# Copyright (c) 2022 Orange and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# pylint: disable=missing-docstring
+
+import os
+
+from xtesting.utils import constants
+
+
+def get_xtesting_config(filename, default):
+    """Search Xtesting configs (i.e. testcases.yaml)"""
+    for path in constants.XTESTING_PATHES:
+        abspath = os.path.abspath(os.path.expanduser(path))
+        if os.path.isfile(os.path.join(abspath, filename)):
+            return os.path.join(abspath, filename)
+    return default
index 18e03f6..dfd46e2 100644 (file)
@@ -1,5 +1,33 @@
 #!/usr/bin/env python
 
+# Copyright (c) 2019 Orange and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+
 # pylint: disable=missing-docstring
 
+import os
+import sys
+
+import pkg_resources
+
 ENV_FILE = '/var/lib/xtesting/conf/env_file'
+
+XTESTING_PATHES = [
+    "~/.xtesting", "/etc/xtesting", os.path.join(sys.prefix + "/etc/xtesting")]
+
+TESTCASE_DESCRIPTION = 'testcases.yaml'
+TESTCASE_DESCRIPTION_DEFAULT = pkg_resources.resource_filename(
+    'xtesting', 'ci/{}'.format(TESTCASE_DESCRIPTION))
+
+RESULTS_DIR = '/var/lib/xtesting/results'
+LOG_PATH = os.path.join(RESULTS_DIR, 'xtesting.log')
+DEBUG_LOG_PATH = os.path.join(RESULTS_DIR, 'xtesting.debug.log')
+
+INI_PATH_DEFAULT = pkg_resources.resource_filename(
+    'xtesting', 'ci/logging.ini')
+DEBUG_INI_PATH_DEFAULT = pkg_resources.resource_filename(
+    'xtesting', 'ci/logging.debug.ini')