164 lines
5.7 KiB
Python
164 lines
5.7 KiB
Python
import unittest
|
|
from datetime import datetime
|
|
import os
|
|
|
|
from sia.repeat_entry import RepeatEntry
|
|
|
|
class RepeatEntryTest(unittest.TestCase):
|
|
def setUp(self):
|
|
"""Set up test cases with fixed id and timestamp"""
|
|
self.test_id = "test-id-1234"
|
|
self.test_timestamp = datetime(2024, 10, 31, 12, 0, 0)
|
|
|
|
# Create a temporary file for testing
|
|
self.test_filename = "test_output.txt"
|
|
self.counter_file = "counter.txt"
|
|
self.cleanup_files()
|
|
|
|
def tearDown(self):
|
|
"""Clean up any test files"""
|
|
self.cleanup_files()
|
|
|
|
def cleanup_files(self):
|
|
"""Helper to remove test files"""
|
|
for filename in [self.test_filename, self.counter_file]:
|
|
if os.path.exists(filename):
|
|
os.remove(filename)
|
|
|
|
def test_initialization(self):
|
|
"""Test entry initialization"""
|
|
script = "echo test"
|
|
entry = RepeatEntry(script, self.test_id, self.test_timestamp)
|
|
|
|
self.assertEqual(entry.script, script)
|
|
self.assertEqual(entry.stdout, "")
|
|
self.assertEqual(entry.stderr, "")
|
|
self.assertIsNone(entry.exit_code)
|
|
self.assertEqual(entry.id, self.test_id)
|
|
self.assertEqual(entry.timestamp, self.test_timestamp)
|
|
|
|
def test_successful_execution(self):
|
|
"""Test successful script execution"""
|
|
script = "echo 'test'"
|
|
entry = RepeatEntry(script, self.test_id, self.test_timestamp)
|
|
entry.update()
|
|
|
|
# Verify entry state
|
|
self.assertEqual(entry.stdout.strip(), "test")
|
|
self.assertEqual(entry.stderr, "")
|
|
self.assertEqual(entry.exit_code, 0)
|
|
|
|
def test_failed_execution(self):
|
|
"""Test failed script execution with invalid command"""
|
|
entry = RepeatEntry("nonexistentcommand", self.test_id, self.test_timestamp)
|
|
entry.update()
|
|
|
|
# Verify entry state
|
|
self.assertEqual(entry.stdout, "")
|
|
self.assertTrue(len(entry.stderr) > 0) # Error message will vary by OS
|
|
self.assertNotEqual(entry.exit_code, 0) # Exit code will be non-zero
|
|
|
|
def test_repeated_execution(self):
|
|
"""Test that script executes on every update"""
|
|
# Create a counter file
|
|
with open(self.counter_file, 'w') as f:
|
|
f.write('0')
|
|
|
|
# Script that increments and reads the counter
|
|
script = (
|
|
f'python3 -c "'
|
|
f'import os; '
|
|
f'f=open(\'{self.counter_file}\', \'r+\'); '
|
|
f'n=int(f.read()); '
|
|
f'f.seek(0); '
|
|
f'f.write(str(n+1)); '
|
|
f'f.truncate(); '
|
|
f'f.close(); '
|
|
f'print(n+1)'
|
|
f'"'
|
|
)
|
|
|
|
entry = RepeatEntry(script, self.test_id, self.test_timestamp)
|
|
|
|
# Run update multiple times
|
|
outputs = []
|
|
for _ in range(3):
|
|
entry.update()
|
|
outputs.append(int(entry.stdout.strip()))
|
|
|
|
# Verify outputs are sequential numbers
|
|
self.assertEqual(outputs, [1, 2, 3])
|
|
|
|
def test_generate_context(self):
|
|
"""Test XML context generation"""
|
|
script = "echo 'test'"
|
|
entry = RepeatEntry(script, self.test_id, self.test_timestamp)
|
|
entry.update()
|
|
element = entry.generate_context()
|
|
|
|
# Verify XML structure
|
|
self.assertEqual(element.tag, "repeat")
|
|
self.assertEqual(element.get("id"), self.test_id)
|
|
self.assertEqual(element.text, f"<![CDATA[{script}]]>")
|
|
self.assertEqual(element.get("exit_code"), "0")
|
|
|
|
# Get stdout and remove trailing newline for comparison
|
|
stdout_text = element.find("stdout").text
|
|
self.assertTrue(stdout_text.startswith("<![CDATA[test"))
|
|
self.assertTrue(stdout_text.endswith("]]>"))
|
|
|
|
self.assertEqual(element.find("stderr").text, "<![CDATA[]]>")
|
|
|
|
def test_changing_output(self):
|
|
"""Test handling of changing command output"""
|
|
# Create a counter file
|
|
with open(self.counter_file, 'w') as f:
|
|
f.write('0')
|
|
|
|
# Script that outputs an incrementing number
|
|
script = (
|
|
f'python3 -c "'
|
|
f'import os; '
|
|
f'f=open(\'{self.counter_file}\', \'r+\'); '
|
|
f'n=int(f.read()); '
|
|
f'f.seek(0); '
|
|
f'f.write(str(n+1)); '
|
|
f'f.truncate(); '
|
|
f'f.close(); '
|
|
f'print(\'Output\', n+1)'
|
|
f'"'
|
|
)
|
|
|
|
entry = RepeatEntry(script, self.test_id, self.test_timestamp)
|
|
|
|
# Execute multiple times and verify output changes
|
|
entry.update()
|
|
first_output = entry.stdout.strip()
|
|
|
|
entry.update()
|
|
second_output = entry.stdout.strip()
|
|
|
|
entry.update()
|
|
third_output = entry.stdout.strip()
|
|
|
|
# Outputs should be different for each run
|
|
outputs = [first_output, second_output, third_output]
|
|
unique_outputs = set(outputs)
|
|
self.assertEqual(len(unique_outputs), 3)
|
|
self.assertEqual(outputs, ['Output 1', 'Output 2', 'Output 3'])
|
|
|
|
def test_error_recovery(self):
|
|
"""Test that entry recovers from errors in subsequent updates"""
|
|
# Start with a failing command
|
|
entry = RepeatEntry("nonexistentcommand", self.test_id, self.test_timestamp)
|
|
entry.update()
|
|
self.assertNotEqual(entry.exit_code, 0)
|
|
|
|
# Change to a working command
|
|
entry.script = "echo 'test'"
|
|
entry.update()
|
|
|
|
# Verify recovery
|
|
self.assertEqual(entry.exit_code, 0)
|
|
self.assertEqual(entry.stdout.strip(), "test")
|
|
self.assertEqual(entry.stderr, "") |