Skip to content

Commit

Permalink
Fix loading plugins from path with relative imports
Browse files Browse the repository at this point in the history
Also:
- fixes `tox -e self` job. It was designed to ensure that relative
  imports are working, but ignored the error.
- changed `rally task sla-check` to return not zero code in case of
  finding non of workloads had been executed.

Change-Id: I723f81abdfbe7c71e9ec36aaf6f0936a42eac425
  • Loading branch information
andreykurilin committed Feb 26, 2020
1 parent 12404b8 commit e9264de
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ Changed

* *path_or_url* plugin follows redirects while validating urls now.

* Move *rally.common.sshutils* to *rally.utils.sshutils*
* *rally task sla-check` fails if there is no data.
Deprecated
~~~~~~~~~~

* Module *rally.common.sshutils* is deprecated. Use *rally.utils.sshutils*
instead.

Removed
~~~~~~~
Expand Down
2 changes: 2 additions & 0 deletions rally/cli/commands/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,8 @@ def sla_check(self, api, task_id=None, tojson=False):
else:
cliutils.print_list(data, ("benchmark", "pos", "criterion",
"status", "detail"))
if not data:
return 2
return failed_criteria

@cliutils.args("--uuid", type=str, dest="task_id",
Expand Down
10 changes: 7 additions & 3 deletions rally/common/plugin/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ def load_plugins(dir_or_file, depth=0):
LOG.info("Loading plugins from directories %s/*" %
directory.rstrip("/"))
for root, _dirs, files in os.walk(directory, followlinks=True):
if root not in sys.path:
# this hack is required to support relative imports
sys.path.append(root)

for plugin in files:
load_plugins(os.path.join(root, plugin), depth=1)

Expand All @@ -141,9 +145,10 @@ def load_plugins(dir_or_file, depth=0):
if depth:
msg = "\t" + msg
LOG.info(msg)
module_name = os.path.splitext(plugin_file.split("/")[-1])[0]
try:
spec = importlib.util.spec_from_file_location(
plugin_file, plugin_file)
module_name, plugin_file)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

Expand All @@ -155,5 +160,4 @@ def load_plugins(dir_or_file, depth=0):
LOG.warning("%(msg)s: %(e)s" % {"msg": msg, "e": e})
return
_loaded_modules.append(module)
LOG.info("\t Loaded module with plugins: %s" %
os.path.splitext(plugin_file.split("/")[-1])[0])
LOG.info("\t Loaded module with plugins: %s" % module_name)
2 changes: 1 addition & 1 deletion tests/ci/playbooks/rally-tox-base/post-run.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
vars:
results_dir: "{{ zuul.project.src_dir }}/.test_results/"
html_report: "{{ tox_env }}_report.html"
json_report: "{{ tox_env }}_report.html"
json_report: "{{ tox_env }}_report.json"
tasks:
- shell: "ls {{ results_dir }}"
register: results_dir_stat
Expand Down
2 changes: 1 addition & 1 deletion tests/ci/rally_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.

"""Simple app based on rally api for testing porpuses"""
"""Simple app based on rally api for testing purpose"""

import sys

Expand Down
14 changes: 7 additions & 7 deletions tests/ci/rally_self_job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ TMP_RALLY_DB="/tmp/self-rally-$RND.sqlite"
DBCONNSTRING="sqlite:///$TMP_RALLY_DB"
RALLY="rally --config-file $TMP_RALLY_CONF"

# Create temp db
# Create temp cfg
cp etc/rally/rally.conf.sample $TMP_RALLY_CONF
sed -i.bak "s|#connection =.*|connection = \"$DBCONNSTRING\"|" $TMP_RALLY_CONF
rally --config-file $TMP_RALLY_CONF db create

# ensure plugins loading
$RALLY --debug --plugin-paths=$PLUGIN_PATHS plugin show --name FakePlugin.testplugin

# Create db
$RALLY db create

# Create self deployment
$RALLY -d env create --name=self
Expand All @@ -44,10 +49,5 @@ set -e
$RALLY task report --html-static --out $HTML_REPORT
$RALLY task report --json --out $JSON_REPORT

if [ -n "$ZUUL_PROJECT" ]; then
gzip -9 -f $HTML_REPORT
gzip -9 -f $JSON_REPORT
fi

# Check sla (this may fail the job)
$RALLY task sla-check
4 changes: 2 additions & 2 deletions tests/unit/common/plugin/test_discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def fake_isdir(p):
test_path = "/somewhere"
discover.load_plugins(test_path)
expected = [
mock.call(p, p)
mock.call(p.rsplit("/", 1)[1][:-3], p)
for p in ("/somewhere/plugin1.py", "/somewhere/subdir/plugin2.py",
"/somewhere/subdir/subsubdir/plugin3.py")
]
Expand All @@ -111,7 +111,7 @@ def test_load_plugins_from_file_successful(
path = "/somewhere/plugin.py"
discover.load_plugins(path)

mock_spec_from_file_location.assert_called_once_with(path, path)
mock_spec_from_file_location.assert_called_once_with("plugin", path)
mock_module_from_spec.assert_called_once_with(
mock_spec_from_file_location.return_value)

Expand Down

0 comments on commit e9264de

Please sign in to comment.