|
1 | 1 | import { Command } from 'commander'; |
2 | | -import { parseEnvironmentVariables, parseDomains, escapeShellArg, joinShellArgs } from './cli'; |
| 2 | +import { parseEnvironmentVariables, parseDomains, escapeShellArg, joinShellArgs, parseVolumeMounts } from './cli'; |
3 | 3 | import { redactSecrets } from './redact-secrets'; |
| 4 | +import * as fs from 'fs'; |
| 5 | +import * as path from 'path'; |
| 6 | +import * as os from 'os'; |
4 | 7 |
|
5 | 8 | describe('cli', () => { |
6 | 9 | describe('domain parsing', () => { |
@@ -338,4 +341,177 @@ describe('cli', () => { |
338 | 341 | expect(dir).toMatch(/^\/tmp\//); |
339 | 342 | }); |
340 | 343 | }); |
| 344 | + |
| 345 | + describe('volume mount parsing', () => { |
| 346 | + let testDir: string; |
| 347 | + |
| 348 | + beforeEach(() => { |
| 349 | + // Create a temporary directory for testing |
| 350 | + testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'awf-test-')); |
| 351 | + }); |
| 352 | + |
| 353 | + afterEach(() => { |
| 354 | + // Clean up the test directory |
| 355 | + if (fs.existsSync(testDir)) { |
| 356 | + fs.rmSync(testDir, { recursive: true, force: true }); |
| 357 | + } |
| 358 | + }); |
| 359 | + |
| 360 | + it('should parse valid mount with read-write mode', () => { |
| 361 | + const mounts = [`${testDir}:/workspace:rw`]; |
| 362 | + const result = parseVolumeMounts(mounts); |
| 363 | + |
| 364 | + expect(result.success).toBe(true); |
| 365 | + if (result.success) { |
| 366 | + expect(result.mounts).toEqual([`${testDir}:/workspace:rw`]); |
| 367 | + } |
| 368 | + }); |
| 369 | + |
| 370 | + it('should parse valid mount with read-only mode', () => { |
| 371 | + const mounts = [`${testDir}:/data:ro`]; |
| 372 | + const result = parseVolumeMounts(mounts); |
| 373 | + |
| 374 | + expect(result.success).toBe(true); |
| 375 | + if (result.success) { |
| 376 | + expect(result.mounts).toEqual([`${testDir}:/data:ro`]); |
| 377 | + } |
| 378 | + }); |
| 379 | + |
| 380 | + it('should parse valid mount without mode (defaults to rw)', () => { |
| 381 | + const mounts = [`${testDir}:/app`]; |
| 382 | + const result = parseVolumeMounts(mounts); |
| 383 | + |
| 384 | + expect(result.success).toBe(true); |
| 385 | + if (result.success) { |
| 386 | + expect(result.mounts).toEqual([`${testDir}:/app`]); |
| 387 | + } |
| 388 | + }); |
| 389 | + |
| 390 | + it('should parse multiple valid mounts', () => { |
| 391 | + const subdir1 = path.join(testDir, 'dir1'); |
| 392 | + const subdir2 = path.join(testDir, 'dir2'); |
| 393 | + fs.mkdirSync(subdir1); |
| 394 | + fs.mkdirSync(subdir2); |
| 395 | + |
| 396 | + const mounts = [`${subdir1}:/workspace:ro`, `${subdir2}:/data:rw`]; |
| 397 | + const result = parseVolumeMounts(mounts); |
| 398 | + |
| 399 | + expect(result.success).toBe(true); |
| 400 | + if (result.success) { |
| 401 | + expect(result.mounts).toEqual([`${subdir1}:/workspace:ro`, `${subdir2}:/data:rw`]); |
| 402 | + } |
| 403 | + }); |
| 404 | + |
| 405 | + it('should reject mount with too few parts', () => { |
| 406 | + const mounts = ['/workspace']; |
| 407 | + const result = parseVolumeMounts(mounts); |
| 408 | + |
| 409 | + expect(result.success).toBe(false); |
| 410 | + if (!result.success) { |
| 411 | + expect(result.invalidMount).toBe('/workspace'); |
| 412 | + expect(result.reason).toContain('host_path:container_path[:mode]'); |
| 413 | + } |
| 414 | + }); |
| 415 | + |
| 416 | + it('should reject mount with too many parts', () => { |
| 417 | + const mounts = [`${testDir}:/workspace:rw:extra`]; |
| 418 | + const result = parseVolumeMounts(mounts); |
| 419 | + |
| 420 | + expect(result.success).toBe(false); |
| 421 | + if (!result.success) { |
| 422 | + expect(result.invalidMount).toBe(`${testDir}:/workspace:rw:extra`); |
| 423 | + expect(result.reason).toContain('host_path:container_path[:mode]'); |
| 424 | + } |
| 425 | + }); |
| 426 | + |
| 427 | + it('should reject mount with empty host path', () => { |
| 428 | + const mounts = [':/workspace:rw']; |
| 429 | + const result = parseVolumeMounts(mounts); |
| 430 | + |
| 431 | + expect(result.success).toBe(false); |
| 432 | + if (!result.success) { |
| 433 | + expect(result.invalidMount).toBe(':/workspace:rw'); |
| 434 | + expect(result.reason).toContain('Host path cannot be empty'); |
| 435 | + } |
| 436 | + }); |
| 437 | + |
| 438 | + it('should reject mount with empty container path', () => { |
| 439 | + const mounts = [`${testDir}::rw`]; |
| 440 | + const result = parseVolumeMounts(mounts); |
| 441 | + |
| 442 | + expect(result.success).toBe(false); |
| 443 | + if (!result.success) { |
| 444 | + expect(result.invalidMount).toBe(`${testDir}::rw`); |
| 445 | + expect(result.reason).toContain('Container path cannot be empty'); |
| 446 | + } |
| 447 | + }); |
| 448 | + |
| 449 | + it('should reject mount with relative host path', () => { |
| 450 | + const mounts = ['./relative/path:/workspace:rw']; |
| 451 | + const result = parseVolumeMounts(mounts); |
| 452 | + |
| 453 | + expect(result.success).toBe(false); |
| 454 | + if (!result.success) { |
| 455 | + expect(result.invalidMount).toBe('./relative/path:/workspace:rw'); |
| 456 | + expect(result.reason).toContain('Host path must be absolute'); |
| 457 | + } |
| 458 | + }); |
| 459 | + |
| 460 | + it('should reject mount with relative container path', () => { |
| 461 | + const mounts = [`${testDir}:relative/path:rw`]; |
| 462 | + const result = parseVolumeMounts(mounts); |
| 463 | + |
| 464 | + expect(result.success).toBe(false); |
| 465 | + if (!result.success) { |
| 466 | + expect(result.invalidMount).toBe(`${testDir}:relative/path:rw`); |
| 467 | + expect(result.reason).toContain('Container path must be absolute'); |
| 468 | + } |
| 469 | + }); |
| 470 | + |
| 471 | + it('should reject mount with invalid mode', () => { |
| 472 | + const mounts = [`${testDir}:/workspace:invalid`]; |
| 473 | + const result = parseVolumeMounts(mounts); |
| 474 | + |
| 475 | + expect(result.success).toBe(false); |
| 476 | + if (!result.success) { |
| 477 | + expect(result.invalidMount).toBe(`${testDir}:/workspace:invalid`); |
| 478 | + expect(result.reason).toContain('Mount mode must be either "ro" or "rw"'); |
| 479 | + } |
| 480 | + }); |
| 481 | + |
| 482 | + it('should reject mount with non-existent host path', () => { |
| 483 | + const nonExistentPath = '/tmp/this-path-definitely-does-not-exist-12345'; |
| 484 | + const mounts = [`${nonExistentPath}:/workspace:rw`]; |
| 485 | + const result = parseVolumeMounts(mounts); |
| 486 | + |
| 487 | + expect(result.success).toBe(false); |
| 488 | + if (!result.success) { |
| 489 | + expect(result.invalidMount).toBe(`${nonExistentPath}:/workspace:rw`); |
| 490 | + expect(result.reason).toContain('Host path does not exist'); |
| 491 | + } |
| 492 | + }); |
| 493 | + |
| 494 | + it('should handle empty array', () => { |
| 495 | + const mounts: string[] = []; |
| 496 | + const result = parseVolumeMounts(mounts); |
| 497 | + |
| 498 | + expect(result.success).toBe(true); |
| 499 | + if (result.success) { |
| 500 | + expect(result.mounts).toEqual([]); |
| 501 | + } |
| 502 | + }); |
| 503 | + |
| 504 | + it('should return error on first invalid entry', () => { |
| 505 | + const subdir = path.join(testDir, 'valid'); |
| 506 | + fs.mkdirSync(subdir); |
| 507 | + |
| 508 | + const mounts = [`${subdir}:/workspace:ro`, 'invalid-mount', `${testDir}:/data:rw`]; |
| 509 | + const result = parseVolumeMounts(mounts); |
| 510 | + |
| 511 | + expect(result.success).toBe(false); |
| 512 | + if (!result.success) { |
| 513 | + expect(result.invalidMount).toBe('invalid-mount'); |
| 514 | + } |
| 515 | + }); |
| 516 | + }); |
341 | 517 | }); |
0 commit comments