Testing Strategy (RSpec)
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The ManageIQ project utilizes RSpec as its primary testing framework to ensure the stability and correctness of its modular architecture. The testing strategy encompasses unit tests for models, integration tests for cross-component workflows, and specialized testing for its extensive plugin system. The environment is configured to maintain strict isolation between tests while providing shared helpers for complex cloud and infrastructure management objects.
The strategy emphasizes database integrity through transactional fixtures, automated seeding of core requirements like MiqRegion, and the use of VCR for recording and replaying HTTP interactions. This ensures that tests are both performant and reproducible across different development and CI environments.
Test Environment Configuration
The RSpec environment is bootstrapped via spec_helper.rb, which configures core behaviors such as mocking, database cleaning, and coverage reporting.
Global Hooks and Lifecycle
The test suite utilizes several lifecycle hooks to maintain a consistent state:
- Before Suite: Seeds mandatory database records, such as
MiqRegion.seed. - After Suite: Cleans up global records like
MiqRegion.delete_all. - Around Each: Clears caches via
EvmSpecHelper.clear_cachesto prevent state leakage between examples. - Database Isolation: Employs
use_transactional_fixtures = trueto wrap every test in a database transaction that is rolled back after completion.
Sources: spec/spec_helper.rb:25-75
Integrity and Contamination Prevention
To prevent "test contamination" (where tests accidentally modify global state in a way that affects subsequent tests), the project includes a specialized TestContamination setup. This fails tests that attempt to include modules or define constants in the main Ruby namespace.
Sources: spec/spec_helper.rb:14-16, spec/support/test_contamination.rb
Plugin and Modular Testing
ManageIQ's architecture relies heavily on engines and plugins. The testing strategy includes specific validation for plugin discovery and behavior.
Plugin Verification
Tests in spec/lib/vmdb/plugins_spec.rb verify that the system correctly identifies core versus plugin classes. This ensures that the engine-based architecture properly registers providers (like Vmware or Amazon) and UI components.
The system validates that every plugin implements necessary methods like .plugin_name and tracks versions through Git SHAs or Bundler specifications.
Sources: spec/lib/vmdb/plugins_spec.rb:1-150
Model and Service Testing Patterns
Model tests form the bulk of the test suite, focusing on validations, associations, and virtual attributes.
User and RBAC Testing
User model tests focus on complex authentication scenarios, including LDAP authorization, OIDC metadata processing, and Role-Based Access Control (RBAC) across regions.
- Failed Logins: Validates account locking logic and the queuing of unlock tasks.
- Cross-Region Access: Tests methods like
.in_all_regionsto ensure user identity is maintained across multi-region deployments.
Sources: spec/models/user_spec.rb:100-350
Multi-Tenancy and Timezones
Testing ensures that current user contexts (Tenant and Timezone) are correctly scoped within code blocks using User.with_user and User.with_my_timezone.
Sources: spec/models/user_spec.rb:380-450
Automation and Asynchronous Tasks
Many ManageIQ features are asynchronous, relying on the MiqQueue and MiqTask systems. Testing these involves verifying that methods correctly queue work rather than executing it immediately.
Queueing Logic
The MiqWidget and MiqSchedule tests provide examples of verifying that background tasks are correctly created with appropriate roles and timeouts.
| Component | Test Focus | Description |
|---|---|---|
| MiqQueue | Task parameters | Verifies role (e.g., reporting), zone, and method name. |
| MiqTask | State tracking | Ensures tasks move from Queued to Active to Finished. |
| MiqSchedule | Interval Calculation | Tests next_interval_time for daily, hourly, and monthly triggers. |
Sources: spec/models/miq_widget_spec.rb:300-450, spec/models/miq_schedule_spec.rb:200-250
External Interaction Handling
To keep tests fast and deterministic, external service calls are handled via:
- VCR: Records and plays back HTTP/REST interactions, stored in
spec/vcr_cassettes. - Mocks/Doubles: Standard RSpec mocks are used to simulate service responses, with a configuration that forbids message expectations on
nilobjects to catch potential bugs.
Sources: spec/spec_helper.rb:21-24, spec/spec_helper.rb:81-90
Setup and CI Integration
The project provides environment setup scripts in lib/manageiq/environment.rb to prepare the database and assets for testing.
| Rake Task | Purpose |
|---|---|
test:vmdb:setup | Full database and environment reset for tests. |
db:migrate | Ensures test schema is up to date. |
evm:compile_assets | Prepares UI assets for integration tests. |
Sources: lib/manageiq/environment.rb:100-130
Conclusion
The ManageIQ RSpec strategy is designed to support a massive, plugin-oriented Rails application. By combining strict global isolation, automated seeding, and robust helpers for complex system objects like Tasks and Regions, the project maintains a reliable environment for both local development and large-scale CI pipelines.