Skip to content

Commit b3dd56a

Browse files
authored
Reverting unittests to the previous state
1 parent 795e5c7 commit b3dd56a

File tree

3 files changed

+102
-247
lines changed

3 files changed

+102
-247
lines changed

tests/test_cli_monitor_basic.py

Lines changed: 31 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,31 @@
1-
import unittest
2-
import subprocess
3-
import platform
4-
5-
class TestCliMonitorBasic(unittest.TestCase):
6-
"""Tests the most basic functionality of cli_monitor.py."""
7-
8-
def test_command_parsing(self):
9-
"""Fixed version correctly passes the command as a list of arguments."""
10-
cmd = ["echo", "hello", "world"]
11-
result = subprocess.run(cmd, capture_output=True, text=True, shell=True)
12-
# The command should be correctly split into arguments and executed as expected
13-
self.assertEqual(result.stdout.strip(), "hello world", "Fixed version should correctly pass arguments!")
14-
15-
def test_echo_command(self):
16-
"""Verify 'echo Hello' runs without errors and prints 'Hello'."""
17-
cmd = [
18-
"python", "cli_monitor.py",
19-
"--command", "echo", "Hello",
20-
"--timer", "2"
21-
]
22-
result = subprocess.run(cmd, capture_output=True, text=True, shell=True)
23-
self.assertEqual(result.returncode, 0)
24-
self.assertIn("Hello", result.stdout)
25-
26-
def test_invalid_frequency(self):
27-
"""Frequency outside allowed range should cause an error exit."""
28-
cmd = [
29-
"python", "cli_monitor.py",
30-
"--command", "echo", "Hello",
31-
"--frequency", "999999999" # way too large
32-
]
33-
result = subprocess.run(cmd, capture_output=True, text=True, shell=True)
34-
self.assertNotEqual(result.returncode, 0) # Expecting a non-zero exit code due to frequency validation
35-
self.assertIn("Frequency must be between", result.stderr) # Check stderr instead of stdout
36-
37-
def test_complex_command(self):
38-
"""Ensure complex commands with multiple arguments work correctly."""
39-
cmd = ["dir"] if platform.system() == "Windows" else ["ls", "-la", "/"]
40-
41-
result = subprocess.run(
42-
["python", "cli_monitor.py", "--timer", "2", "--command"] + cmd,
43-
capture_output=True, text=True, shell=True
44-
)
45-
self.assertEqual(result.returncode, 0)
46-
self.assertIn("total" if platform.system() != "Windows" else "Directory", result.stdout)
47-
48-
def test_command_with_quotes(self):
49-
"""Ensure commands with quotes are properly handled."""
50-
cmd = [
51-
"python", "cli_monitor.py",
52-
"--command", "echo", "Hello World",
53-
"--timer", "2"
54-
]
55-
result = subprocess.run(cmd, capture_output=True, text=True, shell=True)
56-
self.assertEqual(result.returncode, 0)
57-
self.assertIn("Hello World", result.stdout)
58-
59-
if __name__ == "__main__":
60-
unittest.main()
1+
import unittest
2+
import subprocess
3+
4+
class TestCliMonitorBasic(unittest.TestCase):
5+
"""Tests the most basic functionality of cli_monitor.py."""
6+
7+
def test_echo_command(self):
8+
"""Verify 'echo Hello' runs without errors and prints 'Hello'."""
9+
cmd = [
10+
"python", "cli_monitor.py",
11+
"--command", "echo Hello",
12+
"--timer", "2" # short run
13+
]
14+
result = subprocess.run(cmd, capture_output=True, text=True)
15+
self.assertEqual(result.returncode, 0)
16+
self.assertIn("Hello", result.stdout)
17+
18+
def test_invalid_frequency(self):
19+
"""Frequency outside allowed range should cause an error exit."""
20+
cmd = [
21+
"python", "cli_monitor.py",
22+
"--command", "echo Hello",
23+
"--frequency", "999999999" # way too large
24+
]
25+
result = subprocess.run(cmd, capture_output=True, text=True)
26+
# Expecting a non-zero exit code due to frequency validation
27+
self.assertNotEqual(result.returncode, 0)
28+
self.assertIn("Error: Frequency must be between", result.stdout)
29+
30+
if __name__ == "__main__":
31+
unittest.main()

tests/test_cli_monitor_logging.py

Lines changed: 36 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,36 @@
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:\nSTDOUT: {result.stdout}\nSTDERR: {result.stderr}"
151-
)
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()

tests/test_cli_monitor_regex.py

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
1-
import unittest
2-
import subprocess
3-
4-
class TestCliMonitorRegex(unittest.TestCase):
5-
"""Tests regex matching and triggered command execution."""
6-
7-
def test_regex_trigger(self):
8-
"""Should detect 'ERROR' and run the triggered command."""
9-
cmd = [
10-
"python", "cli_monitor.py",
11-
"--regex", "ERROR",
12-
"--timer", "2",
13-
"--command", "echo ERROR OCCURRED",
14-
"--regex-execute", "echo 'Trigger ran!'"
15-
16-
]
17-
result = subprocess.run(cmd, capture_output=True, text=True)
18-
self.assertEqual(result.returncode, 0)
19-
self.assertIn("ERROR OCCURRED", result.stdout) # Main command output
20-
self.assertIn("Trigger ran!", result.stdout) # Triggered command output
21-
22-
def test_no_regex_no_trigger(self):
23-
"""No triggered command should run if no regex is provided."""
24-
cmd = [
25-
"python", "cli_monitor.py",
26-
"--command", "echo Hello Again",
27-
"--timer", "2"
28-
]
29-
result = subprocess.run(cmd, capture_output=True, text=True)
30-
self.assertEqual(result.returncode, 0)
31-
self.assertIn("Hello Again", result.stdout)
32-
# Ensure we DON'T see "Trigger ran!"
33-
self.assertNotIn("Trigger ran!", result.stdout)
34-
35-
if __name__ == "__main__":
36-
unittest.main()
1+
import unittest
2+
import subprocess
3+
4+
class TestCliMonitorRegex(unittest.TestCase):
5+
"""Tests regex matching and triggered command execution."""
6+
7+
def test_regex_trigger(self):
8+
"""Should detect 'ERROR' and run the triggered command."""
9+
cmd = [
10+
"python", "cli_monitor.py",
11+
"--command", "echo ERROR OCCURRED",
12+
"--regex", "ERROR",
13+
"--regex-execute", "echo 'Trigger ran!'",
14+
"--timer", "2"
15+
]
16+
result = subprocess.run(cmd, capture_output=True, text=True)
17+
self.assertEqual(result.returncode, 0)
18+
self.assertIn("ERROR OCCURRED", result.stdout) # Main command output
19+
self.assertIn("Trigger ran!", result.stdout) # Triggered command output
20+
21+
def test_no_regex_no_trigger(self):
22+
"""No triggered command should run if no regex is provided."""
23+
cmd = [
24+
"python", "cli_monitor.py",
25+
"--command", "echo Hello Again",
26+
"--timer", "2"
27+
]
28+
result = subprocess.run(cmd, capture_output=True, text=True)
29+
self.assertEqual(result.returncode, 0)
30+
self.assertIn("Hello Again", result.stdout)
31+
# Ensure we DON'T see "Trigger ran!"
32+
self.assertNotIn("Trigger ran!", result.stdout)
33+
34+
if __name__ == "__main__":
35+
unittest.main()

0 commit comments

Comments
 (0)