|
6 | 6 | from pathlib import Path
|
7 | 7 |
|
8 | 8 | import pytest
|
| 9 | +import yaml |
9 | 10 |
|
10 | 11 |
|
11 | 12 | def run_helm_command(command: list[str]) -> tuple[str, str, int]:
|
@@ -325,7 +326,7 @@ def test_set_value_nonexistent_path(plugin_install, tmp_path):
|
325 | 326 | ["values-manager", "init", "--release", "test-release"]
|
326 | 327 | )
|
327 | 328 | assert init_returncode == 0
|
328 |
| - assert Path("helm-values.json").exists() |
| 329 | + assert Path(work_dir, "helm-values.json").exists() |
329 | 330 |
|
330 | 331 | # Add a deployment
|
331 | 332 | add_deployment_stdout, add_deployment_stderr, add_deployment_returncode = run_helm_command(
|
@@ -375,3 +376,190 @@ def test_set_value_nonexistent_deployment(plugin_install, tmp_path):
|
375 | 376 | )
|
376 | 377 | assert returncode == 1
|
377 | 378 | assert "Deployment 'nonexistent' not found" in stderr
|
| 379 | + |
| 380 | + |
| 381 | +def test_generate_help_command(plugin_install): |
| 382 | + """Test that the generate help command works and shows expected output.""" |
| 383 | + stdout, stderr, returncode = run_helm_command(["values-manager", "generate", "--help"]) |
| 384 | + assert returncode == 0, f"Failed to run help command: {stderr}" |
| 385 | + assert "Generate a values file for a specific deployment" in stdout, "Help text should include command description" |
| 386 | + assert "--deployment" in stdout, "Help text should include deployment option" |
| 387 | + assert "--output" in stdout, "Help text should include output option" |
| 388 | + |
| 389 | + |
| 390 | +def test_generate_command(plugin_install, tmp_path): |
| 391 | + """Test that the generate command works correctly.""" |
| 392 | + # Create a test directory |
| 393 | + test_dir = tmp_path / "test_generate_command" |
| 394 | + test_dir.mkdir() |
| 395 | + os.chdir(test_dir) |
| 396 | + |
| 397 | + # Initialize the plugin |
| 398 | + stdout, stderr, returncode = run_helm_command(["values-manager", "init", "--release", "test-release"]) |
| 399 | + assert returncode == 0, f"Failed to initialize plugin: {stderr}" |
| 400 | + |
| 401 | + # Add a deployment |
| 402 | + stdout, stderr, returncode = run_helm_command(["values-manager", "add-deployment", "dev"]) |
| 403 | + assert returncode == 0, f"Failed to add deployment: {stderr}" |
| 404 | + |
| 405 | + # Add value configs |
| 406 | + stdout, stderr, returncode = run_helm_command( |
| 407 | + ["values-manager", "add-value-config", "--path", "app.replicas", "--description", "Number of replicas"] |
| 408 | + ) |
| 409 | + assert returncode == 0, f"Failed to add value config: {stderr}" |
| 410 | + |
| 411 | + stdout, stderr, returncode = run_helm_command( |
| 412 | + ["values-manager", "add-value-config", "--path", "app.image.repository", "--description", "Image repository"] |
| 413 | + ) |
| 414 | + assert returncode == 0, f"Failed to add value config: {stderr}" |
| 415 | + |
| 416 | + stdout, stderr, returncode = run_helm_command( |
| 417 | + ["values-manager", "add-value-config", "--path", "app.image.tag", "--description", "Image tag"] |
| 418 | + ) |
| 419 | + assert returncode == 0, f"Failed to add value config: {stderr}" |
| 420 | + |
| 421 | + # Set values |
| 422 | + stdout, stderr, returncode = run_helm_command( |
| 423 | + ["values-manager", "set-value", "--path", "app.replicas", "--deployment", "dev", "--value", "3"] |
| 424 | + ) |
| 425 | + assert returncode == 0, f"Failed to set value: {stderr}" |
| 426 | + |
| 427 | + stdout, stderr, returncode = run_helm_command( |
| 428 | + ["values-manager", "set-value", "--path", "app.image.repository", "--deployment", "dev", "--value", "myapp"] |
| 429 | + ) |
| 430 | + assert returncode == 0, f"Failed to set value: {stderr}" |
| 431 | + |
| 432 | + stdout, stderr, returncode = run_helm_command( |
| 433 | + ["values-manager", "set-value", "--path", "app.image.tag", "--deployment", "dev", "--value", "latest"] |
| 434 | + ) |
| 435 | + assert returncode == 0, f"Failed to set value: {stderr}" |
| 436 | + |
| 437 | + # Generate values file |
| 438 | + stdout, stderr, returncode = run_helm_command(["values-manager", "generate", "--deployment", "dev"]) |
| 439 | + assert returncode == 0, f"Failed to generate values file: {stderr}" |
| 440 | + assert "Successfully generated values file for deployment 'dev'" in stdout, f"Unexpected output: {stdout}" |
| 441 | + |
| 442 | + # Verify the values file exists |
| 443 | + values_file = test_dir / "dev.test-release.values.yaml" |
| 444 | + assert values_file.exists(), "Values file should exist" |
| 445 | + |
| 446 | + # Verify the content of the values file |
| 447 | + with open(values_file, "r") as f: |
| 448 | + values = yaml.safe_load(f) |
| 449 | + assert values["app"]["replicas"] == "3", "Values file should contain correct replicas value" |
| 450 | + assert values["app"]["image"]["repository"] == "myapp", "Values file should contain correct repository value" |
| 451 | + assert values["app"]["image"]["tag"] == "latest", "Values file should contain correct tag value" |
| 452 | + |
| 453 | + |
| 454 | +def test_generate_with_output_path(plugin_install, tmp_path): |
| 455 | + """Test that the generate command works with a custom output path.""" |
| 456 | + # Create a test directory |
| 457 | + test_dir = tmp_path / "test_generate_with_output_path" |
| 458 | + test_dir.mkdir() |
| 459 | + os.chdir(test_dir) |
| 460 | + |
| 461 | + # Create a custom output directory |
| 462 | + output_dir = test_dir / "output" |
| 463 | + output_dir.mkdir() |
| 464 | + |
| 465 | + # Initialize the plugin |
| 466 | + stdout, stderr, returncode = run_helm_command(["values-manager", "init", "--release", "test-release"]) |
| 467 | + assert returncode == 0, f"Failed to initialize plugin: {stderr}" |
| 468 | + |
| 469 | + # Add a deployment |
| 470 | + stdout, stderr, returncode = run_helm_command(["values-manager", "add-deployment", "prod"]) |
| 471 | + assert returncode == 0, f"Failed to add deployment: {stderr}" |
| 472 | + |
| 473 | + # Add value configs |
| 474 | + stdout, stderr, returncode = run_helm_command( |
| 475 | + ["values-manager", "add-value-config", "--path", "app.replicas", "--description", "Number of replicas"] |
| 476 | + ) |
| 477 | + assert returncode == 0, f"Failed to add value config: {stderr}" |
| 478 | + |
| 479 | + # Set values |
| 480 | + stdout, stderr, returncode = run_helm_command( |
| 481 | + ["values-manager", "set-value", "--path", "app.replicas", "--deployment", "prod", "--value", "5"] |
| 482 | + ) |
| 483 | + assert returncode == 0, f"Failed to set value: {stderr}" |
| 484 | + |
| 485 | + # Generate values file with custom output path |
| 486 | + stdout, stderr, returncode = run_helm_command( |
| 487 | + ["values-manager", "generate", "--deployment", "prod", "--output", str(output_dir)] |
| 488 | + ) |
| 489 | + assert returncode == 0, f"Failed to generate values file: {stderr}" |
| 490 | + assert "Successfully generated values file for deployment 'prod'" in stdout, f"Unexpected output: {stdout}" |
| 491 | + |
| 492 | + # Verify the values file exists in the custom output directory |
| 493 | + values_file = output_dir / "prod.test-release.values.yaml" |
| 494 | + assert values_file.exists(), "Values file should exist in the custom output directory" |
| 495 | + |
| 496 | + # Verify the content of the values file |
| 497 | + with open(values_file, "r") as f: |
| 498 | + values = yaml.safe_load(f) |
| 499 | + assert values["app"]["replicas"] == "5", "Values file should contain correct replicas value" |
| 500 | + |
| 501 | + |
| 502 | +def test_generate_nonexistent_deployment(plugin_install, tmp_path): |
| 503 | + """Test that generating values for a nonexistent deployment fails with the correct error message.""" |
| 504 | + # Create a test directory |
| 505 | + test_dir = tmp_path / "test_generate_nonexistent_deployment" |
| 506 | + test_dir.mkdir() |
| 507 | + os.chdir(test_dir) |
| 508 | + |
| 509 | + # Initialize the plugin |
| 510 | + stdout, stderr, returncode = run_helm_command(["values-manager", "init", "--release", "test-release"]) |
| 511 | + assert returncode == 0, f"Failed to initialize plugin: {stderr}" |
| 512 | + |
| 513 | + # Try to generate values for a nonexistent deployment |
| 514 | + stdout, stderr, returncode = run_helm_command(["values-manager", "generate", "--deployment", "nonexistent"]) |
| 515 | + assert returncode != 0, "Expected command to fail but it succeeded" |
| 516 | + assert "Deployment 'nonexistent' not found" in stderr, f"Unexpected error message: {stderr}" |
| 517 | + |
| 518 | + |
| 519 | +def test_generate_no_config(plugin_install, tmp_path): |
| 520 | + """Test that generating values without initializing fails with the correct error message.""" |
| 521 | + # Create a test directory |
| 522 | + test_dir = tmp_path / "test_generate_no_config" |
| 523 | + test_dir.mkdir() |
| 524 | + os.chdir(test_dir) |
| 525 | + |
| 526 | + # Try to generate values without initializing |
| 527 | + stdout, stderr, returncode = run_helm_command(["values-manager", "generate", "--deployment", "dev"]) |
| 528 | + assert returncode != 0, "Expected command to fail but it succeeded" |
| 529 | + assert "Configuration file helm-values.json not found" in stderr, f"Unexpected error message: {stderr}" |
| 530 | + |
| 531 | + |
| 532 | +def test_generate_with_missing_required_value(plugin_install, tmp_path): |
| 533 | + """Test generate command with missing required values.""" |
| 534 | + # Change to temp directory |
| 535 | + os.chdir(tmp_path) |
| 536 | + |
| 537 | + # Initialize the config |
| 538 | + stdout, stderr, returncode = run_helm_command(["values-manager", "init", "--release", "test-release"]) |
| 539 | + assert returncode == 0, f"Failed to initialize config: {stderr}" |
| 540 | + |
| 541 | + # Add a deployment |
| 542 | + stdout, stderr, returncode = run_helm_command(["values-manager", "add-deployment", "dev"]) |
| 543 | + assert returncode == 0, f"Failed to add deployment: {stderr}" |
| 544 | + |
| 545 | + # Add a required value config but don't set a value for it |
| 546 | + stdout, stderr, returncode = run_helm_command( |
| 547 | + [ |
| 548 | + "values-manager", |
| 549 | + "add-value-config", |
| 550 | + "--path", |
| 551 | + "app.required", |
| 552 | + "--description", |
| 553 | + "Required value", |
| 554 | + "--required", |
| 555 | + ] |
| 556 | + ) |
| 557 | + assert returncode == 0, f"Failed to add value config: {stderr}" |
| 558 | + |
| 559 | + # Try to generate values without setting the required value |
| 560 | + stdout, stderr, returncode = run_helm_command(["values-manager", "generate", "--deployment", "dev"]) |
| 561 | + |
| 562 | + # Verify the command failed |
| 563 | + assert returncode != 0, "Expected command to fail but it succeeded" |
| 564 | + assert "Missing required values for deployment 'dev'" in stderr |
| 565 | + assert "app.required" in stderr |
0 commit comments