mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-09-02 08:16:57 +00:00
155 lines
3.4 KiB
Go
155 lines
3.4 KiB
Go
package oci
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Tests for MultipartManager
|
|
|
|
func TestCreateSession(t *testing.T) {
|
|
mgr := &MultipartManager{
|
|
sessions: make(map[string]*MultipartSession),
|
|
}
|
|
|
|
session := mgr.CreateSession("sha256:test123", "aws-upload-id")
|
|
|
|
if session.UploadID == "" {
|
|
t.Error("Expected non-empty uploadID")
|
|
}
|
|
if session.Digest != "sha256:test123" {
|
|
t.Errorf("Expected digest=sha256:test123, got %s", session.Digest)
|
|
}
|
|
if session.S3UploadID != "aws-upload-id" {
|
|
t.Errorf("Expected S3UploadID=aws-upload-id, got %s", session.S3UploadID)
|
|
}
|
|
if session.CreatedAt.IsZero() {
|
|
t.Error("Expected CreatedAt to be set")
|
|
}
|
|
}
|
|
|
|
func TestGetSession_Success(t *testing.T) {
|
|
mgr := &MultipartManager{
|
|
sessions: make(map[string]*MultipartSession),
|
|
}
|
|
|
|
created := mgr.CreateSession("sha256:test123", "aws-upload-id")
|
|
|
|
retrieved, err := mgr.GetSession(created.UploadID)
|
|
if err != nil {
|
|
t.Fatalf("Expected success, got error: %v", err)
|
|
}
|
|
|
|
if retrieved.UploadID != created.UploadID {
|
|
t.Errorf("Expected uploadID=%s, got %s", created.UploadID, retrieved.UploadID)
|
|
}
|
|
}
|
|
|
|
func TestGetSession_NotFound(t *testing.T) {
|
|
mgr := &MultipartManager{
|
|
sessions: make(map[string]*MultipartSession),
|
|
}
|
|
|
|
_, err := mgr.GetSession("non-existent-id")
|
|
if err == nil {
|
|
t.Error("Expected error for non-existent session")
|
|
}
|
|
}
|
|
|
|
func TestDeleteSession(t *testing.T) {
|
|
mgr := &MultipartManager{
|
|
sessions: make(map[string]*MultipartSession),
|
|
}
|
|
|
|
session := mgr.CreateSession("sha256:test123", "aws-upload-id")
|
|
uploadID := session.UploadID
|
|
|
|
// Verify it exists
|
|
_, err := mgr.GetSession(uploadID)
|
|
if err != nil {
|
|
t.Fatalf("Session should exist before deletion")
|
|
}
|
|
|
|
// Delete it
|
|
mgr.DeleteSession(uploadID)
|
|
|
|
// Verify it's gone
|
|
_, err = mgr.GetSession(uploadID)
|
|
if err == nil {
|
|
t.Error("Session should not exist after deletion")
|
|
}
|
|
}
|
|
|
|
func TestCleanupExpiredSessions(t *testing.T) {
|
|
mgr := &MultipartManager{
|
|
sessions: make(map[string]*MultipartSession),
|
|
}
|
|
|
|
// Create an old session (>24h)
|
|
oldSession := &MultipartSession{
|
|
UploadID: "old-session",
|
|
Digest: "sha256:old",
|
|
S3UploadID: "aws-old-upload",
|
|
CreatedAt: time.Now().Add(-25 * time.Hour),
|
|
LastActivity: time.Now().Add(-25 * time.Hour),
|
|
}
|
|
mgr.sessions[oldSession.UploadID] = oldSession
|
|
|
|
// Create a recent session
|
|
recentSession := mgr.CreateSession("sha256:recent", "aws-recent-upload")
|
|
|
|
// Run cleanup
|
|
mgr.cleanupExpiredSessions()
|
|
|
|
// Old session should be gone
|
|
_, err := mgr.GetSession("old-session")
|
|
if err == nil {
|
|
t.Error("Old session should have been cleaned up")
|
|
}
|
|
|
|
// Recent session should still exist
|
|
_, err = mgr.GetSession(recentSession.UploadID)
|
|
if err != nil {
|
|
t.Error("Recent session should still exist")
|
|
}
|
|
}
|
|
|
|
// Tests for helper functions
|
|
func TestNormalizeETag(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
etag string
|
|
expected string
|
|
}{
|
|
{
|
|
name: "etag without quotes",
|
|
etag: "abc123",
|
|
expected: "\"abc123\"",
|
|
},
|
|
{
|
|
name: "etag already has quotes",
|
|
etag: "\"abc123\"",
|
|
expected: "\"abc123\"",
|
|
},
|
|
{
|
|
name: "empty etag",
|
|
etag: "",
|
|
expected: "\"\"",
|
|
},
|
|
{
|
|
name: "etag with special characters",
|
|
etag: "abc-123_def",
|
|
expected: "\"abc-123_def\"",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := normalizeETag(tt.etag)
|
|
if result != tt.expected {
|
|
t.Errorf("Expected %s, got %s", tt.expected, result)
|
|
}
|
|
})
|
|
}
|
|
}
|