forked from richardartoul/gobuildcache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
176 lines (147 loc) · 4.58 KB
/
server_test.go
File metadata and controls
176 lines (147 loc) · 4.58 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"os"
"strings"
"testing"
"github.com/richardartoul/gobuildcache/pkg/backends"
"github.com/richardartoul/gobuildcache/pkg/locking"
)
func TestFormatBytes(t *testing.T) {
tests := []struct {
bytes int64
expected string
}{
{0, "0 B"},
{1, "1 B"},
{1023, "1023 B"},
{1024, "1.00 KB"},
{1536, "1.50 KB"},
{1024 * 1024, "1.00 MB"},
{1536 * 1024, "1.50 MB"},
{1024 * 1024 * 1024, "1.00 GB"},
{1024*1024*1024*2 + 1024*1024*512, "2.50 GB"},
{1024 * 1024 * 1024 * 1024, "1.00 TB"},
{1024*1024*1024*1024*3 + 1024*1024*1024*716, "3.70 TB"},
}
for _, tt := range tests {
result := formatBytes(tt.bytes)
if result != tt.expected {
t.Errorf("formatBytes(%d) = %s, expected %s", tt.bytes, result, tt.expected)
}
}
}
// createTestCacheProg creates a CacheProg for testing with the specified readOnly setting.
func createTestCacheProg(t *testing.T, readOnly bool) (*CacheProg, string) {
t.Helper()
// Create a temporary directory for the cache
cacheDir, err := os.MkdirTemp("", "gobuildcache-test-*")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
backend := backends.NewNoop()
locker := locking.NewNoOpGroup()
cp, err := NewCacheProg(backend, locker, cacheDir, false, false, false, readOnly)
if err != nil {
os.RemoveAll(cacheDir)
t.Fatalf("Failed to create CacheProg: %v", err)
}
return cp, cacheDir
}
func TestReadOnlyMode_SkipsPut(t *testing.T) {
cp, cacheDir := createTestCacheProg(t, true) // readOnly = true
defer os.RemoveAll(cacheDir)
// Create a PUT request
req := &Request{
ID: 1,
Command: CmdPut,
ActionID: []byte("test-action-id-12345678"),
OutputID: []byte("test-output-id-12345678"),
Body: strings.NewReader("test body content"),
BodySize: 17,
}
// Execute the PUT
resp, err := cp.handlePut(req)
if err != nil {
t.Fatalf("handlePut returned error: %v", err)
}
// Verify response is successful (no error)
if resp.Err != "" {
t.Errorf("Expected no error in response, got: %s", resp.Err)
}
// Verify skippedPuts counter was incremented (backend write skipped)
if cp.skippedPuts.Load() != 1 {
t.Errorf("Expected skippedPuts to be 1, got: %d", cp.skippedPuts.Load())
}
// Verify putCount WAS incremented (local cache write still happens)
if cp.putCount.Load() != 1 {
t.Errorf("Expected putCount to be 1, got: %d", cp.putCount.Load())
}
// Verify local cache was written to (DiskPath should be set)
if resp.DiskPath == "" {
t.Error("Expected DiskPath to be set (local cache should be written)")
}
// Verify backendBytesWritten is 0 (no backend write)
if cp.backendBytesWritten.Load() != 0 {
t.Errorf("Expected backendBytesWritten to be 0, got: %d", cp.backendBytesWritten.Load())
}
}
func TestReadOnlyMode_Disabled_AllowsPut(t *testing.T) {
cp, cacheDir := createTestCacheProg(t, false) // readOnly = false
defer os.RemoveAll(cacheDir)
// Create a PUT request
req := &Request{
ID: 1,
Command: CmdPut,
ActionID: []byte("test-action-id-12345678"),
OutputID: []byte("test-output-id-12345678"),
Body: strings.NewReader("test body content"),
BodySize: 17,
}
// Execute the PUT
resp, err := cp.handlePut(req)
if err != nil {
t.Fatalf("handlePut returned error: %v", err)
}
// Verify response is successful (no error)
if resp.Err != "" {
t.Errorf("Expected no error in response, got: %s", resp.Err)
}
// Verify skippedPuts was NOT incremented
if cp.skippedPuts.Load() != 0 {
t.Errorf("Expected skippedPuts to be 0, got: %d", cp.skippedPuts.Load())
}
// Verify putCount WAS incremented
if cp.putCount.Load() != 1 {
t.Errorf("Expected putCount to be 1, got: %d", cp.putCount.Load())
}
}
func TestReadOnlyMode_MultipleSkippedPuts(t *testing.T) {
cp, cacheDir := createTestCacheProg(t, true) // readOnly = true
defer os.RemoveAll(cacheDir)
// Execute multiple PUTs with unique action IDs
for i := 0; i < 5; i++ {
actionID := make([]byte, 24)
copy(actionID, []byte("test-action-id-1234567"))
actionID[23] = byte('0' + i)
req := &Request{
ID: int64(i),
Command: CmdPut,
ActionID: actionID,
OutputID: []byte("test-output-id-12345678"),
Body: strings.NewReader("test body"),
BodySize: 9,
}
_, err := cp.handlePut(req)
if err != nil {
t.Fatalf("handlePut %d returned error: %v", i, err)
}
}
// Verify all 5 backend writes were skipped
if cp.skippedPuts.Load() != 5 {
t.Errorf("Expected skippedPuts to be 5, got: %d", cp.skippedPuts.Load())
}
// Verify putCount is 5 (local cache writes still happen)
if cp.putCount.Load() != 5 {
t.Errorf("Expected putCount to be 5, got: %d", cp.putCount.Load())
}
}