-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit_test.go
More file actions
336 lines (307 loc) · 10.2 KB
/
Copy pathgit_test.go
File metadata and controls
336 lines (307 loc) · 10.2 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// SPDX-License-Identifier: Apache-2.0
package git
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ConfigButler/gitops-reverser/internal/ssh"
"github.com/ConfigButler/gitops-reverser/internal/types"
)
func TestIsValidTargetPath(t *testing.T) {
cases := []struct {
name string
path string
want bool
}{
{"empty is root", "", true},
{"dot is root", ".", true},
{"whitespace is root", " ", true},
{"clean relative path", "team/app", true},
{"trailing slash ok", "team/app/", true},
{"absolute rejected", "/team", false},
{"absolute root slash rejected", "/", false},
{"parent traversal rejected", "../team", false},
{"embedded traversal rejected", "team/../other", false},
{"backslash rejected", "team\\app", false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, IsValidTargetPath(tc.path))
})
}
}
func TestToGitPath_NamespacedResource(t *testing.T) {
testCases := []struct {
name string
namespace string
group string
version string
resourcePlural string
expected string
}{
{
name: "test-pod",
namespace: "default",
group: "",
version: "v1",
resourcePlural: "pods",
expected: "default/pods/test-pod.yaml",
},
{
name: "my-service",
namespace: "production",
group: "",
version: "v1",
resourcePlural: "services",
expected: "production/services/my-service.yaml",
},
{
name: "app-config",
namespace: "staging",
group: "",
version: "v1",
resourcePlural: "configmaps",
expected: "staging/configmaps/app-config.yaml",
},
{
name: "complex-name-with-dashes",
namespace: "kube-system",
group: "apps",
version: "v1",
resourcePlural: "deployments",
expected: "kube-system/apps/deployments/complex-name-with-dashes.yaml",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
identifier := types.ResourceIdentifier{
Group: tc.group,
Version: tc.version,
Resource: tc.resourcePlural,
Namespace: tc.namespace,
Name: tc.name,
}
path := identifier.ToGitPath()
assert.Equal(t, tc.expected, path)
})
}
}
func TestToGitPath_ClusterScopedResource(t *testing.T) {
testCases := []struct {
name string
group string
version string
resourcePlural string
expected string
}{
{
name: "my-namespace",
group: "",
version: "v1",
resourcePlural: "namespaces",
expected: "_cluster/namespaces/my-namespace.yaml",
},
{
name: "cluster-admin",
group: "rbac.authorization.k8s.io",
version: "v1",
resourcePlural: "clusterroles",
expected: "_cluster/rbac.authorization.k8s.io/clusterroles/cluster-admin.yaml",
},
{
name: "system-binding",
group: "rbac.authorization.k8s.io",
version: "v1",
resourcePlural: "clusterrolebindings",
expected: "_cluster/rbac.authorization.k8s.io/clusterrolebindings/system-binding.yaml",
},
{
name: "my-pv",
group: "",
version: "v1",
resourcePlural: "persistentvolumes",
expected: "_cluster/persistentvolumes/my-pv.yaml",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
identifier := types.ResourceIdentifier{
Group: tc.group,
Version: tc.version,
Resource: tc.resourcePlural,
Namespace: "",
Name: tc.name,
}
path := identifier.ToGitPath()
assert.Equal(t, tc.expected, path)
})
}
}
func TestToGitPath_EmptyNamespace(t *testing.T) {
identifier := types.ResourceIdentifier{
Group: "",
Version: "v1",
Resource: "testkinds",
Namespace: "", // Empty namespace means cluster-scoped
Name: "test-resource",
}
path := identifier.ToGitPath()
assert.Equal(t, "_cluster/testkinds/test-resource.yaml", path)
}
func TestToGitPath_SpecialCharacters(t *testing.T) {
testCases := []struct {
name string
namespace string
resourcePlural string
expected string
}{
{
name: "test.resource",
namespace: "default",
resourcePlural: "pods",
expected: "default/pods/test.resource.yaml",
},
{
name: "test_resource",
namespace: "default",
resourcePlural: "services",
expected: "default/services/test_resource.yaml",
},
{
name: "test-resource-123",
namespace: "test-ns-456",
resourcePlural: "configmaps",
expected: "test-ns-456/configmaps/test-resource-123.yaml",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
identifier := types.ResourceIdentifier{
Group: "",
Version: "v1",
Resource: tc.resourcePlural,
Namespace: tc.namespace,
Name: tc.name,
}
path := identifier.ToGitPath()
assert.Equal(t, tc.expected, path)
})
}
}
func TestGetAuthMethod_ValidKey(t *testing.T) {
// Since we're testing with a fake key, we expect this to fail
// In a real scenario, this would be a valid SSH private key
privateKey := `-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA4f5wg5l2hKsTeNem/V41fGnJm6gOdrj8ym3rFkEjWT2btYhA
z2R6eMhF+o/5BjKff/1hdX7O+9AHjfTjHOKVn0+aWNs2fQIDAQABAoIBABYWnohQ
e+3Iw1AbYbvylpP2yv9otaanmT0Dcn2TlBXqBTfnIFLd5vbmbnw3WEg5Zf9/5cqm
3z8/Lu8EYFnagqGjlwM62YWtHBtDtrjI2d01q/DuLBGXHFTn/H49TXfn7pwqYBwJ
of5c89fDoGhyoMpo0eDidnH2/cjjS+MCRcNGlWdVrRHpeqGWmj/aaKdVNNepkvdx
piDsrv7TklTOQ+h5VKQY9/myQAEfEczRylCghrWoZVT/OgKX6iZbBHtccHMmrHYr
5DaCWEAEhsJtQJNwKuOB/Dxw6tWdrwm5Mi8AoGBAOjeAjjsWDQmBmxHEkNoFqiGm
T6+dmN2VYBUoVBHtfwpJOFn9E2ynwuJekfwfvQy+Oc/epjyoTuxtbYpx5jjVZiHn
2LLEhCh/G7aQ+9TiuHmNiRpTMuGqxRbAueMI5PlHMlMQnVqsr8jDKBx+f1lFlDc3
xmyh+iFc9TAPNkGSIb2z
-----END RSA PRIVATE KEY-----`
// Test that the function handles invalid keys properly
auth, err := ssh.GetAuthMethod(privateKey, "", "", false)
require.Error(t, err) // Expect error with test key
assert.Nil(t, auth)
}
func TestGetAuthMethod_WithPassphrase(t *testing.T) {
// For passphrase test, we'll test with an encrypted key
// This is a test key encrypted with passphrase "test123"
privateKey := `-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,C8EFDB5A150B0C5F726E8F280553D4AC
kARLcgxZKwaANTVNKBwQqOgbOQJf8NTSQbOvV8eIhOwQJmkOnyoBJEOwQJmkOnyoB
JEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoB
JEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoB
JEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoB
JEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoB
JEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoB
JEOwQJmkOnyoBJEOwQJmkOnyoBJEOw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B
JEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoB
JEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoB
JEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoBJEOwQJmkOnyoB
-----END RSA PRIVATE KEY-----`
passphrase := "test123"
// Since this is a fake encrypted key, it will still fail
// Let's change this test to expect an error for now
auth, err := ssh.GetAuthMethod(privateKey, passphrase, "", false)
require.Error(t, err) // Expect error with fake key
assert.Nil(t, auth)
}
func TestGetAuthMethod_InvalidKey(t *testing.T) {
invalidKey := "this-is-not-a-valid-ssh-key"
auth, err := ssh.GetAuthMethod(invalidKey, "", "", false)
require.Error(t, err)
assert.Nil(t, auth)
}
func TestGetAuthMethod_EmptyKey(t *testing.T) {
auth, err := ssh.GetAuthMethod("", "", "", false)
require.Error(t, err)
assert.Nil(t, auth)
}
func TestCommitFile_Structure(t *testing.T) {
file := CommitFile{
Path: "namespaces/default/Pod/test-pod.yaml",
Content: []byte("apiVersion: v1\nkind: Pod\nmetadata:\n name: test-pod"),
}
assert.Equal(t, "namespaces/default/Pod/test-pod.yaml", file.Path)
assert.Contains(t, string(file.Content), "apiVersion: v1")
assert.Contains(t, string(file.Content), "kind: Pod")
assert.Contains(t, string(file.Content), "name: test-pod")
}
func TestEdgeCases_NilIdentifier(t *testing.T) {
// Test behavior with empty identifier
identifier := types.ResourceIdentifier{}
path := identifier.ToGitPath()
// Empty identifier will produce minimal path
assert.NotEmpty(t, path)
}
func TestPathGeneration_ConsistentOutput(t *testing.T) {
// Test that the same input always produces the same output
identifier := types.ResourceIdentifier{
Group: "",
Version: "v1",
Resource: "pods",
Namespace: "consistent-ns",
Name: "consistent-test",
}
path1 := identifier.ToGitPath()
path2 := identifier.ToGitPath()
path3 := identifier.ToGitPath()
assert.Equal(t, path1, path2)
assert.Equal(t, path2, path3)
assert.Equal(t, "consistent-ns/pods/consistent-test.yaml", path1)
}
func TestToGitPath_DeleteOperation(t *testing.T) {
// Test that file paths are consistent regardless of operation
identifier := types.ResourceIdentifier{
Group: "",
Version: "v1",
Resource: "secrets",
Namespace: "production",
Name: "test-resource",
}
// File path should be same for CREATE, UPDATE, and DELETE
path := identifier.ToGitPath()
expected := "production/secrets/test-resource.yaml"
assert.Equal(t, expected, path)
}