Revision 919db916 test/py/ganeti.cli_unittest.py
b/test/py/ganeti.cli_unittest.py  

21  21  
22  22 
"""Script for unittesting the cli module""" 
23  23  
24 
import unittest 

24 
import copy 

25 
import testutils 

25  26 
import time 
27 
import unittest 

26  28 
from cStringIO import StringIO 
27  29  
28 
import ganeti 

29 
import testutils 

30  
31  30 
from ganeti import constants 
32  31 
from ganeti import cli 
33  32 
from ganeti import errors 
...  ...  
1181  1180  
1182  1181 
class TestCreateIPolicyFromOpts(unittest.TestCase): 
1183  1182 
"""Test case for cli.CreateIPolicyFromOpts.""" 
1183 
def setUp(self): 

1184 
# Policies are big, and we want to see the difference in case of an error 

1185 
self.maxDiff = None 

1186  
1184  1187 
def _RecursiveCheckMergedDicts(self, default_pol, diff_pol, merged_pol): 
1185  1188 
self.assertTrue(type(default_pol) is dict) 
1186  1189 
self.assertTrue(type(diff_pol) is dict) 
...  ...  
1197  1200 
self.assertEqual(val, default_pol[key]) 
1198  1201  
1199  1202 
def testClusterPolicy(self): 
1200 
exp_pol0 = { 

1201 
constants.ISPECS_MINMAX: { 

1202 
constants.ISPECS_MIN: {}, 

1203 
constants.ISPECS_MAX: {}, 

1204 
}, 

1205 
constants.ISPECS_STD: {}, 

1206 
} 

1203 
pol0 = cli.CreateIPolicyFromOpts( 

1204 
ispecs_mem_size={}, 

1205 
ispecs_cpu_count={}, 

1206 
ispecs_disk_count={}, 

1207 
ispecs_disk_size={}, 

1208 
ispecs_nic_count={}, 

1209 
ipolicy_disk_templates=None, 

1210 
ipolicy_vcpu_ratio=None, 

1211 
ipolicy_spindle_ratio=None, 

1212 
fill_all=True 

1213 
) 

1214 
self.assertEqual(pol0, constants.IPOLICY_DEFAULTS) 

1215  
1207  1216 
exp_pol1 = { 
1208  1217 
constants.ISPECS_MINMAX: { 
1209  1218 
constants.ISPECS_MIN: { 
...  ...  
1221  1230 
}, 
1222  1231 
constants.IPOLICY_VCPU_RATIO: 3.1, 
1223  1232 
} 
1233 
pol1 = cli.CreateIPolicyFromOpts( 

1234 
ispecs_mem_size={"max": "12g"}, 

1235 
ispecs_cpu_count={"min": 2, "std": 2}, 

1236 
ispecs_disk_count={"min": 1, "max": 2, "std": 2}, 

1237 
ispecs_disk_size={}, 

1238 
ispecs_nic_count={}, 

1239 
ipolicy_disk_templates=None, 

1240 
ipolicy_vcpu_ratio=3.1, 

1241 
ipolicy_spindle_ratio=None, 

1242 
fill_all=True 

1243 
) 

1244 
self._RecursiveCheckMergedDicts(constants.IPOLICY_DEFAULTS, 

1245 
exp_pol1, pol1) 

1246  
1224  1247 
exp_pol2 = { 
1225  1248 
constants.ISPECS_MINMAX: { 
1226  1249 
constants.ISPECS_MIN: { 
...  ...  
1238  1261 
constants.IPOLICY_SPINDLE_RATIO: 1.3, 
1239  1262 
constants.IPOLICY_DTS: ["templates"], 
1240  1263 
} 
1241 
for fillall in [False, True]: 

1242 
pol0 = cli.CreateIPolicyFromOpts( 

1243 
ispecs_mem_size={}, 

1244 
ispecs_cpu_count={}, 

1245 
ispecs_disk_count={}, 

1246 
ispecs_disk_size={}, 

1247 
ispecs_nic_count={}, 

1264 
pol2 = cli.CreateIPolicyFromOpts( 

1265 
ispecs_mem_size={}, 

1266 
ispecs_cpu_count={"std": 2}, 

1267 
ispecs_disk_count={}, 

1268 
ispecs_disk_size={"min": "0.5g"}, 

1269 
ispecs_nic_count={"min": 2, "max": 3, "std": 3}, 

1270 
ipolicy_disk_templates=["templates"], 

1271 
ipolicy_vcpu_ratio=None, 

1272 
ipolicy_spindle_ratio=1.3, 

1273 
fill_all=True 

1274 
) 

1275 
self._RecursiveCheckMergedDicts(constants.IPOLICY_DEFAULTS, 

1276 
exp_pol2, pol2) 

1277  
1278 
for fill_all in [False, True]: 

1279 
exp_pol3 = { 

1280 
constants.ISPECS_STD: { 

1281 
constants.ISPEC_CPU_COUNT: 2, 

1282 
constants.ISPEC_NIC_COUNT: 3, 

1283 
}, 

1284 
} 

1285 
pol3 = cli.CreateIPolicyFromOpts( 

1286 
std_ispecs={ 

1287 
constants.ISPEC_CPU_COUNT: "2", 

1288 
constants.ISPEC_NIC_COUNT: "3", 

1289 
}, 

1248  1290 
ipolicy_disk_templates=None, 
1249  1291 
ipolicy_vcpu_ratio=None, 
1250  1292 
ipolicy_spindle_ratio=None, 
1251 
fill_all=fillall 

1252 
) 

1253 
if fillall: 

1254 
self.assertEqual(pol0, constants.IPOLICY_DEFAULTS) 

1255 
else: 

1256 
self.assertEqual(pol0, exp_pol0) 

1257 
pol1 = cli.CreateIPolicyFromOpts( 

1258 
ispecs_mem_size={"max": "12g"}, 

1259 
ispecs_cpu_count={"min": 2, "std": 2}, 

1260 
ispecs_disk_count={"min": 1, "max": 2, "std": 2}, 

1261 
ispecs_disk_size={}, 

1262 
ispecs_nic_count={}, 

1263 
ipolicy_disk_templates=None, 

1264 
ipolicy_vcpu_ratio=3.1, 

1265 
ipolicy_spindle_ratio=None, 

1266 
fill_all=fillall 

1293 
fill_all=fill_all 

1267  1294 
) 
1268 
if fillall: 

1295 
if fill_all:


1269  1296 
self._RecursiveCheckMergedDicts(constants.IPOLICY_DEFAULTS, 
1270 
exp_pol1, pol1)


1297 
exp_pol3, pol3)


1271  1298 
else: 
1272 
self.assertEqual(pol1, exp_pol1) 

1273 
pol2 = cli.CreateIPolicyFromOpts( 

1274 
ispecs_mem_size={}, 

1275 
ispecs_cpu_count={"std": 2}, 

1276 
ispecs_disk_count={}, 

1277 
ispecs_disk_size={"min": "0.5g"}, 

1278 
ispecs_nic_count={"min": 2, "max": 3, "std": 3}, 

1279 
ipolicy_disk_templates=["templates"], 

1280 
ipolicy_vcpu_ratio=None, 

1281 
ipolicy_spindle_ratio=1.3, 

1282 
fill_all=fillall 

1283 
) 

1284 
if fillall: 

1285 
self._RecursiveCheckMergedDicts(constants.IPOLICY_DEFAULTS, 

1286 
exp_pol2, pol2) 

1299 
self.assertEqual(pol3, exp_pol3) 

1300  
1301 
def testPartialPolicy(self): 

1302 
exp_pol0 = objects.MakeEmptyIPolicy() 

1303 
pol0 = cli.CreateIPolicyFromOpts( 

1304 
minmax_ispecs=None, 

1305 
std_ispecs=None, 

1306 
ipolicy_disk_templates=None, 

1307 
ipolicy_vcpu_ratio=None, 

1308 
ipolicy_spindle_ratio=None, 

1309 
fill_all=False 

1310 
) 

1311 
self.assertEqual(pol0, exp_pol0) 

1312  
1313 
exp_pol1 = { 

1314 
constants.IPOLICY_VCPU_RATIO: 3.1, 

1315 
} 

1316 
pol1 = cli.CreateIPolicyFromOpts( 

1317 
minmax_ispecs=None, 

1318 
std_ispecs=None, 

1319 
ipolicy_disk_templates=None, 

1320 
ipolicy_vcpu_ratio=3.1, 

1321 
ipolicy_spindle_ratio=None, 

1322 
fill_all=False 

1323 
) 

1324 
self.assertEqual(pol1, exp_pol1) 

1325  
1326 
exp_pol2 = { 

1327 
constants.IPOLICY_SPINDLE_RATIO: 1.3, 

1328 
constants.IPOLICY_DTS: ["templates"], 

1329 
} 

1330 
pol2 = cli.CreateIPolicyFromOpts( 

1331 
minmax_ispecs=None, 

1332 
std_ispecs=None, 

1333 
ipolicy_disk_templates=["templates"], 

1334 
ipolicy_vcpu_ratio=None, 

1335 
ipolicy_spindle_ratio=1.3, 

1336 
fill_all=False 

1337 
) 

1338 
self.assertEqual(pol2, exp_pol2) 

1339  
1340 
def _TestInvalidISpecs(self, minmax_ispecs, std_ispecs, fail=True): 

1341 
for fill_all in [False, True]: 

1342 
if fail: 

1343 
self.assertRaises((errors.OpPrereqError, 

1344 
errors.UnitParseError, 

1345 
errors.TypeEnforcementError), 

1346 
cli.CreateIPolicyFromOpts, 

1347 
minmax_ispecs=minmax_ispecs, 

1348 
std_ispecs=std_ispecs, 

1349 
fill_all=fill_all) 

1287  1350 
else: 
1288 
self.assertEqual(pol2, exp_pol2) 

1351 
cli.CreateIPolicyFromOpts(minmax_ispecs=minmax_ispecs, 

1352 
std_ispecs=std_ispecs, 

1353 
fill_all=fill_all) 

1289  1354  
1290  1355 
def testInvalidPolicies(self): 
1291 
self.assertRaises(errors.TypeEnforcementError, cli.CreateIPolicyFromOpts, 

1292 
ispecs_mem_size={}, ispecs_cpu_count={}, 

1293 
ispecs_disk_count={}, ispecs_disk_size={"std": 1}, 

1294 
ispecs_nic_count={}, ipolicy_disk_templates=None, 

1295 
ipolicy_vcpu_ratio=None, ipolicy_spindle_ratio=None, 

1296 
group_ipolicy=True) 

1356 
self.assertRaises(AssertionError, cli.CreateIPolicyFromOpts, 

1357 
std_ispecs={constants.ISPEC_MEM_SIZE: 1024}, 

1358 
ipolicy_disk_templates=None, ipolicy_vcpu_ratio=None, 

1359 
ipolicy_spindle_ratio=None, group_ipolicy=True) 

1297  1360 
self.assertRaises(errors.OpPrereqError, cli.CreateIPolicyFromOpts, 
1298  1361 
ispecs_mem_size={"wrong": "x"}, ispecs_cpu_count={}, 
1299  1362 
ispecs_disk_count={}, ispecs_disk_size={}, 
1300  1363 
ispecs_nic_count={}, ipolicy_disk_templates=None, 
1301 
ipolicy_vcpu_ratio=None, ipolicy_spindle_ratio=None) 

1364 
ipolicy_vcpu_ratio=None, ipolicy_spindle_ratio=None, 

1365 
fill_all=True) 

1302  1366 
self.assertRaises(errors.TypeEnforcementError, cli.CreateIPolicyFromOpts, 
1303  1367 
ispecs_mem_size={}, ispecs_cpu_count={"min": "default"}, 
1304  1368 
ispecs_disk_count={}, ispecs_disk_size={}, 
1305  1369 
ispecs_nic_count={}, ipolicy_disk_templates=None, 
1306 
ipolicy_vcpu_ratio=None, ipolicy_spindle_ratio=None) 

1370 
ipolicy_vcpu_ratio=None, ipolicy_spindle_ratio=None, 

1371 
fill_all=True) 

1372  
1373 
good_mmspecs = constants.ISPECS_MINMAX_DEFAULTS 

1374 
self._TestInvalidISpecs(good_mmspecs, None, fail=False) 

1375 
broken_mmspecs = copy.deepcopy(good_mmspecs) 

1376 
for key in constants.ISPECS_MINMAX_KEYS: 

1377 
for par in constants.ISPECS_PARAMETERS: 

1378 
old = broken_mmspecs[key][par] 

1379 
del broken_mmspecs[key][par] 

1380 
self._TestInvalidISpecs(broken_mmspecs, None) 

1381 
broken_mmspecs[key][par] = "invalid" 

1382 
self._TestInvalidISpecs(broken_mmspecs, None) 

1383 
broken_mmspecs[key][par] = old 

1384 
broken_mmspecs[key]["invalid_key"] = None 

1385 
self._TestInvalidISpecs(broken_mmspecs, None) 

1386 
del broken_mmspecs[key]["invalid_key"] 

1387 
assert broken_mmspecs == good_mmspecs 

1388  
1389 
good_stdspecs = constants.IPOLICY_DEFAULTS[constants.ISPECS_STD] 

1390 
self._TestInvalidISpecs(None, good_stdspecs, fail=False) 

1391 
broken_stdspecs = copy.deepcopy(good_stdspecs) 

1392 
for par in constants.ISPECS_PARAMETERS: 

1393 
old = broken_stdspecs[par] 

1394 
broken_stdspecs[par] = "invalid" 

1395 
self._TestInvalidISpecs(None, broken_stdspecs) 

1396 
broken_stdspecs[par] = old 

1397 
broken_stdspecs["invalid_key"] = None 

1398 
self._TestInvalidISpecs(None, broken_stdspecs) 

1399 
del broken_stdspecs["invalid_key"] 

1400 
assert broken_stdspecs == good_stdspecs 

1307  1401  
1308  1402 
def testAllowedValues(self): 
1309  1403 
allowedv = "blah" 
1310  1404 
exp_pol1 = { 
1311 
constants.ISPECS_MINMAX: { 

1312 
constants.ISPECS_MIN: { 

1313 
constants.ISPEC_CPU_COUNT: allowedv, 

1314 
}, 

1315 
constants.ISPECS_MAX: { 

1316 
}, 

1317 
}, 

1318 
constants.ISPECS_STD: { 

1319 
}, 

1405 
constants.ISPECS_MINMAX: allowedv, 

1320  1406 
constants.IPOLICY_DTS: allowedv, 
1321  1407 
constants.IPOLICY_VCPU_RATIO: allowedv, 
1322  1408 
constants.IPOLICY_SPINDLE_RATIO: allowedv, 
1323  1409 
} 
1324 
pol1 = cli.CreateIPolicyFromOpts(ispecs_mem_size={}, 

1325 
ispecs_cpu_count={"min": allowedv}, 

1326 
ispecs_disk_count={}, 

1327 
ispecs_disk_size={}, 

1328 
ispecs_nic_count={}, 

1410 
pol1 = cli.CreateIPolicyFromOpts(minmax_ispecs={allowedv: {}}, 

1411 
std_ispecs=None, 

1329  1412 
ipolicy_disk_templates=allowedv, 
1330  1413 
ipolicy_vcpu_ratio=allowedv, 
1331  1414 
ipolicy_spindle_ratio=allowedv, 
...  ...  
1357  1440 
exp_ipol[constants.ISPECS_MINMAX] = exp_minmax 
1358  1441 
else: 
1359  1442 
minmax_ispecs = None 
1360 
if constants.ISPECS_MINMAX not in exp_ipol: 

1361 
empty_minmax = dict((k, {}) for k in constants.ISPECS_MINMAX_KEYS) 

1362 
exp_ipol[constants.ISPECS_MINMAX] = empty_minmax 

1363  1443 
if exp_std is not None: 
1364  1444 
std_ispecs = self._ConvertSpecToStrings(exp_std) 
1365  1445 
exp_ipol[constants.ISPECS_STD] = exp_std 
1366  1446 
else: 
1367  1447 
std_ispecs = None 
1368 
if constants.ISPECS_STD not in exp_ipol: 

1369 
exp_ipol[constants.ISPECS_STD] = {} 

1370  1448  
1371  1449 
self._CheckNewStyleSpecsCall(exp_ipol, minmax_ispecs, std_ispecs, 
1372  1450 
group_ipolicy, fill_all) 
Also available in: Unified diff