Skip to content

Commit 81bba99

Browse files
authored
Merge pull request #793 from sophotechlabs/test/fluxinstance-methods
Add unit tests for FluxInstance methods
2 parents 92a955b + b7f3ab3 commit 81bba99

1 file changed

Lines changed: 216 additions & 0 deletions

File tree

api/v1/fluxinstance_types_test.go

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
// Copyright 2025 Stefan Prodan.
2+
// SPDX-License-Identifier: AGPL-3.0
3+
4+
package v1
5+
6+
import (
7+
"testing"
8+
"time"
9+
10+
. "github.com/onsi/gomega"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
)
13+
14+
func TestFluxInstanceGetInterval(t *testing.T) {
15+
tests := []struct {
16+
name string
17+
annotations map[string]string
18+
expected time.Duration
19+
}{
20+
{
21+
name: "default interval when no annotation",
22+
expected: 60 * time.Minute,
23+
},
24+
{
25+
name: "custom interval from annotation",
26+
annotations: map[string]string{ReconcileEveryAnnotation: "5m"},
27+
expected: 5 * time.Minute,
28+
},
29+
{
30+
name: "default interval on invalid duration",
31+
annotations: map[string]string{ReconcileEveryAnnotation: "not-a-duration"},
32+
expected: 60 * time.Minute,
33+
},
34+
{
35+
name: "zero when disabled",
36+
annotations: map[string]string{ReconcileAnnotation: "disabled"},
37+
expected: 0,
38+
},
39+
{
40+
name: "zero when disabled case-insensitive",
41+
annotations: map[string]string{ReconcileAnnotation: "Disabled"},
42+
expected: 0,
43+
},
44+
}
45+
46+
for _, tt := range tests {
47+
t.Run(tt.name, func(t *testing.T) {
48+
g := NewWithT(t)
49+
instance := &FluxInstance{
50+
ObjectMeta: metav1.ObjectMeta{Annotations: tt.annotations},
51+
}
52+
g.Expect(instance.GetInterval()).To(Equal(tt.expected))
53+
})
54+
}
55+
}
56+
57+
func TestFluxInstanceGetArtifactInterval(t *testing.T) {
58+
tests := []struct {
59+
name string
60+
annotations map[string]string
61+
expected time.Duration
62+
}{
63+
{
64+
name: "default interval when no annotation",
65+
expected: 10 * time.Minute,
66+
},
67+
{
68+
name: "custom interval from annotation",
69+
annotations: map[string]string{ReconcileArtifactEveryAnnotation: "30s"},
70+
expected: 30 * time.Second,
71+
},
72+
{
73+
name: "zero when disabled",
74+
annotations: map[string]string{ReconcileAnnotation: "disabled"},
75+
expected: 0,
76+
},
77+
}
78+
79+
for _, tt := range tests {
80+
t.Run(tt.name, func(t *testing.T) {
81+
g := NewWithT(t)
82+
instance := &FluxInstance{
83+
ObjectMeta: metav1.ObjectMeta{Annotations: tt.annotations},
84+
}
85+
g.Expect(instance.GetArtifactInterval()).To(Equal(tt.expected))
86+
})
87+
}
88+
}
89+
90+
func TestFluxInstanceGetTimeout(t *testing.T) {
91+
tests := []struct {
92+
name string
93+
annotations map[string]string
94+
expected time.Duration
95+
}{
96+
{
97+
name: "default timeout when no annotation",
98+
expected: 5 * time.Minute,
99+
},
100+
{
101+
name: "custom timeout from annotation",
102+
annotations: map[string]string{ReconcileTimeoutAnnotation: "10m"},
103+
expected: 10 * time.Minute,
104+
},
105+
{
106+
name: "default timeout on invalid duration",
107+
annotations: map[string]string{ReconcileTimeoutAnnotation: "invalid"},
108+
expected: 5 * time.Minute,
109+
},
110+
}
111+
112+
for _, tt := range tests {
113+
t.Run(tt.name, func(t *testing.T) {
114+
g := NewWithT(t)
115+
instance := &FluxInstance{
116+
ObjectMeta: metav1.ObjectMeta{Annotations: tt.annotations},
117+
}
118+
g.Expect(instance.GetTimeout()).To(Equal(tt.expected))
119+
})
120+
}
121+
}
122+
123+
func TestFluxInstanceIsDisabled(t *testing.T) {
124+
tests := []struct {
125+
name string
126+
annotations map[string]string
127+
expected bool
128+
}{
129+
{
130+
name: "not disabled when no annotation",
131+
expected: false,
132+
},
133+
{
134+
name: "disabled when annotation set",
135+
annotations: map[string]string{ReconcileAnnotation: "disabled"},
136+
expected: true,
137+
},
138+
{
139+
name: "disabled case-insensitive",
140+
annotations: map[string]string{ReconcileAnnotation: "DISABLED"},
141+
expected: true,
142+
},
143+
{
144+
name: "not disabled for other values",
145+
annotations: map[string]string{ReconcileAnnotation: "enabled"},
146+
expected: false,
147+
},
148+
}
149+
150+
for _, tt := range tests {
151+
t.Run(tt.name, func(t *testing.T) {
152+
g := NewWithT(t)
153+
instance := &FluxInstance{
154+
ObjectMeta: metav1.ObjectMeta{Annotations: tt.annotations},
155+
}
156+
g.Expect(instance.IsDisabled()).To(Equal(tt.expected))
157+
})
158+
}
159+
}
160+
161+
func TestFluxInstanceGetComponents(t *testing.T) {
162+
tests := []struct {
163+
name string
164+
components []Component
165+
expected []string
166+
}{
167+
{
168+
name: "defaults to core controllers when empty",
169+
expected: []string{
170+
FluxSourceController,
171+
FluxKustomizeController,
172+
FluxHelmController,
173+
FluxNotificationController,
174+
},
175+
},
176+
{
177+
name: "returns specified components",
178+
components: []Component{"source-controller", "helm-controller"},
179+
expected: []string{"source-controller", "helm-controller"},
180+
},
181+
}
182+
183+
for _, tt := range tests {
184+
t.Run(tt.name, func(t *testing.T) {
185+
g := NewWithT(t)
186+
instance := &FluxInstance{}
187+
instance.Spec.Components = tt.components
188+
g.Expect(instance.GetComponents()).To(Equal(tt.expected))
189+
})
190+
}
191+
}
192+
193+
func TestFluxInstanceGetCluster(t *testing.T) {
194+
t.Run("defaults when nil", func(t *testing.T) {
195+
g := NewWithT(t)
196+
instance := &FluxInstance{}
197+
cluster := instance.GetCluster()
198+
g.Expect(cluster.Type).To(Equal("kubernetes"))
199+
g.Expect(cluster.Domain).To(Equal("cluster.local"))
200+
g.Expect(cluster.NetworkPolicy).To(BeTrue())
201+
})
202+
203+
t.Run("returns spec value when set", func(t *testing.T) {
204+
g := NewWithT(t)
205+
instance := &FluxInstance{}
206+
instance.Spec.Cluster = &Cluster{
207+
Type: "openshift",
208+
Domain: "custom.local",
209+
NetworkPolicy: false,
210+
}
211+
cluster := instance.GetCluster()
212+
g.Expect(cluster.Type).To(Equal("openshift"))
213+
g.Expect(cluster.Domain).To(Equal("custom.local"))
214+
g.Expect(cluster.NetworkPolicy).To(BeFalse())
215+
})
216+
}

0 commit comments

Comments
 (0)