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
This commit is contained in:
Felipe Reyes 2013-10-04 12:28:24 -03:00 committed by Antoine Musso
parent 93cc27a6a3
commit 894c72efcf
8 changed files with 90 additions and 48 deletions

View File

@ -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)
)

View File

View File

@ -0,0 +1,10 @@
<?xml version="1.0" ?>
<project>
<actions/>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<assignedNode>my-node-name</assignedNode>
<canRoam>false</canRoam>
</project>

View File

@ -0,0 +1,3 @@
name: openstack-infra
project-type: freestyle
node: my-node-name

View File

@ -0,0 +1,9 @@
<?xml version="1.0" ?>
<project>
<actions/>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
</project>

View File

@ -0,0 +1,2 @@
name: openstack-infra
project-type: freestyle

View File

@ -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

View File

@ -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