3131from nodescraper .enums .systeminteraction import SystemInteractionLevel
3232from nodescraper .models .systeminfo import OSFamily
3333from nodescraper .plugins .inband .memory .memory_collector import MemoryCollector
34- from nodescraper .plugins .inband .memory .memorydata import MemoryDataModel
3534
3635
3736@pytest .fixture
@@ -44,24 +43,52 @@ def collector(system_info, conn_mock):
4443
4544
4645def test_run_linux (collector , conn_mock ):
47- conn_mock .run_command .return_value = CommandArtifact (
48- exit_code = 0 ,
49- stdout = (
50- " total used free shared buff/cache available\n "
51- "Mem: 2164113772544 31750934528 2097459761152 893313024 34903076864 2122320150528\n "
52- "Swap: 8589930496 0 8589930496"
53- ),
54- stderr = "" ,
55- command = "free -h" ,
56- )
46+ def mock_run_command (command , ** kwargs ):
47+ if "free" in command :
48+ return CommandArtifact (
49+ exit_code = 0 ,
50+ stdout = (
51+ " total used free shared buff/cache available\n "
52+ "Mem: 2164113772544 31750934528 2097459761152 893313024 34903076864 2122320150528\n "
53+ "Swap: 8589930496 0 8589930496"
54+ ),
55+ stderr = "" ,
56+ command = "free -b" ,
57+ )
58+ elif "lsmem" in command :
59+ return CommandArtifact (
60+ exit_code = 0 ,
61+ stdout = (
62+ "RANGE SIZE STATE REMOVABLE BLOCK\n "
63+ "0x0000000000000000-0x000000007fffffff 2G online yes 0-15\n "
64+ "0x0000000100000000-0x000000207fffffff 126G online yes 32-2047\n "
65+ "\n "
66+ "Memory block size: 128M\n "
67+ "Total online memory: 128G\n "
68+ "Total offline memory: 0B\n "
69+ ),
70+ stderr = "" ,
71+ command = "/usr/bin/lsmem" ,
72+ )
73+ return CommandArtifact (exit_code = 1 , stdout = "" , stderr = "" , command = command )
74+
75+ conn_mock .run_command .side_effect = mock_run_command
5776
5877 result , data = collector .collect_data ()
5978
6079 assert result .status == ExecutionStatus .OK
61- assert data == MemoryDataModel (
62- mem_free = "2097459761152" ,
63- mem_total = "2164113772544" ,
64- )
80+ assert data .mem_free == "2097459761152"
81+ assert data .mem_total == "2164113772544"
82+ assert data .lsmem_output is not None
83+ assert "memory_blocks" in data .lsmem_output
84+ assert "summary" in data .lsmem_output
85+ assert "raw_output" in data .lsmem_output
86+ assert len (data .lsmem_output ["memory_blocks" ]) == 2
87+ assert data .lsmem_output ["memory_blocks" ][0 ]["range" ] == "0x0000000000000000-0x000000007fffffff"
88+ assert data .lsmem_output ["memory_blocks" ][0 ]["size" ] == "2G"
89+ assert data .lsmem_output ["memory_blocks" ][0 ]["state" ] == "online"
90+ assert data .lsmem_output ["summary" ]["memory_block_size" ] == "128M"
91+ assert data .lsmem_output ["summary" ]["total_online_memory" ] == "128G"
6592
6693
6794def test_run_windows (collector , conn_mock ):
@@ -76,10 +103,44 @@ def test_run_windows(collector, conn_mock):
76103 result , data = collector .collect_data ()
77104
78105 assert result .status == ExecutionStatus .OK
79- assert data == MemoryDataModel (
80- mem_free = "12345678" ,
81- mem_total = "123412341234" ,
82- )
106+ assert data .mem_free == "12345678"
107+ assert data .mem_total == "123412341234"
108+ assert data .lsmem_output is None
109+ assert conn_mock .run_command .call_count == 1
110+
111+
112+ def test_run_linux_lsmem_fails (collector , conn_mock ):
113+ def mock_run_command (command , ** kwargs ):
114+ if "free" in command :
115+ return CommandArtifact (
116+ exit_code = 0 ,
117+ stdout = (
118+ " total used free shared buff/cache available\n "
119+ "Mem: 2164113772544 31750934528 2097459761152 893313024 34903076864 2122320150528\n "
120+ "Swap: 8589930496 0 8589930496"
121+ ),
122+ stderr = "" ,
123+ command = "free -b" ,
124+ )
125+ elif "lsmem" in command :
126+ return CommandArtifact (
127+ exit_code = 127 ,
128+ stdout = "" ,
129+ stderr = "lsmem: command not found" ,
130+ command = "/usr/bin/lsmem" ,
131+ )
132+ return CommandArtifact (exit_code = 1 , stdout = "" , stderr = "" , command = command )
133+
134+ conn_mock .run_command .side_effect = mock_run_command
135+
136+ result , data = collector .collect_data ()
137+
138+ assert result .status == ExecutionStatus .OK
139+ assert data .mem_free == "2097459761152"
140+ assert data .mem_total == "2164113772544"
141+ assert data .lsmem_output is None
142+ lsmem_events = [e for e in result .events if "lsmem" in e .description ]
143+ assert len (lsmem_events ) > 0
83144
84145
85146def test_run_error (collector , conn_mock ):
@@ -101,3 +162,42 @@ def test_run_error(collector, conn_mock):
101162 assert data is None
102163 assert result .events [0 ].category == EventCategory .OS .value
103164 assert result .events [0 ].description == "Error checking available and total memory"
165+
166+
167+ def test_parse_lsmem_output (collector ):
168+ """Test parsing of lsmem command output."""
169+ lsmem_output = (
170+ "RANGE SIZE STATE REMOVABLE BLOCK\n "
171+ "0x0000000000000000-0x000000007fffffff 2G online yes 0-15\n "
172+ "0x0000000100000000-0x000000207fffffff 126G online yes 32-2047\n "
173+ "0x0000002080000000-0x000000407fffffff 126G online no 2048-4095\n "
174+ "\n "
175+ "Memory block size: 128M\n "
176+ "Total online memory: 254G\n "
177+ "Total offline memory: 0B\n "
178+ )
179+
180+ result = collector ._parse_lsmem_output (lsmem_output )
181+
182+ assert "raw_output" in result
183+ assert "memory_blocks" in result
184+ assert "summary" in result
185+ assert result ["raw_output" ] == lsmem_output
186+ assert len (result ["memory_blocks" ]) == 3
187+
188+ assert result ["memory_blocks" ][0 ]["range" ] == "0x0000000000000000-0x000000007fffffff"
189+ assert result ["memory_blocks" ][0 ]["size" ] == "2G"
190+ assert result ["memory_blocks" ][0 ]["state" ] == "online"
191+ assert result ["memory_blocks" ][0 ]["removable" ] == "yes"
192+ assert result ["memory_blocks" ][0 ]["block" ] == "0-15"
193+
194+ assert result ["memory_blocks" ][1 ]["range" ] == "0x0000000100000000-0x000000207fffffff"
195+ assert result ["memory_blocks" ][1 ]["size" ] == "126G"
196+ assert result ["memory_blocks" ][1 ]["state" ] == "online"
197+
198+ assert result ["memory_blocks" ][2 ]["removable" ] == "no"
199+ assert result ["memory_blocks" ][2 ]["block" ] == "2048-4095"
200+
201+ assert result ["summary" ]["memory_block_size" ] == "128M"
202+ assert result ["summary" ]["total_online_memory" ] == "254G"
203+ assert result ["summary" ]["total_offline_memory" ] == "0B"
0 commit comments