Watch
1
0
Fork
You've already forked pkg-proxy
1
mirror of https://github.com/git-pkgs/proxy.git synced 2026-08-23 04:14:57 -04:00
pkg-proxy/internal/storage/storage_test.go
Andrew Nesbitt 272e6d9040
Lint and dead-code cleanup (#279)
* Bump go tool golangci-lint to v2.13.1

The .golangci.yml goconst.ignore-tests setting was added in v2.12.0
(golangci/golangci-lint#6480). On the previously pinned v2.10.1,
config verify fails with "additional properties 'ignore-tests' not
allowed" and the setting is silently ignored at run time, so goconst
counts test-file literals toward min-occurrences.

* Apply gofmt and CutSuffix simplification

- gofmt -w internal/server/health_test.go
- Replace HasSuffix+TrimSuffix with CutSuffix in ParseSize

* Remove dead code and migrate tests off legacy Filesystem storage

Migrate the three test call sites of storage.NewFilesystem to
storage.OpenBucket("file://...") and drop the deprecated
StorageConfig.Path field from test configs, then delete code that
deadcode reports as unreachable from cmd/proxy:

- internal/storage/filesystem.go and its tests
- storage.HashingReader
- enrichment.Service.BulkCheckVulnerabilities and NormalizeLicense
- server.ActiveRequestsMiddleware (no-op body; the real tracking
  is the inline r.Use at server.go:226)
- mirror.RegistrySource (unimplemented stub)

metrics.UpdateCircuitBreakerState and RecordCircuitBreakerTrip are
kept because #275 wires them.

Update the CONTRIBUTING.md storage section to reflect blob.go.
2026-08-21 09:26:27 +01:00

65 lines
1.9 KiB
Go

package storage
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"io"
"testing"
)
func TestArtifactPath(t *testing.T) {
tests := []struct {
ecosystem string
namespace string
name string
version string
filename string
want string
}{
{"npm", "", "lodash", "4.17.21", "lodash-4.17.21.tgz", "npm/lodash/4.17.21/lodash-4.17.21.tgz"},
{"npm", "babel", "core", "7.0.0", "core-7.0.0.tgz", "npm/babel/core/7.0.0/core-7.0.0.tgz"},
{"cargo", "", "serde", "1.0.0", "serde-1.0.0.crate", "cargo/serde/1.0.0/serde-1.0.0.crate"},
{"pypi", "", "requests", "2.28.0", "requests-2.28.0.tar.gz", "pypi/requests/2.28.0/requests-2.28.0.tar.gz"},
{"maven", "org.apache", "commons-lang3", "3.12.0", "commons-lang3-3.12.0.jar", "maven/org.apache/commons-lang3/3.12.0/commons-lang3-3.12.0.jar"},
}
for _, tt := range tests {
got := ArtifactPath(tt.ecosystem, tt.namespace, tt.name, tt.version, tt.filename)
if got != tt.want {
t.Errorf("ArtifactPath(%q, %q, %q, %q, %q) = %q, want %q",
tt.ecosystem, tt.namespace, tt.name, tt.version, tt.filename, got, tt.want)
}
}
}
// assertLargeFileRoundTrip stores a 1MB file in the given storage, verifies size and
// hash, then reads it back and confirms the content matches.
func assertLargeFileRoundTrip(t *testing.T, s Storage) {
t.Helper()
ctx := context.Background()
data := bytes.Repeat([]byte("x"), 1024*1024)
size, hash, err := s.Store(ctx, "large/file.bin", bytes.NewReader(data))
if err != nil {
t.Fatalf("Store large file failed: %v", err)
}
if size != int64(len(data)) {
t.Errorf("size = %d, want %d", size, len(data))
}
h := sha256.Sum256(data)
wantHash := hex.EncodeToString(h[:])
if hash != wantHash {
t.Errorf("hash mismatch for large file")
}
r, _ := s.Open(ctx, "large/file.bin")
defer func() { _ = r.Close() }()
readBack, _ := io.ReadAll(r)
if !bytes.Equal(readBack, data) {
t.Error("large file content mismatch")
}
}