def test_update_config_changed_on_env_change(self, *_): # given HardwarePreset.create(name='foo', cpu_cores=1, memory=1200000, disk=2000000) # then assert self.config.max_resource_size == 0 # initial assert not HardwarePresets.update_config(DEFAULT, self.config) assert self.config.max_resource_size == 7e9 assert not HardwarePresets.update_config(DEFAULT, self.config) assert not HardwarePresets.update_config(DEFAULT, self.config) # when env changes (disk space shrinks) with patch('golem.core.hardware.free_partition_space', return_value=5e9): # then assert HardwarePresets.update_config(DEFAULT, self.config) assert self.config.max_resource_size == 5e9 assert not HardwarePresets.update_config(DEFAULT, self.config) # when env changes again (back to previous state) than assert HardwarePresets.update_config(DEFAULT, self.config) assert self.config.max_resource_size == 7e9 assert not HardwarePresets.update_config(DEFAULT, self.config)
def initialize(cls, working_dir): cls.working_dir = working_dir cls.default_values[u'disk'] = free_partition_space(cls.working_dir) HardwarePreset.get_or_create(name=cls.DEFAULT_NAME, defaults=cls.default_values) HardwarePreset.get_or_create(name=cls.CUSTOM_NAME, defaults=cls.CUSTOM_VALUES)
def test_update_config_lower_bounds(self, *_): # given HardwarePreset.create(name='min', cpu_cores=-7, memory=-1, disk=-1) # when config_changed = HardwarePresets.update_config('min', self.config) # then assert not config_changed assert self.config.hardware_preset_name == 'min' assert self.config.num_cores == MIN_CPU_CORES assert self.config.max_memory_size == MIN_MEMORY_SIZE assert self.config.max_resource_size == MIN_DISK_SPACE
def test_update_config_not_changed(self, *_): # given HardwarePreset.create(name='foo', cpu_cores=1, memory=1200000, disk=2000000) # then assert not HardwarePresets.update_config(DEFAULT, self.config) assert not HardwarePresets.update_config(DEFAULT, self.config) assert not HardwarePresets.update_config(DEFAULT, self.config) assert HardwarePresets.update_config(CUSTOM, self.config) assert not HardwarePresets.update_config(CUSTOM, self.config) assert HardwarePresets.update_config(DEFAULT, self.config) assert HardwarePresets.update_config('foo', self.config) assert not HardwarePresets.update_config('foo', self.config)
def test_update_config(self, *_): # given HardwarePreset.create(name='foo', cpu_cores=1, memory=1200000, disk=2000000) # when config_changed = HardwarePresets.update_config('foo', self.config) # then assert not config_changed assert self.config.hardware_preset_name == 'foo' assert self.config.num_cores == 1 assert self.config.max_memory_size == 1200000 assert self.config.max_resource_size == 2000000
def test_update_config_upper_bounds(self, *_): # given HardwarePreset.create(name='max', cpu_cores=1e9, memory=1e18, disk=1e18) caps = HardwarePresets.caps() # when config_changed = HardwarePresets.update_config('max', self.config) # then assert not config_changed assert self.config.hardware_preset_name == 'max' assert self.config.num_cores == caps['cpu_cores'] assert self.config.max_memory_size == caps['memory'] assert self.config.max_resource_size == caps['disk']
def update_hw_preset(cls, preset): preset_dict = cls.__preset_to_dict(preset) name = cls.__sanitize_preset_name(preset_dict['name']) preset = HardwarePreset.get(name=name) preset.apply(preset_dict) preset.save() return preset.to_dict()
def values(cls, preset_or_name): preset_or_name = preset_or_name or DEFAULT_HARDWARE_PRESET_NAME if isinstance(preset_or_name, basestring): preset = HardwarePreset.get(name=preset_or_name) else: preset = preset_or_name return preset.name, { u'cpu_cores': cls.cpu_cores(preset.cpu_cores), u'memory': cls.memory(preset.memory), u'disk': cls.disk(preset.disk) }
def values(cls, preset_or_name: Union[str, HardwarePreset]) \ -> Tuple[str, Dict[str, int]]: preset_or_name = preset_or_name or DEFAULT if isinstance(preset_or_name, str): preset = HardwarePreset.get(name=preset_or_name) else: preset = preset_or_name return preset.name, { 'cpu_cores': cls.cpu_cores(preset.cpu_cores), 'memory': cls.memory(preset.memory), 'disk': cls.disk(preset.disk) }
def upsert_hw_preset(cls, preset): preset_dict = cls.__preset_to_dict(preset) name = cls.__sanitize_preset_name(preset_dict['name']) defaults = dict(preset_dict) defaults.pop('name') preset, created = HardwarePreset.get_or_create(name=name, defaults=defaults) if not created: preset.apply(preset_dict) preset.save() return preset.to_dict()
def test_create_hw_preset(self): preset_name = unicode(uuid.uuid4()) preset_cpu_cores = 1 preset_memory = 1000 * 1024 preset_disk = 1000 * 1024 preset_dict = dict() # try to persist a preset with null values with self.assertRaises(IntegrityError): HardwarePresetsMixin.create_hw_preset(preset_dict) preset_dict['name'] = preset_name with self.assertRaises(IntegrityError): HardwarePresetsMixin.create_hw_preset(preset_dict) preset_dict['cpu_cores'] = preset_cpu_cores with self.assertRaises(IntegrityError): HardwarePresetsMixin.create_hw_preset(preset_dict) preset_dict['memory'] = preset_memory with self.assertRaises(IntegrityError): HardwarePresetsMixin.create_hw_preset(preset_dict) # persist a preset with all values set preset_dict['disk'] = preset_disk assert HardwarePresetsMixin.create_hw_preset(preset_dict) preset = HardwarePresetsMixin.get_hw_preset(preset_name) # try to insert a preset with the same name with self.assertRaises(IntegrityError): HardwarePresetsMixin.create_hw_preset(preset_dict) assert preset assert preset['name'] == preset_name assert preset['cpu_cores'] == preset_cpu_cores assert preset['memory'] == preset_memory assert preset['disk'] == preset_disk # use upsert to create a preset from dict preset_dict['name'] = unicode(uuid.uuid4()) print preset_dict assert HardwarePresetsMixin.upsert_hw_preset(preset_dict) assert HardwarePresetsMixin.get_hw_preset(preset_dict['name']) # use upsert to create a preset from object preset_dict['name'] = unicode(uuid.uuid4()) preset = HardwarePreset(**preset_dict) assert HardwarePresetsMixin.upsert_hw_preset(preset) assert HardwarePresetsMixin.get_hw_preset(preset_dict['name'])
def from_config(cls, config: ClientConfigDescriptor) -> HardwarePreset: return HardwarePreset(name=config.hardware_preset_name, cpu_cores=config.num_cores, memory=config.max_memory_size, disk=config.max_resource_size)
def create_hw_preset(preset_dict): preset = HardwarePreset(**preset_dict) preset.save() return preset.to_dict()
def get_hw_preset(name): return HardwarePreset.get(name=name).to_dict()
def get_hw_presets(): presets = HardwarePreset.select() return [p.to_dict() for p in presets]
def from_config(cls, config): return HardwarePreset(name=config.hardware_preset_name, cpu_cores=config.num_cores, memory=config.max_memory_size, disk=config.max_resource_size)
def test_initialize(self, *_): assert HardwarePreset.get(name=DEFAULT) assert HardwarePreset.get(name=CUSTOM)