Watch
1
0
Fork
You've already forked pkg-proxy
1
mirror of https://github.com/git-pkgs/proxy.git synced 2026-11-09 07:39:19 -05:00
pkg-proxy/internal/mirror/versions_test.go
Simon Chrzanowski 3f83d9c0ce Mirror Julia packages
proxy mirror and mirror jobs now take pkg:julia PURLs with the uuid
qualifier. The tree hash of the version comes from the General registry,
after checking the uuid is the one General lists for the name, and the
tarball is fetched from upstream.julia at /package/{uuid}/{tree-hash} and
cached under the name and tree hash the Julia handler uses.

The handler names a package from the registry's Registry.toml, which it
loads from the upstream. Without the upstream it fell back to caching and
looking up by uuid, so a package cached under its name, mirrored or
fetched before a restart, was not served offline. It now looks the
tarball up by its tree hash in that case.

Julia versions are listed from General whatever upstream.julia is, as
packages are identified by uuid and tree hash on every Pkg server serving
General.
2026-10-25 19:37:46 +01:00

118 lines
3.7 KiB
Go

package mirror
import (
"context"
"encoding/json"
"errors"
"reflect"
"testing"
)
// fakeVersions lists fixed versions and records what it was asked for.
type fakeVersions struct {
versions []string
err error
asked []string
}
func (f *fakeVersions) ListVersions(_ context.Context, ecosystem, name string) ([]string, error) {
f.asked = append(f.asked, ecosystem+"/"+name)
return f.versions, f.err
}
func enumerate(t *testing.T, source Source) []PackageVersion {
t.Helper()
var items []PackageVersion
if err := source.Enumerate(context.Background(), func(pv PackageVersion) error {
items = append(items, pv)
return nil
}); err != nil {
t.Fatalf("Enumerate() error = %v", err)
}
return items
}
func TestPURLSourceListsVersionsThroughLister(t *testing.T) {
versions := &fakeVersions{versions: []string{"1.0.0", "1.1.0"}}
source := &PURLSource{PURLs: []string{"pkg:npm/%40acme/widget"}, Versions: versions}
items := enumerate(t, source)
want := []PackageVersion{
{Ecosystem: "npm", Name: "@acme/widget", Version: "1.0.0"},
{Ecosystem: "npm", Name: "@acme/widget", Version: "1.1.0"},
}
if !reflect.DeepEqual(items, want) {
t.Errorf("items = %v, want %v", items, want)
}
if !reflect.DeepEqual(versions.asked, []string{"npm/@acme/widget"}) {
t.Errorf("lister asked for %v, want npm/@acme/widget", versions.asked)
}
}
func TestPURLSourceReportsListerError(t *testing.T) {
source := &PURLSource{PURLs: []string{"pkg:npm/left-pad"}, Versions: &fakeVersions{err: errors.New("upstream down")}}
err := source.Enumerate(context.Background(), func(PackageVersion) error { return nil })
if err == nil {
t.Fatal("Enumerate() succeeded although listing versions failed")
}
}
func TestSBOMSourceListsVersionsThroughLister(t *testing.T) {
bom := map[string]any{
"bomFormat": "CycloneDX",
"specVersion": "1.4",
"components": []map[string]any{{"type": "library", "name": "left-pad", "purl": "pkg:npm/left-pad"}},
}
versions := &fakeVersions{versions: []string{"1.3.0"}}
source := &SBOMSource{Data: marshalJSON(t, bom), Versions: versions}
items := enumerate(t, source)
if len(items) != 1 || items[0].Version != "1.3.0" {
t.Errorf("items = %v, want left-pad@1.3.0 from the lister", items)
}
}
func TestSourceFromRequestUsesMirrorVersions(t *testing.T) {
m := setupTestMirror(t, 1)
versions := &fakeVersions{}
m.Versions = versions
js := NewJobStore(context.Background(), m)
source, err := js.sourceFromRequest(JobRequest{PURLs: []string{"pkg:npm/left-pad"}})
if err != nil {
t.Fatalf("sourceFromRequest() error = %v", err)
}
if got := source.(*PURLSource).Versions; got != versions {
t.Errorf("PURL source lists versions through %v, want the mirror's lister", got)
}
source, err = js.sourceFromRequest(JobRequest{SBOM: json.RawMessage(`{"bomFormat":"CycloneDX","components":[]}`)})
if err != nil {
t.Fatalf("sourceFromRequest() error = %v", err)
}
if got := source.(*SBOMSource).Versions; got != versions {
t.Errorf("SBOM source lists versions through %v, want the mirror's lister", got)
}
}
func TestPURLSourceKeepsJuliaUUID(t *testing.T) {
const uuid = "7876af07-990d-54b4-ab0e-23690620f79a"
versions := &fakeVersions{versions: []string{"0.5.5"}}
source := &PURLSource{
PURLs: []string{"pkg:julia/Example@0.5.4?uuid=" + uuid, "pkg:julia/Example?uuid=" + uuid},
Versions: versions,
}
items := enumerate(t, source)
qualifiers := map[string]string{"uuid": uuid}
want := []PackageVersion{
{Ecosystem: "julia", Name: "Example", Version: "0.5.4", Qualifiers: qualifiers},
{Ecosystem: "julia", Name: "Example", Version: "0.5.5", Qualifiers: qualifiers},
}
if !reflect.DeepEqual(items, want) {
t.Errorf("items = %v, want %v", items, want)
}
}