|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: MIT-0 |
| 3 | + |
| 4 | +""" |
| 5 | +Tests for deploy command parameter handling |
| 6 | +
|
| 7 | +Verifies that stack updates only modify explicitly provided parameters. |
| 8 | +""" |
| 9 | + |
| 10 | +from idp_cli.deployer import build_parameters |
| 11 | + |
| 12 | + |
| 13 | +class TestParameterPreservation: |
| 14 | + """Test parameter preservation during stack updates""" |
| 15 | + |
| 16 | + def test_build_parameters_new_stack_all_required(self): |
| 17 | + """Test parameter building for new stack with all required parameters""" |
| 18 | + params = build_parameters( |
| 19 | + pattern="pattern-2", |
| 20 | + admin_email="admin@example.com", |
| 21 | + max_concurrent=100, |
| 22 | + log_level="INFO", |
| 23 | + enable_hitl="false", |
| 24 | + ) |
| 25 | + |
| 26 | + assert params["AdminEmail"] == "admin@example.com" |
| 27 | + assert "Pattern2" in params["IDPPattern"] |
| 28 | + assert params["MaxConcurrentWorkflows"] == "100" |
| 29 | + assert params["LogLevel"] == "INFO" |
| 30 | + assert params["EnableHITL"] == "false" |
| 31 | + |
| 32 | + def test_build_parameters_update_no_params(self): |
| 33 | + """Test parameter building for update with no parameters - should be empty""" |
| 34 | + params = build_parameters() |
| 35 | + |
| 36 | + # For updates with no explicit parameters, dict should be empty |
| 37 | + # CloudFormation will automatically use previous values |
| 38 | + assert len(params) == 0 |
| 39 | + |
| 40 | + def test_build_parameters_update_selective_max_concurrent_only(self): |
| 41 | + """Test parameter building for update with only max_concurrent""" |
| 42 | + params = build_parameters(max_concurrent=200) |
| 43 | + |
| 44 | + # Only MaxConcurrentWorkflows should be included |
| 45 | + assert params["MaxConcurrentWorkflows"] == "200" |
| 46 | + assert "AdminEmail" not in params |
| 47 | + assert "IDPPattern" not in params |
| 48 | + assert "LogLevel" not in params |
| 49 | + assert "EnableHITL" not in params |
| 50 | + |
| 51 | + def test_build_parameters_update_selective_log_level_only(self): |
| 52 | + """Test parameter building for update with only log level""" |
| 53 | + params = build_parameters(log_level="DEBUG") |
| 54 | + |
| 55 | + # Only LogLevel should be included |
| 56 | + assert params["LogLevel"] == "DEBUG" |
| 57 | + assert "AdminEmail" not in params |
| 58 | + assert "IDPPattern" not in params |
| 59 | + assert "MaxConcurrentWorkflows" not in params |
| 60 | + assert "EnableHITL" not in params |
| 61 | + |
| 62 | + def test_build_parameters_update_multiple_selective(self): |
| 63 | + """Test parameter building for update with multiple selective parameters""" |
| 64 | + params = build_parameters( |
| 65 | + max_concurrent=150, |
| 66 | + log_level="DEBUG", |
| 67 | + enable_hitl="true", |
| 68 | + ) |
| 69 | + |
| 70 | + # Only the provided parameters should be included |
| 71 | + assert params["MaxConcurrentWorkflows"] == "150" |
| 72 | + assert params["LogLevel"] == "DEBUG" |
| 73 | + assert params["EnableHITL"] == "true" |
| 74 | + assert "AdminEmail" not in params |
| 75 | + assert "IDPPattern" not in params |
| 76 | + |
| 77 | + def test_build_parameters_pattern_config(self): |
| 78 | + """Test pattern-specific configuration parameter""" |
| 79 | + params = build_parameters( |
| 80 | + pattern="pattern-2", |
| 81 | + pattern_config="bank-statement-sample", |
| 82 | + ) |
| 83 | + |
| 84 | + assert "Pattern2" in params["IDPPattern"] |
| 85 | + assert params["Pattern2Configuration"] == "bank-statement-sample" |
| 86 | + |
| 87 | + def test_build_parameters_additional_params(self): |
| 88 | + """Test additional parameters override""" |
| 89 | + params = build_parameters( |
| 90 | + max_concurrent=100, |
| 91 | + additional_params={ |
| 92 | + "DataRetentionInDays": "90", |
| 93 | + "ErrorThreshold": "5", |
| 94 | + }, |
| 95 | + ) |
| 96 | + |
| 97 | + assert params["MaxConcurrentWorkflows"] == "100" |
| 98 | + assert params["DataRetentionInDays"] == "90" |
| 99 | + assert params["ErrorThreshold"] == "5" |
| 100 | + |
| 101 | + def test_build_parameters_pattern_mapping(self): |
| 102 | + """Test pattern name to CloudFormation value mapping""" |
| 103 | + params1 = build_parameters(pattern="pattern-1") |
| 104 | + params2 = build_parameters(pattern="pattern-2") |
| 105 | + params3 = build_parameters(pattern="pattern-3") |
| 106 | + |
| 107 | + assert "Pattern1" in params1["IDPPattern"] |
| 108 | + assert "BDA" in params1["IDPPattern"] |
| 109 | + |
| 110 | + assert "Pattern2" in params2["IDPPattern"] |
| 111 | + assert "Textract and Bedrock" in params2["IDPPattern"] |
| 112 | + |
| 113 | + assert "Pattern3" in params3["IDPPattern"] |
| 114 | + assert "SageMaker" in params3["IDPPattern"] |
| 115 | + |
| 116 | + def test_build_parameters_none_values_excluded(self): |
| 117 | + """Test that None values are not included in parameters dict""" |
| 118 | + params = build_parameters( |
| 119 | + pattern=None, |
| 120 | + admin_email=None, |
| 121 | + max_concurrent=None, |
| 122 | + log_level=None, |
| 123 | + enable_hitl=None, |
| 124 | + pattern_config=None, |
| 125 | + custom_config=None, |
| 126 | + ) |
| 127 | + |
| 128 | + # All None values should result in empty dict |
| 129 | + assert len(params) == 0 |
| 130 | + |
| 131 | + def test_build_parameters_custom_config_not_included_when_none(self): |
| 132 | + """Test that custom_config doesn't affect other parameters when None""" |
| 133 | + params = build_parameters( |
| 134 | + max_concurrent=150, |
| 135 | + custom_config=None, |
| 136 | + ) |
| 137 | + |
| 138 | + # Only max_concurrent should be present |
| 139 | + assert len(params) == 1 |
| 140 | + assert params["MaxConcurrentWorkflows"] == "150" |
| 141 | + assert "CustomConfigPath" not in params |
| 142 | + |
| 143 | + |
| 144 | +class TestParameterPreservationIntegration: |
| 145 | + """Integration tests for parameter preservation behavior""" |
| 146 | + |
| 147 | + def test_update_scenario_only_changes_specified_param(self): |
| 148 | + """ |
| 149 | + Simulate update scenario: |
| 150 | + - Existing stack has: MaxConcurrentWorkflows=200, LogLevel=DEBUG |
| 151 | + - User updates with: --max-concurrent 150 |
| 152 | + - Expected: Only MaxConcurrentWorkflows should be in parameter dict |
| 153 | + - CloudFormation will preserve LogLevel=DEBUG automatically |
| 154 | + """ |
| 155 | + # User only changes max_concurrent to 150 |
| 156 | + params = build_parameters(max_concurrent=150) |
| 157 | + |
| 158 | + # Only the changed parameter should be included |
| 159 | + assert len(params) == 1 |
| 160 | + assert params["MaxConcurrentWorkflows"] == "150" |
| 161 | + |
| 162 | + # These should NOT be in the dict (CloudFormation preserves them) |
| 163 | + assert "LogLevel" not in params |
| 164 | + assert "EnableHITL" not in params |
| 165 | + assert "AdminEmail" not in params |
| 166 | + assert "IDPPattern" not in params |
| 167 | + |
| 168 | + def test_new_stack_scenario_all_required_params(self): |
| 169 | + """ |
| 170 | + Simulate new stack creation: |
| 171 | + - User provides: --pattern pattern-2 --admin-email user@example.com |
| 172 | + - Expected: pattern and admin_email in parameters |
| 173 | + - Optional params with defaults also included |
| 174 | + """ |
| 175 | + params = build_parameters( |
| 176 | + pattern="pattern-2", |
| 177 | + admin_email="user@example.com", |
| 178 | + max_concurrent=100, # Default value |
| 179 | + log_level="INFO", # Default value |
| 180 | + enable_hitl="false", # Default value |
| 181 | + ) |
| 182 | + |
| 183 | + # Required params for new stack |
| 184 | + assert params["AdminEmail"] == "user@example.com" |
| 185 | + assert "Pattern2" in params["IDPPattern"] |
| 186 | + |
| 187 | + # Defaults should be included for new stack |
| 188 | + assert params["MaxConcurrentWorkflows"] == "100" |
| 189 | + assert params["LogLevel"] == "INFO" |
| 190 | + assert params["EnableHITL"] == "false" |
| 191 | + |
| 192 | + def test_update_with_custom_config_only(self): |
| 193 | + """ |
| 194 | + Simulate update with only custom config: |
| 195 | + - User provides: --custom-config ./my-config.yaml |
| 196 | + - Expected: Only CustomConfigPath in parameters |
| 197 | + """ |
| 198 | + # Note: custom_config handling requires region and uploads to S3 |
| 199 | + # For unit test, we skip the upload part and test the parameter inclusion |
| 200 | + params = build_parameters( |
| 201 | + custom_config=None # Would be S3 URI after upload |
| 202 | + ) |
| 203 | + |
| 204 | + # When custom_config is None, shouldn't be in params |
| 205 | + assert "CustomConfigPath" not in params |
0 commit comments