Compare commits

...

2 Commits

Author SHA1 Message Date
d3a71ae56f [test] Add unit tests for ACP and ALRU cleaning policy parameters
- Add tests for wake-up and flush-max-buffers parameter validation
- Test parameter boundary values and error conditions
- Cover parameter parsing from configuration strings
- Verify set_param_cleaning_policy commands construction
- Test proper handling of different cleaning policies in configure_cache

These tests ensure the proper validation and handling of cleaning policy
parameters introduced in the previous commits.
2025-04-16 11:17:32 +08:00
09764e0e15 [fix] Enhance cache cleaning policy configuration
- Add set_param_cleaning_policy method for ACP and ALRU policies
- Add validation for wake-up and flush-max-buffers parameters
- Improve cache configuration to handle different cleaning policies
- Fix casctl stop with flush option for proper shutdown
2025-04-16 11:05:32 +08:00
3 changed files with 176 additions and 5 deletions

View File

@ -476,3 +476,131 @@ def test_cas_config_from_file_insert_cache_insert_core_to_file(
assert set(contents_hashed[cores_index + 1 :]) - set(cores_hashed) == set(
["51/dev/mango_core"]
)
@pytest.mark.parametrize(
"cleaning_policy,wake_up,flush_max_buffers,expected_exception",
[
("acp", "100", "500", None), # Valid normal values
("acp", "0", "1", None), # Boundary value test
("acp", "9999", "9999", None), # Boundary value test
("acp", "-1", "500", ValueError), # Invalid wake_up value
("acp", "10000", "500", ValueError), # Out of range wake_up value
("acp", "abc", "500", ValueError), # Non-numeric wake_up value
("acp", "100", "0", ValueError), # Invalid flush_max_buffers value
("acp", "100", "10000", ValueError), # Out of range flush_max_buffers value
("acp", "100", "abc", ValueError), # Non-numeric flush_max_buffers value
("alru", "100", "500", None), # Valid normal values
("alru", "0", "1", None), # Boundary value test
("alru", "3599", "9999", None), # Boundary value test
("alru", "-1", "500", ValueError), # Invalid wake_up value
("alru", "3600", "500", ValueError), # Out of range wake_up value
("alru", "abc", "500", ValueError), # Non-numeric wake_up value
("alru", "100", "0", ValueError), # Invalid flush_max_buffers value
("alru", "100", "10000", ValueError), # Out of range flush_max_buffers value
("alru", "100", "abc", ValueError), # Non-numeric flush_max_buffers value
("nop", "100", "500", ValueError), # Testing parameters for unsupported cleaning policy
],
)
def test_cache_config_cleaning_policy_parameters(cleaning_policy, wake_up, flush_max_buffers, expected_exception):
"""Test validation of wake_up and flush_max_buffers parameters for different cleaning policies"""
cache = opencas.cas_config.cache_config(1, "/dev/dummy", "WT", cleaning_policy=cleaning_policy)
# Add test parameters
cache.params["wake-up"] = wake_up
cache.params["flush-max-buffers"] = flush_max_buffers
if expected_exception:
with pytest.raises(expected_exception):
cache.check_wake_up_valid(wake_up)
cache.check_flush_max_buffers_valid(flush_max_buffers)
else:
# No exception should be raised
cache.check_wake_up_valid(wake_up)
cache.check_flush_max_buffers_valid(flush_max_buffers)
@pytest.mark.parametrize(
"cache_config_str, exception",
[
("1 /dev/dummy WT cleaning_policy=acp,wake-up=100,flush-max-buffers=500", None),
("1 /dev/dummy WT cleaning_policy=alru,wake-up=60,flush-max-buffers=100", None),
("1 /dev/dummy WT cleaning_policy=acp,wake-up=10000,flush-max-buffers=500", ValueError),
("1 /dev/dummy WT cleaning_policy=acp,wake-up=100,flush-max-buffers=0", ValueError),
("1 /dev/dummy WT cleaning_policy=alru,wake-up=3600,flush-max-buffers=100", ValueError),
("1 /dev/dummy WT cleaning_policy=alru,wake-up=100,flush-max-buffers=10000", ValueError),
("1 /dev/dummy WT cleaning_policy=nop,wake-up=100,flush-max-buffers=500", ValueError),
],
)
def test_cache_config_from_line_with_cleaning_parameters(cache_config_str, exception):
"""Test parsing cache configuration with cleaning policy parameters from config line"""
if exception:
with pytest.raises(exception):
cache = opencas.cas_config.cache_config.from_line(cache_config_str)
else:
cache = opencas.cas_config.cache_config.from_line(cache_config_str)
assert cache.params.get("cleaning_policy") in ["acp", "alru"]
assert "wake-up" in cache.params
assert "flush-max-buffers" in cache.params
@patch("opencas.casadm.run_cmd")
def test_set_param_cleaning_policy(mock_run_cmd):
"""Test the set_param_cleaning_policy method call"""
# ACP policy
opencas.casadm.set_param_cleaning_policy("acp", 1, wake_up="100", flush_max_buffers="500")
mock_run_cmd.assert_called_with([
'/sbin/casadm',
'--set-param', '--name', 'cleaning-acp',
'--cache-id', '1',
'--wake-up', '100',
'--flush-max-buffers', '500'
])
# ALRU policy
mock_run_cmd.reset_mock()
opencas.casadm.set_param_cleaning_policy("alru", 2, wake_up="60", flush_max_buffers="200")
mock_run_cmd.assert_called_with([
'/sbin/casadm',
'--set-param', '--name', 'cleaning-alru',
'--cache-id', '2',
'--wake-up', '60',
'--flush-max-buffers', '200'
])
@patch("opencas.casadm.set_param_cleaning_policy")
@patch("opencas.casadm.set_param")
def test_configure_cache_cleaning_policy(mock_set_param, mock_set_param_cleaning_policy):
"""Test handling different cleaning policies in cache configuration"""
# ACP policy test
cache_acp = opencas.cas_config.cache_config(1, "/dev/dummy", "WT",
cleaning_policy="acp",
wake_up="100",
flush_max_buffers="500")
opencas.configure_cache(cache_acp)
mock_set_param_cleaning_policy.assert_called_once_with(
policy="acp", cache_id=1, wake_up="100", flush_max_buffers="500"
)
mock_set_param.assert_not_called()
# ALRU policy test
mock_set_param.reset_mock()
mock_set_param_cleaning_policy.reset_mock()
cache_alru = opencas.cas_config.cache_config(2, "/dev/dummy", "WT",
cleaning_policy="alru",
wake_up="60",
flush_max_buffers="200")
opencas.configure_cache(cache_alru)
mock_set_param_cleaning_policy.assert_called_once_with(
policy="alru", cache_id=2, wake_up="60", flush_max_buffers="200"
)
mock_set_param.assert_not_called()
# NOP policy test
mock_set_param.reset_mock()
mock_set_param_cleaning_policy.reset_mock()
cache_nop = opencas.cas_config.cache_config(3, "/dev/dummy", "WT", cleaning_policy="nop")
opencas.configure_cache(cache_nop)
mock_set_param.assert_called_once_with("cleaning", cache_id=3, policy="nop")
mock_set_param_cleaning_policy.assert_not_called()

View File

@ -7,4 +7,4 @@
# systemd-shutdown plugin to stop all remaining Open CAS devices
/usr/bin/echo "Open CAS cleanup handler" > /dev/kmsg
/sbin/casctl stop
/sbin/casctl stop --flush

View File

@ -139,7 +139,16 @@ class casadm:
cmd += ['--'+param.replace('_', '-'), str(value)]
return cls.run_cmd(cmd)
@classmethod
def set_param_cleaning_policy(cls, policy, cache_id, **kwargs):
cmd = [cls.casadm_path,
'--set-param', '--name', 'cleaning-'+policy,
'--cache-id', str(cache_id)]
for param, value in kwargs.items():
cmd += ['--'+param.replace('_', '-'), str(value)]
return cls.run_cmd(cmd)
@classmethod
def get_params(cls, namespace, cache_id, **kwargs):
cmd = [cls.casadm_path,
@ -269,6 +278,10 @@ class cas_config(object):
self.check_lazy_startup_valid(param_value)
elif param_name == "target_failover_state":
self.check_failover_state_valid(param_value)
elif param_name == "wake-up":
self.check_wake_up_valid(param_value)
elif param_name == "flush-max-buffers":
self.check_flush_max_buffers_valid(param_value)
else:
raise ValueError(f'{param_name} is invalid parameter name')
@ -315,6 +328,29 @@ class cas_config(object):
if cache_line_size not in ['4', '8', '16', '32', '64']:
raise ValueError(f'{cache_line_size} is invalid cache line size')
def check_wake_up_valid(self, wake_up):
if self.params.get("cleaning_policy") == "acp":
if not wake_up.isdigit():
raise ValueError(f"{wake_up} is invalid wake-up value for acp cleaning policy")
if int(wake_up) not in range(0, 10000):
raise ValueError(f"{wake_up} is invalid wake-up value for acp cleaning policy")
elif self.params.get("cleaning_policy") == "alru":
if not wake_up.isdigit():
raise ValueError(f"{wake_up} is invalid wake-up value for alru cleaning policy")
if int(wake_up) not in range(0, 3600):
raise ValueError(f"{wake_up} is invalid wake-up value for alru cleaning policy")
def check_flush_max_buffers_valid(self, flush_max_buffers):
if self.params.get("cleaning_policy") == "acp":
if not flush_max_buffers.isdigit():
raise ValueError(f"{flush_max_buffers} is invalid flush-max-buffers value for acp cleaning policy")
if int(flush_max_buffers) not in range(1, 10000):
raise ValueError(f"{flush_max_buffers} is invalid flush-max-buffers value for acp cleaning policy")
elif self.params.get("cleaning_policy") == "alru":
if not flush_max_buffers.isdigit():
raise ValueError(f"{flush_max_buffers} is invalid flush-max-buffers value for alru cleaning policy")
if int(flush_max_buffers) not in range(1, 10000):
raise ValueError(f"{flush_max_buffers} is invalid flush-max-buffers value for alru cleaning policy")
def check_recursive(self):
if not self.device.startswith('/dev/cas'):
return
@ -590,9 +626,16 @@ def start_cache(cache, load, force=False):
def configure_cache(cache):
if "cleaning_policy" in cache.params:
casadm.set_param(
"cleaning", cache_id=cache.cache_id, policy=cache.params["cleaning_policy"]
)
if cache.params["cleaning_policy"] == "acp" or cache.params["cleaning_policy"] == "alru":
casadm.set_param_cleaning_policy(
policy=cache.params["cleaning_policy"], cache_id=cache.cache_id, wake_up=cache.params["wake_up"], flush_max_buffers=cache.params["flush_max_buffers"]
)
else:
casadm.set_param(
"cleaning", cache_id=cache.cache_id, policy=cache.params["cleaning_policy"]
)
if "promotion_policy" in cache.params:
casadm.set_param(
"promotion", cache_id=cache.cache_id, policy=cache.params["promotion_policy"]