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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion backupbackingimage/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"fmt"
"net/url"
"path/filepath"
"strings"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -115,9 +116,27 @@
}

func EncodeBackupBackingImageURL(backingImageName, destURL string) string {
if destURL == "" || backingImageName == "" {
return ""
}

u, err := url.Parse(destURL)
if err != nil {
log := backupstore.GetLog()
log.WithError(err).Errorf("Failed to parse destURL %v", destURL)
return ""
}

Check warning on line 128 in backupbackingimage/config.go

View check run for this annotation

Codecov / codecov/patch

backupbackingimage/config.go#L125-L128

Added lines #L125 - L128 were not covered by tests
if u.Scheme == "" {
return ""
}

v := url.Values{}
v.Add("backingImage", backingImageName)
return destURL + "?" + v.Encode()
prefixChar := "?"
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!


func DecodeBackupBackingImageURL(backupURL string) (string, string, error) {
Expand Down
80 changes: 80 additions & 0 deletions backupbackingimage/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package backupbackingimage

import (
"net/url"
"testing"
)

func TestEncodeBackupBackingImageURL(t *testing.T) {
tests := []struct {
backingImageName string
destURL string
expectedURL string
}{
{
backingImageName: "test-image",
destURL: "http://example.com",
expectedURL: "http://example.com?backingImage=test-image",
},
{
backingImageName: "test-image",
destURL: "http://example.com?param=value",
expectedURL: "http://example.com?param=value&backingImage=test-image",
},
{
backingImageName: "another-image",
destURL: "https://example.org/path",
expectedURL: "https://example.org/path?backingImage=another-image",
},
{
backingImageName: "another-image",
destURL: "https://example.org/path?existing=param",
expectedURL: "https://example.org/path?existing=param&backingImage=another-image",
},
{
backingImageName: "test-image",
destURL: "nfs://longhorn-test-nfs-svc.default:/opt/backupstore",
expectedURL: "nfs://longhorn-test-nfs-svc.default:/opt/backupstore?backingImage=test-image",
},
{
backingImageName: "test-image",
destURL: "nfs://longhorn-test-nfs-svc.default:/opt/backupstore?nfsOptions=soft,timeo=330,retrans=3",
expectedURL: "nfs://longhorn-test-nfs-svc.default:/opt/backupstore?nfsOptions=soft,timeo=330,retrans=3&backingImage=test-image",
},
}

for _, tt := range tests {
t.Run(tt.backingImageName, func(t *testing.T) {
result := EncodeBackupBackingImageURL(tt.backingImageName, tt.destURL)
// Validate the result is a well-formed URL
if _, err := url.Parse(result); err != nil {
t.Errorf("Generated URL is not valid: %v", err)
}
if result != tt.expectedURL {
t.Errorf("EncodeBackupBackingImageURL(%s, %s) = %s; want %s", tt.backingImageName, tt.destURL, result, tt.expectedURL)
}
})
}
}

// Add negative test cases
func TestEncodeBackupBackingImageURLInvalid(t *testing.T) {
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"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := EncodeBackupBackingImageURL(tt.backingImageName, tt.destURL)
if result != "" {
t.Errorf("Expected empty result for invalid input, got %s", result)
}
})
}
}