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

Add support for static linters #200

Merged
merged 4 commits into from
Jan 29, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ include requirements.txt
recursive-include moduleframework *
recursive-include docs *
recursive-include examples *
recursive-include tools *
recursive-include tests *
recursive-include distro *
recursive-include man *
recursive-include build_manpages *.py
2 changes: 1 addition & 1 deletion docs/user_guide/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ User Guide

.. code-block:: makefile

MODULE_LINT=/usr/share/moduleframework/tools/modulelint/*.py
MODULE_LINT=/usr/share/moduleframework/tests/generic/*.py
TESTS=*.py
CMD=avocado run $(MODULE_LINT) $(TESTS)

Expand Down
2 changes: 1 addition & 1 deletion docs/user_guide/scheduling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Taskotron Wokflow
- checkout to proper version found by PDC (scmurl)
- Try to find tests there ( if exist `Makefile` in `tests` directory)
- If None: Try to find module dir in MTF project tests in `/usr/share/moduleframework/examples` directory
- If None: Run at least general ModuleLinter (`/usr/share/moduleframework/tools/modulelint`) with general minimal config.yaml located in `docs` directory
- If None: Run at least general ModuleLinter (`/usr/share/moduleframework/tests/modulelint`) with general minimal config.yaml located in `docs` directory

Arbitrary Jenkins Instance
----------------------------
Expand Down
2 changes: 1 addition & 1 deletion examples/testing-module/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CMD=mtf
TESTS=$(shell ls *.py *.sh ../../moduleframework/tools/*.py)
TESTS=$(shell ls *.py *.sh ../../moduleframework/tests/generic/*.py ../../moduleframework/tests/static/*.py)
SIMPLE=simpleTest.py
export MTF_REMOTE_REPOS=yes
export DEBUG=yes
Expand Down
2 changes: 2 additions & 0 deletions moduleframework/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
MODULE_DEFAULT_PROFILE = "default"
TRUE_VALUES_DICT = ['yes', 'YES', 'yes', 'True', 'true', 'ok', 'OK']
OPENSHIFT_INIT_WAIT = 50
STATIC_LINTERS = 'static'
GENERIC_TEST = 'generic'

def generate_unique_name(size=10):
return ''.join(random.choice(string.ascii_lowercase) for _ in range(size))
Expand Down
11 changes: 9 additions & 2 deletions moduleframework/mtf_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def mtfparser():
)
parser.add_argument("--linter", "-l", action="store_true",
default=False, help='adds additional compose checks')
parser.add_argument("--static-linters", action="store_true",
default=False, help='adds static linters checks, like dockerfile and helpmdfile')
parser.add_argument("--setup", action="store_true",
default=False, help='Setup by mtfenvset')
parser.add_argument("--action", action="store", default='run',
Expand Down Expand Up @@ -194,9 +196,14 @@ class AvocadoStart(object):

def __init__(self, args, unknown):
# choose between TESTS and ADDITIONAL ENVIRONMENT from options
if args.static_linters:
self.tests += glob.glob("{MTF_TOOLS}/{STATIC_LINTERS}/*.py".format(
MTF_TOOLS=metadata_common.MetadataLoaderMTF.MTF_LINTER_PATH,
STATIC_LINTERS=common.STATIC_LINTERS))
if args.linter:
self.tests += glob.glob("{MTF_TOOLS}/*.py".format(
MTF_TOOLS=metadata_common.MetadataLoaderMTF.MTF_LINTER_PATH))
self.tests += glob.glob("{MTF_TOOLS}/{GENERIC_TEST}/*.py".format(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change cause regression (now args.linter schedule all generic tests) with your change, just part of them will be sheduled. Is that intentional? we can discuss it. I'm not against this idea, but we should think about it more.

MTF_TOOLS=metadata_common.MetadataLoaderMTF.MTF_LINTER_PATH,
GENERIC_TEST=common.GENERIC_TEST))
self.args = args

for param in unknown:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -28,84 +28,6 @@
from moduleframework.avocado_testers import container_avocado_test


class DockerInstructionsTests(module_framework.AvocadoTest):
"""
:avocado: enable
:avocado: tags=sanity,rhel,fedora,docker,docker_instruction_test

"""

dp = None

def setUp(self):
# it is not intended just for docker, but just docker packages are
# actually properly signed
self.dp = dockerlinter.DockerfileLinter()
if self.dp.dockerfile is None:
self.skip("Dockerfile was not found")

def tearDown(self, *args, **kwargs):
pass

def test_from_is_first_directive(self):
self.assertTrue(self.dp.check_from_is_first())

def test_from_directive_is_valid(self):
self.assertTrue(self.dp.check_from_directive_is_valid())

def test_chained_run_dnf_commands(self):
self.assertTrue(self.dp.check_chained_run_dnf_commands())

def test_chained_run_rest_commands(self):
self.assertTrue(self.dp.check_chained_run_rest_commands())

def test_helpmd_is_present(self):
self.assert_to_warn(self.assertTrue, self.dp.check_helpmd_is_present())


class DockerLabelsTests(DockerInstructionsTests):
"""
:avocado: enable
:avocado: tags=sanity,rhel,fedora,docker,docker_labels_test

"""

def _test_for_env_and_label(self, docker_env, docker_label, env=True):
label_found = True
if env:
label = self.dp.get_docker_specific_env(docker_env)
else:
label = self.dp.get_specific_label(docker_env)
if not label:
label_found = self.dp.get_specific_label(docker_label)
return label_found

def test_architecture_in_env_and_label_exists(self):
self.assertTrue(self.dp.get_specific_label("architecture"))

def test_name_in_env_and_label_exists(self):
self.assertTrue(self.dp.get_docker_specific_env("NAME="))
self.assertTrue(self.dp.get_specific_label("name"))

def test_maintainer_label_exists(self):
self.assertTrue(self.dp.get_specific_label("maintainer"))

def test_release_label_exists(self):
self.assertTrue(self.dp.get_specific_label("release"))

def test_version_label_exists(self):
self.assertTrue(self.dp.get_specific_label("version"))

def test_com_redhat_component_label_exists(self):
self.assertTrue(self.dp.get_specific_label("com.redhat.component"))

def test_summary_label_exists(self):
self.assertTrue(self.dp.get_specific_label("summary"))

def test_run_or_usage_label_exists(self):
self.assertTrue(self._test_for_env_and_label("run", "usage", env=False))


class DockerfileLinterInContainer(container_avocado_test.ContainerAvocadoTest):
"""
:avocado: enable
Expand Down
82 changes: 82 additions & 0 deletions moduleframework/tests/static/dockerfile_lint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from __future__ import print_function

import os
from avocado import Test
from moduleframework import module_framework
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line and remove module_framework.AvocadoTest from class parent

Copy link
Member Author

@phracek phracek Jan 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not possible, because we have in AvocadoTest function assert_to_warn.

from moduleframework import dockerlinter

class DockerInstructionsTests(module_framework.AvocadoTest):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace by inherited from Test

"""
:avocado: enable
:avocado: tags=sanity,rhel,fedora,docker,docker_instruction_test

"""

dp = None

def setUp(self):
# it is not intended just for docker, but just docker packages are
# actually properly signed
self.dp = dockerlinter.DockerfileLinter()
if self.dp.dockerfile is None:
self.skip("Dockerfile was not found")

def test_from_is_first_directive(self):
self.assertTrue(self.dp.check_from_is_first())

def test_from_directive_is_valid(self):
self.assertTrue(self.dp.check_from_directive_is_valid())

def test_chained_run_dnf_commands(self):
self.assertTrue(self.dp.check_chained_run_dnf_commands())

def test_chained_run_rest_commands(self):
self.assertTrue(self.dp.check_chained_run_rest_commands())

def test_helpmd_is_present(self):
self.assert_to_warn(self.assertTrue, self.dp.check_helpmd_is_present())


class DockerLabelsTests(DockerInstructionsTests):
"""
:avocado: enable
:avocado: tags=sanity,rhel,fedora,docker,docker_labels_test

"""

def _test_for_env_and_label(self, docker_env, docker_label, env=True):
label_found = True
if env:
label = self.dp.get_docker_specific_env(docker_env)
else:
label = self.dp.get_specific_label(docker_env)
if not label:
label_found = self.dp.get_specific_label(docker_label)
return label_found

def test_architecture_in_env_and_label_exists(self):
self.assertTrue(self.dp.get_specific_label("architecture"))

def test_name_in_env_and_label_exists(self):
self.assertTrue(self.dp.get_docker_specific_env("NAME="))
self.assertTrue(self.dp.get_specific_label("name"))

def test_maintainer_label_exists(self):
self.assertTrue(self.dp.get_specific_label("maintainer"))

def test_release_label_exists(self):
self.assertTrue(self.dp.get_specific_label("release"))

def test_version_label_exists(self):
self.assertTrue(self.dp.get_specific_label("version"))

def test_com_redhat_component_label_exists(self):
self.assertTrue(self.dp.get_specific_label("com.redhat.component"))

def test_summary_label_exists(self):
self.assertTrue(self.dp.get_specific_label("summary"))

def test_run_or_usage_label_exists(self):
self.assertTrue(self._test_for_env_and_label("run", "usage", env=False))


7 changes: 4 additions & 3 deletions mtf/metadata/tmet/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ class MetadataLoaderMTF(MetadataLoader):
metadata specific class for MTF (avocado) tests
"""
try:
import moduleframework.tools
MTF_LINTER_PATH = os.path.dirname(moduleframework.tools.__file__)
import moduleframework.tests
MTF_LINTER_PATH = os.path.dirname(moduleframework.tests.__file__)
except:
warn("MTF library not installed, linters are ignored")
MTF_LINTER_PATH = None
Expand Down Expand Up @@ -396,7 +396,8 @@ def _import_tests(self, testglob, pathlenght=0):

def _import_linters(self):
if self.MTF_LINTER_PATH:
self._import_tests(os.path.join(self.MTF_LINTER_PATH, "*.py"), pathlenght=-3)
self._import_tests(os.path.join(self.MTF_LINTER_PATH, "generic", "*.py"), pathlenght=-3)
self._import_tests(os.path.join(self.MTF_LINTER_PATH, "static", "*.py"), pathlenght=-3)

def __avcado_tag_args(self, tag_list, defaultparam="--filter-by-tags-include-empty"):
output = []
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def get_dir(system_path=None, virtual_path=None):

data_files = {}

paths = ['docs', 'examples', 'tools']
paths = ['docs', 'examples', 'tests']

for path in paths:
for root, dirs, files in os.walk(path, followlinks=True):
Expand All @@ -77,7 +77,7 @@ def get_dir(system_path=None, virtual_path=None):
author_email='jscotka@redhat.com',
url='https://github.com/fedora-modularity/meta-test-family',
license='GPLv2+',
packages=find_packages(exclude=['docs', 'examples', 'tools']),
packages=find_packages(exclude=['docs', 'examples', 'tests']),
include_package_data=True,
data_files=data_files.items(),
scripts=[],
Expand Down
10 changes: 6 additions & 4 deletions tools/mtf_modulelint.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
# TODO: This is not working now, because it is still not implemented in upsteram avocado:
# TODO: https://github.com/avocado-framework/avocado/issues/1792

from moduleframework.tools.check_compose import ComposeTest
from moduleframework.tools.rpmvalidation import rpmvalidation
from moduleframework.tools.modulelint import *
from moduleframework.tests.generic.check_compose import ComposeTest
from moduleframework.tests.generic.rpmvalidation import rpmvalidation
from moduleframework.tests.generic.modulelint import *
from moduleframework.tests.generic.dockerlint import *
from moduleframework.tests.static.dockerfile_lint import *

class MTFComposeTest(ComposeTest):
"""
Expand All @@ -45,7 +47,7 @@ class MTFRpmValidation(rpmvalidation):
pass


class MTFDockerFileLinter(DockerFileLinter):
class MTFDockerFileLinter(DockerfileLinterInContainer):
"""
:avocado: recursive
"""
Expand Down