Skip to content

Commit 37524fc

Browse files
author
York Chen
committed
test: make the test work
1 parent a02e7e0 commit 37524fc

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

internal/helm/repository/chart_repository.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,14 @@ func (r *ChartRepository) DownloadChart(chart *repo.ChartVersion) (*bytes.Buffer
284284
defer transport.Release(t)
285285
start := time.Now()
286286
buffer, err := r.Client.Get(u.String(), clientOpts...)
287-
r.Recorder.RecordChartRepoEventDuration(
288-
ChartRepoTypeHelm,
289-
ChartRepoEventTypeDownloadChart,
290-
r.Namespace,
291-
r.URL,
292-
start)
287+
if r.Recorder != nil {
288+
r.Recorder.RecordChartRepoEventDuration(
289+
ChartRepoTypeHelm,
290+
ChartRepoEventDownloadChart,
291+
r.Namespace,
292+
r.URL,
293+
start)
294+
}
293295
return buffer, err
294296
}
295297

@@ -459,12 +461,15 @@ func (r *ChartRepository) DownloadIndex(w io.Writer) (err error) {
459461
if err != nil {
460462
return err
461463
}
462-
r.Recorder.RecordChartRepoEventDuration(
463-
ChartRepoTypeHelm,
464-
ChartRepoEventTypeDownloadIndex,
465-
r.Namespace,
466-
r.URL,
467-
start)
464+
if r.Recorder != nil {
465+
r.Recorder.RecordChartRepoEventDuration(
466+
ChartRepoTypeHelm,
467+
ChartRepoEventDownloadIndex,
468+
r.Namespace,
469+
r.URL,
470+
start)
471+
}
472+
468473
if _, err = io.Copy(w, res); err != nil {
469474
return err
470475
}

internal/helm/repository/chart_repository_test.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,12 @@ func TestNewChartRepository(t *testing.T) {
6363
New: helmgetter.NewHTTPGetter,
6464
},
6565
}
66-
repoRecorder := MustMakeMetrics()
6766
options := []helmgetter.Option{helmgetter.WithBasicAuth("username", "password")}
6867

6968
t.Run("should construct chart repository", func(t *testing.T) {
7069
g := NewWithT(t)
7170

72-
r, err := NewChartRepository(repositoryURL, "", providers, nil, options, "fake-namespace", repoRecorder)
71+
r, err := NewChartRepository(repositoryURL, "", providers, nil, options, "fake-namespace", nil)
7372
g.Expect(err).ToNot(HaveOccurred())
7473
g.Expect(r).ToNot(BeNil())
7574
g.Expect(r.URL).To(Equal(repositoryURL))
@@ -79,7 +78,7 @@ func TestNewChartRepository(t *testing.T) {
7978

8079
t.Run("should error on URL parsing failure", func(t *testing.T) {
8180
g := NewWithT(t)
82-
r, err := NewChartRepository("https://ex ample.com", "", nil, nil, nil, "fake-namespace", repoRecorder)
81+
r, err := NewChartRepository("https://ex ample.com", "", nil, nil, nil, "fake-namespace", nil)
8382
g.Expect(err).To(HaveOccurred())
8483
g.Expect(err).To(BeAssignableToTypeOf(&url.Error{}))
8584
g.Expect(r).To(BeNil())
@@ -89,7 +88,7 @@ func TestNewChartRepository(t *testing.T) {
8988
t.Run("should error on unsupported scheme", func(t *testing.T) {
9089
g := NewWithT(t)
9190

92-
r, err := NewChartRepository("http://example.com", "", providers, nil, nil, "fake-namespace", repoRecorder)
91+
r, err := NewChartRepository("http://example.com", "", providers, nil, nil, "fake-namespace", nil)
9392
g.Expect(err).To(HaveOccurred())
9493
g.Expect(err.Error()).To(Equal("scheme \"http\" not supported"))
9594
g.Expect(r).To(BeNil())
@@ -236,9 +235,12 @@ func TestChartRepository_DownloadChart(t *testing.T) {
236235
t.Parallel()
237236

238237
mg := mockGetter{}
238+
239239
r := &ChartRepository{
240-
URL: tt.url,
241-
Client: &mg,
240+
URL: tt.url,
241+
Namespace: "dummy",
242+
Client: &mg,
243+
Recorder: nil,
242244
}
243245
res, err := r.DownloadChart(tt.chartVersion)
244246
if tt.wantErr {

internal/helm/repository/metrics.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
const (
2626
ChartRepoTypeHelm = "helm"
2727
//ChartRepoTypeOCI = "oci"
28-
ChartRepoEventTypeDownloadIndex = "chart_repository_download"
29-
ChartRepoEventTypeDownloadChart = "chart_download"
28+
ChartRepoEventDownloadIndex = "chart_repository_download"
29+
ChartRepoEventDownloadChart = "chart_download"
3030
)
3131

3232
// Recorder is a recorder for chart repository events.
@@ -58,7 +58,7 @@ func NewRepositoryRecorder() *Recorder {
5858
Help: "The duration in seconds of an event for a Helm Chart Repository.",
5959
Buckets: prometheus.ExponentialBuckets(10e-9, 10, 10),
6060
},
61-
[]string{"name", "repo_type", "namespace", "url", "checksum"},
61+
[]string{"name", "repo_type", "namespace", "url"},
6262
),
6363
}
6464
}

0 commit comments

Comments
 (0)