162 lines
5.8 KiB
Python
162 lines
5.8 KiB
Python
import unittest
|
|
from pathlib import Path
|
|
import os
|
|
|
|
from sia.entry.repeat_entry import RepeatEntry
|
|
|
|
class RepeatEntryTest(unittest.TestCase):
|
|
def setUp(self):
|
|
"""Set up test cases with fixed id"""
|
|
self.test_id = "test-id-1234"
|
|
self.work_dir = Path(os.getcwd()) # Use current directory for tests
|
|
|
|
# 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(self.test_id, self.work_dir, script, None, None)
|
|
|
|
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.assertFalse(entry.timed_out)
|
|
|
|
def test_successful_execution(self):
|
|
"""Test successful script execution"""
|
|
script = "echo 'test'"
|
|
entry = RepeatEntry(self.test_id, self.work_dir, script, None, None)
|
|
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(self.test_id, self.work_dir, "nonexistentcommand", None, None)
|
|
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(self.test_id, self.work_dir, script, None, None)
|
|
|
|
# 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(self.test_id, self.work_dir, script, None, None)
|
|
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, script)
|
|
self.assertEqual(element.get("exit_code"), "0")
|
|
stdout_text = element.find("stdout").text
|
|
self.assertEqual(stdout_text, "test\n")
|
|
self.assertEqual(element.find("stderr").text, "")
|
|
|
|
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(self.test_id, self.work_dir, script, None, None)
|
|
|
|
# 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_serialize(self):
|
|
"""Test serialization of the entry"""
|
|
script = "echo 'test'"
|
|
entry = RepeatEntry(self.test_id, self.work_dir, script, None, None)
|
|
|
|
# Check initial serialized state
|
|
serialized = entry.serialize()
|
|
self.assertEqual(serialized["type"], "repeat")
|
|
self.assertEqual(serialized["id"], self.test_id)
|
|
self.assertEqual(serialized["script"], script)
|
|
self.assertEqual(serialized["timed_out"], False)
|
|
|
|
# Update and check updated serialized state
|
|
entry.update()
|
|
serialized = entry.serialize()
|
|
self.assertEqual(serialized["exit_code"], 0)
|
|
self.assertEqual(serialized["stdout"].strip(), "test") |