2016-11-30 06:21:53 +01:00
|
|
|
"""Code coverage utilities."""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .target import (
|
2017-01-19 01:31:34 +01:00
|
|
|
walk_module_targets,
|
2017-05-15 18:51:49 +02:00
|
|
|
walk_compile_targets,
|
2017-01-19 01:31:34 +01:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .util import (
|
2017-01-19 01:31:34 +01:00
|
|
|
display,
|
|
|
|
ApplicationError,
|
2017-05-15 11:42:11 +02:00
|
|
|
common_environment,
|
2019-08-07 20:43:42 +02:00
|
|
|
ANSIBLE_TEST_DATA_ROOT,
|
2017-01-19 01:31:34 +01:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .util_common import (
|
2019-07-11 07:00:34 +02:00
|
|
|
run_command,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .config import (
|
2017-07-07 01:14:44 +02:00
|
|
|
CoverageConfig,
|
|
|
|
CoverageReportConfig,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .executor import (
|
2017-01-19 01:31:34 +01:00
|
|
|
Delegate,
|
|
|
|
install_command_requirements,
|
|
|
|
)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .data import (
|
2019-07-23 04:24:48 +02:00
|
|
|
data_context,
|
|
|
|
)
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
COVERAGE_DIR = 'test/results/coverage'
|
|
|
|
COVERAGE_FILE = os.path.join(COVERAGE_DIR, 'coverage')
|
2017-05-15 11:42:11 +02:00
|
|
|
COVERAGE_GROUPS = ('command', 'target', 'environment', 'version')
|
2019-08-07 20:43:42 +02:00
|
|
|
COVERAGE_CONFIG_PATH = os.path.join(ANSIBLE_TEST_DATA_ROOT, 'coveragerc')
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
def command_coverage_combine(args):
|
|
|
|
"""Patch paths in coverage files and merge into a single file.
|
|
|
|
:type args: CoverageConfig
|
2017-05-15 11:42:11 +02:00
|
|
|
:rtype: list[str]
|
2016-11-30 06:21:53 +01:00
|
|
|
"""
|
|
|
|
coverage = initialize_coverage(args)
|
|
|
|
|
2018-07-25 03:28:08 +02:00
|
|
|
modules = dict((t.module, t.path) for t in list(walk_module_targets()) if t.path.endswith('.py'))
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2017-05-11 07:25:02 +02:00
|
|
|
coverage_files = [os.path.join(COVERAGE_DIR, f) for f in os.listdir(COVERAGE_DIR) if '=coverage.' in f]
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
ansible_path = os.path.abspath('lib/ansible/') + '/'
|
2019-07-23 04:24:48 +02:00
|
|
|
root_path = data_context().content.root + '/'
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2017-02-25 01:58:56 +01:00
|
|
|
counter = 0
|
2017-05-15 11:42:11 +02:00
|
|
|
groups = {}
|
2017-02-25 01:58:56 +01:00
|
|
|
|
2017-05-15 18:51:49 +02:00
|
|
|
if args.all or args.stub:
|
|
|
|
sources = sorted(os.path.abspath(target.path) for target in walk_compile_targets())
|
|
|
|
else:
|
|
|
|
sources = []
|
|
|
|
|
|
|
|
if args.stub:
|
2019-03-11 23:14:09 +01:00
|
|
|
stub_group = []
|
|
|
|
stub_groups = [stub_group]
|
|
|
|
stub_line_limit = 500000
|
|
|
|
stub_line_count = 0
|
|
|
|
|
|
|
|
for source in sources:
|
|
|
|
with open(source, 'r') as source_fd:
|
|
|
|
source_line_count = len(source_fd.read().splitlines())
|
|
|
|
|
|
|
|
stub_group.append(source)
|
|
|
|
stub_line_count += source_line_count
|
|
|
|
|
|
|
|
if stub_line_count > stub_line_limit:
|
|
|
|
stub_line_count = 0
|
|
|
|
stub_group = []
|
|
|
|
stub_groups.append(stub_group)
|
|
|
|
|
|
|
|
for stub_index, stub_group in enumerate(stub_groups):
|
|
|
|
if not stub_group:
|
|
|
|
continue
|
|
|
|
|
|
|
|
groups['=stub-%02d' % (stub_index + 1)] = dict((source, set()) for source in stub_group)
|
2017-05-15 18:51:49 +02:00
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
if data_context().content.collection:
|
|
|
|
collection_search_re = re.compile(r'/%s/' % data_context().content.collection.directory)
|
|
|
|
collection_sub_re = re.compile(r'^.*?/%s/' % data_context().content.collection.directory)
|
|
|
|
else:
|
|
|
|
collection_search_re = None
|
|
|
|
collection_sub_re = None
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
for coverage_file in coverage_files:
|
2017-02-25 01:58:56 +01:00
|
|
|
counter += 1
|
|
|
|
display.info('[%4d/%4d] %s' % (counter, len(coverage_files), coverage_file), verbosity=2)
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
original = coverage.CoverageData()
|
|
|
|
|
2017-05-15 11:42:11 +02:00
|
|
|
group = get_coverage_group(args, coverage_file)
|
|
|
|
|
|
|
|
if group is None:
|
|
|
|
display.warning('Unexpected name for coverage file: %s' % coverage_file)
|
|
|
|
continue
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
if os.path.getsize(coverage_file) == 0:
|
|
|
|
display.warning('Empty coverage file: %s' % coverage_file)
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
original.read_file(coverage_file)
|
|
|
|
except Exception as ex: # pylint: disable=locally-disabled, broad-except
|
2018-10-02 21:21:36 +02:00
|
|
|
display.error(u'%s' % ex)
|
2016-11-30 06:21:53 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
for filename in original.measured_files():
|
2017-05-11 07:25:02 +02:00
|
|
|
arcs = set(original.arcs(filename) or [])
|
|
|
|
|
|
|
|
if not arcs:
|
|
|
|
# This is most likely due to using an unsupported version of coverage.
|
|
|
|
display.warning('No arcs found for "%s" in coverage file: %s' % (filename, coverage_file))
|
|
|
|
continue
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
if '/ansible_modlib.zip/ansible/' in filename:
|
AnsiballZ improvements
Now that we don't need to worry about python-2.4 and 2.5, we can make
some improvements to the way AnsiballZ handles modules.
* Change AnsiballZ wrapper to use import to invoke the module
We need the module to think of itself as a script because it could be
coded as:
main()
or as:
if __name__ == '__main__':
main()
Or even as:
if __name__ == '__main__':
random_function_name()
A script will invoke all of those. Prior to this change, we invoked
a second Python interpreter on the module so that it really was
a script. However, this means that we have to run python twice (once
for the AnsiballZ wrapper and once for the module). This change makes
the module think that it is a script (because __name__ in the module ==
'__main__') but it's actually being invoked by us importing the module
code.
There's three ways we've come up to do this.
* The most elegant is to use zipimporter and tell the import mechanism
that the module being loaded is __main__:
* https://github.com/abadger/ansible/blob/5959f11c9ddb7b6eaa9c3214560bd85e631d4055/lib/ansible/executor/module_common.py#L175
* zipimporter is nice because we do not have to extract the module from
the zip file and save it to the disk when we do that. The import
machinery does it all for us.
* The drawback is that modules do not have a __file__ which points
to a real file when they do this. Modules could be using __file__
to for a variety of reasons, most of those probably have
replacements (the most common one is to find a writable directory
for temporary files. AnsibleModule.tmpdir should be used instead)
We can monkeypatch __file__ in fom AnsibleModule initialization
but that's kind of gross. There's no way I can see to do this
from the wrapper.
* Next, there's imp.load_module():
* https://github.com/abadger/ansible/blob/340edf7489/lib/ansible/executor/module_common.py#L151
* imp has the nice property of allowing us to set __name__ to
__main__ without changing the name of the file itself
* We also don't have to do anything special to set __file__ for
backwards compatibility (although the reason for that is the
drawback):
* Its drawback is that it requires the file to exist on disk so we
have to explicitly extract it from the zipfile and save it to
a temporary file
* The last choice is to use exec to execute the module:
* https://github.com/abadger/ansible/blob/f47a4ccc76/lib/ansible/executor/module_common.py#L175
* The code we would have to maintain for this looks pretty clean.
In the wrapper we create a ModuleType, set __file__ on it, read
the module's contents in from the zip file and then exec it.
* Drawbacks: We still have to explicitly extract the file's contents
from the zip archive instead of letting python's import mechanism
handle it.
* Exec also has hidden performance issues and breaks certain
assumptions that modules could be making about their own code:
http://lucumr.pocoo.org/2011/2/1/exec-in-python/
Our plan is to use imp.load_module() for now, deprecate the use of
__file__ in modules, and switch to zipimport once the deprecation
period for __file__ is over (without monkeypatching a fake __file__ in
via AnsibleModule).
* Rename the name of the AnsiBallZ wrapped module
This makes it obvious that the wrapped module isn't the module file that
we distribute. It's part of trying to mitigate the fact that the module
is now named __main)).py in tracebacks.
* Shield all wrapper symbols inside of a function
With the new import code, all symbols in the wrapper become visible in
the module. To mitigate the chance of collisions, move most symbols
into a toplevel function. The only symbols left in the global namespace
are now _ANSIBALLZ_WRAPPER and _ansiballz_main.
revised porting guide entry
Integrate code coverage collection into AnsiballZ.
ci_coverage
ci_complete
2018-06-20 20:23:59 +02:00
|
|
|
# Rewrite the module_utils path from the remote host to match the controller. Ansible 2.6 and earlier.
|
2016-11-30 06:21:53 +01:00
|
|
|
new_name = re.sub('^.*/ansible_modlib.zip/ansible/', ansible_path, filename)
|
|
|
|
display.info('%s -> %s' % (filename, new_name), verbosity=3)
|
|
|
|
filename = new_name
|
2019-07-23 04:24:48 +02:00
|
|
|
elif collection_search_re and collection_search_re.search(filename):
|
|
|
|
new_name = os.path.abspath(collection_sub_re.sub('', filename))
|
|
|
|
display.info('%s -> %s' % (filename, new_name), verbosity=3)
|
|
|
|
filename = new_name
|
AnsiballZ improvements
Now that we don't need to worry about python-2.4 and 2.5, we can make
some improvements to the way AnsiballZ handles modules.
* Change AnsiballZ wrapper to use import to invoke the module
We need the module to think of itself as a script because it could be
coded as:
main()
or as:
if __name__ == '__main__':
main()
Or even as:
if __name__ == '__main__':
random_function_name()
A script will invoke all of those. Prior to this change, we invoked
a second Python interpreter on the module so that it really was
a script. However, this means that we have to run python twice (once
for the AnsiballZ wrapper and once for the module). This change makes
the module think that it is a script (because __name__ in the module ==
'__main__') but it's actually being invoked by us importing the module
code.
There's three ways we've come up to do this.
* The most elegant is to use zipimporter and tell the import mechanism
that the module being loaded is __main__:
* https://github.com/abadger/ansible/blob/5959f11c9ddb7b6eaa9c3214560bd85e631d4055/lib/ansible/executor/module_common.py#L175
* zipimporter is nice because we do not have to extract the module from
the zip file and save it to the disk when we do that. The import
machinery does it all for us.
* The drawback is that modules do not have a __file__ which points
to a real file when they do this. Modules could be using __file__
to for a variety of reasons, most of those probably have
replacements (the most common one is to find a writable directory
for temporary files. AnsibleModule.tmpdir should be used instead)
We can monkeypatch __file__ in fom AnsibleModule initialization
but that's kind of gross. There's no way I can see to do this
from the wrapper.
* Next, there's imp.load_module():
* https://github.com/abadger/ansible/blob/340edf7489/lib/ansible/executor/module_common.py#L151
* imp has the nice property of allowing us to set __name__ to
__main__ without changing the name of the file itself
* We also don't have to do anything special to set __file__ for
backwards compatibility (although the reason for that is the
drawback):
* Its drawback is that it requires the file to exist on disk so we
have to explicitly extract it from the zipfile and save it to
a temporary file
* The last choice is to use exec to execute the module:
* https://github.com/abadger/ansible/blob/f47a4ccc76/lib/ansible/executor/module_common.py#L175
* The code we would have to maintain for this looks pretty clean.
In the wrapper we create a ModuleType, set __file__ on it, read
the module's contents in from the zip file and then exec it.
* Drawbacks: We still have to explicitly extract the file's contents
from the zip archive instead of letting python's import mechanism
handle it.
* Exec also has hidden performance issues and breaks certain
assumptions that modules could be making about their own code:
http://lucumr.pocoo.org/2011/2/1/exec-in-python/
Our plan is to use imp.load_module() for now, deprecate the use of
__file__ in modules, and switch to zipimport once the deprecation
period for __file__ is over (without monkeypatching a fake __file__ in
via AnsibleModule).
* Rename the name of the AnsiBallZ wrapped module
This makes it obvious that the wrapped module isn't the module file that
we distribute. It's part of trying to mitigate the fact that the module
is now named __main)).py in tracebacks.
* Shield all wrapper symbols inside of a function
With the new import code, all symbols in the wrapper become visible in
the module. To mitigate the chance of collisions, move most symbols
into a toplevel function. The only symbols left in the global namespace
are now _ANSIBALLZ_WRAPPER and _ansiballz_main.
revised porting guide entry
Integrate code coverage collection into AnsiballZ.
ci_coverage
ci_complete
2018-06-20 20:23:59 +02:00
|
|
|
elif re.search(r'/ansible_[^/]+_payload\.zip/ansible/', filename):
|
|
|
|
# Rewrite the module_utils path from the remote host to match the controller. Ansible 2.7 and later.
|
|
|
|
new_name = re.sub(r'^.*/ansible_[^/]+_payload\.zip/ansible/', ansible_path, filename)
|
|
|
|
display.info('%s -> %s' % (filename, new_name), verbosity=3)
|
|
|
|
filename = new_name
|
2016-11-30 06:21:53 +01:00
|
|
|
elif '/ansible_module_' in filename:
|
AnsiballZ improvements
Now that we don't need to worry about python-2.4 and 2.5, we can make
some improvements to the way AnsiballZ handles modules.
* Change AnsiballZ wrapper to use import to invoke the module
We need the module to think of itself as a script because it could be
coded as:
main()
or as:
if __name__ == '__main__':
main()
Or even as:
if __name__ == '__main__':
random_function_name()
A script will invoke all of those. Prior to this change, we invoked
a second Python interpreter on the module so that it really was
a script. However, this means that we have to run python twice (once
for the AnsiballZ wrapper and once for the module). This change makes
the module think that it is a script (because __name__ in the module ==
'__main__') but it's actually being invoked by us importing the module
code.
There's three ways we've come up to do this.
* The most elegant is to use zipimporter and tell the import mechanism
that the module being loaded is __main__:
* https://github.com/abadger/ansible/blob/5959f11c9ddb7b6eaa9c3214560bd85e631d4055/lib/ansible/executor/module_common.py#L175
* zipimporter is nice because we do not have to extract the module from
the zip file and save it to the disk when we do that. The import
machinery does it all for us.
* The drawback is that modules do not have a __file__ which points
to a real file when they do this. Modules could be using __file__
to for a variety of reasons, most of those probably have
replacements (the most common one is to find a writable directory
for temporary files. AnsibleModule.tmpdir should be used instead)
We can monkeypatch __file__ in fom AnsibleModule initialization
but that's kind of gross. There's no way I can see to do this
from the wrapper.
* Next, there's imp.load_module():
* https://github.com/abadger/ansible/blob/340edf7489/lib/ansible/executor/module_common.py#L151
* imp has the nice property of allowing us to set __name__ to
__main__ without changing the name of the file itself
* We also don't have to do anything special to set __file__ for
backwards compatibility (although the reason for that is the
drawback):
* Its drawback is that it requires the file to exist on disk so we
have to explicitly extract it from the zipfile and save it to
a temporary file
* The last choice is to use exec to execute the module:
* https://github.com/abadger/ansible/blob/f47a4ccc76/lib/ansible/executor/module_common.py#L175
* The code we would have to maintain for this looks pretty clean.
In the wrapper we create a ModuleType, set __file__ on it, read
the module's contents in from the zip file and then exec it.
* Drawbacks: We still have to explicitly extract the file's contents
from the zip archive instead of letting python's import mechanism
handle it.
* Exec also has hidden performance issues and breaks certain
assumptions that modules could be making about their own code:
http://lucumr.pocoo.org/2011/2/1/exec-in-python/
Our plan is to use imp.load_module() for now, deprecate the use of
__file__ in modules, and switch to zipimport once the deprecation
period for __file__ is over (without monkeypatching a fake __file__ in
via AnsibleModule).
* Rename the name of the AnsiBallZ wrapped module
This makes it obvious that the wrapped module isn't the module file that
we distribute. It's part of trying to mitigate the fact that the module
is now named __main)).py in tracebacks.
* Shield all wrapper symbols inside of a function
With the new import code, all symbols in the wrapper become visible in
the module. To mitigate the chance of collisions, move most symbols
into a toplevel function. The only symbols left in the global namespace
are now _ANSIBALLZ_WRAPPER and _ansiballz_main.
revised porting guide entry
Integrate code coverage collection into AnsiballZ.
ci_coverage
ci_complete
2018-06-20 20:23:59 +02:00
|
|
|
# Rewrite the module path from the remote host to match the controller. Ansible 2.6 and earlier.
|
2017-08-23 20:09:50 +02:00
|
|
|
module_name = re.sub('^.*/ansible_module_(?P<module>.*).py$', '\\g<module>', filename)
|
|
|
|
if module_name not in modules:
|
|
|
|
display.warning('Skipping coverage of unknown module: %s' % module_name)
|
2017-05-11 07:25:02 +02:00
|
|
|
continue
|
2017-08-23 20:09:50 +02:00
|
|
|
new_name = os.path.abspath(modules[module_name])
|
2016-11-30 06:21:53 +01:00
|
|
|
display.info('%s -> %s' % (filename, new_name), verbosity=3)
|
|
|
|
filename = new_name
|
AnsiballZ improvements
Now that we don't need to worry about python-2.4 and 2.5, we can make
some improvements to the way AnsiballZ handles modules.
* Change AnsiballZ wrapper to use import to invoke the module
We need the module to think of itself as a script because it could be
coded as:
main()
or as:
if __name__ == '__main__':
main()
Or even as:
if __name__ == '__main__':
random_function_name()
A script will invoke all of those. Prior to this change, we invoked
a second Python interpreter on the module so that it really was
a script. However, this means that we have to run python twice (once
for the AnsiballZ wrapper and once for the module). This change makes
the module think that it is a script (because __name__ in the module ==
'__main__') but it's actually being invoked by us importing the module
code.
There's three ways we've come up to do this.
* The most elegant is to use zipimporter and tell the import mechanism
that the module being loaded is __main__:
* https://github.com/abadger/ansible/blob/5959f11c9ddb7b6eaa9c3214560bd85e631d4055/lib/ansible/executor/module_common.py#L175
* zipimporter is nice because we do not have to extract the module from
the zip file and save it to the disk when we do that. The import
machinery does it all for us.
* The drawback is that modules do not have a __file__ which points
to a real file when they do this. Modules could be using __file__
to for a variety of reasons, most of those probably have
replacements (the most common one is to find a writable directory
for temporary files. AnsibleModule.tmpdir should be used instead)
We can monkeypatch __file__ in fom AnsibleModule initialization
but that's kind of gross. There's no way I can see to do this
from the wrapper.
* Next, there's imp.load_module():
* https://github.com/abadger/ansible/blob/340edf7489/lib/ansible/executor/module_common.py#L151
* imp has the nice property of allowing us to set __name__ to
__main__ without changing the name of the file itself
* We also don't have to do anything special to set __file__ for
backwards compatibility (although the reason for that is the
drawback):
* Its drawback is that it requires the file to exist on disk so we
have to explicitly extract it from the zipfile and save it to
a temporary file
* The last choice is to use exec to execute the module:
* https://github.com/abadger/ansible/blob/f47a4ccc76/lib/ansible/executor/module_common.py#L175
* The code we would have to maintain for this looks pretty clean.
In the wrapper we create a ModuleType, set __file__ on it, read
the module's contents in from the zip file and then exec it.
* Drawbacks: We still have to explicitly extract the file's contents
from the zip archive instead of letting python's import mechanism
handle it.
* Exec also has hidden performance issues and breaks certain
assumptions that modules could be making about their own code:
http://lucumr.pocoo.org/2011/2/1/exec-in-python/
Our plan is to use imp.load_module() for now, deprecate the use of
__file__ in modules, and switch to zipimport once the deprecation
period for __file__ is over (without monkeypatching a fake __file__ in
via AnsibleModule).
* Rename the name of the AnsiBallZ wrapped module
This makes it obvious that the wrapped module isn't the module file that
we distribute. It's part of trying to mitigate the fact that the module
is now named __main)).py in tracebacks.
* Shield all wrapper symbols inside of a function
With the new import code, all symbols in the wrapper become visible in
the module. To mitigate the chance of collisions, move most symbols
into a toplevel function. The only symbols left in the global namespace
are now _ANSIBALLZ_WRAPPER and _ansiballz_main.
revised porting guide entry
Integrate code coverage collection into AnsiballZ.
ci_coverage
ci_complete
2018-06-20 20:23:59 +02:00
|
|
|
elif re.search(r'/ansible_[^/]+_payload(_[^/]+|\.zip)/__main__\.py$', filename):
|
|
|
|
# Rewrite the module path from the remote host to match the controller. Ansible 2.7 and later.
|
|
|
|
# AnsiballZ versions using zipimporter will match the `.zip` portion of the regex.
|
|
|
|
# AnsiballZ versions not using zipimporter will match the `_[^/]+` portion of the regex.
|
|
|
|
module_name = re.sub(r'^.*/ansible_(?P<module>[^/]+)_payload(_[^/]+|\.zip)/__main__\.py$', '\\g<module>', filename).rstrip('_')
|
|
|
|
if module_name not in modules:
|
|
|
|
display.warning('Skipping coverage of unknown module: %s' % module_name)
|
|
|
|
continue
|
|
|
|
new_name = os.path.abspath(modules[module_name])
|
|
|
|
display.info('%s -> %s' % (filename, new_name), verbosity=3)
|
|
|
|
filename = new_name
|
2017-05-11 07:25:02 +02:00
|
|
|
elif re.search('^(/.*?)?/root/ansible/', filename):
|
AnsiballZ improvements
Now that we don't need to worry about python-2.4 and 2.5, we can make
some improvements to the way AnsiballZ handles modules.
* Change AnsiballZ wrapper to use import to invoke the module
We need the module to think of itself as a script because it could be
coded as:
main()
or as:
if __name__ == '__main__':
main()
Or even as:
if __name__ == '__main__':
random_function_name()
A script will invoke all of those. Prior to this change, we invoked
a second Python interpreter on the module so that it really was
a script. However, this means that we have to run python twice (once
for the AnsiballZ wrapper and once for the module). This change makes
the module think that it is a script (because __name__ in the module ==
'__main__') but it's actually being invoked by us importing the module
code.
There's three ways we've come up to do this.
* The most elegant is to use zipimporter and tell the import mechanism
that the module being loaded is __main__:
* https://github.com/abadger/ansible/blob/5959f11c9ddb7b6eaa9c3214560bd85e631d4055/lib/ansible/executor/module_common.py#L175
* zipimporter is nice because we do not have to extract the module from
the zip file and save it to the disk when we do that. The import
machinery does it all for us.
* The drawback is that modules do not have a __file__ which points
to a real file when they do this. Modules could be using __file__
to for a variety of reasons, most of those probably have
replacements (the most common one is to find a writable directory
for temporary files. AnsibleModule.tmpdir should be used instead)
We can monkeypatch __file__ in fom AnsibleModule initialization
but that's kind of gross. There's no way I can see to do this
from the wrapper.
* Next, there's imp.load_module():
* https://github.com/abadger/ansible/blob/340edf7489/lib/ansible/executor/module_common.py#L151
* imp has the nice property of allowing us to set __name__ to
__main__ without changing the name of the file itself
* We also don't have to do anything special to set __file__ for
backwards compatibility (although the reason for that is the
drawback):
* Its drawback is that it requires the file to exist on disk so we
have to explicitly extract it from the zipfile and save it to
a temporary file
* The last choice is to use exec to execute the module:
* https://github.com/abadger/ansible/blob/f47a4ccc76/lib/ansible/executor/module_common.py#L175
* The code we would have to maintain for this looks pretty clean.
In the wrapper we create a ModuleType, set __file__ on it, read
the module's contents in from the zip file and then exec it.
* Drawbacks: We still have to explicitly extract the file's contents
from the zip archive instead of letting python's import mechanism
handle it.
* Exec also has hidden performance issues and breaks certain
assumptions that modules could be making about their own code:
http://lucumr.pocoo.org/2011/2/1/exec-in-python/
Our plan is to use imp.load_module() for now, deprecate the use of
__file__ in modules, and switch to zipimport once the deprecation
period for __file__ is over (without monkeypatching a fake __file__ in
via AnsibleModule).
* Rename the name of the AnsiBallZ wrapped module
This makes it obvious that the wrapped module isn't the module file that
we distribute. It's part of trying to mitigate the fact that the module
is now named __main)).py in tracebacks.
* Shield all wrapper symbols inside of a function
With the new import code, all symbols in the wrapper become visible in
the module. To mitigate the chance of collisions, move most symbols
into a toplevel function. The only symbols left in the global namespace
are now _ANSIBALLZ_WRAPPER and _ansiballz_main.
revised porting guide entry
Integrate code coverage collection into AnsiballZ.
ci_coverage
ci_complete
2018-06-20 20:23:59 +02:00
|
|
|
# Rewrite the path of code running on a remote host or in a docker container as root.
|
2017-05-11 07:25:02 +02:00
|
|
|
new_name = re.sub('^(/.*?)?/root/ansible/', root_path, filename)
|
2016-11-30 06:21:53 +01:00
|
|
|
display.info('%s -> %s' % (filename, new_name), verbosity=3)
|
|
|
|
filename = new_name
|
2019-02-25 20:45:49 +01:00
|
|
|
elif '/.ansible/test/tmp/' in filename:
|
|
|
|
# Rewrite the path of code running from an integration test temporary directory.
|
|
|
|
new_name = re.sub(r'^.*/\.ansible/test/tmp/[^/]+/', root_path, filename)
|
|
|
|
display.info('%s -> %s' % (filename, new_name), verbosity=3)
|
|
|
|
filename = new_name
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2017-05-15 11:42:11 +02:00
|
|
|
if group not in groups:
|
|
|
|
groups[group] = {}
|
|
|
|
|
|
|
|
arc_data = groups[group]
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
if filename not in arc_data:
|
2017-02-28 00:46:15 +01:00
|
|
|
arc_data[filename] = set()
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2017-02-28 00:46:15 +01:00
|
|
|
arc_data[filename].update(arcs)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2017-05-15 11:42:11 +02:00
|
|
|
output_files = []
|
2019-03-08 23:06:33 +01:00
|
|
|
invalid_path_count = 0
|
|
|
|
invalid_path_chars = 0
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2017-05-15 11:42:11 +02:00
|
|
|
for group in sorted(groups):
|
|
|
|
arc_data = groups[group]
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2017-05-15 11:42:11 +02:00
|
|
|
updated = coverage.CoverageData()
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2017-05-15 11:42:11 +02:00
|
|
|
for filename in arc_data:
|
|
|
|
if not os.path.isfile(filename):
|
2019-07-24 03:21:04 +02:00
|
|
|
if collection_search_re and collection_search_re.search(filename) and os.path.basename(filename) == '__init__.py':
|
|
|
|
# the collection loader uses implicit namespace packages, so __init__.py does not need to exist on disk
|
|
|
|
continue
|
|
|
|
|
2019-03-08 23:06:33 +01:00
|
|
|
invalid_path_count += 1
|
|
|
|
invalid_path_chars += len(filename)
|
|
|
|
|
|
|
|
if args.verbosity > 1:
|
|
|
|
display.warning('Invalid coverage path: %s' % filename)
|
|
|
|
|
2017-05-15 11:42:11 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
updated.add_arcs({filename: list(arc_data[filename])})
|
|
|
|
|
2017-05-15 18:51:49 +02:00
|
|
|
if args.all:
|
|
|
|
updated.add_arcs(dict((source, []) for source in sources))
|
|
|
|
|
2017-05-15 11:42:11 +02:00
|
|
|
if not args.explain:
|
|
|
|
output_file = COVERAGE_FILE + group
|
|
|
|
updated.write_file(output_file)
|
|
|
|
output_files.append(output_file)
|
|
|
|
|
2019-03-08 23:06:33 +01:00
|
|
|
if invalid_path_count > 0:
|
|
|
|
display.warning('Ignored %d characters from %d invalid coverage path(s).' % (invalid_path_chars, invalid_path_count))
|
|
|
|
|
2017-05-15 11:42:11 +02:00
|
|
|
return sorted(output_files)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
def command_coverage_report(args):
|
|
|
|
"""
|
2017-07-05 19:24:56 +02:00
|
|
|
:type args: CoverageReportConfig
|
2016-11-30 06:21:53 +01:00
|
|
|
"""
|
2017-05-15 11:42:11 +02:00
|
|
|
output_files = command_coverage_combine(args)
|
|
|
|
|
|
|
|
for output_file in output_files:
|
2017-05-15 18:51:49 +02:00
|
|
|
if args.group_by or args.stub:
|
2017-05-15 11:42:11 +02:00
|
|
|
display.info('>>> Coverage Group: %s' % ' '.join(os.path.basename(output_file).split('=')[1:]))
|
|
|
|
|
2017-07-05 19:24:56 +02:00
|
|
|
options = []
|
|
|
|
|
|
|
|
if args.show_missing:
|
|
|
|
options.append('--show-missing')
|
|
|
|
|
2018-03-29 01:42:57 +02:00
|
|
|
if args.include:
|
|
|
|
options.extend(['--include', args.include])
|
|
|
|
|
|
|
|
if args.omit:
|
|
|
|
options.extend(['--omit', args.omit])
|
|
|
|
|
2017-05-15 11:42:11 +02:00
|
|
|
env = common_environment()
|
|
|
|
env.update(dict(COVERAGE_FILE=output_file))
|
2019-08-07 20:43:42 +02:00
|
|
|
run_command(args, env=env, cmd=['coverage', 'report', '--rcfile', COVERAGE_CONFIG_PATH] + options)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
def command_coverage_html(args):
|
|
|
|
"""
|
|
|
|
:type args: CoverageConfig
|
|
|
|
"""
|
2017-05-15 11:42:11 +02:00
|
|
|
output_files = command_coverage_combine(args)
|
|
|
|
|
|
|
|
for output_file in output_files:
|
|
|
|
dir_name = 'test/results/reports/%s' % os.path.basename(output_file)
|
|
|
|
env = common_environment()
|
|
|
|
env.update(dict(COVERAGE_FILE=output_file))
|
2019-08-07 20:43:42 +02:00
|
|
|
run_command(args, env=env, cmd=['coverage', 'html', '--rcfile', COVERAGE_CONFIG_PATH, '-i', '-d', dir_name])
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
def command_coverage_xml(args):
|
|
|
|
"""
|
|
|
|
:type args: CoverageConfig
|
|
|
|
"""
|
2017-05-15 11:42:11 +02:00
|
|
|
output_files = command_coverage_combine(args)
|
|
|
|
|
|
|
|
for output_file in output_files:
|
|
|
|
xml_name = 'test/results/reports/%s.xml' % os.path.basename(output_file)
|
|
|
|
env = common_environment()
|
|
|
|
env.update(dict(COVERAGE_FILE=output_file))
|
2019-08-07 20:43:42 +02:00
|
|
|
run_command(args, env=env, cmd=['coverage', 'xml', '--rcfile', COVERAGE_CONFIG_PATH, '-i', '-o', xml_name])
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
def command_coverage_erase(args):
|
|
|
|
"""
|
|
|
|
:type args: CoverageConfig
|
|
|
|
"""
|
|
|
|
initialize_coverage(args)
|
|
|
|
|
|
|
|
for name in os.listdir(COVERAGE_DIR):
|
2017-05-11 07:25:02 +02:00
|
|
|
if not name.startswith('coverage') and '=coverage.' not in name:
|
2016-11-30 06:21:53 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
path = os.path.join(COVERAGE_DIR, name)
|
|
|
|
|
|
|
|
if not args.explain:
|
|
|
|
os.remove(path)
|
|
|
|
|
|
|
|
|
|
|
|
def initialize_coverage(args):
|
|
|
|
"""
|
|
|
|
:type args: CoverageConfig
|
|
|
|
:rtype: coverage
|
|
|
|
"""
|
|
|
|
if args.delegate:
|
|
|
|
raise Delegate()
|
|
|
|
|
|
|
|
if args.requirements:
|
|
|
|
install_command_requirements(args)
|
|
|
|
|
|
|
|
try:
|
|
|
|
import coverage
|
|
|
|
except ImportError:
|
|
|
|
coverage = None
|
|
|
|
|
|
|
|
if not coverage:
|
|
|
|
raise ApplicationError('You must install the "coverage" python module to use this command.')
|
|
|
|
|
|
|
|
return coverage
|
|
|
|
|
|
|
|
|
2017-05-15 11:42:11 +02:00
|
|
|
def get_coverage_group(args, coverage_file):
|
|
|
|
"""
|
|
|
|
:type args: CoverageConfig
|
|
|
|
:type coverage_file: str
|
|
|
|
:rtype: str
|
|
|
|
"""
|
|
|
|
parts = os.path.basename(coverage_file).split('=', 4)
|
|
|
|
|
|
|
|
if len(parts) != 5 or not parts[4].startswith('coverage.'):
|
|
|
|
return None
|
|
|
|
|
|
|
|
names = dict(
|
|
|
|
command=parts[0],
|
|
|
|
target=parts[1],
|
|
|
|
environment=parts[2],
|
|
|
|
version=parts[3],
|
|
|
|
)
|
|
|
|
|
|
|
|
group = ''
|
|
|
|
|
|
|
|
for part in COVERAGE_GROUPS:
|
|
|
|
if part in args.group_by:
|
|
|
|
group += '=%s' % names[part]
|
|
|
|
|
|
|
|
return group
|