Skip to content

Fix MongoDB 6.0+ Compatibility and Add Comprehensive Test Coverage - #265

Merged
comtihon merged 1 commit into
comtihon:masterfrom
raxdcx:fix-auto-detect-protocol-bson-bug
Dec 16, 2025
Merged

Fix MongoDB 6.0+ Compatibility and Add Comprehensive Test Coverage#265
comtihon merged 1 commit into
comtihon:masterfrom
raxdcx:fix-auto-detect-protocol-bson-bug

Conversation

@samwar

@samwar samwar commented Dec 1, 2025

Copy link
Copy Markdown
Contributor

Fix MongoDB 6.0+ Compatibility and Add Comprehensive Test Coverage

Overview

This PR fixes critical BSON document formatting issues preventing the mongodb-erlang driver from working with MongoDB 6.0+, adds comprehensive test coverage including property-based testing, and establishes authentication testing infrastructure.

Problem

MongoDB 6.0+ requires proper BSON document formatting and the OP_MSG protocol. The driver had multiple issues with incorrect tuple formats instead of proper BSON documents, causing crashes during connection initialization, command execution, and index operations.

Solution

Core Fixes (7 critical BSON formatting fixes)

  1. Protocol Detection - Fixed hello command format in connection initialization
  2. OP_MSG Commands - Proper $db field handling and error response processing
  3. Read Preferences - Wrapped in bson:document() for correct format
  4. Index Creation - Changed from tuple to list format with proper wrapping
  5. Server Monitoring - Fixed isMaster/hello commands for topology management
  6. Write Concerns - Added type checking for command_doc field
  7. Index API - Fixed ensure_index for op_msg protocol

Additional Improvements

  • Fixed negative batchsize handling (singleBatch field)
  • Added automatic tuple-to-map normalization for MongoDB 6.0+
  • Improved error handling with try/catch blocks
  • Fixed auth_source type conversion (atom → binary)

Test Coverage (73 tests, +26 new)

New Test Suites:

  • protocol_detection_SUITE (8 tests) - Protocol auto-detection
  • bson_format_SUITE (7 tests) - BSON document operations
  • error_handling_SUITE (6 tests) - Connection and error scenarios
  • property_SUITE (5 properties, 240 cases) - Property-based testing with PropEr
  • query_conversion_SUITE (7 tests) - Query to OP_MSG conversion
  • batchsize_behavior_SUITE (8 tests) - Integration tests for batchsize
  • auth_SUITE (5 tests) - Authentication scenarios

Enhanced Existing Tests:

  • Added authentication fallback to mc_worker_api_SUITE, mongo_api_SUITE, switch_db_SUITE
  • Fixed tuple_normalization_SUITE by exporting normalize_map_values/1

Authentication Testing Infrastructure

Scripts:

  • start_mongo_auth.sh - Auth MongoDB on port 27021
  • start_mongo_single_node.sh - Single node on port 27017
  • start_mongo_cluster.sh - Replica set on ports 27018-27020

Docker:

  • docker-compose-auth.yml - Local auth testing
  • mongo-init-auth.js - Test user creation

CI/CD Updates

  • Updated GitHub Actions test matrix (6 combinations: Erlang 24/25/26 × MongoDB 5.0/6.0/7.0)
  • Fixed netcat package for Debian Bookworm compatibility
  • Added artifact uploads for logs on failure
  • Updated coverage workflow to match test matrix

Test Results

All 73 tests passing (68 existing + 5 new auth tests + 240 property test cases)
3 tests skipped (srv_connect_SUITE - requires MongoDB Atlas)

Compatibility

Erlang MongoDB 5.0 MongoDB 6.0 MongoDB 7.0
24 N/A
25 N/A
26 N/A

Protocols: OP_MSG (auto-detected) + Legacy (backward compatible)

Breaking Changes

None. All changes maintain backward compatibility.

Impact

The driver now:

  • ✅ Works with MongoDB 6.0+ and 7.0
  • ✅ Auto-detects and uses OP_MSG protocol
  • ✅ Handles authentication correctly
  • ✅ Maintains backward compatibility with MongoDB 5.0
  • ✅ Has comprehensive test coverage with property-based testing
  • ✅ Runs CI across multiple Erlang/MongoDB version combinations

@samwar
samwar force-pushed the fix-auto-detect-protocol-bson-bug branch 7 times, most recently from ba7296d to 6a9cfc9 Compare December 1, 2025 22:03
@samwar

samwar commented Dec 1, 2025

Copy link
Copy Markdown
Contributor Author

@comtihon - I found a few errors with the driver when I started to use it with MongoDB 6. Please take a look at this and merge when ready.

@samwar
samwar marked this pull request as ready for review December 1, 2025 22:11
@samwar
samwar marked this pull request as draft December 2, 2025 02:44
@samwar
samwar force-pushed the fix-auto-detect-protocol-bson-bug branch 5 times, most recently from 8427baf to 41fd368 Compare December 2, 2025 04:03
@comtihon

comtihon commented Dec 2, 2025

Copy link
Copy Markdown
Owner

hi @samwar , thank you very much for the contribution.
I see that test_command_requires_bson_not_map is failing. Looks like we don't need it anymore, can you please remove it?

@samwar

samwar commented Dec 2, 2025

Copy link
Copy Markdown
Contributor Author

@comtihon yeah this is a small work in progress. i'll get everything fixed up and ready and then bring it out of draft status.

@samwar
samwar force-pushed the fix-auto-detect-protocol-bson-bug branch from 845d7f7 to 4655ed9 Compare December 3, 2025 06:22
@samwar samwar changed the title Fix BSON document creation for MongoDB 6.0+ compatibility and add comprehensive tests Fix MongoDB 6.0+ Compatibility and Add Comprehensive Test Coverage Dec 3, 2025
This commit fixes critical BSON document formatting issues that prevented
the mongodb-erlang driver from working with MongoDB 6.0+, adds comprehensive
test coverage including property-based testing, and establishes authentication
testing infrastructure.

Problem:
MongoDB 6.0+ requires proper BSON document formatting and the OP_MSG protocol.
The driver had multiple issues with incorrect tuple formats instead of proper
BSON documents, causing crashes during connection initialization, command
execution, and index operations.

Core BSON Formatting Fixes (7 critical fixes):

* mc_worker_pid_info.erl: Fixed protocol detection hello command format
  - Changed from bson:fields({hello, 1}) to bson:document([{hello, 1}])

* mongo_protocol.erl: Fixed OP_MSG command construction
  - Added conditional handling for command_doc field (tuple vs list)
  - Properly adds $db field using bson:append or list concatenation
  - Fixed OP_MSG error response handling for authorization errors (code 13)

* mc_connection_man.erl: Fixed read preference handling
  - Wrapped read preference in bson:document() for proper BSON format

* mc_worker_logic.erl: Fixed index creation
  - Changed from tuple to list format: {<<"name">>, ...} → [{<<"name">>, ...}]
  - Wrapped with bson:document() before merging

* mc_monitor.erl: Fixed server monitoring commands
  - Fixed isMaster/hello commands to use bson:document() format
  - Ensures topology management works correctly

* mc_worker.erl: Fixed write concern handling
  - Added type checking to handle both tuple and list formats for command_doc

* mc_worker_api.erl: Fixed index API for op_msg protocol
  - Wrapped IndexSpec in array for createIndexes command
  - Changed return value to 'ok' on success for consistency

Additional Protocol Fixes:

* Fixed negative batchsize handling (singleBatch field) in OP_MSG protocol
* Added automatic tuple-to-map normalization for MongoDB 6.0+ compatibility
* Proper handling of maps with nested maps in query_to_op_msg_cmd
* Convert auth_source to binary if provided as atom
* Exported normalize_map_values/1 for testing

Test Coverage (73 tests total, +26 new):

New Test Suites:
* protocol_detection_SUITE (8 tests) - Protocol auto-detection and selection
* bson_format_SUITE (7 tests) - BSON document operations and validation
* error_handling_SUITE (6 tests) - Connection failures and error scenarios
* property_SUITE (5 properties, 240 cases) - Property-based testing with PropEr
  - BSON round-trip, append associativity, ping idempotency, insert/find
* query_conversion_SUITE (7 tests) - Query to OP_MSG conversion validation
* batchsize_behavior_SUITE (8 tests) - Integration tests for batchsize behavior
* auth_SUITE (5 tests) - Authentication success, failure, and operations

Enhanced Existing Tests:
* Added authentication fallback to mc_worker_api_SUITE, mongo_api_SUITE,
  switch_db_SUITE for Jenkins compatibility
* Fixed tuple_normalization_SUITE by exporting normalize_map_values/1
* Updated bson_format_SUITE to reflect that maps are now accepted

Authentication Testing Infrastructure:

Scripts:
* scripts/start_mongo_auth.sh - Starts MongoDB with auth on port 27021
* scripts/start_mongo_single_node.sh - Single node on port 27017
* scripts/start_mongo_cluster.sh - Replica set on ports 27018-27020

Docker Support:
* docker-compose-auth.yml - Local auth testing on port 27021
* test/mongo-init-auth.js - Creates test users (admin, testuser)

Port Allocation:
* 27017: Single node MongoDB (most tests)
* 27018-27020: Replica set cluster (3 nodes)
* 27021: Authentication-only MongoDB instance

CI/CD Improvements:

* Updated GitHub Actions test matrix (6 combinations):
  - Erlang 24 & 25: MongoDB 5.0, 6.0 (4 combinations)
  - Erlang 26: MongoDB 6.0, 7.0 (2 combinations)
* Fixed netcat package for Debian Bookworm (netcat → netcat-openbsd)
* Added artifact uploads for replica set logs and CT logs on failure
* Updated coverage workflow to match test matrix

Debug and Error Handling:

* Added try/catch blocks for better error reporting in critical paths
* Filtered hello commands from debug logging to reduce log spam
* Added validation checks before BSON operations
* Improved error messages for debugging BSON conversion issues

Test Results:
✅ All 73 tests passing (68 existing + 5 new auth tests)
✅ 240 property-based test cases across 5 properties
✅ 3 tests skipped (srv_connect_SUITE - requires MongoDB Atlas)

Compatibility:
✅ MongoDB 5.0.2, 6.0.16, 7.0.12
✅ Erlang/OTP 24, 25, 26
✅ OP_MSG protocol (auto-detected) + Legacy protocol (backward compatible)

Breaking Changes: None

Impact:
The driver now successfully connects to and communicates with MongoDB 6.0+
servers, properly detects and uses the OP_MSG protocol, handles authentication
correctly, maintains backward compatibility with MongoDB 5.0, and has
comprehensive test coverage with property-based testing.
@samwar
samwar force-pushed the fix-auto-detect-protocol-bson-bug branch from 4655ed9 to 79d042d Compare December 3, 2025 18:21
@samwar
samwar marked this pull request as ready for review December 3, 2025 18:30
@samwar

samwar commented Dec 4, 2025

Copy link
Copy Markdown
Contributor Author

@comtihon this is good to go.

@samwar

samwar commented Dec 8, 2025

Copy link
Copy Markdown
Contributor Author

@comtihon - are you able to merge this PR?

@comtihon
comtihon merged commit d62bf62 into comtihon:master Dec 16, 2025
13 checks passed
@comtihon

Copy link
Copy Markdown
Owner

thank you very much for your contribution!

@samwar

samwar commented Dec 18, 2025

Copy link
Copy Markdown
Contributor Author

@comtihon you need to make a new tag and release now :)

@comtihon

Copy link
Copy Markdown
Owner

Just did it. Thank you very much again and wish you happy holidays!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants