Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix import logic trying to load incorrect module #102

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/102-fix-incorrect-module-loading.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- fix turbo mode loading incorrect module (https://github.com/ansible-collections/cloud.common/pull/102).
21 changes: 10 additions & 11 deletions plugins/module_utils/turbo/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,30 +127,29 @@ async def load(self):

# resettle the loaded modules that were associated
# with a different Ansiblez.
for path, module in tuple(sys.modules.items()):
for path, module in sorted(tuple(sys.modules.items())):
if path and module and path.startswith("ansible_collections"):
try:
prefix = sys.modules[path].__loader__.prefix
except AttributeError:
# Not from a zipimporter loader, skipping
continue
py_path = self.ansiblez_path + os.sep + prefix
my_loader = zipimporter(py_path)
sys.meta_path.append(my_loader)
# Reload package modules only, to pick up new modules from
# packages that have been previously loaded.
if hasattr(sys.modules[path], "__path__"):
sys.modules[path].__path__ = py_path

py_path = self.ansiblez_path + os.sep + prefix
my_loader = zipimporter(py_path)
sys.modules[path].__loader__ = my_loader
try:
importlib.reload(sys.modules[path])
except ModuleNotFoundError:
pass
# Finally, load the plugin class.
self.module_class = importlib.import_module(self.module_path)

async def unload(self):
async with sys_path_lock:
sys.path = [i for i in sys.path if i != self.ansiblez_path]
sys.meta_path = [
i
for i in sys.meta_path
if not (isinstance(i, zipimporter) and i.archive == self.ansiblez_path)
]

def create_profiler(self):
if self.debug_mode:
Expand Down
1 change: 1 addition & 0 deletions plugins/module_utils/turbo_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This module is part of the test suite to check the import logic of turbo mode
2 changes: 2 additions & 0 deletions plugins/modules/turbo_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def run_module():


def main():
from ansible_collections.cloud.common.plugins.module_utils import turbo_demo

run_module()


Expand Down
42 changes: 42 additions & 0 deletions plugins/modules/turbo_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright: (C) 2022, Red Hat
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

DOCUMENTATION = r"""
---
module: turbo_import
short_description: A demo module to test import logic for turbo mode
version_added: "1.0.0"
description:
- "This module tests the import logic for turbo mode."
author:
- Mike Graves (@gravesm)
"""

EXAMPLES = r"""
- name: Run the module
cloud.common.turbo_import:
"""


from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
AnsibleTurboModule as AnsibleModule,
)


def run_module():
module = AnsibleModule(argument_spec={})
module.collection_name = "cloud.common"
module.exit_json(changed=False)


def main():
from ansible_collections.cloud.common.plugins.module_utils import turbo_demo

run_module()


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions tests/integration/targets/turbo_mode/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- cloud.common.turbo_import:

- cloud.common.turbo_demo:
with_sequence: count=10
register: _result
Expand Down
8 changes: 8 additions & 0 deletions tests/sanity/ignore-2.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,11 @@ tests/unit/plugins/module_utils/turbo/conftest.py future-import-boilerplate!skip
tests/unit/plugins/module_utils/turbo/conftest.py metaclass-boilerplate!skip
tests/unit/plugins/module_utils/turbo/test_module.py future-import-boilerplate!skip
tests/unit/plugins/module_utils/turbo/test_module.py metaclass-boilerplate!skip
plugins/modules/turbo_import.py compile-2.6!skip
plugins/modules/turbo_import.py compile-2.7!skip
plugins/modules/turbo_import.py compile-3.5!skip
plugins/modules/turbo_import.py future-import-boilerplate!skip
plugins/modules/turbo_import.py import-2.6!skip
plugins/modules/turbo_import.py import-2.7!skip
plugins/modules/turbo_import.py import-3.5!skip
plugins/modules/turbo_import.py metaclass-boilerplate!skip
8 changes: 8 additions & 0 deletions tests/sanity/ignore-2.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,11 @@ tests/unit/plugins/module_utils/turbo/conftest.py future-import-boilerplate!skip
tests/unit/plugins/module_utils/turbo/conftest.py metaclass-boilerplate!skip
tests/unit/plugins/module_utils/turbo/test_module.py future-import-boilerplate!skip
tests/unit/plugins/module_utils/turbo/test_module.py metaclass-boilerplate!skip
plugins/modules/turbo_import.py compile-2.6!skip
plugins/modules/turbo_import.py compile-2.7!skip
plugins/modules/turbo_import.py compile-3.5!skip
plugins/modules/turbo_import.py future-import-boilerplate!skip
plugins/modules/turbo_import.py import-2.6!skip
plugins/modules/turbo_import.py import-2.7!skip
plugins/modules/turbo_import.py import-3.5!skip
plugins/modules/turbo_import.py metaclass-boilerplate!skip
8 changes: 8 additions & 0 deletions tests/sanity/ignore-2.12.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,11 @@ tests/unit/plugins/module_utils/turbo/conftest.py future-import-boilerplate!skip
tests/unit/plugins/module_utils/turbo/conftest.py metaclass-boilerplate!skip
tests/unit/plugins/module_utils/turbo/test_module.py future-import-boilerplate!skip
tests/unit/plugins/module_utils/turbo/test_module.py metaclass-boilerplate!skip
plugins/modules/turbo_import.py compile-2.6!skip
plugins/modules/turbo_import.py compile-2.7!skip
plugins/modules/turbo_import.py compile-3.5!skip
plugins/modules/turbo_import.py future-import-boilerplate!skip
plugins/modules/turbo_import.py import-2.6!skip
plugins/modules/turbo_import.py import-2.7!skip
plugins/modules/turbo_import.py import-3.5!skip
plugins/modules/turbo_import.py metaclass-boilerplate!skip
6 changes: 6 additions & 0 deletions tests/sanity/ignore-2.13.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ tests/unit/plugins/module_utils/turbo/conftest.py future-import-boilerplate!skip
tests/unit/plugins/module_utils/turbo/conftest.py metaclass-boilerplate!skip
tests/unit/plugins/module_utils/turbo/test_module.py future-import-boilerplate!skip
tests/unit/plugins/module_utils/turbo/test_module.py metaclass-boilerplate!skip
plugins/modules/turbo_import.py compile-2.7!skip
plugins/modules/turbo_import.py compile-3.5!skip
plugins/modules/turbo_import.py future-import-boilerplate!skip
plugins/modules/turbo_import.py import-2.7!skip
plugins/modules/turbo_import.py import-3.5!skip
plugins/modules/turbo_import.py metaclass-boilerplate!skip
8 changes: 8 additions & 0 deletions tests/sanity/ignore-2.9.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,11 @@ tests/unit/plugins/module_utils/turbo/conftest.py future-import-boilerplate!skip
tests/unit/plugins/module_utils/turbo/conftest.py metaclass-boilerplate!skip
tests/unit/plugins/module_utils/turbo/test_module.py future-import-boilerplate!skip
tests/unit/plugins/module_utils/turbo/test_module.py metaclass-boilerplate!skip
plugins/modules/turbo_import.py compile-2.6!skip
plugins/modules/turbo_import.py compile-2.7!skip
plugins/modules/turbo_import.py compile-3.5!skip
plugins/modules/turbo_import.py future-import-boilerplate!skip
plugins/modules/turbo_import.py import-2.6!skip
plugins/modules/turbo_import.py import-2.7!skip
plugins/modules/turbo_import.py import-3.5!skip
plugins/modules/turbo_import.py metaclass-boilerplate!skip