|
| 1 | +# Azure Event Hubs Client Library Developer Guide |
| 2 | + |
| 3 | +This guide is intended for developers contributing to the Azure Event Hubs Python client library. It provides information on setting up your development environment, running tests, and contributing to the codebase. |
| 4 | + |
| 5 | +## Setting Up Development Environment |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | + |
| 9 | +- Python version [supported by the client library](https://github.com/Azure/azure-sdk-for-python/wiki/Azure-SDKs-Python-version-support-policy) |
| 10 | +- Git |
| 11 | +- pip and setuptools |
| 12 | +- Azure subscription to create Event Hubs resources |
| 13 | + |
| 14 | +### Setup |
| 15 | + |
| 16 | +1. Clone the repository: |
| 17 | + ```bash |
| 18 | + git clone https://github.com/Azure/azure-sdk-for-python.git |
| 19 | + cd azure-sdk-for-python/sdk/eventhub/azure-eventhub |
| 20 | + ``` |
| 21 | + |
| 22 | +2. Create a virtual environment: |
| 23 | + ```bash |
| 24 | + # Linux/macOS |
| 25 | + python -m venv .venv && source .venv/bin/activate && pip install -r dev_requirements.txt |
| 26 | + |
| 27 | + # Windows PowerShell |
| 28 | + python -m venv .venv; .\.venv\Scripts\Activate.ps1; pip install -r dev_requirements.txt |
| 29 | + ``` |
| 30 | + |
| 31 | +3. Install the package in development mode: |
| 32 | + ```bash |
| 33 | + pip install -e . |
| 34 | + ``` |
| 35 | + |
| 36 | + **Note**: If the editable install fails or you encounter import errors, you may need to remove checkpoint store packages first: |
| 37 | + ```bash |
| 38 | + pip uninstall azure-eventhub-checkpointstoreblob azure-eventhub-checkpointstoreblob-aio -y |
| 39 | + pip install -e . |
| 40 | + ``` |
| 41 | + |
| 42 | + Alternatively, you can install the checkpointstoreblob packages, but will need to install the client library in non-editable mode: |
| 43 | + ```bash |
| 44 | + pip install . |
| 45 | + ``` |
| 46 | + Note: You'll need to rerun this command after making changes to the package. |
| 47 | + |
| 48 | + If you encounter import errors when running tests, use the standard installation approach above. |
| 49 | + |
| 50 | +## Running Tests |
| 51 | + |
| 52 | +### Unit Tests |
| 53 | + |
| 54 | +Unit tests don't require any Azure resources: |
| 55 | + |
| 56 | +```bash |
| 57 | +# Run all unit tests |
| 58 | +pytest tests/unittest |
| 59 | + |
| 60 | +# Run specific unit test |
| 61 | +pytest tests/unittest/test_specific_file.py::test_specific_test |
| 62 | +``` |
| 63 | + |
| 64 | +### Live Tests |
| 65 | + |
| 66 | +Live tests require an Azure Event Hubs namespace. Resources are created dynamically for live tests using fixtures in the `conftest.py` file. |
| 67 | + |
| 68 | +1. Login to Azure: |
| 69 | + ```bash |
| 70 | + az login |
| 71 | + ``` |
| 72 | + |
| 73 | +2. Set required environment variables (or create a `.env` file): |
| 74 | + |
| 75 | + The following need to be set to run live tests locally and authenticate with AzureCliCredential: |
| 76 | + ``` |
| 77 | + AZURE_SUBSCRIPTION_ID=<your-subscription-id> |
| 78 | + EVENTHUB_RESOURCE_GROUP=<your-resource-group> |
| 79 | + EVENTHUB_NAMESPACE=<your-namespace> # Only the name, not the full URL |
| 80 | + AZURE_TEST_RUN_LIVE=true |
| 81 | + ``` |
| 82 | + |
| 83 | + If using CLI: |
| 84 | + ``` |
| 85 | + AZURE_TEST_USE_CLI_AUTH=true |
| 86 | + ``` |
| 87 | + |
| 88 | + OR |
| 89 | + |
| 90 | + If using pwsh: |
| 91 | + ``` |
| 92 | + AZURE_TEST_USE_PWSH_AUTH=true |
| 93 | + ``` |
| 94 | + |
| 95 | +3. Run tests: |
| 96 | + ```bash |
| 97 | + # Run a specific test to check if your environment is set up correctly |
| 98 | + pytest tests/livetest/synctests/test_consumer_client.py::test_receive_partition |
| 99 | + |
| 100 | + # Run all live tests |
| 101 | + pytest tests/livetest |
| 102 | + |
| 103 | + # Run with output visible even if tests pass |
| 104 | + pytest -s tests/livetest/synctests/test_specific_file.py::test_specific_function |
| 105 | + ``` |
| 106 | + |
| 107 | +### Common Test Issues |
| 108 | + |
| 109 | +- **Authentication errors**: If you see "The access token is invalid" errors, run `az login` to refresh your authentication. |
| 110 | +- **Resource Not Found**: Ensure your environment variables are correctly set. For `EVENTHUB_NAMESPACE`, only use the name part (without the `.servicebus.windows.net` suffix). |
| 111 | +- **ImportError**: If you see errors like "cannot import name 'EventHubProducerClient' from 'azure.eventhub'", use `pip install .` instead of `pip install -e .` to install azure-eventhub. |
| 112 | +- **PytestUnknownMarkWarning**: You can ignore warnings about "Unknown pytest.mark.livetest" as they're related to the Azure SDK test framework. |
| 113 | +- **asyncio_default_fixture_loop_scope warnings**: These warnings from pytest-asyncio can be ignored. |
| 114 | + |
| 115 | +## Stress Testing |
| 116 | + |
| 117 | +Stress tests help verify reliability and performance under load. They are designed to test the library under extreme conditions and for extended periods. |
| 118 | + |
| 119 | +### Structure of Stress Test Files |
| 120 | + |
| 121 | +Stress test files are typically organized in the `stress` directory with the following structure: |
| 122 | + |
| 123 | +``` |
| 124 | +stress/ |
| 125 | +├── scenarios/ # Contains stress test scenario definitions |
| 126 | +├── scripts/ # Support scripts for stress test deployment |
| 127 | +│ └── dev_requirement.txt # Dependencies for the stress test |
| 128 | +└── templates/ # Kubernetes templates for test deployment |
| 129 | + └── testjob.yaml # Test job configuration |
| 130 | +``` |
| 131 | + |
| 132 | +### Prerequisites |
| 133 | + |
| 134 | +- Docker Desktop |
| 135 | +- Kubernetes CLI (kubectl) |
| 136 | +- PowerShell |
| 137 | +- Helm |
| 138 | + |
| 139 | +### Running Stress Tests |
| 140 | + |
| 141 | +1. Ensure Docker Desktop is running with Kubernetes enabled. |
| 142 | + |
| 143 | +2. Deploy stress tests from the library folder: |
| 144 | + ```powershell |
| 145 | + ..\..\..\eng\common\scripts\stress-testing\deploy-stress-tests.ps1 -Namespace <your-namespace> |
| 146 | + ``` |
| 147 | + |
| 148 | +3. Common kubectl commands: |
| 149 | + ```bash |
| 150 | + # Check on pods |
| 151 | + kubectl get pods -n <your-namespace> |
| 152 | + |
| 153 | + # Delete namespace |
| 154 | + kubectl delete namespace <your-namespace> |
| 155 | + |
| 156 | + # List all active namespaces |
| 157 | + helm list --all-namespaces |
| 158 | + |
| 159 | + # Show logs in console |
| 160 | + kubectl logs -n <your-namespace> <pod-name> |
| 161 | + |
| 162 | + # For init failure |
| 163 | + kubectl logs -n <your-namespace> <pod-name> -c init-azure-deployer |
| 164 | + ``` |
| 165 | + |
| 166 | +4. Testing branch changes: |
| 167 | + - To run stress tests against changes in a specific branch, update the `stress/scripts/dev_requirement.txt` file to replace the library with: |
| 168 | + ``` |
| 169 | + git+<gh-repo-url>@<branch>#subdirectory=<path/to/sdk>&egg=<package-name> |
| 170 | + ``` |
| 171 | + |
| 172 | +5. Monitoring performance: |
| 173 | + - Check the CPU/Memory/errors in the Production Dashboard specified in the Reliability Testing wiki |
| 174 | + - Add `--debug_level Debug` in `stress/templates/testjob.yaml` to output debug logs to the File Share (link available in Reliability Testing wiki) |
| 175 | + |
| 176 | +For complete stress testing requirements and setup, refer to the [Reliability Testing documentation](https://dev.azure.com/azure-sdk/internal/_wiki/wikis/internal.wiki/463/Reliability-Testing). |
| 177 | + |
| 178 | +## Performance Testing |
| 179 | + |
| 180 | +The Performance framework helps measure performance metrics for the client library. |
| 181 | + |
| 182 | +### Running Performance Tests Locally |
| 183 | + |
| 184 | +1. Navigate to the performance test directory: |
| 185 | + ```bash |
| 186 | + cd tests/perfstress |
| 187 | + ``` |
| 188 | + |
| 189 | +2. Review the README file for specific test setup and instructions on running tests: |
| 190 | + ```bash |
| 191 | + cat README.md |
| 192 | + ``` |
| 193 | + |
| 194 | +### Running Performance Tests in Pipelines |
| 195 | + |
| 196 | +- Performance test resources are deployed dynamically and specified in the service level directory's `perf-resources.bicep` file |
| 197 | +- Default parameters are in the service level directory in `perf.yml`, and baseline and source package versions are specified in `perf-tests.yml` |
| 198 | + |
| 199 | +To run performance tests in a pipeline for a PR: |
| 200 | +1. Add a comment to the PR: `/azp run python - eventhub - perf` |
| 201 | +2. To run manually with custom parameters: |
| 202 | + - Find the perf pipelines in azure-sdk internal DevOps pipelines |
| 203 | + - Click Run and set the commit to: `refs/pull/<PR #>/merge` |
| 204 | + |
| 205 | +## Additional Resources |
| 206 | + |
| 207 | +- [Event Hubs Architecture](https://docs.microsoft.com/azure/event-hubs/event-hubs-about) |
| 208 | +- [Azure SDK Design Guidelines](https://azure.github.io/azure-sdk/python_design.html) |
| 209 | +- [Event Hubs Samples](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/eventhub/azure-eventhub/samples) |
0 commit comments