From 25bc26b9149f8c28439238ecfe7d45e394e111e6 Mon Sep 17 00:00:00 2001 From: Kota Tsuyuzaki Date: Mon, 11 Jul 2016 14:47:03 -0700 Subject: [PATCH] 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 --- pyeclib/ec_iface.py | 6 ++++++ test/test_pyeclib_api.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/pyeclib/ec_iface.py b/pyeclib/ec_iface.py index 9310ae7..dd3a8b9 100644 --- a/pyeclib/ec_iface.py +++ b/pyeclib/ec_iface.py @@ -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: diff --git a/test/test_pyeclib_api.py b/test/test_pyeclib_api.py index cd304b7..03b05be 100644 --- a/test/test_pyeclib_api.py +++ b/test/test_pyeclib_api.py @@ -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 = []