diff --git a/tests/base.py b/tests/base.py index 0acdde82a..59df77774 100644 --- a/tests/base.py +++ b/tests/base.py @@ -19,6 +19,11 @@ import os import re +import doctest +import testtools +import xml.etree.ElementTree as XML +import yaml +from jenkins_jobs.builder import XmlJob, YamlParser, ModuleRegistry def get_scenarios(fixtures_path): @@ -43,3 +48,47 @@ def get_scenarios(fixtures_path): })) return scenarios + + +class BaseTestCase(object): + scenarios = [] + fixtures_path = None + + # TestCase settings: + maxDiff = None # always dump text difference + longMessage = True # keep normal error message when providing our + + def __read_content(self): + # Read XML content, assuming it is unicode encoded + xml_filepath = os.path.join(self.fixtures_path, self.xml_filename) + xml_content = u"%s" % open(xml_filepath, 'r').read() + + yaml_filepath = os.path.join(self.fixtures_path, self.yaml_filename) + with file(yaml_filepath, 'r') as yaml_file: + yaml_content = yaml.load(yaml_file) + + return (yaml_content, xml_content) + + def test_yaml_snippet(self): + if not self.xml_filename or not self.yaml_filename: + return + + yaml_content, expected_xml = self.__read_content() + + xml_project = XML.Element('project') # root element + parser = YamlParser() + pub = self.klass(ModuleRegistry({})) + + # Generate the XML tree directly with modules/general + pub.gen_xml(parser, xml_project, yaml_content) + + # Prettify generated XML + pretty_xml = XmlJob(xml_project, 'fixturejob').output() + + self.assertThat( + pretty_xml, + testtools.matchers.DocTestMatches(expected_xml, + doctest.ELLIPSIS | + doctest.NORMALIZE_WHITESPACE | + doctest.REPORT_NDIFF) + ) diff --git a/tests/general/__init__.py b/tests/general/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/general/fixtures/assigned-node001.xml b/tests/general/fixtures/assigned-node001.xml new file mode 100644 index 000000000..37a87ba9b --- /dev/null +++ b/tests/general/fixtures/assigned-node001.xml @@ -0,0 +1,10 @@ + + + + false + false + false + false + my-node-name + false + diff --git a/tests/general/fixtures/assigned-node001.yaml b/tests/general/fixtures/assigned-node001.yaml new file mode 100644 index 000000000..afee1afe1 --- /dev/null +++ b/tests/general/fixtures/assigned-node001.yaml @@ -0,0 +1,3 @@ +name: openstack-infra +project-type: freestyle +node: my-node-name diff --git a/tests/general/fixtures/assigned-node002.xml b/tests/general/fixtures/assigned-node002.xml new file mode 100644 index 000000000..01aff283d --- /dev/null +++ b/tests/general/fixtures/assigned-node002.xml @@ -0,0 +1,9 @@ + + + + false + false + false + false + true + diff --git a/tests/general/fixtures/assigned-node002.yaml b/tests/general/fixtures/assigned-node002.yaml new file mode 100644 index 000000000..c9f142e27 --- /dev/null +++ b/tests/general/fixtures/assigned-node002.yaml @@ -0,0 +1,2 @@ +name: openstack-infra +project-type: freestyle diff --git a/tests/general/test_general.py b/tests/general/test_general.py new file mode 100644 index 000000000..6ff848458 --- /dev/null +++ b/tests/general/test_general.py @@ -0,0 +1,11 @@ +import os +from testtools import TestCase +from testscenarios.testcase import TestWithScenarios +from jenkins_jobs.modules import general +from tests.base import get_scenarios, BaseTestCase + + +class TestCaseModuleGeneral(TestWithScenarios, TestCase, BaseTestCase): + fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') + scenarios = get_scenarios(fixtures_path) + klass = general.General diff --git a/tests/publishers/test_publishers.py b/tests/publishers/test_publishers.py index ae601d743..67fdc2ba2 100644 --- a/tests/publishers/test_publishers.py +++ b/tests/publishers/test_publishers.py @@ -17,56 +17,14 @@ # License for the specific language governing permissions and limitations # under the License. -import doctest import os from testscenarios.testcase import TestWithScenarios -import testtools -import xml.etree.ElementTree as XML -import yaml - -from jenkins_jobs.builder import XmlJob, YamlParser, ModuleRegistry +from testtools import TestCase from jenkins_jobs.modules import publishers -from ..base import get_scenarios - -FIXTURES_PATH = os.path.join( - os.path.dirname(__file__), 'fixtures') +from tests.base import get_scenarios, BaseTestCase -class TestCaseModulePublisher(TestWithScenarios, testtools.TestCase): - scenarios = get_scenarios(FIXTURES_PATH) - - # TestCase settings: - maxDiff = None # always dump text difference - longMessage = True # keep normal error message when providing our - - def __read_content(self): - # Read XML content, assuming it is unicode encoded - xml_filepath = os.path.join(FIXTURES_PATH, self.xml_filename) - xml_content = u"%s" % open(xml_filepath, 'r').read() - - yaml_filepath = os.path.join(FIXTURES_PATH, self.yaml_filename) - with file(yaml_filepath, 'r') as yaml_file: - yaml_content = yaml.load(yaml_file) - - return (yaml_content, xml_content) - - def test_yaml_snippet(self): - yaml_content, expected_xml = self.__read_content() - - xml_project = XML.Element('project') # root element - parser = YamlParser() - pub = publishers.Publishers(ModuleRegistry({})) - - # Generate the XML tree directly with modules/publishers/* - pub.gen_xml(parser, xml_project, yaml_content) - - # Prettify generated XML - pretty_xml = XmlJob(xml_project, 'fixturejob').output() - - self.assertThat( - pretty_xml, - testtools.matchers.DocTestMatches(expected_xml, - doctest.ELLIPSIS | - doctest.NORMALIZE_WHITESPACE | - doctest.REPORT_NDIFF) - ) +class TestCaseModulePublisher(TestWithScenarios, TestCase, BaseTestCase): + fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') + scenarios = get_scenarios(fixtures_path) + klass = publishers.Publishers