-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathpatch_test.go
More file actions
268 lines (251 loc) · 7.59 KB
/
patch_test.go
File metadata and controls
268 lines (251 loc) · 7.59 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package chartify
import (
"os"
"path/filepath"
"strings"
"testing"
)
// TestPatch_PreserveCRDLocation tests that CRDs from templates/crds/ stay in templates/crds/
// This is the fix for https://github.com/helmfile/helmfile/issues/2291
func TestPatch_PreserveCRDLocation(t *testing.T) {
tests := []struct {
name string
setupFiles map[string]string // path -> content
expectedCRDLocation string // Expected directory for CRDs after patching
expectedCRDsPreserved bool // Should CRDs be in templates/crds/?
}{
{
name: "CRDs from templates/crds/ should stay in templates/crds/",
setupFiles: map[string]string{
"templates/crds/crd-scaledobjects.yaml": `apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: scaledobjects.keda.sh
spec:
group: keda.sh
names:
kind: ScaledObject`,
"templates/deployment.yaml": `apiVersion: apps/v1
kind: Deployment
metadata:
name: test-deployment
spec:
replicas: 1`,
},
expectedCRDLocation: "templates/crds",
expectedCRDsPreserved: true,
},
{
name: "CRDs from root crds/ should go to crds/",
setupFiles: map[string]string{
"crds/crd-example.yaml": `apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: examples.test.io
spec:
group: test.io
names:
kind: Example`,
"templates/deployment.yaml": `apiVersion: apps/v1
kind: Deployment
metadata:
name: test-deployment`,
},
expectedCRDLocation: "crds",
expectedCRDsPreserved: false,
},
{
name: "Mixed resources without CRDs in templates/crds/",
setupFiles: map[string]string{
"templates/configmap.yaml": `apiVersion: v1
kind: ConfigMap
metadata:
name: test-cm
data:
key: value`,
"templates/service.yaml": `apiVersion: v1
kind: Service
metadata:
name: test-svc
spec:
ports:
- port: 80`,
},
expectedCRDLocation: "crds", // Default location when no CRDs in templates/
expectedCRDsPreserved: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create temporary directory
tempDir, err := os.MkdirTemp("", "chartify-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
// Setup test files
var generatedFiles []string
for path, content := range tt.setupFiles {
fullPath := filepath.Join(tempDir, path)
dir := filepath.Dir(fullPath)
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("Failed to create directory %s: %v", dir, err)
}
if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil {
t.Fatalf("Failed to write file %s: %v", fullPath, err)
}
generatedFiles = append(generatedFiles, fullPath)
}
// Test the detection logic
crdsFromTemplates := false
for _, f := range generatedFiles {
relPath := strings.Replace(f, tempDir+string(filepath.Separator), "", 1)
// Normalize path separators for cross-platform compatibility
relPath = filepath.ToSlash(relPath)
// Use same logic as the fix
if strings.HasPrefix(relPath, "templates/crds/") {
crdsFromTemplates = true
break
}
}
// Verify detection result matches expectation
if crdsFromTemplates != tt.expectedCRDsPreserved {
t.Errorf("CRD detection mismatch: got %v, want %v", crdsFromTemplates, tt.expectedCRDsPreserved)
}
// Verify expected CRD location logic
var expectedDir string
if crdsFromTemplates {
expectedDir = filepath.Join(tempDir, "templates", "crds")
} else {
expectedDir = filepath.Join(tempDir, "crds")
}
// Normalize paths and check if expected location is in the path
expectedNorm := filepath.ToSlash(expectedDir)
wantNorm := filepath.ToSlash(filepath.Join(tempDir, tt.expectedCRDLocation))
if !strings.HasPrefix(expectedNorm, wantNorm) {
t.Errorf("Expected CRD location mismatch: got %s, want to contain %s",
expectedDir, tt.expectedCRDLocation)
}
})
}
}
// TestPatch_CRDLocationDetection tests the CRD location detection logic in isolation
func TestPatch_CRDLocationDetection(t *testing.T) {
tests := []struct {
name string
relPaths []string // Already relative paths (what we get after removing tempDir)
wantTemplates bool
}{
{
name: "templates/crds/ files should be detected",
relPaths: []string{
"templates/crds/crd1.yaml",
"templates/deployment.yaml",
},
wantTemplates: true,
},
{
name: "root crds/ should not be detected as templates",
relPaths: []string{
"crds/crd1.yaml",
"templates/deployment.yaml",
},
wantTemplates: false,
},
{
name: "no crds should not be detected",
relPaths: []string{
"templates/deployment.yaml",
"templates/service.yaml",
},
wantTemplates: false,
},
{
name: "templates with other subdirs, no crds",
relPaths: []string{
"templates/manager/deployment.yaml",
"templates/webhooks/service.yaml",
},
wantTemplates: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Simulate the detection logic (same as in patch.go)
crdsFromTemplates := false
for _, relPath := range tt.relPaths {
// Normalize path separators for cross-platform compatibility
relPath = filepath.ToSlash(relPath)
// Check if path starts with templates/crds/
if strings.HasPrefix(relPath, "templates/crds/") {
crdsFromTemplates = true
break
}
}
if crdsFromTemplates != tt.wantTemplates {
t.Errorf("Detection mismatch for %v: got %v, want %v",
tt.relPaths, crdsFromTemplates, tt.wantTemplates)
}
})
}
}
// TestPatch_KEDA_RealWorld tests the fix with a structure similar to the real KEDA chart
// This verifies the fix for issue #2291
func TestPatch_KEDA_RealWorld(t *testing.T) {
tempDir, err := os.MkdirTemp("", "chartify-keda-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
// Create a structure similar to KEDA chart
kedaCRDs := map[string]string{
"templates/crds/crd-scaledobjects.yaml": `apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: scaledobjects.keda.sh
spec:
group: keda.sh`,
"templates/crds/crd-scaledjobs.yaml": `apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: scaledjobs.keda.sh
spec:
group: keda.sh`,
"templates/manager/deployment.yaml": `apiVersion: apps/v1
kind: Deployment
metadata:
name: keda-operator
spec:
replicas: 1`,
}
var generatedFiles []string
for path, content := range kedaCRDs {
fullPath := filepath.Join(tempDir, path)
dir := filepath.Dir(fullPath)
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("Failed to create dir: %v", err)
}
if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil {
t.Fatalf("Failed to write file: %v", err)
}
generatedFiles = append(generatedFiles, fullPath)
}
// Test the fix: CRDs from templates/crds/ should be preserved
crdsFromTemplates := false
for _, f := range generatedFiles {
relPath := strings.Replace(f, tempDir+string(filepath.Separator), "", 1)
// Normalize path separators for cross-platform compatibility
relPath = filepath.ToSlash(relPath)
if strings.HasPrefix(relPath, "templates/crds/") {
crdsFromTemplates = true
t.Logf("Detected CRD in templates/: %s", relPath)
break
}
}
if !crdsFromTemplates {
t.Errorf("KEDA-like chart: Expected to detect CRDs in templates/crds/, but did not")
}
// Verify the CRDs would be placed in templates/crds/ (preserving location)
expectedCRDDir := filepath.Join(tempDir, "templates", "crds")
t.Logf("CRDs would be placed in: %s (original location preserved)", expectedCRDDir)
}