def validate(data, power_admission_control=False): """ Validate configuration Parameters data: configuration (dict) """ # validates config schema schema, resolver = ConfigStore.load_json_schema('appqos.json') jsonschema.validate(data, schema, resolver=resolver) ConfigStore._validate_pools(data) ConfigStore._validate_apps(data) power.validate_power_profiles(data, power_admission_control)
def test_validate_power_profiles(self): data = { "power_profiles": [{ "id": 0, "min_freq": 1500, "max_freq": 2500, "epp": "performance", "name": "default" }, { "id": 0, "min_freq": 1000, "max_freq": 1000, "epp": "power", "name": "low_priority" }] } with mock.patch('power._is_max_freq_valid', return_value = True) as mock_is_max,\ mock.patch('power._is_min_freq_valid', return_value = True) as mock_is_min,\ mock.patch('power._is_epp_valid', return_value = True) as mock_is_epp,\ mock.patch('power._do_admission_control_check', return_value = True),\ mock.patch('power._admission_control_check', return_value = True) as mock_admission_control_check: with pytest.raises( ValueError, match="Power Profile 0, multiple profiles with same id."): power.validate_power_profiles(data, True) mock_admission_control_check.assert_not_called() # fix profile ID issue data['power_profiles'][-1]['id'] = 1 with mock.patch('power._is_max_freq_valid', return_value = True) as mock_is_max,\ mock.patch('power._is_min_freq_valid', return_value = True) as mock_is_min,\ mock.patch('power._is_epp_valid', return_value = True) as mock_is_epp,\ mock.patch('power._do_admission_control_check', return_value = True),\ mock.patch('power._admission_control_check', return_value = True) as mock__admission_control_check: power.validate_power_profiles(data, True) mock_is_max.assert_any_call(2500) mock_is_max.assert_any_call(1000) mock_is_min.assert_any_call(1500) mock_is_min.assert_any_call(1000) mock_is_epp.assert_any_call("performance") mock_is_epp.assert_any_call("power") mock__admission_control_check.assert_called_once() with mock.patch('power._is_max_freq_valid', return_value = True) as mock_is_max,\ mock.patch('power._is_min_freq_valid', return_value = True) as mock_is_min,\ mock.patch('power._is_epp_valid', return_value = True) as mock_is_epp,\ mock.patch('power._do_admission_control_check', return_value = False),\ mock.patch('power._admission_control_check') as mock__admission_control_check: power.validate_power_profiles(data, True) mock__admission_control_check.assert_not_called() with mock.patch('power._is_max_freq_valid', return_value = True) as mock_is_max,\ mock.patch('power._is_min_freq_valid', return_value = True) as mock_is_min,\ mock.patch('power._is_epp_valid', return_value = True) as mock_is_epp,\ mock.patch('power._do_admission_control_check', return_value = True),\ mock.patch('power._admission_control_check') as mock__admission_control_check: power.validate_power_profiles(data, False) mock__admission_control_check.assert_not_called() sys = mock.MagicMock() sys.request_config = mock.MagicMock(return_value=True) sys.refresh_all = mock.MagicMock() cores = [] for id in range(0, 3): core = mock.MagicMock() core.core_id = id cores.append(core) data.update({ "pools": [{ "id": 0, "cores": [0, 1, 2, 3], "power_profile": 0 }, { "id": 1, "cores": [4, 5, 6, 7] }] }) with mock.patch('power._is_max_freq_valid', return_value = True) as mock_is_max,\ mock.patch('power._is_min_freq_valid', return_value = True) as mock_is_min,\ mock.patch('power._is_epp_valid', return_value = True) as mock_is_epp,\ mock.patch('power._do_admission_control_check', return_value = True),\ mock.patch('power_common.get_pwr_cores', return_value=cores) as mock_get_cores,\ mock.patch('power_common.get_pwr_sys', return_value=sys) as mock_get_sys: power.validate_power_profiles(data, True) sys.request_config.assert_called_once() sys.refresh_all.assert_called_once() for core in cores: assert hasattr(core, "min_freq") assert hasattr(core, "max_freq") assert hasattr(core, "epp") sys.request_config = mock.MagicMock(return_value=False) sys.refresh_all.reset_mock() with mock.patch('power._is_max_freq_valid', return_value = True) as mock_is_max,\ mock.patch('power._is_min_freq_valid', return_value = True) as mock_is_min,\ mock.patch('power._is_epp_valid', return_value = True) as mock_is_epp,\ mock.patch('power._do_admission_control_check', return_value = True),\ mock.patch('power_common.get_pwr_cores', return_value=cores) as mock_get_cores,\ mock.patch('power_common.get_pwr_sys', return_value=sys) as mock_get_sys: with pytest.raises( power.AdmissionControlError, match= "Power Profiles configuration would cause CPU to be oversubscribed." ): power.validate_power_profiles(data, True) sys.request_config.assert_called_once() sys.refresh_all.assert_called_once() with mock.patch('power._is_max_freq_valid', return_value = False ) as mock_is_max,\ mock.patch('power._is_min_freq_valid', return_value = True) as mock_is_min,\ mock.patch('power._is_epp_valid', return_value = True) as mock_is_epp,\ mock.patch('power._do_admission_control_check', return_value = True),\ mock.patch('power._admission_control_check', return_value = True) as mock__admission_control_check: with pytest.raises( ValueError, match="Power Profile 0, Invalid max. freq 2500."): power.validate_power_profiles(data, True) mock__admission_control_check.assert_not_called() with mock.patch('power._is_max_freq_valid', return_value = True ) as mock_is_max,\ mock.patch('power._is_min_freq_valid', return_value = False) as mock_is_min,\ mock.patch('power._is_epp_valid', return_value = True) as mock_is_epp,\ mock.patch('power._do_admission_control_check', return_value = True),\ mock.patch('power._admission_control_check', return_value = True) as mock__admission_control_check: with pytest.raises( ValueError, match="Power Profile 0, Invalid min. freq 1500."): power.validate_power_profiles(data, True) mock__admission_control_check.assert_not_called() with mock.patch('power._is_max_freq_valid', return_value = True ) as mock_is_max,\ mock.patch('power._is_min_freq_valid', return_value = True) as mock_is_min,\ mock.patch('power._is_epp_valid', return_value = False) as mock_is_epp,\ mock.patch('power._do_admission_control_check', return_value = True),\ mock.patch('power._admission_control_check', return_value = True) as mock__admission_control_check: with pytest.raises( ValueError, match="Power Profile 0, Invalid EPP value performance."): power.validate_power_profiles(data, True) mock__admission_control_check.assert_not_called() # set invalid min. freq, higher than max. freq data['power_profiles'][-1][ 'min_freq'] = data['power_profiles'][-1]['max_freq'] + 1 with mock.patch('power._is_max_freq_valid', return_value = True) as mock_is_max,\ mock.patch('power._is_min_freq_valid', return_value = True) as mock_is_min,\ mock.patch('power._is_epp_valid', return_value = True) as mock_is_epp,\ mock.patch('power._do_admission_control_check', return_value = True),\ mock.patch('power._admission_control_check', return_value = True) as mock__admission_control_check: with pytest.raises( ValueError, match= "Power Profile 1, Invalid freqs! min. freq is higher than max. freq." ): power.validate_power_profiles(data, True) mock__admission_control_check.assert_not_called()