From 894c72efcf07a6aae447b9502fc03e7957ff0530 Mon Sep 17 00:00:00 2001 From: Felipe Reyes Date: Fri, 4 Oct 2013 12:28:24 -0300 Subject: [PATCH] New base class to declare test cases and tests to the node option This patch refactors the tests suite to use a base class to declare test cases just using a class declaration, the existing test class was modified to use it and new tests are using it as well. These tests verify the XML generated when the 'node' option is set and when it's absent. Change-Id: Iddcdd63e0ab7c459f6513b1d1ebf19e19a362c1d --- tests/base.py | 49 ++++++++++++++++++ tests/general/__init__.py | 0 tests/general/fixtures/assigned-node001.xml | 10 ++++ tests/general/fixtures/assigned-node001.yaml | 3 ++ tests/general/fixtures/assigned-node002.xml | 9 ++++ tests/general/fixtures/assigned-node002.yaml | 2 + tests/general/test_general.py | 11 ++++ tests/publishers/test_publishers.py | 54 +++----------------- 8 files changed, 90 insertions(+), 48 deletions(-) create mode 100644 tests/general/__init__.py create mode 100644 tests/general/fixtures/assigned-node001.xml create mode 100644 tests/general/fixtures/assigned-node001.yaml create mode 100644 tests/general/fixtures/assigned-node002.xml create mode 100644 tests/general/fixtures/assigned-node002.yaml create mode 100644 tests/general/test_general.py 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