-
Notifications
You must be signed in to change notification settings - Fork 33
Add admin command tests for logRotate #642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vic-tsang
wants to merge
1
commit into
documentdb:main
Choose a base branch
from
vic-tsang:admin/commands/logRotate/test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
92 changes: 92 additions & 0 deletions
92
...ity/tests/system/administration/commands/logRotate/test_logRotate_bson_type_validation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """Tests for logRotate BSON type validation. | ||
|
|
||
| The value field accepts numeric/bool types and the string "server"; other | ||
| types are rejected with TypeMismatch. The comment field accepts all types. | ||
| """ | ||
|
|
||
| import pytest | ||
|
|
||
| from documentdb_tests.framework.assertions import assertFailureCode, assertSuccessPartial | ||
| from documentdb_tests.framework.bson_type_validator import ( | ||
| BsonType, | ||
| BsonTypeTestCase, | ||
| generate_bson_acceptance_test_cases, | ||
| generate_bson_rejection_test_cases, | ||
| ) | ||
| from documentdb_tests.framework.error_codes import FILE_RENAME_FAILED_ERROR, TYPE_MISMATCH_ERROR | ||
| from documentdb_tests.framework.executor import ( | ||
| execute_admin_command, | ||
| execute_admin_with_retry_command, | ||
| ) | ||
|
|
||
| pytestmark = [pytest.mark.admin, pytest.mark.no_parallel] | ||
|
|
||
|
|
||
| LOG_ROTATE_VALUE_PARAMS = [ | ||
| BsonTypeTestCase( | ||
| id="logRotate_value", | ||
| msg="logRotate value should accept numeric, bool, and the 'server' string", | ||
| keyword="logRotate", | ||
| valid_types=[ | ||
| BsonType.INT, | ||
| BsonType.DOUBLE, | ||
| BsonType.LONG, | ||
| BsonType.BOOL, | ||
| BsonType.DECIMAL, | ||
| BsonType.STRING, | ||
| ], | ||
| valid_inputs={BsonType.STRING: "server"}, | ||
| default_error_code=TYPE_MISMATCH_ERROR, | ||
| ), | ||
| ] | ||
|
|
||
| COMMENT_PARAMS = [ | ||
| BsonTypeTestCase( | ||
| id="comment", | ||
| msg="logRotate should accept all BSON types for the comment field", | ||
| keyword="comment", | ||
| valid_types=list(BsonType), | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| LOG_ROTATE_VALUE_ACCEPTANCE = generate_bson_acceptance_test_cases(LOG_ROTATE_VALUE_PARAMS) | ||
| LOG_ROTATE_VALUE_REJECTIONS = generate_bson_rejection_test_cases(LOG_ROTATE_VALUE_PARAMS) | ||
| COMMENT_ACCEPTANCE = generate_bson_acceptance_test_cases(COMMENT_PARAMS) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("bson_type,sample_value,spec", LOG_ROTATE_VALUE_ACCEPTANCE) | ||
| def test_logRotate_value_bson_type_accepted(collection, bson_type, sample_value, spec): | ||
| """Test logRotate accepts numeric, bool, and the 'server' string value.""" | ||
| result = execute_admin_with_retry_command( | ||
| collection, {"logRotate": sample_value}, retry_code=FILE_RENAME_FAILED_ERROR | ||
| ) | ||
| assertSuccessPartial( | ||
| result, | ||
| {"ok": 1.0}, | ||
| msg=f"{spec.msg} (bson_type={bson_type.value})", | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("bson_type,sample_value,spec", COMMENT_ACCEPTANCE) | ||
| def test_logRotate_comment_bson_type_accepted(collection, bson_type, sample_value, spec): | ||
| """Test comment field accepts all BSON types.""" | ||
| result = execute_admin_with_retry_command( | ||
| collection, {"logRotate": 1, "comment": sample_value}, retry_code=FILE_RENAME_FAILED_ERROR | ||
| ) | ||
| assertSuccessPartial( | ||
| result, | ||
| {"ok": 1.0}, | ||
| msg=f"comment should accept {bson_type.value}", | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("bson_type,sample_value,spec", LOG_ROTATE_VALUE_REJECTIONS) | ||
| def test_logRotate_value_bson_type_rejected(collection, bson_type, sample_value, spec): | ||
| """Test logRotate value rejects non-numeric, non-string BSON types.""" | ||
| result = execute_admin_command(collection, {"logRotate": sample_value}) | ||
| assertFailureCode( | ||
| result, | ||
| spec.expected_code(bson_type), | ||
| msg=f"logRotate should reject {bson_type.value} for the command value", | ||
| ) | ||
74 changes: 74 additions & 0 deletions
74
...sts/compatibility/tests/system/administration/commands/logRotate/test_logRotate_errors.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| """Tests for logRotate command error cases.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from documentdb_tests.compatibility.tests.system.administration.utils.admin_test_case import ( | ||
| AdminTestCase, | ||
| ) | ||
| from documentdb_tests.framework.assertions import assertFailureCode | ||
| from documentdb_tests.framework.error_codes import ( | ||
| NO_SUCH_KEY_ERROR, | ||
| UNAUTHORIZED_ERROR, | ||
| UNRECOGNIZED_COMMAND_FIELD_ERROR, | ||
| ) | ||
| from documentdb_tests.framework.executor import execute_admin_command, execute_command | ||
| from documentdb_tests.framework.parametrize import pytest_params | ||
|
|
||
| pytestmark = [pytest.mark.admin, pytest.mark.no_parallel] | ||
|
|
||
|
|
||
| INVALID_STRING_TESTS: list[AdminTestCase] = [ | ||
| AdminTestCase( | ||
| "invalid_string", | ||
| command={"logRotate": "invalid"}, | ||
| error_code=NO_SUCH_KEY_ERROR, | ||
| msg="Should reject invalid string value", | ||
| ), | ||
| AdminTestCase( | ||
| "empty_string", | ||
| command={"logRotate": ""}, | ||
| error_code=NO_SUCH_KEY_ERROR, | ||
| msg="Should reject empty string", | ||
| ), | ||
| AdminTestCase( | ||
| "case_sensitive_SERVER", | ||
| command={"logRotate": "SERVER"}, | ||
| error_code=NO_SUCH_KEY_ERROR, | ||
| msg="Should reject uppercase SERVER (case-sensitive)", | ||
| ), | ||
| AdminTestCase( | ||
| "case_sensitive_Audit", | ||
| command={"logRotate": "Audit"}, | ||
| error_code=NO_SUCH_KEY_ERROR, | ||
| msg="Should reject mixed-case Audit (case-sensitive)", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test", pytest_params(INVALID_STRING_TESTS)) | ||
| def test_logRotate_invalid_arguments(collection, test): | ||
| """Test that logRotate rejects invalid string values.""" | ||
| result = execute_admin_command(collection, test.command) | ||
| assertFailureCode(result, test.error_code, msg=test.msg) | ||
|
|
||
|
|
||
| def test_logRotate_unrecognized_field(collection): | ||
| """Test that logRotate rejects unrecognized top-level fields.""" | ||
| result = execute_admin_command(collection, {"logRotate": 1, "unknownField": 1}) | ||
| assertFailureCode( | ||
| result, UNRECOGNIZED_COMMAND_FIELD_ERROR, msg="Should reject unrecognized fields" | ||
| ) | ||
|
|
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing: does an unrecognized field still fail when a valid comment is also present? |
||
| def test_logRotate_non_admin_database(collection): | ||
| """Test that logRotate fails when run on a non-admin database.""" | ||
| result = execute_command(collection, {"logRotate": 1}) | ||
| assertFailureCode(result, UNAUTHORIZED_ERROR, msg="Should fail on non-admin database") | ||
|
|
||
|
|
||
| def test_logRotate_audit_target_rejected_when_disabled(collection): | ||
| """Test that the 'audit' log target is rejected when auditing is disabled.""" | ||
| result = execute_admin_command(collection, {"logRotate": "audit"}) | ||
| assertFailureCode( | ||
| result, NO_SUCH_KEY_ERROR, msg="Should reject audit target when auditing is disabled" | ||
| ) | ||
37 changes: 37 additions & 0 deletions
37
...ibility/tests/system/administration/commands/logRotate/test_logRotate_value_acceptance.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| """Tests for logRotate acceptance of specific scalar values. | ||
|
|
||
| Covers values the shared BSON-type harness does not sample: boolean `false`, `0`, | ||
| and negative integers/longs (it only feeds `True` and `INT32_MAX`/`INT64_MAX`). | ||
| Each rotation goes through `execute_admin_with_retry_command`, which retries past the | ||
| transient FileRenameFailed so the test can assert a clean success. | ||
| """ | ||
|
|
||
| import pytest | ||
| from bson.int64 import Int64 | ||
|
|
||
| from documentdb_tests.framework.assertions import assertSuccessPartial | ||
| from documentdb_tests.framework.error_codes import FILE_RENAME_FAILED_ERROR | ||
| from documentdb_tests.framework.executor import execute_admin_with_retry_command | ||
|
|
||
| pytestmark = [pytest.mark.admin, pytest.mark.no_parallel] | ||
|
|
||
|
|
||
| def test_logRotate_value_bool_false_accepted(collection): | ||
| """Test logRotate accepts boolean false (the BSON sample only covers true).""" | ||
| result = execute_admin_with_retry_command( | ||
| collection, {"logRotate": False}, retry_code=FILE_RENAME_FAILED_ERROR | ||
| ) | ||
| assertSuccessPartial(result, {"ok": 1.0}, msg="logRotate value should accept boolean false") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "value", | ||
| [0, -1, Int64(-5)], | ||
| ids=["zero", "negative_int", "negative_long"], | ||
| ) | ||
| def test_logRotate_value_zero_and_negative_accepted(collection, value): | ||
| """Test logRotate accepts 0 and negative integers (BSON samples only cover max values).""" | ||
| result = execute_admin_with_retry_command( | ||
| collection, {"logRotate": value}, retry_code=FILE_RENAME_FAILED_ERROR | ||
| ) | ||
| assertSuccessPartial(result, {"ok": 1.0}, msg=f"logRotate value should accept {value!r}") |
17 changes: 7 additions & 10 deletions
17
...ests/compatibility/tests/system/administration/commands/logRotate/test_smoke_logRotate.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,17 @@ | ||
| """ | ||
| Smoke test for logRotate command. | ||
|
|
||
| Tests basic logRotate functionality. | ||
| """ | ||
| """Smoke test for logRotate command.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from documentdb_tests.framework.assertions import assertSuccessPartial | ||
| from documentdb_tests.framework.executor import execute_admin_command | ||
| from documentdb_tests.framework.error_codes import FILE_RENAME_FAILED_ERROR | ||
| from documentdb_tests.framework.executor import execute_admin_with_retry_command | ||
|
|
||
| pytestmark = [pytest.mark.smoke, pytest.mark.no_parallel] | ||
|
|
||
|
|
||
| def test_smoke_logRotate(collection): | ||
| """Test basic logRotate behavior.""" | ||
| result = execute_admin_command(collection, {"logRotate": 1}) | ||
|
|
||
| expected = {"ok": 1.0} | ||
| assertSuccessPartial(result, expected, msg="Should support logRotate command") | ||
| result = execute_admin_with_retry_command( | ||
| collection, {"logRotate": 1}, retry_code=FILE_RENAME_FAILED_ERROR | ||
| ) | ||
| assertSuccessPartial(result, {"ok": 1.0}, msg="Should support logRotate command") |
Empty file.
22 changes: 22 additions & 0 deletions
22
documentdb_tests/compatibility/tests/system/administration/utils/admin_test_case.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| """Shared test case for administration command tests.""" | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| from documentdb_tests.framework.test_case import BaseTestCase | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class AdminTestCase(BaseTestCase): | ||
| """Test case for administration command tests. | ||
|
|
||
| Inherits ``id``, ``expected``, ``error_code``, ``msg``, and ``marks`` from | ||
| ``BaseTestCase``. | ||
|
|
||
| Attributes: | ||
| command: The command document to execute. | ||
| use_admin: If True, execute against the admin database. | ||
| """ | ||
|
|
||
| command: Optional[Dict[str, Any]] = None | ||
| use_admin: bool = True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other admin command tests (killOp, getParameter) assert "comment": NotExists() in the response. Here only {"ok": 1.0} is checked. Add NotExists() to verify the comment is consumed and not echoed back