def allocate_nodes(self, nodes: int = 1, cores: int = 1, memory_per_node: Union[str, bitmath.Byte] = None, walltime: Union[str, Walltime] = None, native_args: Optional[ Dict[str, Optional[ str]]] = None) -> Nodes: # noqa, pylint: disable=bad-whitespace,line-too-long if memory_per_node is None: memory_per_node = bitmath.GiB(1) elif isinstance(memory_per_node, str): memory_per_node = bitmath.parse_string(memory_per_node) if walltime is None: walltime = Walltime(minutes=10) elif isinstance(walltime, str): walltime = Walltime.from_string(walltime) parameters = AllocationParameters(nodes=nodes, cores=cores, memory_per_node=memory_per_node, walltime=walltime, native_args=native_args) return allocate_slurm_nodes(parameters=parameters, config=self._config)
def test_walltime_minutes(): """Values for minutes parameter.""" with pytest.raises(ValueError): Walltime(minutes=-1) assert Walltime(minutes=10).minutes == 10 with pytest.raises(ValueError): Walltime(minutes=60)
def test_walltime_seconds(): """Values for seconds parameter.""" with pytest.raises(ValueError): Walltime(seconds=-1) assert Walltime(seconds=10).seconds == 10 with pytest.raises(ValueError): Walltime(seconds=60)
def test_walltime_hours(): """Values for hours parameter.""" with pytest.raises(ValueError): Walltime(hours=-1) assert Walltime(hours=10).hours == 10 with pytest.raises(ValueError): Walltime(hours=24)
def test_serialize_deserialize(): value = AllocationParameters(nodes=20, cores=10, memory_per_node=bitmath.GiB(20), walltime=Walltime(days=1, hours=12, minutes=5, seconds=32), native_args={ '--arg1': 'value 1', '--arg2': None, '--arg3': '65' }) serialized = value.serialize() assert serialized == { 'type': 'SerializableTypes.ALLOCATION_PARAMETERS', 'nodes': 20, 'cores': 10, 'memory_per_node': '20.0 GiB', 'walltime': '1-12:05:32', 'native_args': { '--arg1': 'value 1', '--arg2': None, '--arg3': '65' } } deserialized = AllocationParameters.deserialize(serialized=serialized) assert deserialized == value
def test_walltime_values(): """Basic walltime values.""" walltime = Walltime(days=1, hours=2, minutes=3, seconds=4) assert walltime.days == 1 assert walltime.hours == 2 assert walltime.minutes == 3 assert walltime.seconds == 4
def deserialize(serialized: dict) -> 'AllocationParameters': try: assert serialized['type'] == str( SerializableTypes.ALLOCATION_PARAMETERS) return AllocationParameters( nodes=serialized['nodes'], cores=serialized['cores'], memory_per_node=(None if serialized['memory_per_node'] is None else bitmath.parse_string( serialized['memory_per_node'])), walltime=(None if serialized['walltime'] is None else Walltime.from_string(serialized['walltime'])), native_args=serialized['native_args']) except KeyError as e: raise RuntimeError("Unable to deserialize.") from e
def test_walltime_to_string(): assert str(Walltime(days=0, hours=1, minutes=2, seconds=3)) == '01:02:03' assert str(Walltime(days=1, hours=2, minutes=3, seconds=4)) == '1-02:03:04' assert str(Walltime(days=123, hours=4, minutes=5, seconds=6)) == '123-04:05:06' assert str(Walltime(days=123, hours=10, minutes=20, seconds=30)) == '123-10:20:30' assert str(Walltime(days=123, hours=0, minutes=0, seconds=0)) == '123-00:00:00' walltime = Walltime(days=1, hours=2, minutes=3, seconds=4) assert str(walltime) == repr(walltime)
def test_walltime_from_string(): """Values for walltime conversion from string.""" walltime_1 = Walltime.from_string('1:02:03') assert walltime_1.days == 0 assert walltime_1.hours == 1 assert walltime_1.minutes == 2 assert walltime_1.seconds == 3 walltime_2 = Walltime.from_string('4-1:02:03') assert walltime_2.days == 4 assert walltime_2.hours == 1 assert walltime_2.minutes == 2 assert walltime_2.seconds == 3 walltime_3 = Walltime.from_string('4-05:02:03') assert walltime_3.days == 4 assert walltime_3.hours == 5 assert walltime_3.minutes == 2 assert walltime_3.seconds == 3 walltime_4 = Walltime.from_string('400-12:30:59') assert walltime_4.days == 400 assert walltime_4.hours == 12 assert walltime_4.minutes == 30 assert walltime_4.seconds == 59 with pytest.raises(ValueError): Walltime.from_string('0:00:00') with pytest.raises(ValueError): Walltime.from_string('-1:02:03') with pytest.raises(ValueError): Walltime.from_string('1:02: 03') with pytest.raises(ValueError): Walltime.from_string('02:03') with pytest.raises(ValueError): Walltime.from_string('3') with pytest.raises(ValueError): Walltime.from_string('3-4') with pytest.raises(ValueError): Walltime.from_string('3-4:05') with pytest.raises(ValueError): Walltime.from_string('1:2:3') with pytest.raises(ValueError): Walltime.from_string('abc')
def test_walltime_days(): """Values for days parameter.""" with pytest.raises(ValueError): Walltime(days=-1) assert Walltime(days=10).days == 10
def test_walltime_must_not_be_zero(): """Zero walltime is not allowed.""" with pytest.raises(ValueError): Walltime()