-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsync.test.ts
More file actions
617 lines (534 loc) · 18.6 KB
/
sync.test.ts
File metadata and controls
617 lines (534 loc) · 18.6 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
import * as assert from "@std/assert";
import { runtime } from "@eserstack/standards/cross-runtime";
import * as formats from "@eserstack/formats";
import {
buildConfigMapFromContext,
buildSecretFromContext,
KubernetesResourceNameError,
sync,
validateKubernetesResourceName,
validateResourceReference,
} from "./sync.ts";
// Test helper to set up environment variables
function setupTestEnv(): Record<string, string> {
const testEnv = {
"DD_SITE": "datadoghq.com",
"DD_API_KEY": "test-api-key",
"DB_HOST": "localhost",
"DB_PORT": "5432",
"DB_NAME": "test_db",
};
for (const [key, value] of Object.entries(testEnv)) {
runtime.env.set(key, value);
}
return testEnv;
}
// Test helper to clean up environment variables
function cleanupTestEnv(keys: string[]) {
for (const key of keys) {
runtime.env.delete(key);
}
}
Deno.test("buildConfigMapFromContext() should create valid ConfigMap", () => {
const data = new Map([
["DD_SITE", "datadoghq.com"],
["DD_API_KEY", "test-api-key"],
["DB_HOST", "localhost"],
]);
const configMap = buildConfigMapFromContext(
"test-config",
"test-namespace",
data,
);
assert.assertEquals(configMap.apiVersion, "v1");
assert.assertEquals(configMap.kind, "ConfigMap");
assert.assertEquals(configMap.metadata.name, "test-config");
assert.assertEquals(configMap.metadata.namespace, "test-namespace");
assert.assertEquals(configMap.data?.["DD_SITE"], "datadoghq.com");
assert.assertEquals(configMap.data?.["DD_API_KEY"], "test-api-key");
assert.assertEquals(configMap.data?.["DB_HOST"], "localhost");
});
Deno.test("buildConfigMapFromContext() should omit default namespace", () => {
const data = new Map([["TEST_VAR", "test-value"]]);
const configMap = buildConfigMapFromContext(
"default-config",
"default",
data,
);
assert.assertEquals(configMap.metadata.name, "default-config");
assert.assertEquals(configMap.metadata.namespace, undefined);
});
Deno.test("buildConfigMapFromContext() should handle undefined namespace", () => {
const data = new Map([["TEST_VAR", "test-value"]]);
const configMap = buildConfigMapFromContext(
"no-namespace-config",
undefined,
data,
);
assert.assertEquals(configMap.metadata.name, "no-namespace-config");
assert.assertEquals(configMap.metadata.namespace, undefined);
});
Deno.test("buildSecretFromContext() should create valid Secret with base64 encoding", () => {
const data = new Map([
["DD_SITE", "datadoghq.com"],
["DD_API_KEY", "test-api-key"],
["DB_PASSWORD", "secret123"],
]);
const secret = buildSecretFromContext("test-secret", "test-namespace", data);
assert.assertEquals(secret.apiVersion, "v1");
assert.assertEquals(secret.kind, "Secret");
assert.assertEquals(secret.metadata.name, "test-secret");
assert.assertEquals(secret.metadata.namespace, "test-namespace");
assert.assertEquals(secret.type, "Opaque");
// Verify base64 encoding
assert.assertEquals(secret.data?.["DD_SITE"], btoa("datadoghq.com"));
assert.assertEquals(secret.data?.["DD_API_KEY"], btoa("test-api-key"));
assert.assertEquals(secret.data?.["DB_PASSWORD"], btoa("secret123"));
});
Deno.test("buildSecretFromContext() should handle special characters in values", () => {
const data = new Map([
["SPECIAL_VAR", "value with spaces & symbols!@#$%"],
["MULTILINE_VAR", "line1\\nline2\\nline3"],
]);
const secret = buildSecretFromContext("special-secret", "test-ns", data);
assert.assertEquals(
secret.data?.["SPECIAL_VAR"],
btoa("value with spaces & symbols!@#$%"),
);
assert.assertEquals(
secret.data?.["MULTILINE_VAR"],
btoa("line1\\nline2\\nline3"),
);
});
Deno.test("buildSecretFromContext() should omit default namespace", () => {
const data = new Map([["TEST_SECRET", "secret-value"]]);
const secret = buildSecretFromContext("default-secret", "default", data);
assert.assertEquals(secret.metadata.name, "default-secret");
assert.assertEquals(secret.metadata.namespace, undefined);
});
Deno.test("buildSecretFromContext() should handle empty data", () => {
const secret = buildSecretFromContext("empty-secret", "test-ns", new Map());
assert.assertEquals(secret.kind, "Secret");
assert.assertEquals(secret.metadata.name, "empty-secret");
assert.assertEquals(Object.keys(secret.data ?? {}).length, 0);
});
Deno.test("sync() should handle empty kubectl response", async () => {
try {
// We can't easily mock the import, so we test the expected behavior
const result = await sync({
resource: { type: "configmap", name: "empty-config" },
});
// Should handle empty result gracefully
assert.assertStringIncludes(result, "No data found");
} catch (error) {
// Expected if kubectl is not available in test environment
assert.assertStringIncludes(
(error as Error).message,
"kubectl",
);
}
});
Deno.test("sync() should generate kubectl patch command for configmap type", async () => {
const testEnv = setupTestEnv();
try {
// This test will fail in environments without kubectl, which is expected
// In a real test environment, we'd mock the kubectl execution
const result = await sync({
resource: {
type: "configmap",
name: "test-config",
namespace: "test-ns",
},
});
// This assertion will only run if kubectl is available
if (
!result.includes("No data found") && !result.includes("Failed to sync")
) {
assert.assertStringIncludes(result, "kubectl patch cm test-config");
assert.assertStringIncludes(result, "-n test-ns");
assert.assertStringIncludes(result, "--type=merge");
}
} catch (error) {
// Expected behavior when kubectl is not available
assert.assertStringIncludes((error as Error).message, "kubectl");
} finally {
cleanupTestEnv(Object.keys(testEnv));
}
});
Deno.test("sync() should generate kubectl patch command for secret type", async () => {
const testEnv = setupTestEnv();
try {
const result = await sync({
resource: { type: "secret", name: "test-secret", namespace: "test-ns" },
});
// This assertion will only run if kubectl is available
if (
!result.includes("No data found") && !result.includes("Failed to sync")
) {
assert.assertStringIncludes(result, "kubectl patch secret test-secret");
assert.assertStringIncludes(result, "-n test-ns");
assert.assertStringIncludes(result, "--type=merge");
}
} catch (error) {
// Expected behavior when kubectl is not available
assert.assertStringIncludes((error as Error).message, "kubectl");
} finally {
cleanupTestEnv(Object.keys(testEnv));
}
});
Deno.test("sync() should generate proper kubectl patch command format", async () => {
const testEnv = setupTestEnv();
try {
const result = await sync({
resource: { type: "configmap", name: "json-config" },
});
// This assertion will only run if kubectl is available and returns data
if (
!result.includes("No data found") && !result.includes("Failed to sync")
) {
// Should contain proper kubectl patch format
assert.assertStringIncludes(result, "kubectl patch cm json-config");
assert.assertStringIncludes(result, "--type=merge");
assert.assertStringIncludes(result, '-p \'{"data":');
}
} catch (error) {
// Expected behavior when kubectl is not available
assert.assertStringIncludes((error as Error).message, "kubectl");
} finally {
cleanupTestEnv(Object.keys(testEnv));
}
});
Deno.test("sync() should support YAML format patch commands", async () => {
const testEnv = setupTestEnv();
try {
const result = await sync({
resource: { type: "configmap", name: "yaml-config" },
format: "yaml",
});
if (
!result.includes("No data found") && !result.includes("Failed to sync")
) {
// Should contain YAML format
assert.assertStringIncludes(result, "kubectl patch cm yaml-config");
assert.assertStringIncludes(result, "--type=merge");
assert.assertStringIncludes(result, "data:");
}
} catch (error) {
assert.assertStringIncludes((error as Error).message, "kubectl");
} finally {
cleanupTestEnv(Object.keys(testEnv));
}
});
Deno.test("sync() should support string-only output", async () => {
const testEnv = setupTestEnv();
try {
const result = await sync({
resource: { type: "configmap", name: "string-only-config" },
stringOnly: true,
});
if (
!result.includes("No data found") && !result.includes("Failed to sync")
) {
// Should only contain JSON patch data, no kubectl command
assert.assertStringIncludes(result, '{"data":');
assert.assert(!result.includes("kubectl"));
}
} catch (error) {
assert.assertStringIncludes((error as Error).message, "kubectl");
} finally {
cleanupTestEnv(Object.keys(testEnv));
}
});
// CLI parsing tests (testing the parseKubectlResource function indirectly)
Deno.test("CLI should handle configmap resource format", () => {
// Test that our resource type mapping works correctly
const configMapType = "configmap";
assert.assertEquals(configMapType, "configmap");
const cmShortType = "cm";
// In the actual implementation, "cm" gets mapped to "configmap"
assert.assertEquals(
cmShortType === "cm" ? "configmap" : cmShortType,
"configmap",
);
});
Deno.test("CLI should handle secret resource format", () => {
const secretType = "secret";
assert.assertEquals(secretType, "secret");
});
// Test integration with @eserstack/formats for YAML/JSON output
Deno.test("ConfigMap YAML output should be properly formatted", () => {
const configMap = buildConfigMapFromContext(
"format-test",
"test-ns",
new Map([
["APP_NAME", "my-app"],
["APP_VERSION", "1.0.0"],
]),
);
const yamlOutput = formats.serialize([configMap], "yaml", { pretty: true });
assert.assertStringIncludes(yamlOutput, "apiVersion: v1");
assert.assertStringIncludes(yamlOutput, "kind: ConfigMap");
assert.assertStringIncludes(yamlOutput, "name: format-test");
assert.assertStringIncludes(yamlOutput, "namespace: test-ns");
assert.assertStringIncludes(yamlOutput, "APP_NAME: my-app");
assert.assertStringIncludes(yamlOutput, "APP_VERSION: 1.0.0");
});
Deno.test("Secret YAML output should be properly formatted", () => {
const secret = buildSecretFromContext(
"secret-format-test",
"test-ns",
new Map([
["API_KEY", "secret123"],
["DB_PASSWORD", "password456"],
]),
);
const yamlOutput = formats.serialize([secret], "yaml", { pretty: true });
assert.assertStringIncludes(yamlOutput, "apiVersion: v1");
assert.assertStringIncludes(yamlOutput, "kind: Secret");
assert.assertStringIncludes(yamlOutput, "name: secret-format-test");
assert.assertStringIncludes(yamlOutput, "namespace: test-ns");
assert.assertStringIncludes(yamlOutput, "type: Opaque");
// Data should be base64 encoded
assert.assertStringIncludes(yamlOutput, btoa("secret123"));
assert.assertStringIncludes(yamlOutput, btoa("password456"));
});
Deno.test("JSON output should be valid JSON", () => {
const configMap = buildConfigMapFromContext(
"json-format-test",
"test-ns",
new Map([
["CONFIG_KEY", "config-value"],
]),
);
const jsonOutput = formats.serialize([configMap], "json", { pretty: true });
// Should be parseable as JSON
const parsed = JSON.parse(jsonOutput);
assert.assertEquals(Array.isArray(parsed), true);
assert.assertEquals(parsed[0].apiVersion, "v1");
assert.assertEquals(parsed[0].kind, "ConfigMap");
assert.assertEquals(parsed[0].metadata.name, "json-format-test");
assert.assertEquals(parsed[0].data["CONFIG_KEY"], "config-value");
});
// Error handling tests
Deno.test("sync() should handle kubectl command failures", async () => {
// This tests error handling when kubectl command fails
try {
const result = await sync({
resource: { type: "configmap", name: "nonexistent-resource" },
});
// Should either return a "No data found" message or throw an error
if (
!result.includes("No data found") && !result.includes("Failed to sync")
) {
// If it doesn't contain expected messages, kubectl was not available
assert.assertStringIncludes(result, "ConfigMap");
}
} catch (error) {
// Expected behavior when kubectl fails
assert.assertStringIncludes((error as Error).message, "kubectl");
}
});
// Test that demonstrates the full workflow
Deno.test("Full workflow test with mock data", () => {
const testEnv = setupTestEnv();
try {
// Simulate environment values for the test
const envValues = new Map([
["DD_SITE", "datadoghq.com"],
["DD_API_KEY", "test-api-key"],
["DB_HOST", "localhost"],
]);
// Build ConfigMap
const configMap = buildConfigMapFromContext(
"workflow-test",
"test-ns",
envValues,
);
// Generate YAML output
const yamlOutput = formats.serialize([configMap], "yaml", { pretty: true });
// Verify the complete workflow
assert.assertStringIncludes(yamlOutput, "kind: ConfigMap");
assert.assertStringIncludes(yamlOutput, "name: workflow-test");
assert.assertStringIncludes(yamlOutput, "DD_SITE: datadoghq.com");
assert.assertStringIncludes(yamlOutput, "DD_API_KEY: test-api-key");
assert.assertStringIncludes(yamlOutput, "DB_HOST: localhost");
// Test Secret workflow
const secret = buildSecretFromContext(
"workflow-secret",
"test-ns",
envValues,
);
const secretYaml = formats.serialize([secret], "yaml", { pretty: true });
assert.assertStringIncludes(secretYaml, "kind: Secret");
assert.assertStringIncludes(secretYaml, "name: workflow-secret");
assert.assertStringIncludes(secretYaml, "type: Opaque");
// Verify base64 encoding
assert.assertStringIncludes(secretYaml, btoa("datadoghq.com"));
assert.assertStringIncludes(secretYaml, btoa("test-api-key"));
} finally {
cleanupTestEnv(Object.keys(testEnv));
}
});
// ============================================================================
// Kubernetes Resource Name Validation Tests (Security)
// ============================================================================
Deno.test("validateKubernetesResourceName() should accept valid names", () => {
// Valid RFC 1123 DNS subdomain names
const validNames = [
"a",
"my-app",
"my-app-v1",
"app123",
"123app",
"a1b2c3",
"my.app.name",
"my-app.v1.release",
"a".repeat(253), // Maximum length
];
for (const name of validNames) {
// Should not throw
validateKubernetesResourceName(name);
}
});
Deno.test("validateKubernetesResourceName() should reject empty names", () => {
assert.assertThrows(
() => validateKubernetesResourceName(""),
KubernetesResourceNameError,
"cannot be empty",
);
});
Deno.test("validateKubernetesResourceName() should reject names exceeding 253 characters", () => {
const longName = "a".repeat(254);
assert.assertThrows(
() => validateKubernetesResourceName(longName),
KubernetesResourceNameError,
"253 characters or less",
);
});
Deno.test("validateKubernetesResourceName() should reject names with uppercase letters", () => {
assert.assertThrows(
() => validateKubernetesResourceName("MyApp"),
KubernetesResourceNameError,
"lowercase alphanumeric",
);
});
Deno.test("validateKubernetesResourceName() should reject names starting with non-alphanumeric", () => {
const invalidStarts = ["-app", ".app", "_app"];
for (const name of invalidStarts) {
assert.assertThrows(
() => validateKubernetesResourceName(name),
KubernetesResourceNameError,
);
}
});
Deno.test("validateKubernetesResourceName() should reject names ending with non-alphanumeric", () => {
const invalidEnds = ["app-", "app.", "app_"];
for (const name of invalidEnds) {
assert.assertThrows(
() => validateKubernetesResourceName(name),
KubernetesResourceNameError,
);
}
});
Deno.test("validateKubernetesResourceName() should reject names with shell metacharacters", () => {
// These characters could be used for command injection
const dangerousNames = [
"app;ls",
"app|cat",
"app$HOME",
"app`id`",
"app$(whoami)",
"app&",
"app>file",
"app<file",
"app'test",
'app"test',
"app\\test",
"app\ntest",
"app test", // spaces
];
for (const name of dangerousNames) {
assert.assertThrows(
() => validateKubernetesResourceName(name),
KubernetesResourceNameError,
);
}
});
Deno.test("validateKubernetesResourceName() should reject names with consecutive dots or dashes", () => {
assert.assertThrows(
() => validateKubernetesResourceName("app..name"),
KubernetesResourceNameError,
"consecutive dots or dashes",
);
assert.assertThrows(
() => validateKubernetesResourceName("app--name"),
KubernetesResourceNameError,
"consecutive dots or dashes",
);
});
Deno.test("validateKubernetesResourceName() should use custom field name in error messages", () => {
try {
validateKubernetesResourceName("", "namespace");
assert.assert(false, "Should have thrown");
} catch (error) {
assert.assertStringIncludes((error as Error).message, "namespace");
}
});
Deno.test("validateResourceReference() should validate both name and namespace", () => {
// Valid reference
validateResourceReference({
type: "configmap",
name: "my-config",
namespace: "my-namespace",
});
// Valid reference without namespace
validateResourceReference({
type: "secret",
name: "my-secret",
});
});
Deno.test("validateResourceReference() should reject invalid resource names", () => {
assert.assertThrows(
() =>
validateResourceReference({
type: "configmap",
name: "Invalid-Name",
namespace: "default",
}),
KubernetesResourceNameError,
);
});
Deno.test("validateResourceReference() should reject invalid namespace", () => {
assert.assertThrows(
() =>
validateResourceReference({
type: "configmap",
name: "my-config",
namespace: "Invalid-Namespace",
}),
KubernetesResourceNameError,
);
});
Deno.test("sync() should reject invalid resource names", async () => {
await assert.assertRejects(
() =>
sync({
resource: { type: "configmap", name: "invalid;name" },
}),
KubernetesResourceNameError,
);
});
Deno.test("sync() should reject invalid namespace", async () => {
await assert.assertRejects(
() =>
sync({
resource: {
type: "configmap",
name: "valid-name",
namespace: "invalid|namespace",
},
}),
KubernetesResourceNameError,
);
});