77
88import unittest
99from unittest .mock import patch , MagicMock
10- import subprocess
1110
12- # Import the sar_facts module from the collection.
1311from ansible_collections .ans2dev .general .plugins .modules import sar_facts # type: ignore
1412
1513
@@ -22,15 +20,31 @@ def fail_json(*args, **kwargs):
2220
2321
2422class TestSarFactsModule (unittest .TestCase ):
23+ def run_sar_fact_test (self , test_params , fake_sar_output , expected_fact_key , expected_sample_value ):
24+ with patch .object (sar_facts , 'AnsibleModule' ) as mock_AnsibleModule :
25+ fake_module = MagicMock ()
26+ fake_module .params = test_params
27+ fake_module .exit_json .side_effect = exit_json
28+ fake_module .fail_json .side_effect = fail_json
29+ fake_module .run_command .return_value = (0 , fake_sar_output , "" )
30+ fake_module .get_bin_path .return_value = "/usr/bin/sar"
31+ mock_AnsibleModule .return_value = fake_module
32+
33+ with patch .object (sar_facts , 'find_sar_file' , return_value = "/dummy/sa01" ):
34+ with self .assertRaises (Exception ) as context :
35+ sar_facts .main ()
36+ result_str = str (context .exception )
37+
38+ self .assertIn (expected_fact_key , result_str )
39+ self .assertIn (expected_sample_value , result_str )
40+ self .assertIn ("2025-05-01" , result_str )
41+
2542 def test_sar_cpu (self ):
26- # Simulated output for the 'sar' command.
2743 fake_sar_output = (
2844 "08:00:00 AM %user %system %idle\n "
2945 "08:00:00 AM 5.00 2.00 93.00\n "
3046 "08:10:00 AM 6.00 3.00 91.00\n "
3147 )
32-
33- # Parameters for a SAR facts call (CPU type).
3448 test_params = {
3549 'date_start' : '2025-05-01' ,
3650 'date_end' : '2025-05-01' ,
@@ -40,36 +54,92 @@ def test_sar_cpu(self):
4054 'average' : False ,
4155 'partition' : False
4256 }
57+ self .run_sar_fact_test (test_params , fake_sar_output , "sar_cpu" , "5.00" )
4358
44- # Patch AnsibleModule in sar_facts.
45- with patch .object (sar_facts , 'AnsibleModule' ) as mock_AnsibleModule :
46- fake_module = MagicMock ()
47- fake_module .params = test_params
48- fake_module .exit_json .side_effect = exit_json
49- fake_module .fail_json .side_effect = fail_json
50- fake_module .run_command .return_value = (0 , fake_sar_output , "" )
51- mock_AnsibleModule .return_value = fake_module
59+ def test_sar_memory (self ):
60+ fake_sar_output = (
61+ "08:00:00 AM %memused %commit\n "
62+ "08:00:00 AM 75.00 60.00\n "
63+ "08:10:00 AM 76.00 59.50\n "
64+ )
65+ test_params = {
66+ 'date_start' : '2025-05-01' ,
67+ 'date_end' : '2025-05-01' ,
68+ 'time_start' : '08:00:00' ,
69+ 'time_end' : '10:00:00' ,
70+ 'type' : 'memory' ,
71+ 'average' : False ,
72+ 'partition' : False
73+ }
74+ self .run_sar_fact_test (test_params , fake_sar_output , "sar_mem" , "75.00" )
5275
53- # Patch file-check and external command functions.
54- with patch .object (sar_facts , 'find_sar_file' , return_value = "/dummy/sa01" ), \
55- patch .object (sar_facts , 'subprocess' ) as mock_subprocess :
76+ def test_sar_swap (self ):
77+ fake_sar_output = (
78+ "08:00:00 AM %swpused %swpcad\n "
79+ "08:00:00 AM 10.00 0.50\n "
80+ "08:10:00 AM 11.00 0.60\n "
81+ )
82+ test_params = {
83+ 'date_start' : '2025-05-01' ,
84+ 'date_end' : '2025-05-01' ,
85+ 'time_start' : '08:00:00' ,
86+ 'time_end' : '10:00:00' ,
87+ 'type' : 'swap' ,
88+ 'average' : False ,
89+ 'partition' : False
90+ }
91+ self .run_sar_fact_test (test_params , fake_sar_output , "sar_swap" , "10.00" )
5692
57- # Simulate a successful subprocess.run call.
58- mock_subprocess .run .return_value = subprocess .CompletedProcess (
59- args = ["/usr/bin/sar" , "-f" , "/dummy/sa01" ],
60- returncode = 0 ,
61- stdout = fake_sar_output
62- )
93+ def test_sar_network (self ):
94+ fake_sar_output = (
95+ "08:00:00 AM IFACE rxpck/s txpck/s %ifutil\n "
96+ "08:00:00 AM eth0 100.00 200.00 0.50\n "
97+ "08:10:00 AM eth0 110.00 210.00 0.55\n "
98+ )
99+ test_params = {
100+ 'date_start' : '2025-05-01' ,
101+ 'date_end' : '2025-05-01' ,
102+ 'time_start' : '08:00:00' ,
103+ 'time_end' : '10:00:00' ,
104+ 'type' : 'network' ,
105+ 'average' : False ,
106+ 'partition' : False
107+ }
108+ self .run_sar_fact_test (test_params , fake_sar_output , "sar_net" , "100.00" )
63109
64- with self .assertRaises (Exception ) as context :
65- sar_facts .main ()
110+ def test_sar_disk (self ):
111+ fake_sar_output = (
112+ "08:00:00 AM DEV %util await rkB/s wkB/s\n "
113+ "08:00:00 AM sda 90.00 5.00 100.00 200.00\n "
114+ "08:10:00 AM sda 91.00 5.10 101.00 201.00\n "
115+ )
116+ test_params = {
117+ 'date_start' : '2025-05-01' ,
118+ 'date_end' : '2025-05-01' ,
119+ 'time_start' : '08:00:00' ,
120+ 'time_end' : '10:00:00' ,
121+ 'type' : 'disk' ,
122+ 'average' : False ,
123+ 'partition' : False
124+ }
125+ self .run_sar_fact_test (test_params , fake_sar_output , "sar_disk" , "90.00" )
66126
67- result_str = str (context .exception )
68- # Verify that exit_json output contains expected SAR fact keys and values.
69- self .assertIn ("sar_cpu" , result_str )
70- self .assertIn ("5.00" , result_str )
71- self .assertIn ("2025-05-01" , result_str )
72- self .assertIn ("exit_json called" , result_str )
127+ def test_sar_load (self ):
128+ fake_sar_output = (
129+ "08:00:00 AM ldavg-1 ldavg-5 ldavg-15\n "
130+ "08:00:00 AM 0.50 0.60 0.70\n "
131+ "08:10:00 AM 0.55 0.65 0.75\n "
132+ )
133+ test_params = {
134+ 'date_start' : '2025-05-01' ,
135+ 'date_end' : '2025-05-01' ,
136+ 'time_start' : '08:00:00' ,
137+ 'time_end' : '10:00:00' ,
138+ 'type' : 'load' ,
139+ 'average' : False ,
140+ 'partition' : False
141+ }
142+ self .run_sar_fact_test (test_params , fake_sar_output , "sar_load" , "0.50" )
73143
74144
75145if __name__ == '__main__' :
0 commit comments