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

fix(backupbackingimage): encode URL for NFS backup target with options #204

Merged
merged 1 commit into from
Oct 24, 2024

Conversation

mantissahz
Copy link
Contributor

@mantissahz mantissahz commented Oct 24, 2024

Which issue(s) this PR fixes:

Issue longhorn/longhorn#9702

What this PR does / why we need it:

Special notes for your reviewer:

Additional documentation or context

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Improved URL construction logic for backup backing images, ensuring correct formatting of query parameters.
    • Added comprehensive test coverage for the URL encoding functionality.
  • Bug Fixes

    • Fixed potential issues with handling empty parameters and invalid URLs, returning appropriate results.

@mantissahz mantissahz requested review from ChanYiLin, derekbit and a team October 24, 2024 02:28
@mantissahz mantissahz self-assigned this Oct 24, 2024
Copy link

coderabbitai bot commented Oct 24, 2024

Warning

Rate limit exceeded

@mantissahz has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 2 minutes and 31 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Files that changed from the base of the PR and between 677324f and d3199a9.

Walkthrough

The changes involve a modification to the EncodeBackupBackingImageURL function in the backupbackingimage/config.go file. The update enhances the logic for constructing a URL with query parameters, ensuring that new parameters are correctly appended based on the presence of existing parameters in the destination URL. A new test file, config_test.go, has also been introduced to validate the functionality of this updated function through various test cases.

Changes

File Change Summary
backupbackingimage/config.go Modified the logic in EncodeBackupBackingImageURL to check for empty parameters and validate the URL scheme. Revised logic for appending query parameters.
backupbackingimage/config_test.go Added new test functions TestEncodeBackupBackingImageURL and TestEncodeBackupBackingImageURLInvalid to validate the updated URL encoding logic with various valid and invalid test cases.

Assessment against linked issues

Objective Addressed Explanation
Ensure correct URL encoding for NFS backup target with options (#9702)
Validate that backup backing image information is synchronized (#9702) The changes do not address synchronization issues.
Enable deletion of backup backing images (#9702) The changes do not resolve deletion issues.

🐇 In the code we hop and play,
To build URLs the right way!
With question marks and ampersands,
Our logic now truly stands.
So let’s encode with joy and cheer,
For every backup, we hold dear! 🌟


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.

Copy link

codecov bot commented Oct 24, 2024

Codecov Report

Attention: Patch coverage is 70.58824% with 5 lines in your changes missing coverage. Please review.

Project coverage is 8.08%. Comparing base (ef7b22d) to head (d3199a9).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
backupbackingimage/config.go 70.58% 4 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##           master    #204      +/-   ##
=========================================
+ Coverage    7.54%   8.08%   +0.54%     
=========================================
  Files          23      23              
  Lines        2519    2535      +16     
=========================================
+ Hits          190     205      +15     
  Misses       2303    2303              
- Partials       26      27       +1     
Flag Coverage Δ
unittests 8.08% <70.58%> (+0.54%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c988d4f and 6355615.

📒 Files selected for processing (2)
  • backupbackingimage/config.go (2 hunks)
  • backupbackingimage/config_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • backupbackingimage/config.go
🔇 Additional comments (2)
backupbackingimage/config_test.go (2)

1-5: LGTM!

Package declaration and imports are appropriate for a test file.


1-43: Verify similar URL encoding patterns in the codebase.

Since this fixes a URL encoding issue with NFS targets, we should check for similar patterns that might need the same fix.

✅ Verification successful

Let me analyze the results and generate additional verification scripts to get a complete picture.

The results show NFS URL handling patterns in tests, but we need to verify the actual implementations to ensure consistent URL encoding across the codebase. The additional scripts will help us understand how URLs are constructed and encoded in the implementation code.


Let me try one final verification to check the specific URL encoding functions in the codebase.

This will help us see the actual implementation of URL-related functions and determine if there are any inconsistencies in URL encoding patterns.


Based on the search results, I can now provide a final response regarding the URL encoding verification:

URL encoding is consistently handled across the codebase

The codebase shows consistent URL encoding patterns:

  • EncodeBackupBackingImageURL follows the same pattern as EncodeBackupURL by:
    • Properly handling query parameters using url.Values
    • Checking for existing query parameters with ? vs &
    • Using proper URL encoding through Go's URL package

The test cases in backupbackingimage/config_test.go verify this behavior matches the existing backup URL encoding patterns in backupstore_test.go.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for URL construction patterns in the codebase
echo "Searching for URL construction patterns..."
rg -l 'nfs://' | xargs rg -B 2 -A 2 '\?.*=' || true

# Search for other URL encoding function calls
echo "Searching for URL encoding functions..."
rg -l 'url\..*Escape' || true

Length of output: 6403


Script:

#!/bin/bash
# Let's check the implementation of URL encoding in the codebase
echo "Checking URL encoding implementations..."
ast-grep --pattern 'func $NAME($$$) {
  $$$
  url.$$$Escape($$$)
  $$$
}'

# Let's also check the backupstore package implementation
echo "Checking backupstore URL handling..."
ast-grep --pattern 'func $NAME($$$) string {
  $$$
  url$$$
  $$$
}'

Length of output: 320


Script:

#!/bin/bash
# Check for URL encoding/construction functions
echo "Checking URL encoding functions..."
rg -A 5 -B 5 'func.*URL' || true

# Check specific backupstore URL handling
echo "Checking backupstore URL handling..."
rg -A 10 'func.*EncodeBackupURL|func.*DecodeBackupURL' || true

Length of output: 176208

backupbackingimage/config_test.go Outdated Show resolved Hide resolved
backupbackingimage/config_test.go Outdated Show resolved Hide resolved
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: 0

🧹 Outside diff range and nitpick comments (2)
backupbackingimage/config_test.go (2)

8-63: LGTM with a minor enhancement suggestion!

The test cases are comprehensive, covering:

  • Basic HTTP/HTTPS URLs
  • URLs with existing query parameters
  • NFS URLs with and without options
  • URLs containing special characters
  • Proper URL validation

Consider adding descriptions to the test cases to make their purpose clearer:

 tests := []struct {
+    name             string
     backingImageName string
     destURL          string
     expectedURL      string
 }{
     {
+        name:             "basic http url",
         backingImageName: "test-image",
         destURL:          "http://example.com",
         expectedURL:      "http://example.com?backingImage=test-image",
     },
     // ... rest of the test cases

65-85: Consider adding more edge cases to negative tests.

The current negative test cases cover basic invalid inputs. Consider adding:

 tests := []struct {
     name             string
     backingImageName string
     destURL          string
 }{
     {"empty backing image", "", "nfs://valid.host:/path"},
     {"empty dest URL", "image", ""},
     {"invalid URL", "image", "not-a-url"},
+    {"malformed nfs url", "image", "nfs:/missing-colon/path"},
+    {"url with invalid chars", "image", "nfs://host:/path\x00"},
+    {"url with invalid options", "image", "nfs://host:/path?nfsOptions=invalid\options"},
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 6355615 and db17e68.

📒 Files selected for processing (2)
  • backupbackingimage/config.go (2 hunks)
  • backupbackingimage/config_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • backupbackingimage/config.go
🔇 Additional comments (1)
backupbackingimage/config_test.go (1)

1-6: LGTM!

Package declaration and imports are correctly defined with the necessary dependencies.

@mantissahz mantissahz force-pushed the issue9702 branch 2 times, most recently from 04ac088 to 677324f Compare October 24, 2024 03:36
And add an unit test case for config.go

ref: longhorn/longhorn 9702

Signed-off-by: James Lu <[email protected]>
Copy link
Member

@derekbit derekbit left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@derekbit derekbit left a comment

Choose a reason for hiding this comment

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

LGTM

@derekbit derekbit merged commit 62c849e into longhorn:master Oct 24, 2024
8 checks passed
if strings.Contains(destURL, "?") {
prefixChar = "&"
}
return destURL + prefixChar + v.Encode()
}
Copy link
Contributor

@james-munson james-munson Oct 24, 2024

Choose a reason for hiding this comment

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

This will work, but rather than doing manual string building, it would be preferable to add ("backingImage", backingImageName) to the existing u.Query() and then let Encode() handle whether to add the "&" or not. There's no reason to try to duplicate the parsing logic already in the library.

Copy link
Member

Choose a reason for hiding this comment

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

Good suggestion!

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.

3 participants