Skip to content

Commit f6ba5cb

Browse files
committed
test: add unit tests for GetTargets function with various label match modes
Signed-off-by: XploY04 <[email protected]>
1 parent 6a3965a commit f6ba5cb

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

pkg/types/types_test.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package types
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGetTargets_UnionModeDefault(t *testing.T) {
8+
// Test that union is the default mode when not specified
9+
targets := "deployment:default:[app=nginx,tier=frontend]"
10+
result := GetTargets(targets)
11+
12+
if len(result) != 1 {
13+
t.Errorf("Expected 1 AppDetail, got %d", len(result))
14+
}
15+
16+
if result[0].LabelMatchMode != "union" {
17+
t.Errorf("Expected default LabelMatchMode to be 'union', got '%s'", result[0].LabelMatchMode)
18+
}
19+
20+
if result[0].Kind != "deployment" {
21+
t.Errorf("Expected Kind to be 'deployment', got '%s'", result[0].Kind)
22+
}
23+
24+
if result[0].Namespace != "default" {
25+
t.Errorf("Expected Namespace to be 'default', got '%s'", result[0].Namespace)
26+
}
27+
28+
if len(result[0].Labels) != 2 {
29+
t.Errorf("Expected 2 labels, got %d", len(result[0].Labels))
30+
}
31+
}
32+
33+
func TestGetTargets_ExplicitUnionMode(t *testing.T) {
34+
// Test explicit union mode
35+
targets := "statefulset:prod:[app=postgres,role=primary]:union"
36+
result := GetTargets(targets)
37+
38+
if len(result) != 1 {
39+
t.Errorf("Expected 1 AppDetail, got %d", len(result))
40+
}
41+
42+
if result[0].LabelMatchMode != "union" {
43+
t.Errorf("Expected LabelMatchMode to be 'union', got '%s'", result[0].LabelMatchMode)
44+
}
45+
46+
if result[0].Kind != "statefulset" {
47+
t.Errorf("Expected Kind to be 'statefulset', got '%s'", result[0].Kind)
48+
}
49+
}
50+
51+
func TestGetTargets_IntersectionMode(t *testing.T) {
52+
// Test intersection mode
53+
targets := "cluster:default:[cnpg.io/instanceRole=primary,cnpg.io/cluster=pg-eu]:intersection"
54+
result := GetTargets(targets)
55+
56+
if len(result) != 1 {
57+
t.Errorf("Expected 1 AppDetail, got %d", len(result))
58+
}
59+
60+
if result[0].LabelMatchMode != "intersection" {
61+
t.Errorf("Expected LabelMatchMode to be 'intersection', got '%s'", result[0].LabelMatchMode)
62+
}
63+
64+
if result[0].Kind != "cluster" {
65+
t.Errorf("Expected Kind to be 'cluster', got '%s'", result[0].Kind)
66+
}
67+
68+
if result[0].Namespace != "default" {
69+
t.Errorf("Expected Namespace to be 'default', got '%s'", result[0].Namespace)
70+
}
71+
72+
if len(result[0].Labels) != 2 {
73+
t.Errorf("Expected 2 labels, got %d", len(result[0].Labels))
74+
}
75+
76+
expectedLabels := []string{"cnpg.io/instanceRole=primary", "cnpg.io/cluster=pg-eu"}
77+
for i, label := range result[0].Labels {
78+
if label != expectedLabels[i] {
79+
t.Errorf("Expected label[%d] to be '%s', got '%s'", i, expectedLabels[i], label)
80+
}
81+
}
82+
}
83+
84+
func TestGetTargets_InvalidMode(t *testing.T) {
85+
// Test that invalid mode falls back to union
86+
targets := "deployment:default:[app=nginx]:invalid"
87+
result := GetTargets(targets)
88+
89+
if len(result) != 1 {
90+
t.Errorf("Expected 1 AppDetail, got %d", len(result))
91+
}
92+
93+
// Invalid mode should fall back to union
94+
if result[0].LabelMatchMode != "union" {
95+
t.Errorf("Expected invalid mode to fall back to 'union', got '%s'", result[0].LabelMatchMode)
96+
}
97+
}
98+
99+
func TestGetTargets_MultipleSemicolonSeparated(t *testing.T) {
100+
// Test multiple targets with different modes
101+
targets := "deployment:ns1:[app=web]:union;statefulset:ns2:[db=postgres,env=prod]:intersection"
102+
result := GetTargets(targets)
103+
104+
if len(result) != 2 {
105+
t.Errorf("Expected 2 AppDetails, got %d", len(result))
106+
}
107+
108+
// First target - union
109+
if result[0].LabelMatchMode != "union" {
110+
t.Errorf("Expected first target LabelMatchMode to be 'union', got '%s'", result[0].LabelMatchMode)
111+
}
112+
113+
// Second target - intersection
114+
if result[1].LabelMatchMode != "intersection" {
115+
t.Errorf("Expected second target LabelMatchMode to be 'intersection', got '%s'", result[1].LabelMatchMode)
116+
}
117+
}
118+
119+
func TestGetTargets_WithNames(t *testing.T) {
120+
// Test that Names parsing still works with the new field
121+
targets := "pod:default:[pod1,pod2,pod3]"
122+
result := GetTargets(targets)
123+
124+
if len(result) != 1 {
125+
t.Errorf("Expected 1 AppDetail, got %d", len(result))
126+
}
127+
128+
if len(result[0].Names) != 3 {
129+
t.Errorf("Expected 3 names, got %d", len(result[0].Names))
130+
}
131+
132+
if len(result[0].Labels) != 0 {
133+
t.Errorf("Expected 0 labels when Names are provided, got %d", len(result[0].Labels))
134+
}
135+
136+
// Default mode should still be union
137+
if result[0].LabelMatchMode != "union" {
138+
t.Errorf("Expected default LabelMatchMode to be 'union', got '%s'", result[0].LabelMatchMode)
139+
}
140+
}

0 commit comments

Comments
 (0)