Skip to content

Commit b40e5b6

Browse files
committed
Remove trailing slash
Signed-off-by: Somtochi Onyekwere <[email protected]>
1 parent 3dcb142 commit b40e5b6

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

internal/helm/repository/oci_chart_repository.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"crypto/tls"
2222
"fmt"
2323
"net/url"
24+
"path"
2425
"sort"
2526
"strings"
2627

@@ -121,7 +122,9 @@ func NewOCIChartRepository(repositoryURL string, chartRepoOpts ...OCIChartReposi
121122
func (r *OCIChartRepository) Get(name, ver string) (*repo.ChartVersion, error) {
122123
// Find chart versions matching the given name.
123124
// Either in an index file or from a registry.
124-
cvs, err := r.getTags(fmt.Sprintf("%s/%s", r.URL.String(), name))
125+
cpURL := r.URL
126+
cpURL.Path = path.Join(cpURL.Path, name)
127+
cvs, err := r.getTags(cpURL.String())
125128
if err != nil {
126129
return nil, err
127130
}
@@ -136,7 +139,7 @@ func (r *OCIChartRepository) Get(name, ver string) (*repo.ChartVersion, error) {
136139
// If semver constraint string, try to find a match
137140
tag, err := getLastMatchingVersionOrConstraint(cvs, ver)
138141
return &repo.ChartVersion{
139-
URLs: []string{fmt.Sprintf("%s/%s:%s", r.URL.String(), name, tag)},
142+
URLs: []string{fmt.Sprintf("%s:%s", cpURL.String(), tag)},
140143
Metadata: &chart.Metadata{
141144
Name: name,
142145
Version: tag,

internal/helm/repository/oci_chart_repository_test.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"fmt"
2222
"net/url"
23+
"path"
2324
"strings"
2425
"testing"
2526

@@ -46,8 +47,8 @@ type mockRegistryClient struct {
4647
LastCalledURL string
4748
}
4849

49-
func (m *mockRegistryClient) Tags(url string) ([]string, error) {
50-
m.LastCalledURL = url
50+
func (m *mockRegistryClient) Tags(urlStr string) ([]string, error) {
51+
m.LastCalledURL = urlStr
5152
return m.tags, nil
5253
}
5354

@@ -91,7 +92,7 @@ func TestNewOCIChartRepository(t *testing.T) {
9192

9293
}
9394

94-
func TestOCIChartRepoisitory_Get(t *testing.T) {
95+
func TestOCIChartRepository_Get(t *testing.T) {
9596
registryClient := &mockRegistryClient{
9697
tags: []string{
9798
"0.0.1",
@@ -117,52 +118,66 @@ func TestOCIChartRepoisitory_Get(t *testing.T) {
117118

118119
testCases := []struct {
119120
name string
121+
url string
120122
version string
121123
expected string
122124
expectedErr string
123125
}{
124126
{
125127
name: "should return latest stable version",
126128
version: "",
129+
url: "oci://localhost:5000/my_repo",
127130
expected: "1.0.0",
128131
},
129132
{
130133
name: "should return latest stable version (asterisk)",
131134
version: "*",
135+
url: "oci://localhost:5000/my_repo",
132136
expected: "1.0.0",
133137
},
134138
{
135139
name: "should return latest stable version (semver range)",
136140
version: ">=0.1.5",
141+
url: "oci://localhost:5000/my_repo",
137142
expected: "1.0.0",
138143
},
139144
{
140145
name: "should return 0.2.0 (semver range)",
141146
version: "0.2.x",
147+
url: "oci://localhost:5000/my_repo",
142148
expected: "0.2.0",
143149
},
144150
{
145151
name: "should return a perfect match",
146152
version: "0.1.0",
153+
url: "oci://localhost:5000/my_repo",
147154
expected: "0.1.0",
148155
},
149156
{
150157
name: "should return 0.10.0",
151158
version: "0.*",
159+
url: "oci://localhost:5000/my_repo",
152160
expected: "0.10.0",
153161
},
154162
{
155163
name: "should an error for unfunfilled range",
156164
version: ">2.0.0",
165+
url: "oci://localhost:5000/my_repo",
157166
expectedErr: "could not locate a version matching provided version string >2.0.0",
158167
},
168+
{
169+
name: "shouldn't error out with trailing slash",
170+
version: "",
171+
url: "oci://localhost:5000/my_repo/",
172+
expected: "1.0.0",
173+
},
159174
}
160175

161-
url := "oci://localhost:5000/my_repo"
162176
for _, tc := range testCases {
177+
163178
t.Run(tc.name, func(t *testing.T) {
164179
g := NewWithT(t)
165-
r, err := NewOCIChartRepository(url, WithOCIRegistryClient(registryClient), WithOCIGetter(providers))
180+
r, err := NewOCIChartRepository(tc.url, WithOCIRegistryClient(registryClient), WithOCIGetter(providers))
166181
g.Expect(err).ToNot(HaveOccurred())
167182
g.Expect(r).ToNot(BeNil())
168183

@@ -173,15 +188,18 @@ func TestOCIChartRepoisitory_Get(t *testing.T) {
173188
g.Expect(err.Error()).To(Equal(tc.expectedErr))
174189
return
175190
}
191+
g.Expect(err).ToNot(HaveOccurred())
176192

193+
u, err := url.Parse(tc.url)
177194
g.Expect(err).ToNot(HaveOccurred())
178-
g.Expect(cv.URLs[0]).To(Equal(fmt.Sprintf("%s/%s:%s", url, chart, tc.expected)))
179-
g.Expect(registryClient.LastCalledURL).To(Equal(fmt.Sprintf("%s/%s", strings.TrimPrefix(url, fmt.Sprintf("%s://", registry.OCIScheme)), chart)))
195+
u.Path = path.Join(u.Path, chart)
196+
g.Expect(cv.URLs[0]).To(Equal(fmt.Sprintf("%s:%s", u.String(), tc.expected)))
197+
g.Expect(registryClient.LastCalledURL).To(Equal(strings.TrimPrefix(u.String(), fmt.Sprintf("%s://", registry.OCIScheme))))
180198
})
181199
}
182200
}
183201

184-
func TestOCIChartRepoisitory_DownloadChart(t *testing.T) {
202+
func TestOCIChartRepository_DownloadChart(t *testing.T) {
185203
client := &mockRegistryClient{}
186204
testCases := []struct {
187205
name string

0 commit comments

Comments
 (0)