-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcache_test.go
More file actions
95 lines (83 loc) · 2.37 KB
/
cache_test.go
File metadata and controls
95 lines (83 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestPerformCleanup(t *testing.T) {
tempDir, err := os.MkdirTemp("", "test-cleanup-*")
if err != nil {
t.Fatal(err)
}
defer cleanupTempDir(t, tempDir)
maxAge := 2 * time.Hour
// Create a new file (should NOT be removed)
newFile := filepath.Join(tempDir, "new_file.tar.gz")
if err := os.WriteFile(newFile, []byte("new"), 0644); err != nil {
t.Fatal(err)
}
// Create an old file (should be removed)
oldFile := filepath.Join(tempDir, "old_file.tar.gz")
if err := os.WriteFile(oldFile, []byte("old"), 0644); err != nil {
t.Fatal(err)
}
// Manually set the modification time to be older than maxAge
oldTime := time.Now().Add(-maxAge - time.Hour)
if err := os.Chtimes(oldFile, oldTime, oldTime); err != nil {
t.Fatal(err)
}
cache, _ := NewCacheManager(tempDir, maxAge)
cache.PerformCleanup()
// Check if the new file still exists
if _, err := os.Stat(newFile); os.IsNotExist(err) {
t.Errorf("new file was incorrectly removed")
}
// Check if the old file was removed
if _, err := os.Stat(oldFile); !os.IsNotExist(err) {
t.Errorf("old file was not removed")
}
}
func TestGetCacheFilename(t *testing.T) {
cache, _ := NewCacheManager("", 1*time.Hour)
defer func(path string) {
err := os.RemoveAll(path)
if err != nil {
t.Errorf("failed to remove temporary directory: %v", err)
}
}(cache.Dir())
tests := []struct {
imageName string
platform Platform
expected string
}{
{
imageName: "alpine:latest",
platform: Platform{OS: "linux", Architecture: "amd64"},
expected: "library_alpine_latest_linux_amd64.tar.gz",
},
{
imageName: "library/ubuntu:20.04",
platform: Platform{OS: "linux", Architecture: "arm64"},
expected: "library_ubuntu_20.04_linux_arm64.tar.gz",
},
{
imageName: "ghcr.io/username/repo:v1.2.3",
platform: Platform{OS: "linux", Architecture: "amd64"},
expected: "username_repo_v1.2.3_linux_amd64.tar.gz",
},
{
imageName: "alpine:latest",
platform: Platform{OS: "linux", Architecture: "arm", Variant: "v7"},
expected: "library_alpine_latest_linux_arm_v7.tar.gz",
},
}
for _, tt := range tests {
t.Run(tt.imageName, func(t *testing.T) {
got := cache.GetCacheFilename(tt.imageName, tt.platform)
if got != tt.expected {
t.Errorf("GetCacheFilename(%q) = %q, want %q", tt.imageName, got, tt.expected)
}
})
}
}