|
| 1 | +""" |
| 2 | +Test for installation review log message fix |
| 3 | +
|
| 4 | +This test verifies that the log message accurately reflects the actual |
| 5 | +configuration value for enable_installation_review instead of hardcoding 'false'. |
| 6 | +""" |
| 7 | +import sys |
| 8 | +import os |
| 9 | +import logging |
| 10 | +from io import StringIO |
| 11 | + |
| 12 | +# Add the app directory to Python path |
| 13 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'ha_sentry', 'rootfs', 'app')) |
| 14 | + |
| 15 | +from config_manager import ConfigManager |
| 16 | +from sentry_service import SentryService |
| 17 | + |
| 18 | + |
| 19 | +def test_log_message_shows_true_value(): |
| 20 | + """Test that the log message correctly shows enable_installation_review=True when it's enabled""" |
| 21 | + |
| 22 | + print("Testing log message when enable_installation_review=True...") |
| 23 | + |
| 24 | + # Set up test environment with installation review ENABLED |
| 25 | + os.environ['AI_ENABLED'] = 'false' |
| 26 | + os.environ['SUPERVISOR_TOKEN'] = 'test_token' |
| 27 | + os.environ['ENABLE_INSTALLATION_REVIEW'] = 'true' |
| 28 | + os.environ['INSTALLATION_REVIEW_SCHEDULE'] = 'weekly' |
| 29 | + |
| 30 | + # Capture logs |
| 31 | + log_capture = StringIO() |
| 32 | + handler = logging.StreamHandler(log_capture) |
| 33 | + handler.setLevel(logging.DEBUG) |
| 34 | + logger = logging.getLogger('sentry_service') |
| 35 | + logger.addHandler(handler) |
| 36 | + logger.setLevel(logging.DEBUG) |
| 37 | + |
| 38 | + config = ConfigManager() |
| 39 | + service = SentryService(config) |
| 40 | + |
| 41 | + # Verify configuration is actually enabled |
| 42 | + assert config.enable_installation_review is True, "Configuration should be enabled" |
| 43 | + print(f"✓ Configuration correctly set: enable_installation_review={config.enable_installation_review}") |
| 44 | + |
| 45 | + # Call the method that produces the log message |
| 46 | + # Since the feature is enabled, this should not produce the "disabled" log |
| 47 | + result = service._should_run_installation_review() |
| 48 | + |
| 49 | + # Get the log output |
| 50 | + log_output = log_capture.getvalue() |
| 51 | + |
| 52 | + # When the feature is enabled and it's the first run, it should return True |
| 53 | + # and should NOT log the "disabled" message |
| 54 | + assert result is True, "Should return True for first run with feature enabled" |
| 55 | + assert "Feature is disabled" not in log_output, "Should not log 'disabled' message when feature is enabled" |
| 56 | + |
| 57 | + print("✓ No 'disabled' message logged when feature is enabled") |
| 58 | + print(f" _should_run_installation_review returned: {result}") |
| 59 | + |
| 60 | + # Clean up |
| 61 | + logger.removeHandler(handler) |
| 62 | + |
| 63 | + return True |
| 64 | + |
| 65 | + |
| 66 | +def test_log_message_shows_false_value(): |
| 67 | + """Test that the log message correctly shows enable_installation_review=False when it's disabled""" |
| 68 | + |
| 69 | + print("Testing log message when enable_installation_review=False...") |
| 70 | + |
| 71 | + # Set up test environment with installation review DISABLED |
| 72 | + os.environ['AI_ENABLED'] = 'false' |
| 73 | + os.environ['SUPERVISOR_TOKEN'] = 'test_token' |
| 74 | + os.environ['ENABLE_INSTALLATION_REVIEW'] = 'false' |
| 75 | + os.environ['INSTALLATION_REVIEW_SCHEDULE'] = 'weekly' |
| 76 | + |
| 77 | + # Capture logs |
| 78 | + log_capture = StringIO() |
| 79 | + handler = logging.StreamHandler(log_capture) |
| 80 | + handler.setLevel(logging.DEBUG) |
| 81 | + logger = logging.getLogger('sentry_service') |
| 82 | + logger.addHandler(handler) |
| 83 | + logger.setLevel(logging.DEBUG) |
| 84 | + |
| 85 | + config = ConfigManager() |
| 86 | + service = SentryService(config) |
| 87 | + |
| 88 | + # Verify configuration is actually disabled |
| 89 | + assert config.enable_installation_review is False, "Configuration should be disabled" |
| 90 | + print(f"✓ Configuration correctly set: enable_installation_review={config.enable_installation_review}") |
| 91 | + |
| 92 | + # Call the method that produces the log message |
| 93 | + result = service._should_run_installation_review() |
| 94 | + |
| 95 | + # Get the log output |
| 96 | + log_output = log_capture.getvalue() |
| 97 | + |
| 98 | + # When the feature is disabled, it should return False and log the disabled message |
| 99 | + assert result is False, "Should return False when feature is disabled" |
| 100 | + assert "Feature is disabled" in log_output, "Should log 'disabled' message when feature is disabled" |
| 101 | + |
| 102 | + # The critical fix: ensure the log message shows the actual value (False) |
| 103 | + assert "enable_installation_review=False" in log_output, \ |
| 104 | + "Log message should show actual value 'enable_installation_review=False'" |
| 105 | + |
| 106 | + print("✓ Correct 'disabled' message logged with actual value") |
| 107 | + print(f" Log message contains: 'enable_installation_review=False'") |
| 108 | + print(f" _should_run_installation_review returned: {result}") |
| 109 | + |
| 110 | + # Clean up |
| 111 | + logger.removeHandler(handler) |
| 112 | + |
| 113 | + return True |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + print("Running Installation Review Log Message Tests...\n") |
| 118 | + |
| 119 | + tests = [ |
| 120 | + test_log_message_shows_true_value, |
| 121 | + test_log_message_shows_false_value |
| 122 | + ] |
| 123 | + |
| 124 | + passed = 0 |
| 125 | + failed = 0 |
| 126 | + |
| 127 | + for test in tests: |
| 128 | + try: |
| 129 | + if test(): |
| 130 | + passed += 1 |
| 131 | + else: |
| 132 | + failed += 1 |
| 133 | + except Exception as e: |
| 134 | + print(f"✗ Test failed with exception: {e}") |
| 135 | + import traceback |
| 136 | + traceback.print_exc() |
| 137 | + failed += 1 |
| 138 | + print() |
| 139 | + |
| 140 | + print(f"{'='*50}") |
| 141 | + print(f"Tests completed: {passed} passed, {failed} failed") |
| 142 | + print(f"{'='*50}") |
| 143 | + |
| 144 | + sys.exit(0 if failed == 0 else 1) |
0 commit comments