Revision 31155d60
b/lib/errors.py | ||
---|---|---|
228 | 228 |
""" |
229 | 229 |
|
230 | 230 |
|
231 |
class ParseError(GenericError): |
|
232 |
"""Generic parse error. |
|
233 |
|
|
234 |
Raised when unable to parse user input. |
|
235 |
|
|
236 |
""" |
|
237 |
|
|
238 |
|
|
231 | 239 |
class TypeEnforcementError(GenericError): |
232 | 240 |
"""Unable to enforce data type. |
233 | 241 |
|
b/lib/utils.py | ||
---|---|---|
1299 | 1299 |
return value |
1300 | 1300 |
|
1301 | 1301 |
|
1302 |
def ParseCpuMask(cpu_mask): |
|
1303 |
"""Parse a CPU mask definition and return the list of CPU IDs. |
|
1304 |
|
|
1305 |
CPU mask format: comma-separated list of CPU IDs |
|
1306 |
or dash-separated ID ranges |
|
1307 |
Example: "0-2,5" -> "0,1,2,5" |
|
1308 |
|
|
1309 |
@type cpu_mask: str |
|
1310 |
@param cpu_mask: CPU mask definition |
|
1311 |
@rtype: list of int |
|
1312 |
@return: list of CPU IDs |
|
1313 |
|
|
1314 |
""" |
|
1315 |
if not cpu_mask: |
|
1316 |
return [] |
|
1317 |
cpu_list = [] |
|
1318 |
for range_def in cpu_mask.split(","): |
|
1319 |
boundaries = range_def.split("-") |
|
1320 |
n_elements = len(boundaries) |
|
1321 |
if n_elements > 2: |
|
1322 |
raise errors.ParseError("Invalid CPU ID range definition" |
|
1323 |
" (only one hyphen allowed): %s" % range_def) |
|
1324 |
try: |
|
1325 |
lower = int(boundaries[0]) |
|
1326 |
except (ValueError, TypeError), err: |
|
1327 |
raise errors.ParseError("Invalid CPU ID value for lower boundary of" |
|
1328 |
" CPU ID range: %s" % str(err)) |
|
1329 |
try: |
|
1330 |
higher = int(boundaries[-1]) |
|
1331 |
except (ValueError, TypeError), err: |
|
1332 |
raise errors.ParseError("Invalid CPU ID value for higher boundary of" |
|
1333 |
" CPU ID range: %s" % str(err)) |
|
1334 |
if lower > higher: |
|
1335 |
raise errors.ParseError("Invalid CPU ID range definition" |
|
1336 |
" (%d > %d): %s" % (lower, higher, range_def)) |
|
1337 |
cpu_list.extend(range(lower, higher + 1)) |
|
1338 |
return cpu_list |
|
1339 |
|
|
1340 |
|
|
1302 | 1341 |
def AddAuthorizedKey(file_name, key): |
1303 | 1342 |
"""Adds an SSH public key to an authorized_keys file. |
1304 | 1343 |
|
b/test/ganeti.utils_unittest.py | ||
---|---|---|
996 | 996 |
self.assertRaises(errors.UnitParseError, ParseUnit, '1,3' + suffix) |
997 | 997 |
|
998 | 998 |
|
999 |
class TestParseCpuMask(unittest.TestCase): |
|
1000 |
"""Test case for the ParseCpuMask function.""" |
|
1001 |
|
|
1002 |
def testWellFormed(self): |
|
1003 |
self.assertEqual(utils.ParseCpuMask(""), []) |
|
1004 |
self.assertEqual(utils.ParseCpuMask("1"), [1]) |
|
1005 |
self.assertEqual(utils.ParseCpuMask("0-2,4,5-5"), [0,1,2,4,5]) |
|
1006 |
|
|
1007 |
def testInvalidInput(self): |
|
1008 |
self.assertRaises(errors.ParseError, |
|
1009 |
utils.ParseCpuMask, |
|
1010 |
"garbage") |
|
1011 |
self.assertRaises(errors.ParseError, |
|
1012 |
utils.ParseCpuMask, |
|
1013 |
"0,") |
|
1014 |
self.assertRaises(errors.ParseError, |
|
1015 |
utils.ParseCpuMask, |
|
1016 |
"0-1-2") |
|
1017 |
self.assertRaises(errors.ParseError, |
|
1018 |
utils.ParseCpuMask, |
|
1019 |
"2-1") |
|
1020 |
|
|
999 | 1021 |
class TestSshKeys(testutils.GanetiTestCase): |
1000 | 1022 |
"""Test case for the AddAuthorizedKey function""" |
1001 | 1023 |
|
Also available in: Unified diff