def test_create_volume_dfn(self): rsc_dfn = self.execute_with_single_resp( ['resource-definition', 'create', 'rscvlm']) self.assert_api_succuess(rsc_dfn) vlm_dfn = self.execute_with_single_resp( ['volume-definition', 'create', 'rscvlm', '128MiB']) self.assert_api_succuess(vlm_dfn) self.assert_volume_def( 'rscvlm', 0, None, SizeCalc.convert_round_up(128, SizeCalc.UNIT_MiB, SizeCalc.UNIT_KiB)) vlm_dfn = self.execute_with_single_resp( ['volume-definition', 'create', 'rscvlm', '0']) self.assertTrue(vlm_dfn.is_error()) self.assertEqual( self.signed_mask(MASK_VLM_DFN | MASK_CRT | FAIL_INVLD_VLM_SIZE), vlm_dfn.ret_code) vlm_dfn = self.execute_with_single_resp([ 'volume-definition', 'create', 'rscvlm', '--vlmnr', '3', '256Mib' ]) self.assert_api_succuess(vlm_dfn) self.assertEqual(MASK_VLM_DFN | MASK_CRT | CREATED, vlm_dfn.ret_code) self.assert_volume_def( 'rscvlm', 3, None, SizeCalc.convert_round_up(256, SizeCalc.UNIT_MiB, SizeCalc.UNIT_KiB)) with self.assertRaises(SystemExit): self.execute_with_single_resp( ['volume-definition', 'create', 'rscvlm', '1Gi'])
def parse_size_str(cls, size_str, default_unit="GiB"): if size_str is None: return None m = re.match(r'(\d+)(\D*)', size_str) size = 0 try: size = int(m.group(1)) except AttributeError: sys.stderr.write('Size is not a valid number\n') sys.exit(ExitCode.ARGPARSE_ERROR) unit_str = m.group(2) if unit_str == "": unit_str = default_unit try: _, unit = SizeCalc.UNITS_MAP[unit_str.lower()] except KeyError: sys.stderr.write('"%s" is not a valid unit!\n' % unit_str) sys.stderr.write('Valid units: %s\n' % SizeCalc.UNITS_LIST_STR) sys.exit(ExitCode.ARGPARSE_ERROR) _, unit = SizeCalc.UNITS_MAP[unit_str.lower()] if unit != SizeCalc.UNIT_KiB: size = SizeCalc.convert_round_up(size, unit, SizeCalc.UNIT_KiB) return size