|
| 1 | +# HW_Mgmt_Logger Test Suite 🧪 |
| 2 | + |
| 3 | +A comprehensive, beautiful, and robust test suite for the `HW_Mgmt_Logger` class with colorful output, detailed error reporting, and randomized testing capabilities. |
| 4 | + |
| 5 | +## 🚀 Features |
| 6 | + |
| 7 | +- **Comprehensive Coverage**: Tests all aspects of the HW_Mgmt_Logger class |
| 8 | +- **Beautiful Output**: Colorful terminal output with icons and formatting |
| 9 | +- **Detailed Error Reporting**: Complete error details with input parameters |
| 10 | +- **Randomized Testing**: Configurable random iterations for stress testing |
| 11 | +- **Thread Safety Testing**: Multi-threaded logging verification |
| 12 | +- **Resource Management**: Proper cleanup and resource leak detection |
| 13 | +- **Standalone Executable**: Can be run directly without external dependencies |
| 14 | + |
| 15 | +## 📁 Files |
| 16 | + |
| 17 | +- `test_hw_mgmt_logger.py` - Main comprehensive test suite |
| 18 | +- `run_tests.py` - Easy-to-use test runner with predefined configurations |
| 19 | +- `README.md` - This documentation file |
| 20 | + |
| 21 | +## 🎯 Test Categories |
| 22 | + |
| 23 | +### Functional Tests |
| 24 | +- Basic logger initialization |
| 25 | +- File logging (regular files, stdout, stderr) |
| 26 | +- Syslog logging functionality |
| 27 | +- Log level filtering |
| 28 | +- Message repeat/collapse functionality |
| 29 | +- Parameter validation |
| 30 | +- Unicode and special character handling |
| 31 | +- Resource cleanup verification |
| 32 | + |
| 33 | +### Stress Tests |
| 34 | +- Thread safety verification |
| 35 | +- Randomized message generation |
| 36 | +- Random repeat patterns |
| 37 | +- Edge case handling |
| 38 | + |
| 39 | +### Edge Cases |
| 40 | +- None and empty messages |
| 41 | +- Invalid parameters |
| 42 | +- Directory validation |
| 43 | +- Memory and resource limits |
| 44 | + |
| 45 | +## 🏃♂️ Quick Start |
| 46 | + |
| 47 | +### Method 1: Using the Test Runner (Recommended) |
| 48 | +```bash |
| 49 | +# Quick test (5 random iterations) |
| 50 | +./run_tests.py --quick |
| 51 | + |
| 52 | +# Standard test (10 random iterations) - DEFAULT |
| 53 | +./run_tests.py --standard |
| 54 | + |
| 55 | +# Thorough test (25 random iterations) |
| 56 | +./run_tests.py --thorough |
| 57 | + |
| 58 | +# Stress test (100 random iterations) |
| 59 | +./run_tests.py --stress |
| 60 | + |
| 61 | +# Custom configuration |
| 62 | +./run_tests.py -r 50 -v 2 |
| 63 | +``` |
| 64 | + |
| 65 | +### Method 2: Direct Execution |
| 66 | +```bash |
| 67 | +# Run with default settings (10 random iterations) |
| 68 | +./test_hw_mgmt_logger.py |
| 69 | + |
| 70 | +# Custom random iterations |
| 71 | +./test_hw_mgmt_logger.py -r 25 |
| 72 | + |
| 73 | +# Quiet output |
| 74 | +./test_hw_mgmt_logger.py -v 0 |
| 75 | + |
| 76 | +# Help |
| 77 | +./test_hw_mgmt_logger.py --help |
| 78 | +``` |
| 79 | + |
| 80 | +### Method 3: Python Module |
| 81 | +```bash |
| 82 | +python3 test_hw_mgmt_logger.py --random-iterations 20 --verbosity 2 |
| 83 | +python3 -m unittest test_hw_mgmt_logger -v |
| 84 | +``` |
| 85 | + |
| 86 | +## 📊 Output Examples |
| 87 | + |
| 88 | +### ✅ Successful Test Run |
| 89 | +``` |
| 90 | +================================================================================ |
| 91 | +📝 HW_Mgmt_Logger Comprehensive Test Suite |
| 92 | +================================================================================ |
| 93 | +ℹ️ Random iterations: 10 |
| 94 | +ℹ️ Test verbosity: 2 |
| 95 | +
|
| 96 | +ℹ️ Running: TestHWMgmtLogger.test_basic_initialization |
| 97 | +✅ test_basic_initialization PASSED |
| 98 | +
|
| 99 | +ℹ️ Running: TestHWMgmtLogger.test_file_logging_initialization |
| 100 | +✅ test_file_logging_initialization PASSED |
| 101 | +
|
| 102 | +🎲 Random test iteration 1/10 |
| 103 | +🎲 Random test iteration 2/10 |
| 104 | +... |
| 105 | +
|
| 106 | +================================================================================ |
| 107 | +📝 Test Results Summary |
| 108 | +================================================================================ |
| 109 | +✅ Passed: 45 |
| 110 | +❌ Failed: 0 |
| 111 | +❌ Errors: 0 |
| 112 | +⏭️ Skipped: 0 |
| 113 | +ℹ️ Total: 45 |
| 114 | +ℹ️ Time: 12.34s |
| 115 | +
|
| 116 | +✅ ALL TESTS PASSED! 🎉 |
| 117 | +``` |
| 118 | + |
| 119 | +### ❌ Failed Test with Detailed Error Report |
| 120 | +``` |
| 121 | +❌ test_parameter_validation FAILED |
| 122 | +
|
| 123 | +============================================================ |
| 124 | +FAILURE DETAILS |
| 125 | +============================================================ |
| 126 | +Test: TestHWMgmtLogger.test_parameter_validation |
| 127 | +Input Parameters: |
| 128 | + log_repeat: -1 |
| 129 | + log_file: '/tmp/test.log' |
| 130 | + log_level: 20 |
| 131 | +Error Type: AssertionError |
| 132 | +Error Message: ValueError not raised |
| 133 | +Traceback: |
| 134 | + File "test_hw_mgmt_logger.py", line 245, in test_parameter_validation |
| 135 | + with self.assertRaises(ValueError): |
| 136 | + File "contextlib.py", line 88, in __exit__ |
| 137 | + next(self.gen) |
| 138 | +============================================================ |
| 139 | +``` |
| 140 | + |
| 141 | +## 🔧 Configuration Options |
| 142 | + |
| 143 | +### Command Line Arguments |
| 144 | + |
| 145 | +| Argument | Description | Default | |
| 146 | +|----------|-------------|---------| |
| 147 | +| `-r, --random-iterations` | Number of randomized test iterations | 10 | |
| 148 | +| `-v, --verbosity` | Test output verbosity (0=quiet, 1=normal, 2=verbose) | 2 | |
| 149 | + |
| 150 | +### Test Runner Presets |
| 151 | + |
| 152 | +| Preset | Iterations | Use Case | |
| 153 | +|--------|------------|----------| |
| 154 | +| `--quick` | 5 | Fast development testing | |
| 155 | +| `--standard` | 10 | Regular CI/CD testing | |
| 156 | +| `--thorough` | 25 | Pre-release validation | |
| 157 | +| `--stress` | 100 | Performance and stability testing | |
| 158 | + |
| 159 | +## 🧪 Test Coverage |
| 160 | + |
| 161 | +The test suite covers the following areas: |
| 162 | + |
| 163 | +### Core Functionality ✅ |
| 164 | +- [x] Logger initialization with various parameters |
| 165 | +- [x] File logging (files, stdout, stderr) |
| 166 | +- [x] Syslog integration |
| 167 | +- [x] Log level management |
| 168 | +- [x] Message formatting and encoding |
| 169 | + |
| 170 | +### Advanced Features ✅ |
| 171 | +- [x] Message repeat/collapse functionality |
| 172 | +- [x] Thread safety |
| 173 | +- [x] Resource management and cleanup |
| 174 | +- [x] Unicode and special character support |
| 175 | +- [x] Parameter validation |
| 176 | + |
| 177 | +### Error Handling ✅ |
| 178 | +- [x] Invalid parameter detection |
| 179 | +- [x] File system error handling |
| 180 | +- [x] Syslog error handling |
| 181 | +- [x] Memory and resource limits |
| 182 | + |
| 183 | +### Randomized Testing ✅ |
| 184 | +- [x] Random message generation |
| 185 | +- [x] Random parameter combinations |
| 186 | +- [x] Random repeat patterns |
| 187 | +- [x] Stress testing scenarios |
| 188 | + |
| 189 | +## 🐛 Troubleshooting |
| 190 | + |
| 191 | +### Common Issues |
| 192 | + |
| 193 | +**Import Error**: `Failed to import HW_Mgmt_Logger` |
| 194 | +- Ensure `hw_management_lib.py` is in the correct path |
| 195 | +- Check Python path and module structure |
| 196 | + |
| 197 | +**Permission Error**: Tests fail with permission issues |
| 198 | +- Ensure write permissions in temp directory |
| 199 | +- Run with appropriate user privileges for syslog testing |
| 200 | + |
| 201 | +**Syslog Tests Fail**: Syslog-related tests don't work |
| 202 | +- Syslog daemon may not be running |
| 203 | +- Check system syslog configuration |
| 204 | +- May require root privileges on some systems |
| 205 | + |
| 206 | +### Debug Mode |
| 207 | +Run tests with maximum verbosity to see detailed output: |
| 208 | +```bash |
| 209 | +./test_hw_mgmt_logger.py -v 2 -r 1 |
| 210 | +``` |
| 211 | + |
| 212 | +## 🔄 Continuous Integration |
| 213 | + |
| 214 | +Example CI configuration: |
| 215 | + |
| 216 | +### GitHub Actions |
| 217 | +```yaml |
| 218 | +- name: Run HW_Mgmt_Logger Tests |
| 219 | + run: | |
| 220 | + cd unittest/hw_management_lib/HW_Mgmt_Logger |
| 221 | + ./run_tests.py --thorough |
| 222 | +``` |
| 223 | +
|
| 224 | +### Jenkins |
| 225 | +```groovy |
| 226 | +stage('HW_Mgmt_Logger Tests') { |
| 227 | + steps { |
| 228 | + dir('unittest/hw_management_lib/HW_Mgmt_Logger') { |
| 229 | + sh './run_tests.py --standard' |
| 230 | + } |
| 231 | + } |
| 232 | +} |
| 233 | +``` |
| 234 | + |
| 235 | +## 📈 Performance Benchmarks |
| 236 | + |
| 237 | +Typical execution times on modern hardware: |
| 238 | + |
| 239 | +| Test Type | Iterations | Duration | Tests Run | |
| 240 | +|-----------|------------|----------|-----------| |
| 241 | +| Quick | 5 | ~5s | ~25 tests | |
| 242 | +| Standard | 10 | ~10s | ~35 tests | |
| 243 | +| Thorough | 25 | ~25s | ~55 tests | |
| 244 | +| Stress | 100 | ~90s | ~155 tests | |
| 245 | + |
| 246 | +## 🤝 Contributing |
| 247 | + |
| 248 | +To add new tests: |
| 249 | + |
| 250 | +1. Add test methods to existing test classes |
| 251 | +2. Follow naming convention: `test_descriptive_name` |
| 252 | +3. Use `_store_test_params()` for error reporting |
| 253 | +4. Include both positive and negative test cases |
| 254 | +5. Add appropriate assertions and cleanup |
| 255 | + |
| 256 | +## 📄 License |
| 257 | + |
| 258 | +Same license as the parent project - Dual BSD/GPL License. |
| 259 | + |
| 260 | +--- |
| 261 | + |
| 262 | +*Created with ❤️ for robust hardware management logging testing* |
0 commit comments