Commit 9bec0ce8 authored by cpomies's avatar cpomies
Browse files

Add unit test on the CheckReadySlots script with 100% of code coverage

parent eb2ac124
......@@ -41,18 +41,22 @@ class Script(LbUtils.Script.PlainScript):
for file_name in files:
with open(file_name) as data_file:
data = json.load(data_file)
#check if slot is not disable
if (not 'disabled' in data) or data['disabled'] == False:
#extract attribute slot if exist
if 'slot' in data:
slot_name = data['slot']
# if not extract slot name from filename
else:
slot_name = splitext(basename(file_name))[0]
slots.add(slot_name)
self.log.debug('Add %s to the slot list from %s', slot_name, file_name)
try:
with open(file_name) as data_file:
data = json.load(data_file)
#check if slot is not disable
if (not 'disabled' in data) or data['disabled'] == False:
#extract attribute slot if exist
if 'slot' in data:
slot_name = data['slot']
# if not extract slot name from filename
else:
slot_name = splitext(basename(file_name))[0]
slots.add(slot_name)
self.log.debug('Add %s to the slot list from %s', slot_name, file_name)
except:
self.log.warning('Can''t find or open %s', file_name)
self.log.info('%s slots from %s', len(slots), file_format_json)
......
......@@ -17,9 +17,9 @@ from LbNightlyTools import CheckReadySlots
import os
import re
from sets import Set
import json
from os.path import normpath, join
from os.path import normpath, join, exists
from LbNightlyTools.tests.utils import TemporaryDir
_testdata = normpath(join(*([__file__] + [os.pardir] * 4 + ['testdata'])))
......@@ -35,24 +35,289 @@ def teardown():
os.environ.clear()
os.environ.update(_env_bk)
def test_wrong_number_argument():
with TemporaryDir(chdir=True):
try:
CheckReadySlots.Script().run(['slot-param-{0}.txt','other_param'])
assert False, 'Script should have exited'
except SystemExit, x:
assert x.code != 0
assert len([x for x in os.listdir('.') if re.match(r'^slot-param-.*\.txt', x)]) == 0
def test_no_data():
with TemporaryDir(chdir=True):
retval = CheckReadySlots.Script().run(['slot-param-{0}.txt'])
assert retval == 0
assert len([x for x in os.listdir('.') if re.match(r'^slot-param-.*\.txt', x)]) == 0
def test_no_file():
with TemporaryDir(chdir=True):
os.makedirs('./configs')
retval = CheckReadySlots.Script().run(['slot-param-{0}.txt'])
assert retval == 0
assert len([x for x in os.listdir('.') if re.match(r'^slot-param-.*\.txt', x)]) == 0
def test_one_file_json_chmod_111():
conf_data = {'slot': 'lhcb-TEST',
'description': 'Test for unit test',
'disabled': False,
'projects': [],
'default_platforms': ['x86_64-slc6-gcc48-opt', 'x86_64-slc6-gcc46-opt'],
'USE_CMT': True}
with TemporaryDir(chdir=True):
os.makedirs('./configs')
with open('configs/lhcb-TEST.json', 'w') as slot_file:
slot_file.write(json.dumps(conf_data))
os.chmod('configs/lhcb-TEST.json', 0111)
slots = CheckReadySlots.Script().extractFromJson('lhcb-*.json')
CheckReadySlots.Script().writeFiles(slots, 'slot-param-{0}.txt')
retval = CheckReadySlots.Script().run(['slot-param-{0}.txt'])
assert retval == 0
assert exists(join('configs/lhcb-TEST.json'))
assert len([x for x in os.listdir('.') if re.match(r'^slot-param-.*\.txt', x)]) == 0
def test_one_file_json_disabled_flase():
conf_data = {'slot': 'lhcb-TEST',
'description': 'Test for unit test',
'disabled': False,
'projects': [],
'default_platforms': ['x86_64-slc6-gcc48-opt', 'x86_64-slc6-gcc46-opt'],
'USE_CMT': True}
with TemporaryDir(chdir=True):
os.makedirs('./configs')
with open('configs/lhcb-TEST.json', 'w') as slot_file:
slot_file.write(json.dumps(conf_data))
retval = CheckReadySlots.Script().run(['slot-param-{0}.txt'])
assert retval == 0
assert exists(join('configs/lhcb-TEST.json'))
assert json.load(open('configs/lhcb-TEST.json')) == conf_data
assert len([x for x in os.listdir('.') if re.match(r'^slot-param-.*\.txt', x)]) == 1
def test_one_file_json_disabled_true():
conf_data = {'slot': 'lhcb-TEST',
'description': 'Test for unit test',
'disabled': True,
'projects': [],
'default_platforms': ['x86_64-slc6-gcc48-opt', 'x86_64-slc6-gcc46-opt'],
'USE_CMT': True}
with TemporaryDir(chdir=True):
os.makedirs('./configs')
with open('configs/lhcb-TEST.json', 'w') as slot_file:
slot_file.write(json.dumps(conf_data))
retval = CheckReadySlots.Script().run(['slot-param-{0}.txt'])
assert retval == 0
assert exists(join('configs/lhcb-TEST.json'))
assert json.load(open('configs/lhcb-TEST.json')) == conf_data
assert len([x for x in os.listdir('.') if re.match(r'^slot-param-.*\.txt', x)]) == 0
def test_no_file_xml():
def test_one_file_json_no_disabled():
conf_data = {'slot': 'lhcb-TEST',
'description': 'Test for unit test',
'projects': [],
'default_platforms': ['x86_64-slc6-gcc48-opt', 'x86_64-slc6-gcc46-opt'],
'USE_CMT': True}
with TemporaryDir(chdir=True):
os.makedirs('./configs')
with open('configs/lhcb-TEST.json', 'w') as slot_file:
slot_file.write(json.dumps(conf_data))
retval = CheckReadySlots.Script().run(['slot-param-{0}.txt'])
assert retval == 0
assert exists(join('configs/lhcb-TEST.json'))
assert json.load(open('configs/lhcb-TEST.json')) == conf_data
assert len([x for x in os.listdir('.') if re.match(r'^slot-param-.*\.txt', x)]) == 1
def test_one_file_json_no_slot():
conf_data = {'description': 'Test for unit test',
'disabled': False,
'projects': [],
'default_platforms': ['x86_64-slc6-gcc48-opt', 'x86_64-slc6-gcc46-opt'],
'USE_CMT': True}
with TemporaryDir(chdir=True):
os.makedirs('./configs')
with open('configs/lhcb-TEST.json', 'w') as slot_file:
slot_file.write(json.dumps(conf_data))
retval = CheckReadySlots.Script().run(['slot-param-{0}.txt'])
assert retval == 0
assert exists(join('configs/lhcb-TEST.json'))
assert json.load(open('configs/lhcb-TEST.json')) == conf_data
assert len([x for x in os.listdir('.') if re.match(r'^slot-param-.*\.txt', x)]) == 1
def test_two_file_json():
conf_data1 = {'description': 'Test for unit test',
'disabled': False,
'projects': [],
'default_platforms': ['x86_64-slc6-gcc48-opt', 'x86_64-slc6-gcc46-opt'],
'USE_CMT': True}
conf_data2 = {'slot': 'lhcb-TEST2',
'description': 'Test for unit test',
'disabled': False,
'projects': [],
'default_platforms': ['x86_64-slc6-gcc48-opt', 'x86_64-slc6-gcc46-opt'],
'USE_CMT': True}
with TemporaryDir(chdir=True):
slots = CheckReadySlots.Script().extractFromXml('configuration.xml')
assert len(slots) == 0
os.makedirs('./configs')
with open('configs/lhcb-TEST1.json', 'w') as slot_file:
slot_file.write(json.dumps(conf_data1))
with open('configs/lhcb-TEST2.json', 'w') as slot_file:
slot_file.write(json.dumps(conf_data2))
retval = CheckReadySlots.Script().run(['slot-param-{0}.txt'])
assert retval == 0
assert exists(join('configs/lhcb-TEST1.json'))
assert exists(join('configs/lhcb-TEST2.json'))
assert json.load(open('configs/lhcb-TEST1.json')) == conf_data1
assert json.load(open('configs/lhcb-TEST2.json')) == conf_data2
assert len([x for x in os.listdir('.') if re.match(r'^slot-param-.*\.txt', x)]) == 2
def test_no_file_json():
def test_one_job_xml_disbaled_false():
test_xml = u'''
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="configuration.xsd">
<slot disabled="false" hidden="false" name="lhcb-TEST" renice="+2" mails="true" description="lhcb-TEST use for unit TEST">
</slot>
</configuration>
'''
with TemporaryDir(chdir=True):
slots = CheckReadySlots.Script().extractFromXml('lhcb-*.json')
assert len(slots) == 0
os.makedirs('./configs')
with open('configs/configuration.xml', 'w') as cfg_file:
cfg_file.write(test_xml)
retval = CheckReadySlots.Script().run(['slot-param-{0}.txt'])
assert retval == 0
assert exists(join('configs/configuration.xml'))
assert len([x for x in os.listdir('.') if re.match(r'^slot-param-.*\.txt', x)]) == 1
def test_one_job_xml_disabled_true():
def test_no_slot_to_write():
test_xml = u'''
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="configuration.xsd">
<slot disabled="true" hidden="false" name="lhcb-TEST" renice="+2" mails="true" description="lhcb-TEST use for unit TEST">
</slot>
</configuration>
'''
with TemporaryDir(chdir=True):
CheckReadySlots.Script().writeFiles(Set(), 'slot-param-{0}.txt')
os.makedirs('./configs')
with open('configs/configuration.xml', 'w') as cfg_file:
cfg_file.write(test_xml)
retval = CheckReadySlots.Script().run(['slot-param-{0}.txt'])
assert retval == 0
assert exists(join('configs/configuration.xml'))
assert len([x for x in os.listdir('.') if re.match(r'^slot-param-.*\.txt', x)]) == 0
def test_one_job_xml_no_disabled():
test_xml = u'''
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="configuration.xsd">
<slot hidden="false" name="lhcb-TEST" renice="+2" mails="true" description="lhcb-TEST use for unit TEST">
</slot>
</configuration>
'''
with TemporaryDir(chdir=True):
os.makedirs('./configs')
with open('configs/configuration.xml', 'w') as cfg_file:
cfg_file.write(test_xml)
retval = CheckReadySlots.Script().run(['slot-param-{0}.txt'])
assert retval == 0
assert exists(join('configs/configuration.xml'))
assert len([x for x in os.listdir('.') if re.match(r'^slot-param-.*\.txt', x)]) == 1
def test_two_job_xml():
test_xml = u'''
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="configuration.xsd">
<slot disabled="false" hidden="false" name="lhcb-TEST1" renice="+2" mails="true" description="lhcb-TEST1 use for unit TEST">
</slot>
<slot disabled="false" hidden="false" name="lhcb-TEST2" renice="+2" mails="true" description="lhcb-TEST2 use for unit TEST">
</slot>
</configuration>
'''
with TemporaryDir(chdir=True):
os.makedirs('./configs')
with open('configs/configuration.xml', 'w') as cfg_file:
cfg_file.write(test_xml)
retval = CheckReadySlots.Script().run(['slot-param-{0}.txt'])
assert retval == 0
assert exists(join('configs/configuration.xml'))
assert len([x for x in os.listdir('.') if re.match(r'^slot-param-.*\.txt', x)]) == 2
def test_same_job_xml_and_json():
test_xml = u'''
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="configuration.xsd">
<slot disabled="false" hidden="false" name="lhcb-TEST" renice="+2" mails="true" description="lhcb-TEST use for unit TEST">
</slot>
</configuration>
'''
conf_data = {'slot': 'lhcb-TEST',
'description': 'Test for unit test',
'disabled': False,
'projects': [],
'default_platforms': ['x86_64-slc6-gcc48-opt', 'x86_64-slc6-gcc46-opt'],
'USE_CMT': True}
with TemporaryDir(chdir=True):
os.makedirs('./configs')
with open('configs/configuration.xml', 'w') as cfg_file:
cfg_file.write(test_xml)
with open('configs/lhcb-TEST.json', 'w') as slot_file:
slot_file.write(json.dumps(conf_data))
retval = CheckReadySlots.Script().run(['slot-param-{0}.txt'])
assert retval == 0
assert exists(join('configs/configuration.xml'))
assert exists(join('configs/lhcb-TEST.json'))
assert len([x for x in os.listdir('.') if re.match(r'^slot-param-.*\.txt', x)]) == 1
def test_different_job_xml_and_json():
test_xml = u'''
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="configuration.xsd">
<slot disabled="false" hidden="false" name="lhcb-TEST1" renice="+2" mails="true" description="lhcb-TEST1 use for unit TEST">
</slot>
</configuration>
'''
conf_data = {'slot': 'lhcb-TEST2',
'description': 'Test for unit test',
'disabled': False,
'projects': [],
'default_platforms': ['x86_64-slc6-gcc48-opt', 'x86_64-slc6-gcc46-opt'],
'USE_CMT': True}
with TemporaryDir(chdir=True):
os.makedirs('./configs')
with open('configs/configuration.xml', 'w') as cfg_file:
cfg_file.write(test_xml)
with open('configs/lhcb-TEST.json', 'w') as slot_file:
slot_file.write(json.dumps(conf_data))
retval = CheckReadySlots.Script().run(['slot-param-{0}.txt'])
assert retval == 0
assert exists(join('configs/configuration.xml'))
assert exists(join('configs/lhcb-TEST.json'))
assert len([x for x in os.listdir('.') if re.match(r'^slot-param-.*\.txt', x)]) == 2
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment