Skip to content
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

test(robot): v2 volume should block trim when volume is degraded #2114

Merged
merged 1 commit into from
Nov 10, 2024

Conversation

c3y1huang
Copy link
Collaborator

@c3y1huang c3y1huang commented Sep 24, 2024

Which issue(s) this PR fixes:

Issue longhorn/longhorn#8430

What this PR does / why we need it:

Propose a new robot case to test v2 volume should block trim when the volume is degraded.

Special notes for your reviewer:

None

Additional documentation or context

None

Summary by CodeRabbit

  • New Features

    • Introduced a new keyword for trimming workload volumes based on specified conditions.
    • Added methods to handle filesystem trimming operations with expected outcomes.
  • Bug Fixes

    • Enhanced error handling for filesystem trimming to differentiate between expected and unexpected failures.
  • Tests

    • Added a comprehensive test case to ensure trimming fails when a volume is in a degraded state and succeeds when it is not.

Copy link

coderabbitai bot commented Nov 1, 2024

Walkthrough

The changes introduce a new keyword for workload resource management, enabling conditional volume trimming based on the state of the workload. This includes a new method for trimming filesystem volumes across multiple classes, enhancing the functionality and error handling of volume operations. Additionally, a new test case has been added to validate the behavior of the trimming operation under specific conditions, particularly when the volume is degraded.

Changes

File Path Change Summary
e2e/keywords/workload.resource Added keyword Trim ${workload_kind} ${workload_id} volume should ${condition}.
e2e/libs/keywords/workload_keywords.py Added method trim_workload_volume_filesystem(self, workload_name, is_expect_fail=False).
e2e/libs/volume/crd.py Added method trim_filesystem(self, volume_name, is_expect_fail=False) in CRD class.
e2e/libs/volume/rest.py Added method trim_filesystem(self, volume_name, is_expect_fail=False) in Rest class.
e2e/libs/volume/volume.py Added method trim_filesystem(self, volume_name, is_expect_fail=False) in Volume class.
e2e/tests/regression/test_v2.robot Added new test case "V2 Volume Should Block Trim When Volume Is Degraded" and resource imports.

Assessment against linked issues

Objective Addressed Explanation
Support for auto salvage in v2 volume (#8430)

Possibly related PRs

Suggested reviewers

  • khushboo-rancher
  • chriscchien

🐰 In the meadow, where the volumes grow,
A keyword was born, with a trim to bestow.
With conditions to check, and logic so bright,
Our workloads now dance, in the soft moonlight.
Hopping along, with tests that ensure,
That trimming is safe, and our volumes are pure! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@c3y1huang c3y1huang force-pushed the 8430-feat-v2-auto-salvage branch 2 times, most recently from c6a4959 to 3b959ca Compare November 6, 2024 04:41
@c3y1huang c3y1huang marked this pull request as ready for review November 6, 2024 04:41
@c3y1huang c3y1huang requested a review from a team as a code owner November 6, 2024 04:41
longhorn/longhorn-8430

Signed-off-by: Chin-Ya Huang <chin-ya.huang@suse.com>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (7)
e2e/tests/regression/test_v2.robot (2)

56-57: Add documentation for the test case.

Consider adding a [Documentation] tag to describe the test's purpose, prerequisites, and expected behavior.

 V2 Volume Should Block Trim When Volume Is Degraded
+    [Documentation]    Verifies that volume trim operations are blocked when the volume
+    ...    is in a degraded state, and succeed when the volume returns to healthy state.
+    ...    Prerequisites: Cluster must support v2 volumes and trim operations.

70-73: Consider adding retry mechanism for workload stability check.

The workload stability check might be sensitive to timing issues. Consider adding retry logic with appropriate timeout.

-        When Wait for workloads pods stable
-        ...    deployment 0
-        And Check deployment 0 works
+        When Wait for workloads pods stable with timeout    deployment 0    timeout=300s
+        And Run Keyword And Retry On Failure    Check deployment 0 works    retry=3
e2e/libs/keywords/workload_keywords.py (1)

196-198: LGTM! Consider adding docstring.

The implementation is clean and well-integrated with the existing codebase. It properly handles both success and failure scenarios needed for testing trim blocking behavior.

Consider adding a docstring to document the method's purpose and parameters:

 def trim_workload_volume_filesystem(self, workload_name, is_expect_fail=False):
+    """Trim the filesystem of the volume associated with the given workload.
+
+    Args:
+        workload_name (str): Name of the workload whose volume needs to be trimmed
+        is_expect_fail (bool, optional): If True, expects the trim operation to fail.
+            Defaults to False.
+    """
     volume_name = get_workload_volume_name(workload_name)
     self.volume.trim_filesystem(volume_name, is_expect_fail=is_expect_fail)
e2e/keywords/workload.resource (2)

193-202: Add documentation for the new keyword.

The keyword implementation looks good but lacks documentation. Consider adding documentation to describe:

  • Purpose of the keyword
  • Expected values for condition
  • Example usage

Add documentation above the keyword:

+[Documentation]    Tests volume trim operation behavior.
+...               condition: "pass" - expects trim to succeed
+...               condition: "fail" - expects trim to fail
+...               Example: Trim pod my-pod volume should fail
 Trim ${workload_kind} ${workload_id} volume should ${condition}

196-202: Consider improving error handling and condition validation.

While the implementation is functionally correct, consider these improvements:

  1. Define conditions as variables for better maintainability
  2. Provide more descriptive error message
  3. Add validation for workload existence before attempting trim

Example implementation:

+    ${TRIM_CONDITION_FAIL} =    Set Variable    fail
+    ${TRIM_CONDITION_PASS} =    Set Variable    pass
+
+    # Validate workload exists
+    ${exists} =    workload_exists    ${workload_name}
+    IF    not ${exists}
+        Fail    "Workload ${workload_name} does not exist"
+    END
+
-    IF    $condition == "fail"
+    IF    $condition == "${TRIM_CONDITION_FAIL}"
         trim_workload_volume_filesystem    ${workload_name}    is_expect_fail=True
-    ELSE IF    $condition == "pass"
+    ELSE IF    $condition == "${TRIM_CONDITION_PASS}"
         trim_workload_volume_filesystem    ${workload_name}    is_expect_fail=False
     ELSE
-        Fail    "Invalid condition value: ${condition}"
+        Fail    "Invalid condition value: '${condition}'. Expected values: '${TRIM_CONDITION_PASS}' or '${TRIM_CONDITION_FAIL}'"
     END
e2e/libs/volume/rest.py (2)

374-389: Add docstring and type hints to improve code clarity.

The method implementation looks good, but could benefit from better documentation.

-    def trim_filesystem(self, volume_name, is_expect_fail=False):
+    def trim_filesystem(self, volume_name: str, is_expect_fail: bool = False) -> None:
+        """Trim the filesystem of a specified volume.
+
+        Args:
+            volume_name: Name of the volume to trim
+            is_expect_fail: If True, the operation is expected to fail
+
+        Raises:
+            Exception: If the operation fails unexpectedly or succeeds when expected to fail
+        """

374-389: Add success logging for better debugging.

The method logs failures but not successes, which could make debugging more difficult.

         try:
             self.get(volume_name).trimFilesystem(name=volume_name)
+            logging(f"Successfully trimmed filesystem for volume {volume_name}")
 
             if is_expect_fail:
                 is_unexpected_pass = True
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 78eca51 and 5b626a9.

📒 Files selected for processing (6)
  • e2e/keywords/workload.resource (1 hunks)
  • e2e/libs/keywords/workload_keywords.py (1 hunks)
  • e2e/libs/volume/crd.py (1 hunks)
  • e2e/libs/volume/rest.py (1 hunks)
  • e2e/libs/volume/volume.py (1 hunks)
  • e2e/tests/regression/test_v2.robot (2 hunks)
🔇 Additional comments (7)
e2e/tests/regression/test_v2.robot (2)

14-15: LGTM: Resource imports are properly organized.

The new resource imports follow the established pattern and are appropriately placed with related imports.


62-64: Consider adding validation for data consistency.

The test writes data continuously but doesn't verify data integrity after the cluster restart. Consider adding data validation steps.

     FOR    ${i}    IN RANGE    ${LOOP_COUNT}
-        And Keep writing data to pod of deployment 0
+        And Keep writing data to pod of deployment 0
+        ${written_data}=    Get Written Data From Pod    deployment 0
e2e/libs/volume/volume.py (1)

157-159: LGTM! The implementation follows the established patterns.

The new trim_filesystem method:

  • Maintains consistency with the facade pattern used throughout the class
  • Properly delegates to the underlying implementation
  • Includes support for expected failures which is crucial for testing negative scenarios (e.g., trim being blocked when volume is degraded)

Let's verify the corresponding implementations in the underlying classes:

✅ Verification successful

The implementation is correctly verified across all layers

The verification confirms that:

  • The trim_filesystem method is properly implemented in both CRD and REST classes
  • CRD implementation correctly delegates to REST implementation
  • REST implementation contains the actual trimming logic with proper error handling for expected failures
  • The method signatures are consistent across all three layers (Volume, CRD, and REST)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify trim_filesystem implementations in CRD and REST classes
# Expected: Both classes should have corresponding implementations

# Check CRD implementation
echo "Checking CRD implementation..."
rg -A 5 "def trim_filesystem" "e2e/libs/volume/crd.py"

# Check REST implementation
echo "Checking REST implementation..."
rg -A 5 "def trim_filesystem" "e2e/libs/volume/rest.py"

Length of output: 619

e2e/libs/keywords/workload_keywords.py (1)

196-198: Verify trim_filesystem implementation.

Let's verify the implementation of the underlying trim_filesystem method to ensure it properly handles the is_expect_fail parameter.

✅ Verification successful

trim_filesystem implementation properly handles is_expect_fail parameter

The implementation in e2e/libs/volume/rest.py correctly handles the is_expect_fail parameter:

  • Attempts to trim the filesystem via REST API
  • Properly handles expected failures through try-catch block
  • Logs failures when is_expect_fail=True
  • Propagates exceptions for unexpected failures

The method is also being used correctly in test cases as shown in e2e/keywords/workload.resource with both positive and negative test scenarios.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for trim_filesystem implementation in volume-related files
echo "Searching for trim_filesystem implementation..."
rg -A 10 "def trim_filesystem.*is_expect_fail" --type python

# Search for test cases using this new method
echo "Searching for test cases using trim_workload_volume_filesystem..."
rg "trim_workload_volume_filesystem" --type robot

Length of output: 389


Script:

#!/bin/bash
# Search for trim_filesystem implementation without file type restriction
echo "Searching for trim_filesystem implementation..."
rg -A 10 "def trim_filesystem"

# Search for test cases using trim_workload_volume_filesystem without file type restriction
echo "Searching for test cases using trim_workload_volume_filesystem..."
rg "trim_workload_volume_filesystem"

# Search for volume-related files that might contain the implementation
echo "Searching for volume-related files..."
fd "volume.*\.py$"

Length of output: 2087

e2e/keywords/workload.resource (1)

197-197: Verify trim operation behavior in degraded state.

The implementation correctly handles expected pass/fail conditions, which aligns with the PR objective to test trim blocking in degraded state.

Let's verify the trim operation implementation:

Also applies to: 199-199

✅ Verification successful

Let me gather more context about the trim operation implementation, particularly focusing on the actual implementation in the REST layer and any test cases.


Based on the code inspection, I can now generate the final response:

Trim operation's error handling for degraded state is properly implemented

The implementation in rest.py correctly handles the expected failure scenario through:

  • Proper error handling with try-catch block
  • is_expect_fail parameter propagation through all layers
  • Appropriate logging of expected failures
  • Correct detection of unexpected passes
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify trim operation implementation in workload keywords

# Test 1: Check if trim_workload_volume_filesystem method exists and handles is_expect_fail
rg -A 5 "def trim_workload_volume_filesystem" "e2e/libs/keywords/workload_keywords.py"

# Test 2: Check volume trim implementation in volume libraries
rg -A 5 "trim_filesystem.*is_expect_fail" "e2e/libs/volume/"

Length of output: 1139


Script:

#!/bin/bash
# Check the complete implementation of trim operation in REST layer
rg -A 10 "def trim_filesystem" "e2e/libs/volume/rest.py"

# Check for related test cases
rg -A 5 "trim.*filesystem.*degraded" .
rg -A 5 "test.*trim.*degraded" .

Length of output: 509

e2e/libs/volume/rest.py (1)

374-389: Implementation looks good and aligns with PR objectives.

The method correctly implements filesystem trimming with proper error handling for both expected and unexpected failures. This implementation supports the PR objective of testing v2 volume trim behavior in degraded states.

Key strengths:

  • Proper error handling
  • Support for testing negative scenarios via is_expect_fail
  • Consistent with the class's error handling patterns
e2e/libs/volume/crd.py (1)

514-516: LGTM! The implementation aligns with the PR objectives.

The new trim_filesystem method is well-structured and follows the established patterns in the codebase. It appropriately delegates to the Rest class while providing the ability to test both successful and failing scenarios through the is_expect_fail parameter.

Let's verify the Rest class implementation:

✅ Verification successful

The implementation is correctly integrated with the Rest class

The verification confirms that:

  • The trim_filesystem method exists in e2e/libs/volume/rest.py with matching parameters
  • The implementation follows the proper delegation chain: CRD -> Rest -> Volume
  • The method handles both success and failure scenarios as expected through is_expect_fail
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the trim_filesystem implementation in Rest class
# Expected: The Rest class should have a corresponding trim_filesystem method

ast-grep --pattern 'class Rest {
  $$$
  trim_filesystem($_, $_) {
    $$$
  }
  $$$
}'

Length of output: 89


Script:

#!/bin/bash
# Let's try a different approach to find the Rest class and its trim_filesystem method
# First, let's locate the Rest class file
fd -t f "rest.py" -x rg -l "class Rest"

# Then search for trim_filesystem method
rg "def trim_filesystem" -A 5

Length of output: 1041

e2e/tests/regression/test_v2.robot Show resolved Hide resolved
Copy link
Contributor

@chriscchien chriscchien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

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