1
- import unittest
2
- import subprocess
3
- import os
4
-
5
- class TestCliMonitorLogging (unittest .TestCase ):
6
- """Tests output-file logging and log pruning."""
7
-
8
- def setUp (self ):
9
- self .log_file = "test_log_output.txt"
10
- # Remove log file if it exists
11
- if os .path .exists (self .log_file ):
12
- os .remove (self .log_file )
13
-
14
- def tearDown (self ):
15
- # Clean up log file
16
- if os .path .exists (self .log_file ):
17
- os .remove (self .log_file )
18
-
19
- def test_log_pruning (self ):
20
- """Check that log file pruning occurs once it exceeds max size."""
21
- cmd = [
22
- "python" , "cli_monitor.py" ,
23
- "--command" , "echo TestLogLine" ,
24
- "--output-file" , self .log_file ,
25
- "--max-log-size" , "1" , # 1KB to force quick pruning
26
- "--timer" , "3" # run a few iterations
27
- ]
28
- result = subprocess .run (cmd , capture_output = True , text = True )
29
- self .assertEqual (result .returncode , 0 )
30
- # We expect the log file to exist, but be under or ~1KB
31
- self .assertTrue (os .path .exists (self .log_file ))
32
- file_size = os .path .getsize (self .log_file )
33
- self .assertLessEqual (file_size , 1024 , "Log file should be pruned to <= 1KB" )
34
-
35
- if __name__ == "__main__" :
36
- unittest .main ()
1
+ import unittest
2
+ import os
3
+ import subprocess
4
+
5
+ class TestLogPruning (unittest .TestCase ):
6
+ """Tests log pruning logic for different max-log-size values."""
7
+
8
+ def setUp (self ):
9
+ """Create a temporary log file for testing."""
10
+ self .log_file = "test_log.txt"
11
+
12
+ def tearDown (self ):
13
+ """Clean up the test log file after each test."""
14
+ if os .path .exists (self .log_file ):
15
+ os .remove (self .log_file )
16
+
17
+ def test_log_pruning_1KB (self ):
18
+ """Ensure pruning works correctly when max-log-size is 1KB."""
19
+ cmd = [
20
+ "python" , "cli_monitor.py" ,
21
+ "--command" , "echo" , "TestLogLine" ,
22
+ "--output-file" , self .log_file ,
23
+ "--max-log-size" , "1" , # 1 KB
24
+ "--timer" , "2" ,
25
+ "--frequency" , "0.1"
26
+ ]
27
+ result = subprocess .run (cmd , capture_output = True , text = True )
28
+ self .assertEqual (result .returncode , 0 )
29
+
30
+ # Ensure log file is pruned correctly
31
+ self .assertTrue (os .path .exists (self .log_file ))
32
+ file_size = os .path .getsize (self .log_file )
33
+ self .assertLessEqual (file_size , 1024 , f"Log file exceeded 1KB, actual: { file_size } bytes" )
34
+
35
+ def test_log_pruning_10KB (self ):
36
+ """Ensure pruning works correctly when max-log-size is 10KB."""
37
+ cmd = [
38
+ "python" , "cli_monitor.py" ,
39
+ "--command" , "echo" , "TestLogLine" ,
40
+ "--output-file" , self .log_file ,
41
+ "--max-log-size" , "10" , # 10 KB
42
+ "--timer" , "3" ,
43
+ "--frequency" , "0.1"
44
+ ]
45
+ result = subprocess .run (cmd , capture_output = True , text = True )
46
+ self .assertEqual (result .returncode , 0 )
47
+
48
+ # Ensure log file is pruned correctly
49
+ self .assertTrue (os .path .exists (self .log_file ))
50
+ file_size = os .path .getsize (self .log_file )
51
+ self .assertLessEqual (file_size , 10 * 1024 , f"Log file exceeded 10KB, actual: { file_size } bytes" )
52
+
53
+ def test_log_pruning_100KB (self ):
54
+ """Ensure pruning works correctly when max-log-size is 100KB."""
55
+ cmd = [
56
+ "python" , "cli_monitor.py" ,
57
+ "--command" , "echo" , "TestLogLine" ,
58
+ "--output-file" , self .log_file ,
59
+ "--max-log-size" , "100" , # 100 KB
60
+ "--timer" , "5" ,
61
+ "--frequency" , "0.1"
62
+ ]
63
+ result = subprocess .run (cmd , capture_output = True , text = True )
64
+ self .assertEqual (result .returncode , 0 )
65
+
66
+ # Ensure log file is pruned correctly
67
+ self .assertTrue (os .path .exists (self .log_file ))
68
+ file_size = os .path .getsize (self .log_file )
69
+ self .assertLessEqual (file_size , 100 * 1024 , f"Log file exceeded 100KB, actual: { file_size } bytes" )
70
+
71
+ def test_log_pruning_min_size (self ):
72
+ """Test with the smallest allowed log size (1KB)."""
73
+ cmd = [
74
+ "python" , "cli_monitor.py" ,
75
+ "--command" , "echo" , "TestLogLine" ,
76
+ "--output-file" , self .log_file ,
77
+ "--max-log-size" , "1" , # 1 KB (Minimum)
78
+ "--timer" , "2"
79
+ ]
80
+ result = subprocess .run (cmd , capture_output = True , text = True )
81
+ self .assertEqual (result .returncode , 0 )
82
+ self .assertTrue (os .path .exists (self .log_file ))
83
+ file_size = os .path .getsize (self .log_file )
84
+ self .assertLessEqual (file_size , 1024 , f"Log file exceeded 1KB, actual: { file_size } bytes" )
85
+
86
+ def test_log_pruning_large_size (self ):
87
+ """Test with a very large log size (10MB) to check if it still works."""
88
+ cmd = [
89
+ "python" , "cli_monitor.py" ,
90
+ "--command" , "echo" , "TestLogLine" ,
91
+ "--output-file" , self .log_file ,
92
+ "--max-log-size" , "10240" , # 10MB
93
+ "--timer" , "2"
94
+ ]
95
+ result = subprocess .run (cmd , capture_output = True , text = True )
96
+ self .assertEqual (result .returncode , 0 )
97
+ self .assertTrue (os .path .exists (self .log_file ))
98
+ file_size = os .path .getsize (self .log_file )
99
+ self .assertLessEqual (file_size , 10 * 1024 * 1024 , f"Log file exceeded 10MB, actual: { file_size } bytes" )
100
+
101
+ def test_negative_log_size (self ):
102
+ """Ensure negative log size is rejected."""
103
+ cmd = [
104
+ "python" , "cli_monitor.py" ,
105
+ "--command" , "echo" , "TestLogLine" ,
106
+ "--output-file" , self .log_file ,
107
+ "--max-log-size" , "-5" # Negative values should not be allowed
108
+ ]
109
+ result = subprocess .run (cmd , capture_output = True , text = True )
110
+ self .assertNotEqual (result .returncode , 0 ) # Should fail
111
+ self .assertIn ("error" , result .stderr .lower ()) # Expect an error message
112
+
113
+ def test_zero_log_size (self ):
114
+ """Ensure zero log size is rejected."""
115
+ cmd = [
116
+ "python" , "cli_monitor.py" ,
117
+ "--command" , "echo" , "TestLogLine" ,
118
+ "--output-file" , self .log_file ,
119
+ "--max-log-size" , "0" # Zero is invalid
120
+ ]
121
+ result = subprocess .run (cmd , capture_output = True , text = True )
122
+ self .assertNotEqual (result .returncode , 0 ) # Should fail
123
+ self .assertIn ("error" , result .stderr .lower ()) # Expect an error message
124
+
125
+ def test_non_integer_log_size (self ):
126
+ """Ensure non-integer values for max-log-size are rejected."""
127
+ cmd = [
128
+ "python" , "cli_monitor.py" ,
129
+ "--command" , "echo" , "TestLogLine" ,
130
+ "--output-file" , self .log_file ,
131
+ "--max-log-size" , "ABC" # Invalid string
132
+ ]
133
+ result = subprocess .run (cmd , capture_output = True , text = True )
134
+ self .assertNotEqual (result .returncode , 0 ) # Should fail
135
+ self .assertIn ("invalid" , result .stderr .lower ()) # Expect an error message
136
+
137
+ def test_max_log_size_without_output_file (self ):
138
+ """Ensure using --max-log-size without --output-file prints a warning but does not fail."""
139
+ cmd = [
140
+ "python" , "cli_monitor.py" ,
141
+ "--command" , "echo" , "TestLogLine" ,
142
+ "--max-log-size" , "10" , # ✅ Should trigger a warning but not an error
143
+ "--timer" , "1"
144
+ ]
145
+ result = subprocess .run (cmd , capture_output = True , text = True )
146
+ self .assertEqual (result .returncode , 0 )
147
+ warning_message = "Warning: --max-log-size is ignored since --output-file is not set." .lower ()
148
+ self .assertTrue ( # Check for the warning in both stdout and stderr
149
+ warning_message in result .stderr .lower () or warning_message in result .stdout .lower (),
150
+ f"Expected warning not found. Output received:\n STDOUT: { result .stdout } \n STDERR: { result .stderr } "
151
+ )
0 commit comments