Skip to content

Commit

Permalink
test: make the test work
Browse files Browse the repository at this point in the history
  • Loading branch information
York Chen committed Sep 22, 2022
1 parent 972c129 commit 1fc82a1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
29 changes: 17 additions & 12 deletions internal/helm/repository/chart_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,14 @@ func (r *ChartRepository) DownloadChart(chart *repo.ChartVersion) (*bytes.Buffer
defer transport.Release(t)
start := time.Now()
buffer, err := r.Client.Get(u.String(), clientOpts...)
r.Recorder.RecordChartRepoEventDuration(
ChartRepoTypeHelm,
ChartRepoEventTypeDownloadChart,
r.Namespace,
r.URL,
start)
if r.Recorder != nil {
r.Recorder.RecordChartRepoEventDuration(
ChartRepoTypeHelm,
ChartRepoEventDownloadChart,
r.Namespace,
r.URL,
start)
}
return buffer, err
}

Expand Down Expand Up @@ -459,12 +461,15 @@ func (r *ChartRepository) DownloadIndex(w io.Writer) (err error) {
if err != nil {
return err
}
r.Recorder.RecordChartRepoEventDuration(
ChartRepoTypeHelm,
ChartRepoEventTypeDownloadIndex,
r.Namespace,
r.URL,
start)
if r.Recorder != nil {
r.Recorder.RecordChartRepoEventDuration(
ChartRepoTypeHelm,
ChartRepoEventDownloadIndex,
r.Namespace,
r.URL,
start)
}

if _, err = io.Copy(w, res); err != nil {
return err
}
Expand Down
14 changes: 8 additions & 6 deletions internal/helm/repository/chart_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@ func TestNewChartRepository(t *testing.T) {
New: helmgetter.NewHTTPGetter,
},
}
repoRecorder := MustMakeMetrics()
options := []helmgetter.Option{helmgetter.WithBasicAuth("username", "password")}

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

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

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

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

mg := mockGetter{}

r := &ChartRepository{
URL: tt.url,
Client: &mg,
URL: tt.url,
Namespace: "dummy",
Client: &mg,
Recorder: nil,
}
res, err := r.DownloadChart(tt.chartVersion)
if tt.wantErr {
Expand Down
6 changes: 3 additions & 3 deletions internal/helm/repository/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
const (
ChartRepoTypeHelm = "helm"
//ChartRepoTypeOCI = "oci"
ChartRepoEventTypeDownloadIndex = "chart_repository_download"
ChartRepoEventTypeDownloadChart = "chart_download"
ChartRepoEventDownloadIndex = "chart_repository_download"
ChartRepoEventDownloadChart = "chart_download"
)

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

0 comments on commit 1fc82a1

Please sign in to comment.