Fix arg k, m to be required

Right now ECDriver doesn't check if k, m exist in the kwargs for init.
That can tigger unfortunately success to init and that will fail at
encode/decode (or result in odd with get_segment_info)

Once, we have jerasure_rs_vand in default, that worked becuase jerasure
did the assertion for the k, m parameters but we can reproduce the
failures which this patch want fix with like:

driver = ECDriver(ec_type='liberasurecode_rs_vand')   # <- this is
default and it succeeded!
driver.encode(' ')  # <- And then, this will result in ECDriverError:
Out of memory, whooa!

This patch fixes this to check the k, m existence in the init process.

Change-Id: I0757c0a4e510ba42f357db0cac22861919d0ca26
This commit is contained in:
Kota Tsuyuzaki 2016-07-11 14:47:03 -07:00
parent 5ebce22641
commit 25bc26b914
2 changed files with 34 additions and 0 deletions

View File

@ -114,6 +114,12 @@ class ECDriver(object):
self.ec_type = None
self.chksum_type = None
self.validate = False
for required in ('k', 'm'):
if required not in kwargs:
raise ECDriverError(
"Invalid Argument: %s is required" % required)
for (key, value) in kwargs.items():
if key == "k":
try:

View File

@ -109,6 +109,34 @@ class TestPyECLibDriver(unittest.TestCase):
def tearDown(self):
pass
def test_invalid_km_args(self):
for ec_type in VALID_EC_TYPES:
# missing k
with self.assertRaises(ECDriverError) as err_context:
ECDriver(ec_type=ec_type, m=1)
self.assertEqual(str(err_context.exception),
"Invalid Argument: k is required")
# missing m
with self.assertRaises(ECDriverError) as err_context:
ECDriver(ec_type=ec_type, k=1)
self.assertEqual(str(err_context.exception),
"Invalid Argument: m is required")
with self.assertRaises(ECDriverError) as err_context:
# m is smaller than 1
ECDriver(ec_type=ec_type, k=-100, m=1)
self.assertEqual(str(err_context.exception),
"Invalid number of data fragments (k)")
with self.assertRaises(ECDriverError) as err_context:
# m is smaller than 1
ECDriver(ec_type=ec_type, k=1, m=-100)
self.assertEqual(str(err_context.exception),
"Invalid number of data fragments (m)")
def test_valid_ec_types(self):
# Build list of available types and compare to VALID_EC_TYPES
available_ec_types = []