summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason DeTiberus <detiber@gmail.com>2017-01-18 16:33:16 -0500
committerGitHub <noreply@github.com>2017-01-18 16:33:16 -0500
commitf185fa6ce66a25b8f44c81aa963e0c27e2742586 (patch)
tree09ea4643ef8e295e327b0d5509685d7678224a3d
parente23e9bf557c883d294462e76bdd9399520b01ebc (diff)
parentb6576d28ec21411f8da58cedab7a6c6c57dc172b (diff)
downloadopenshift-f185fa6ce66a25b8f44c81aa963e0c27e2742586.tar.gz
openshift-f185fa6ce66a25b8f44c81aa963e0c27e2742586.tar.bz2
openshift-f185fa6ce66a25b8f44c81aa963e0c27e2742586.tar.xz
openshift-f185fa6ce66a25b8f44c81aa963e0c27e2742586.zip
Merge pull request #3107 from kwoodson/generate_tox_test
Adding tox tests for generated module code.
-rwxr-xr-xroles/lib_openshift/src/generate.py30
-rw-r--r--roles/lib_utils/library/yedit.py6
-rwxr-xr-xroles/lib_utils/src/generate.py32
-rw-r--r--setup.py54
-rw-r--r--tox.ini3
5 files changed, 100 insertions, 25 deletions
diff --git a/roles/lib_openshift/src/generate.py b/roles/lib_openshift/src/generate.py
index 8451d99ab..6daade108 100755
--- a/roles/lib_openshift/src/generate.py
+++ b/roles/lib_openshift/src/generate.py
@@ -10,6 +10,7 @@ import six
OPENSHIFT_ANSIBLE_PATH = os.path.dirname(os.path.realpath(__file__))
OPENSHIFT_ANSIBLE_SOURCES_PATH = os.path.join(OPENSHIFT_ANSIBLE_PATH, 'sources.yml') # noqa: E501
+LIBRARY = os.path.join(OPENSHIFT_ANSIBLE_PATH, '..', 'library/')
class GenerateAnsibleException(Exception):
@@ -42,22 +43,29 @@ def generate(parts):
return data
-def main():
- ''' combine the necessary files to create the ansible module '''
- args = parse_args()
+def get_sources():
+ '''return the path to the generate sources'''
+ return yaml.load(open(OPENSHIFT_ANSIBLE_SOURCES_PATH).read())
- library = os.path.join(OPENSHIFT_ANSIBLE_PATH, '..', 'library/')
- sources = yaml.load(open(OPENSHIFT_ANSIBLE_SOURCES_PATH).read())
- for fname, parts in sources.items():
+def verify():
+ '''verify if the generated code matches the library code'''
+ for fname, parts in get_sources().items():
data = generate(parts)
- fname = os.path.join(library, fname)
- if args.verify:
- if not open(fname).read() == data.getvalue():
- raise GenerateAnsibleException('Generated content does not match for %s' % fname)
+ fname = os.path.join(LIBRARY, fname)
+ if not open(fname).read() == data.getvalue():
+ raise GenerateAnsibleException('Generated content does not match for %s' % fname)
+
- continue
+def main():
+ ''' combine the necessary files to create the ansible module '''
+ args = parse_args()
+ if args.verify:
+ verify()
+ for fname, parts in get_sources().items():
+ data = generate(parts)
+ fname = os.path.join(LIBRARY, fname)
with open(fname, 'w') as afd:
afd.seek(0)
afd.write(data.getvalue())
diff --git a/roles/lib_utils/library/yedit.py b/roles/lib_utils/library/yedit.py
index d882c983e..6a5b40dcc 100644
--- a/roles/lib_utils/library/yedit.py
+++ b/roles/lib_utils/library/yedit.py
@@ -134,6 +134,12 @@ options:
required: false
default: true
aliases: []
+ separator:
+ description:
+ - The separator being used when parsing strings.
+ required: false
+ default: '.'
+ aliases: []
author:
- "Kenny Woodson <kwoodson@redhat.com>"
extends_documentation_fragment: []
diff --git a/roles/lib_utils/src/generate.py b/roles/lib_utils/src/generate.py
index cece68fb4..6daade108 100755
--- a/roles/lib_utils/src/generate.py
+++ b/roles/lib_utils/src/generate.py
@@ -5,11 +5,12 @@
import argparse
import os
-import six
import yaml
+import six
OPENSHIFT_ANSIBLE_PATH = os.path.dirname(os.path.realpath(__file__))
OPENSHIFT_ANSIBLE_SOURCES_PATH = os.path.join(OPENSHIFT_ANSIBLE_PATH, 'sources.yml') # noqa: E501
+LIBRARY = os.path.join(OPENSHIFT_ANSIBLE_PATH, '..', 'library/')
class GenerateAnsibleException(Exception):
@@ -42,22 +43,29 @@ def generate(parts):
return data
-def main():
- ''' combine the necessary files to create the ansible module '''
- args = parse_args()
+def get_sources():
+ '''return the path to the generate sources'''
+ return yaml.load(open(OPENSHIFT_ANSIBLE_SOURCES_PATH).read())
- library = os.path.join(OPENSHIFT_ANSIBLE_PATH, '..', 'library/')
- sources = yaml.load(open(OPENSHIFT_ANSIBLE_SOURCES_PATH).read())
- for fname, parts in sources.items():
+def verify():
+ '''verify if the generated code matches the library code'''
+ for fname, parts in get_sources().items():
data = generate(parts)
- fname = os.path.join(library, fname)
- if args.verify:
- if not open(fname).read() == data.getvalue():
- raise GenerateAnsibleException('Generated content does not match for %s' % fname)
+ fname = os.path.join(LIBRARY, fname)
+ if not open(fname).read() == data.getvalue():
+ raise GenerateAnsibleException('Generated content does not match for %s' % fname)
- continue
+def main():
+ ''' combine the necessary files to create the ansible module '''
+ args = parse_args()
+ if args.verify:
+ verify()
+
+ for fname, parts in get_sources().items():
+ data = generate(parts)
+ fname = os.path.join(LIBRARY, fname)
with open(fname, 'w') as afd:
afd.seek(0)
afd.write(data.getvalue())
diff --git a/setup.py b/setup.py
index c826c167f..372aca9ff 100644
--- a/setup.py
+++ b/setup.py
@@ -6,7 +6,7 @@ from __future__ import print_function
import os
import fnmatch
import re
-
+import sys
import yaml
# Always prefer setuptools over distutils
@@ -146,6 +146,57 @@ class OpenShiftAnsiblePylint(PylintCommand):
return func(*func_args, **func_kwargs)
+class OpenShiftAnsibleGenerateValidation(Command):
+ ''' Command to run generated module validation'''
+ description = "Run generated module validation"
+ user_options = []
+
+ def initialize_options(self):
+ ''' initialize_options '''
+ pass
+
+ def finalize_options(self):
+ ''' finalize_options '''
+ pass
+
+ # self isn't used but I believe is required when it is called.
+ # pylint: disable=no-self-use
+ def run(self):
+ ''' run command '''
+ # find the files that call generate
+ generate_files = find_files('roles',
+ ['inventory',
+ 'test',
+ 'playbooks',
+ 'utils'],
+ None,
+ 'generate.py$')
+
+ if len(generate_files) < 1:
+ print('Did not find any code generation. Please verify module code generation.') # noqa: E501
+ raise SystemExit(1)
+
+ errors = False
+ for gen in generate_files:
+ print('Checking generated module code: {0}'.format(gen))
+ try:
+ sys.path.insert(0, os.path.dirname(gen))
+ # we are importing dynamically. This isn't in
+ # the python path.
+ # pylint: disable=import-error
+ import generate
+ generate.verify()
+ except generate.GenerateAnsibleException as gae:
+ print(gae.args)
+ errors = True
+
+ if errors:
+ print('Found errors while generating module code.')
+ raise SystemExit(1)
+
+ print('\nAll generate scripts passed.\n')
+
+
class UnsupportedCommand(Command):
''' Basic Command to override unsupported commands '''
user_options = []
@@ -188,6 +239,7 @@ setup(
'sdist': UnsupportedCommand,
'lint': OpenShiftAnsiblePylint,
'yamllint': OpenShiftAnsibleYamlLint,
+ 'generate_validation': OpenShiftAnsibleGenerateValidation,
},
packages=[],
)
diff --git a/tox.ini b/tox.ini
index 158974fbe..4d3594023 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,7 +1,7 @@
[tox]
minversion=2.3.1
envlist =
- py{27,35}-ansible22-{pylint,unit,flake8,yamllint}
+ py{27,35}-ansible22-{pylint,unit,flake8,yamllint,generate_validation}
skipsdist=True
skip_missing_interpreters=True
@@ -16,3 +16,4 @@ commands =
pylint: python setup.py lint
yamllint: python setup.py yamllint
unit: nosetests
+ generate_validation: python setup.py generate_validation