Remove trailing slash

Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
This commit is contained in:
Somtochi Onyekwere 2022-06-23 02:21:12 +01:00
parent 3dcb142076
commit 35a7ea1efa
2 changed files with 33 additions and 11 deletions

View File

@ -21,6 +21,7 @@ import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"net/url" "net/url"
"path"
"sort" "sort"
"strings" "strings"
@ -121,7 +122,9 @@ func NewOCIChartRepository(repositoryURL string, chartRepoOpts ...OCIChartReposi
func (r *OCIChartRepository) Get(name, ver string) (*repo.ChartVersion, error) { func (r *OCIChartRepository) Get(name, ver string) (*repo.ChartVersion, error) {
// Find chart versions matching the given name. // Find chart versions matching the given name.
// Either in an index file or from a registry. // Either in an index file or from a registry.
cvs, err := r.getTags(fmt.Sprintf("%s/%s", r.URL.String(), name)) cpURL := r.URL
cpURL.Path = path.Join(cpURL.Path, name)
cvs, err := r.getTags(cpURL.String())
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -136,7 +139,7 @@ func (r *OCIChartRepository) Get(name, ver string) (*repo.ChartVersion, error) {
// If semver constraint string, try to find a match // If semver constraint string, try to find a match
tag, err := getLastMatchingVersionOrConstraint(cvs, ver) tag, err := getLastMatchingVersionOrConstraint(cvs, ver)
return &repo.ChartVersion{ return &repo.ChartVersion{
URLs: []string{fmt.Sprintf("%s/%s:%s", r.URL.String(), name, tag)}, URLs: []string{fmt.Sprintf("%s:%s", cpURL.String(), tag)},
Metadata: &chart.Metadata{ Metadata: &chart.Metadata{
Name: name, Name: name,
Version: tag, Version: tag,

View File

@ -20,6 +20,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"net/url" "net/url"
"path"
"strings" "strings"
"testing" "testing"
@ -46,8 +47,8 @@ type mockRegistryClient struct {
LastCalledURL string LastCalledURL string
} }
func (m *mockRegistryClient) Tags(url string) ([]string, error) { func (m *mockRegistryClient) Tags(urlStr string) ([]string, error) {
m.LastCalledURL = url m.LastCalledURL = urlStr
return m.tags, nil return m.tags, nil
} }
@ -91,7 +92,7 @@ func TestNewOCIChartRepository(t *testing.T) {
} }
func TestOCIChartRepoisitory_Get(t *testing.T) { func TestOCIChartRepository_Get(t *testing.T) {
registryClient := &mockRegistryClient{ registryClient := &mockRegistryClient{
tags: []string{ tags: []string{
"0.0.1", "0.0.1",
@ -114,9 +115,11 @@ func TestOCIChartRepoisitory_Get(t *testing.T) {
New: helmgetter.NewOCIGetter, New: helmgetter.NewOCIGetter,
}, },
} }
testURL := "oci://localhost:5000/my_repo"
testCases := []struct { testCases := []struct {
name string name string
url string
version string version string
expected string expected string
expectedErr string expectedErr string
@ -124,45 +127,58 @@ func TestOCIChartRepoisitory_Get(t *testing.T) {
{ {
name: "should return latest stable version", name: "should return latest stable version",
version: "", version: "",
url: testURL,
expected: "1.0.0", expected: "1.0.0",
}, },
{ {
name: "should return latest stable version (asterisk)", name: "should return latest stable version (asterisk)",
version: "*", version: "*",
url: testURL,
expected: "1.0.0", expected: "1.0.0",
}, },
{ {
name: "should return latest stable version (semver range)", name: "should return latest stable version (semver range)",
version: ">=0.1.5", version: ">=0.1.5",
url: testURL,
expected: "1.0.0", expected: "1.0.0",
}, },
{ {
name: "should return 0.2.0 (semver range)", name: "should return 0.2.0 (semver range)",
version: "0.2.x", version: "0.2.x",
url: testURL,
expected: "0.2.0", expected: "0.2.0",
}, },
{ {
name: "should return a perfect match", name: "should return a perfect match",
version: "0.1.0", version: "0.1.0",
url: testURL,
expected: "0.1.0", expected: "0.1.0",
}, },
{ {
name: "should return 0.10.0", name: "should return 0.10.0",
version: "0.*", version: "0.*",
url: testURL,
expected: "0.10.0", expected: "0.10.0",
}, },
{ {
name: "should an error for unfunfilled range", name: "should an error for unfunfilled range",
version: ">2.0.0", version: ">2.0.0",
url: testURL,
expectedErr: "could not locate a version matching provided version string >2.0.0", expectedErr: "could not locate a version matching provided version string >2.0.0",
}, },
{
name: "shouldn't error out with trailing slash",
version: "",
url: "oci://localhost:5000/my_repo/",
expected: "1.0.0",
},
} }
url := "oci://localhost:5000/my_repo"
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t) g := NewWithT(t)
r, err := NewOCIChartRepository(url, WithOCIRegistryClient(registryClient), WithOCIGetter(providers)) r, err := NewOCIChartRepository(tc.url, WithOCIRegistryClient(registryClient), WithOCIGetter(providers))
g.Expect(err).ToNot(HaveOccurred()) g.Expect(err).ToNot(HaveOccurred())
g.Expect(r).ToNot(BeNil()) g.Expect(r).ToNot(BeNil())
@ -173,15 +189,18 @@ func TestOCIChartRepoisitory_Get(t *testing.T) {
g.Expect(err.Error()).To(Equal(tc.expectedErr)) g.Expect(err.Error()).To(Equal(tc.expectedErr))
return return
} }
g.Expect(err).ToNot(HaveOccurred()) g.Expect(err).ToNot(HaveOccurred())
g.Expect(cv.URLs[0]).To(Equal(fmt.Sprintf("%s/%s:%s", url, chart, tc.expected)))
g.Expect(registryClient.LastCalledURL).To(Equal(fmt.Sprintf("%s/%s", strings.TrimPrefix(url, fmt.Sprintf("%s://", registry.OCIScheme)), chart))) u, err := url.Parse(tc.url)
g.Expect(err).ToNot(HaveOccurred())
u.Path = path.Join(u.Path, chart)
g.Expect(cv.URLs[0]).To(Equal(fmt.Sprintf("%s:%s", u.String(), tc.expected)))
g.Expect(registryClient.LastCalledURL).To(Equal(strings.TrimPrefix(u.String(), fmt.Sprintf("%s://", registry.OCIScheme))))
}) })
} }
} }
func TestOCIChartRepoisitory_DownloadChart(t *testing.T) { func TestOCIChartRepository_DownloadChart(t *testing.T) {
client := &mockRegistryClient{} client := &mockRegistryClient{}
testCases := []struct { testCases := []struct {
name string name string