@@ -2,8 +2,11 @@ package mirror
22
33import (
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
4447const 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+
4657func 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+
114173func TestProgressTrackerSnapshot (t * testing.T ) {
115174 pt := newProgressTracker ()
116175 pt .total .Store (10 )
0 commit comments