Coverage for mockslurm/mock_scontrol.py: 81%

16 statements  

« prev     ^ index     » next       coverage.py v7.6.3, created at 2024-10-16 23:37 +0000

1#!/usr/bin/env python 

2"""Implements a mock of scontrol 

3 

4This extremely minimal mock only prints hardcoded values corresponding to the 

5commands SAG uses in its tests in the SDE. 

6""" 

7 

8 

9import sys 

10 

11SCONTROL_SHOW_NODE_STR = """NodeName=localhost Arch=x86_64 CoresPerSocket=1  

12 CPUAlloc=0 CPUTot=8 CPULoad=2.79 

13 AvailableFeatures=(null) 

14 ActiveFeatures=(null) 

15 Gres=(null) 

16 NodeAddr=localhost NodeHostName=localhost Version=20.02.7 

17 OS=Linux 6.5.0-27-generic #28~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Mar 15 10:51:06 UTC 2  

18 RealMemory=5120 AllocMem=0 FreeMem=13771 Sockets=8 Boards=1 

19 State=IDLE ThreadsPerCore=1 TmpDisk=0 Weight=1 Owner=N/A MCS_label=N/A 

20 Partitions=test,prod  

21 BootTime=2024-04-19T08:09:45 SlurmdStartTime=2024-04-19T09:38:48 

22 CfgTRES=cpu=8,mem=5G,billing=8 

23 AllocTRES= 

24 CapWatts=n/a 

25 CurrentWatts=0 AveWatts=0 

26 ExtSensorsJoules=n/s ExtSensorsWatts=0 ExtSensorsTemp=n/s 

27  

28 

29""" 

30 

31 

32def main(): 

33 

34 if len(sys.argv) < 4: 

35 raise ValueError( 

36 'scontrol mock only supports "scontrol show node node_name" or ' 

37 '"scontrol show hostnames node_name" commands, but got "{}"'.format(" ".join(["scontrol"] + sys.argv[1:])) 

38 ) 

39 

40 if sys.argv[1] != "show": 40 ↛ 41line 40 didn't jump to line 41 because the condition on line 40 was never true

41 raise NotImplementedError('scontrol mock only supports the "show" command, but got {}'.format(sys.argv[1])) 

42 

43 if sys.argv[3] != "localhost": 

44 raise NotImplementedError('scontrol mock only supports node "localhost" but got {}'.format(sys.argv[3])) 

45 

46 match sys.argv[2]: 

47 case "node": 

48 print(SCONTROL_SHOW_NODE_STR, end="") 

49 case "hostnames": 49 ↛ 51line 49 didn't jump to line 51 because the pattern on line 49 always matched

50 print("localhost") 

51 case _: 

52 raise NotImplementedError( 

53 'scontrol show mock only supports "hostnames" and "node" commands, but got {}'.format(sys.argv[2]) 

54 )