Skip to content

Commit f76f4e0

Browse files
committed
Address artifact cache review
1 parent 66bcfe3 commit f76f4e0

4 files changed

Lines changed: 71 additions & 4 deletions

File tree

‎internal/database/database_test.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,12 @@ func TestCachedArtifactRowArtifact(t *testing.T) {
368368
if !strings.Contains(err.Error(), test.wantErr) {
369369
t.Errorf("error = %q, want %q", err, test.wantErr)
370370
}
371+
if !strings.Contains(err.Error(), test.versionPURL) {
372+
t.Errorf("error = %q, want version PURL %q", err, test.versionPURL)
373+
}
374+
if !strings.Contains(err.Error(), "example.tgz") {
375+
t.Errorf("error = %q, want filename", err)
376+
}
371377
return
372378
}
373379
if err != nil {

‎internal/database/queries.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,10 @@ type cachedArtifactRow struct {
259259

260260
func (row cachedArtifactRow) artifact(versionPURL, filename string) (*CachedArtifact, error) {
261261
if !row.ContentHash.Valid || row.ContentHash.String == "" {
262-
return nil, fmt.Errorf("cached artifact content hash is missing")
262+
return nil, fmt.Errorf("cached artifact for %q, filename %q: content hash is missing", versionPURL, filename)
263263
}
264264
if !row.Size.Valid {
265-
return nil, fmt.Errorf("cached artifact size is missing")
265+
return nil, fmt.Errorf("cached artifact for %q, filename %q: size is missing", versionPURL, filename)
266266
}
267267

268268
mediaType := ""
@@ -277,7 +277,7 @@ func (row cachedArtifactRow) artifact(versionPURL, filename string) (*CachedArti
277277
mediaType,
278278
)
279279
if err != nil {
280-
return nil, fmt.Errorf("invalid cached artifact: %w", err)
280+
return nil, fmt.Errorf("invalid cached artifact for %q, filename %q: %w", versionPURL, filename, err)
281281
}
282282

283283
return &CachedArtifact{

‎internal/mirror/mirror.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ func (m *Mirror) mirrorOne(ctx context.Context, pv PackageVersion, tracker *prog
212212
return
213213
}
214214

215-
_ = result.Reader.Close()
215+
if result.Reader != nil {
216+
_ = result.Reader.Close()
217+
}
216218

217219
if result.Cached {
218220
tracker.skipped.Add(1)

‎internal/mirror/mirror_test.go‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package mirror
22

33
import (
44
"context"
5+
"crypto/sha256"
6+
"database/sql"
57
"log/slog"
68
"os"
9+
"strings"
710
"testing"
811
"time"
912

@@ -43,6 +46,14 @@ func setupTestMirror(t *testing.T, workers int) *Mirror {
4346

4447
const testPackageLodash = "lodash"
4548

49+
type signedURLStorage struct {
50+
storage.Storage
51+
}
52+
53+
func (signedURLStorage) SignedURL(context.Context, string, time.Duration) (string, error) {
54+
return "https://storage.example/artifact", nil
55+
}
56+
4657
func TestMirrorRunEmptySource(t *testing.T) {
4758
m := setupTestMirror(t, 2)
4859

@@ -111,6 +122,54 @@ func TestMirrorRunCanceled(t *testing.T) {
111122
}
112123
}
113124

125+
func TestMirrorOneDirectServeCacheHit(t *testing.T) {
126+
m := setupTestMirror(t, 1)
127+
m.proxy.DirectServe = true
128+
m.proxy.Storage = signedURLStorage{Storage: m.storage}
129+
130+
packagePURL := "pkg:npm/example"
131+
versionPURL := packagePURL + "@1.0.0"
132+
if err := m.db.UpsertPackage(&database.Package{
133+
PURL: packagePURL,
134+
Ecosystem: "npm",
135+
Name: "example",
136+
}); err != nil {
137+
t.Fatalf("UpsertPackage() error = %v", err)
138+
}
139+
if err := m.db.UpsertVersion(&database.Version{
140+
PURL: versionPURL,
141+
PackagePURL: packagePURL,
142+
}); err != nil {
143+
t.Fatalf("UpsertVersion() error = %v", err)
144+
}
145+
if err := m.db.UpsertArtifact(&database.Artifact{
146+
VersionPURL: versionPURL,
147+
Filename: "",
148+
UpstreamURL: "https://registry.example/artifact",
149+
StoragePath: sql.NullString{String: "npm/example/1.0.0/artifact", Valid: true},
150+
ContentHash: sql.NullString{String: strings.Repeat("a", sha256.Size*2), Valid: true},
151+
Size: sql.NullInt64{Int64: 1, Valid: true},
152+
FetchedAt: sql.NullTime{Time: time.Now(), Valid: true},
153+
}); err != nil {
154+
t.Fatalf("UpsertArtifact() error = %v", err)
155+
}
156+
157+
tracker := newProgressTracker()
158+
m.mirrorOne(context.Background(), PackageVersion{
159+
Ecosystem: "npm",
160+
Name: "example",
161+
Version: "1.0.0",
162+
}, tracker)
163+
164+
progress := tracker.snapshot()
165+
if progress.Skipped != 1 {
166+
t.Errorf("skipped = %d, want 1", progress.Skipped)
167+
}
168+
if progress.Failed != 0 {
169+
t.Errorf("failed = %d, want 0", progress.Failed)
170+
}
171+
}
172+
114173
func TestProgressTrackerSnapshot(t *testing.T) {
115174
pt := newProgressTracker()
116175
pt.total.Store(10)

0 commit comments

Comments
 (0)