Skip to content

Commit f4e08de

Browse files
committed
fix(tests): migrate all kopia tests to use Ginkgo
1 parent ea40e77 commit f4e08de

1 file changed

Lines changed: 132 additions & 134 deletions

File tree

internal/controller/mover/kopia/mover_test.go

Lines changed: 132 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -20,143 +20,141 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
2020
package kopia
2121

2222
import (
23-
"testing"
24-
23+
. "github.com/onsi/ginkgo/v2"
24+
. "github.com/onsi/gomega"
2525
"k8s.io/apimachinery/pkg/api/resource"
2626
)
2727

28-
func TestCalculateCacheLimits(t *testing.T) {
29-
tests := []struct {
30-
name string
31-
cacheCapacity *resource.Quantity
32-
expectedMetadata int32
33-
expectedContent int32
34-
}{
35-
{
36-
name: "nil capacity returns zeros",
37-
cacheCapacity: nil,
38-
expectedMetadata: 0,
39-
expectedContent: 0,
40-
},
41-
{
42-
name: "1Gi capacity calculates 70% and 20%",
43-
cacheCapacity: resource.NewQuantity(1024*1024*1024, resource.BinarySI),
44-
expectedMetadata: 716, // 1024 * 0.70
45-
expectedContent: 204, // 1024 * 0.20
46-
},
47-
{
48-
name: "5Gi capacity calculates 70% and 20%",
49-
cacheCapacity: resource.NewQuantity(5*1024*1024*1024, resource.BinarySI),
50-
expectedMetadata: 3584, // 5120 * 0.70
51-
expectedContent: 1024, // 5120 * 0.20
52-
},
53-
{
54-
name: "small capacity (100Mi) calculates correctly",
55-
cacheCapacity: resource.NewQuantity(100*1024*1024, resource.BinarySI),
56-
expectedMetadata: 70, // 100 * 0.70
57-
expectedContent: 20, // 100 * 0.20
58-
},
59-
}
60-
61-
for _, tt := range tests {
62-
t.Run(tt.name, func(t *testing.T) {
63-
m := &Mover{cacheCapacity: tt.cacheCapacity}
64-
metaMB, contentMB := m.calculateCacheLimits()
65-
66-
if metaMB != tt.expectedMetadata {
67-
t.Errorf("metadataMB = %d, want %d", metaMB, tt.expectedMetadata)
68-
}
69-
if contentMB != tt.expectedContent {
70-
t.Errorf("contentMB = %d, want %d", contentMB, tt.expectedContent)
71-
}
28+
var _ = Describe("Kopia Cache Limits", func() {
29+
Describe("calculateCacheLimits", func() {
30+
Context("when cacheCapacity is nil", func() {
31+
It("should return zeros", func() {
32+
m := &Mover{cacheCapacity: nil}
33+
metaMB, contentMB := m.calculateCacheLimits()
34+
Expect(metaMB).To(Equal(int32(0)))
35+
Expect(contentMB).To(Equal(int32(0)))
36+
})
37+
})
38+
39+
Context("when cacheCapacity is 1Gi", func() {
40+
It("should calculate 70% for metadata and 20% for content", func() {
41+
capacity := resource.NewQuantity(1024*1024*1024, resource.BinarySI)
42+
m := &Mover{cacheCapacity: capacity}
43+
metaMB, contentMB := m.calculateCacheLimits()
44+
Expect(metaMB).To(Equal(int32(716))) // 1024 * 0.70
45+
Expect(contentMB).To(Equal(int32(204))) // 1024 * 0.20
46+
})
47+
})
48+
49+
Context("when cacheCapacity is 5Gi", func() {
50+
It("should calculate 70% for metadata and 20% for content", func() {
51+
capacity := resource.NewQuantity(5*1024*1024*1024, resource.BinarySI)
52+
m := &Mover{cacheCapacity: capacity}
53+
metaMB, contentMB := m.calculateCacheLimits()
54+
Expect(metaMB).To(Equal(int32(3584))) // 5120 * 0.70
55+
Expect(contentMB).To(Equal(int32(1024))) // 5120 * 0.20
56+
})
57+
})
58+
59+
Context("when cacheCapacity is small (100Mi)", func() {
60+
It("should calculate correctly", func() {
61+
capacity := resource.NewQuantity(100*1024*1024, resource.BinarySI)
62+
m := &Mover{cacheCapacity: capacity}
63+
metaMB, contentMB := m.calculateCacheLimits()
64+
Expect(metaMB).To(Equal(int32(70))) // 100 * 0.70
65+
Expect(contentMB).To(Equal(int32(20))) // 100 * 0.20
66+
})
67+
})
68+
})
69+
70+
Describe("addCacheLimitEnvVars", func() {
71+
int32Ptr := func(i int32) *int32 { return &i }
72+
73+
Context("when explicit limits are provided", func() {
74+
It("should use the explicit limits", func() {
75+
m := &Mover{
76+
metadataCacheSizeLimitMB: int32Ptr(2000),
77+
contentCacheSizeLimitMB: int32Ptr(500),
78+
cacheCapacity: nil,
79+
}
80+
envVars := m.addCacheLimitEnvVars(nil)
81+
82+
envMap := make(map[string]string)
83+
for _, ev := range envVars {
84+
envMap[ev.Name] = ev.Value
85+
}
86+
87+
Expect(envMap).To(HaveLen(2))
88+
Expect(envMap["KOPIA_METADATA_CACHE_SIZE_LIMIT_MB"]).To(Equal("2000"))
89+
Expect(envMap["KOPIA_CONTENT_CACHE_SIZE_LIMIT_MB"]).To(Equal("500"))
90+
})
91+
})
92+
93+
Context("when limits are nil but capacity is set", func() {
94+
It("should auto-calculate limits from capacity", func() {
95+
capacity := resource.NewQuantity(1024*1024*1024, resource.BinarySI)
96+
m := &Mover{
97+
metadataCacheSizeLimitMB: nil,
98+
contentCacheSizeLimitMB: nil,
99+
cacheCapacity: capacity,
100+
}
101+
envVars := m.addCacheLimitEnvVars(nil)
102+
103+
envMap := make(map[string]string)
104+
for _, ev := range envVars {
105+
envMap[ev.Name] = ev.Value
106+
}
107+
108+
Expect(envMap).To(HaveLen(2))
109+
Expect(envMap["KOPIA_METADATA_CACHE_SIZE_LIMIT_MB"]).To(Equal("716"))
110+
Expect(envMap["KOPIA_CONTENT_CACHE_SIZE_LIMIT_MB"]).To(Equal("204"))
111+
})
72112
})
73-
}
74-
}
75-
76-
//nolint:funlen
77-
func TestAddCacheLimitEnvVars(t *testing.T) {
78-
int32Ptr := func(i int32) *int32 { return &i }
79-
80-
tests := []struct {
81-
name string
82-
metadataCacheSizeLimitMB *int32
83-
contentCacheSizeLimitMB *int32
84-
cacheCapacity *resource.Quantity
85-
expectedEnvVars map[string]string // env var name -> value
86-
}{
87-
{
88-
name: "explicit limits are used",
89-
metadataCacheSizeLimitMB: int32Ptr(2000),
90-
contentCacheSizeLimitMB: int32Ptr(500),
91-
cacheCapacity: nil,
92-
expectedEnvVars: map[string]string{
93-
"KOPIA_METADATA_CACHE_SIZE_LIMIT_MB": "2000",
94-
"KOPIA_CONTENT_CACHE_SIZE_LIMIT_MB": "500",
95-
},
96-
},
97-
{
98-
name: "nil limits with capacity triggers auto-calc",
99-
metadataCacheSizeLimitMB: nil,
100-
contentCacheSizeLimitMB: nil,
101-
cacheCapacity: resource.NewQuantity(1024*1024*1024, resource.BinarySI),
102-
expectedEnvVars: map[string]string{
103-
"KOPIA_METADATA_CACHE_SIZE_LIMIT_MB": "716",
104-
"KOPIA_CONTENT_CACHE_SIZE_LIMIT_MB": "204",
105-
},
106-
},
107-
{
108-
name: "zero limits mean unlimited - no env vars",
109-
metadataCacheSizeLimitMB: int32Ptr(0),
110-
contentCacheSizeLimitMB: int32Ptr(0),
111-
cacheCapacity: resource.NewQuantity(1024*1024*1024, resource.BinarySI),
112-
expectedEnvVars: map[string]string{},
113-
},
114-
{
115-
name: "nil limits without capacity - no env vars",
116-
metadataCacheSizeLimitMB: nil,
117-
contentCacheSizeLimitMB: nil,
118-
cacheCapacity: nil,
119-
expectedEnvVars: map[string]string{},
120-
},
121-
{
122-
name: "mixed explicit and auto-calc",
123-
metadataCacheSizeLimitMB: int32Ptr(1000),
124-
contentCacheSizeLimitMB: nil,
125-
cacheCapacity: resource.NewQuantity(1024*1024*1024, resource.BinarySI),
126-
expectedEnvVars: map[string]string{
127-
"KOPIA_METADATA_CACHE_SIZE_LIMIT_MB": "1000",
128-
"KOPIA_CONTENT_CACHE_SIZE_LIMIT_MB": "204",
129-
},
130-
},
131-
}
132-
133-
for _, tt := range tests {
134-
t.Run(tt.name, func(t *testing.T) {
135-
m := &Mover{
136-
metadataCacheSizeLimitMB: tt.metadataCacheSizeLimitMB,
137-
contentCacheSizeLimitMB: tt.contentCacheSizeLimitMB,
138-
cacheCapacity: tt.cacheCapacity,
139-
}
140-
141-
envVars := m.addCacheLimitEnvVars(nil)
142-
143-
// Convert to map for easier comparison
144-
envMap := make(map[string]string)
145-
for _, ev := range envVars {
146-
envMap[ev.Name] = ev.Value
147-
}
148-
149-
if len(envMap) != len(tt.expectedEnvVars) {
150-
t.Errorf("got %d env vars, want %d", len(envMap), len(tt.expectedEnvVars))
151-
}
152-
153-
for name, expectedValue := range tt.expectedEnvVars {
154-
if gotValue, ok := envMap[name]; !ok {
155-
t.Errorf("missing env var %s", name)
156-
} else if gotValue != expectedValue {
157-
t.Errorf("env var %s = %s, want %s", name, gotValue, expectedValue)
113+
114+
Context("when limits are explicitly set to zero", func() {
115+
It("should not add env vars (unlimited)", func() {
116+
capacity := resource.NewQuantity(1024*1024*1024, resource.BinarySI)
117+
m := &Mover{
118+
metadataCacheSizeLimitMB: int32Ptr(0),
119+
contentCacheSizeLimitMB: int32Ptr(0),
120+
cacheCapacity: capacity,
158121
}
159-
}
122+
envVars := m.addCacheLimitEnvVars(nil)
123+
Expect(envVars).To(BeEmpty())
124+
})
125+
})
126+
127+
Context("when limits are nil and capacity is nil", func() {
128+
It("should not add env vars", func() {
129+
m := &Mover{
130+
metadataCacheSizeLimitMB: nil,
131+
contentCacheSizeLimitMB: nil,
132+
cacheCapacity: nil,
133+
}
134+
envVars := m.addCacheLimitEnvVars(nil)
135+
Expect(envVars).To(BeEmpty())
136+
})
137+
})
138+
139+
Context("when mixing explicit and auto-calculated limits", func() {
140+
It("should use explicit for one and auto-calculate for the other", func() {
141+
capacity := resource.NewQuantity(1024*1024*1024, resource.BinarySI)
142+
m := &Mover{
143+
metadataCacheSizeLimitMB: int32Ptr(1000),
144+
contentCacheSizeLimitMB: nil,
145+
cacheCapacity: capacity,
146+
}
147+
envVars := m.addCacheLimitEnvVars(nil)
148+
149+
envMap := make(map[string]string)
150+
for _, ev := range envVars {
151+
envMap[ev.Name] = ev.Value
152+
}
153+
154+
Expect(envMap).To(HaveLen(2))
155+
Expect(envMap["KOPIA_METADATA_CACHE_SIZE_LIMIT_MB"]).To(Equal("1000"))
156+
Expect(envMap["KOPIA_CONTENT_CACHE_SIZE_LIMIT_MB"]).To(Equal("204"))
157+
})
160158
})
161-
}
162-
}
159+
})
160+
})

0 commit comments

Comments
 (0)