Spaces:
Running
Running
File size: 20,613 Bytes
c745a99 | 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 | """Unit tests for ResourceVerifier — resource existence checks, state checks, and JSON path extraction.
Uses a mock AwsBackend so tests run without MiniStack/Docker.
Run:
python -m pytest tests/test_resource_verifier.py -v
"""
from __future__ import annotations
import json
from unittest.mock import MagicMock
from server.services.environment_strategy import EnvironmentStrategy
from server.services.resource_verifier import ResourceVerifier, _extract_json_path
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _mock_backend(responses: dict[str, tuple[bool, str, str]]) -> EnvironmentStrategy:
"""Create a mock AwsBackend that returns preset responses keyed by substring match."""
backend = MagicMock(spec=EnvironmentStrategy)
def execute(cmd: str) -> tuple[bool, str, str]:
for pattern, result in responses.items():
if pattern in cmd:
return result
return (False, "", "unknown command")
backend.execute_command.side_effect = execute
return backend
# ---------------------------------------------------------------------------
# _extract_json_path
# ---------------------------------------------------------------------------
class TestExtractJsonPath:
def test_simple_dot_path(self) -> None:
data = {"Table": {"Name": "orders"}}
assert _extract_json_path(data, "$.Table.Name") == "orders"
def test_nested_numeric(self) -> None:
data = {"Table": {"ProvisionedThroughput": {"ReadCapacityUnits": 50}}}
assert (
_extract_json_path(data, "$.Table.ProvisionedThroughput.ReadCapacityUnits")
== 50
)
def test_array_index(self) -> None:
data = {"Rules": [{"ID": "first"}, {"ID": "second"}]}
assert _extract_json_path(data, "$.Rules[0].ID") == "first"
assert _extract_json_path(data, "$.Rules[1].ID") == "second"
def test_array_index_out_of_bounds(self) -> None:
data = {"Rules": [{"ID": "only"}]}
assert _extract_json_path(data, "$.Rules[5].ID") is None
def test_wildcard_array(self) -> None:
data = {"Buckets": [{"Name": "a"}, {"Name": "b"}]}
assert _extract_json_path(data, "$.Buckets[].Name") == ["a", "b"]
def test_wildcard_no_remaining(self) -> None:
data = {"Items": [1, 2, 3]}
assert _extract_json_path(data, "$.Items[]") == [1, 2, 3]
def test_missing_key(self) -> None:
assert _extract_json_path({"a": 1}, "$.b.c") is None
def test_none_data(self) -> None:
assert _extract_json_path(None, "$.foo") is None
def test_non_dict_intermediate(self) -> None:
data = {"a": "string_not_dict"}
assert _extract_json_path(data, "$.a.b") is None
def test_services_nested_path(self) -> None:
data = {"services": [{"desiredCount": 3}]}
assert _extract_json_path(data, "$.services[0].desiredCount") == 3
def test_attributes_path(self) -> None:
data = {"Attributes": {"VisibilityTimeout": "120"}}
assert _extract_json_path(data, "$.Attributes.VisibilityTimeout") == "120"
# ---------------------------------------------------------------------------
# ResourceVerifier.check_state
# ---------------------------------------------------------------------------
class TestCheckState:
def test_output_contains_pass(self) -> None:
backend = _mock_backend({"list-attached": (True, "AmazonSQSFullAccess", "")})
v = ResourceVerifier(backend)
assert v.check_state(
{"command": "aws iam list-attached-role-policies", "output_contains": "SQS"}
)
def test_output_contains_fail(self) -> None:
backend = _mock_backend({"list-attached": (True, "AmazonS3ReadOnly", "")})
v = ResourceVerifier(backend)
assert not v.check_state(
{"command": "aws iam list-attached-role-policies", "output_contains": "SQS"}
)
def test_command_fails(self) -> None:
backend = _mock_backend({"describe": (False, "", "not found")})
v = ResourceVerifier(backend)
assert not v.check_state(
{"command": "aws describe-something", "output_contains": "ok"}
)
def test_empty_command(self) -> None:
backend = _mock_backend({})
v = ResourceVerifier(backend)
assert not v.check_state({"command": ""})
assert not v.check_state({})
def test_json_path_expected(self) -> None:
stdout = json.dumps(
{"Table": {"ProvisionedThroughput": {"ReadCapacityUnits": 50}}}
)
backend = _mock_backend({"describe-table": (True, stdout, "")})
v = ResourceVerifier(backend)
assert v.check_state(
{
"command": "aws dynamodb describe-table --table-name t",
"json_path": "$.Table.ProvisionedThroughput.ReadCapacityUnits",
"expected": 50,
}
)
def test_json_path_string_comparison(self) -> None:
stdout = json.dumps({"Attributes": {"VisibilityTimeout": "120"}})
backend = _mock_backend({"get-queue": (True, stdout, "")})
v = ResourceVerifier(backend)
assert v.check_state(
{
"command": "aws sqs get-queue-attributes",
"json_path": "$.Attributes.VisibilityTimeout",
"expected": "120",
}
)
def test_json_path_mismatch(self) -> None:
stdout = json.dumps(
{"Table": {"ProvisionedThroughput": {"ReadCapacityUnits": 5}}}
)
backend = _mock_backend({"describe-table": (True, stdout, "")})
v = ResourceVerifier(backend)
assert not v.check_state(
{
"command": "aws dynamodb describe-table",
"json_path": "$.Table.ProvisionedThroughput.ReadCapacityUnits",
"expected": 50,
}
)
def test_json_path_invalid_json(self) -> None:
backend = _mock_backend({"describe": (True, "not-json{", "")})
v = ResourceVerifier(backend)
assert not v.check_state(
{
"command": "aws describe-something",
"json_path": "$.foo",
"expected": "bar",
}
)
def test_both_output_contains_and_json_path(self) -> None:
stdout = json.dumps({"Timeout": 30, "FunctionName": "payment-webhook"})
backend = _mock_backend({"get-function": (True, stdout, "")})
v = ResourceVerifier(backend)
# Both checks must pass
assert v.check_state(
{
"command": "aws lambda get-function-configuration",
"output_contains": "payment-webhook",
"json_path": "$.Timeout",
"expected": 30,
}
)
def test_output_contains_pass_json_path_fail(self) -> None:
stdout = json.dumps({"Timeout": 3, "FunctionName": "payment-webhook"})
backend = _mock_backend({"get-function": (True, stdout, "")})
v = ResourceVerifier(backend)
assert not v.check_state(
{
"command": "aws lambda get-function-configuration",
"output_contains": "payment-webhook",
"json_path": "$.Timeout",
"expected": 30,
}
)
def test_only_json_path_no_expected_still_passes(self) -> None:
# json_path without expected is not evaluated
backend = _mock_backend({"cmd": (True, '{"a":1}', "")})
v = ResourceVerifier(backend)
assert v.check_state({"command": "aws cmd", "json_path": "$.a"})
# ---------------------------------------------------------------------------
# ResourceVerifier.resource_exists — service verifiers
# ---------------------------------------------------------------------------
class TestResourceExistsS3:
def test_bucket_exists(self) -> None:
stdout = json.dumps({"Buckets": [{"Name": "my-bucket"}, {"Name": "other"}]})
backend = _mock_backend({"list-buckets": (True, stdout, "")})
v = ResourceVerifier(backend)
assert v.resource_exists("s3", "my-bucket")
def test_bucket_missing(self) -> None:
stdout = json.dumps({"Buckets": [{"Name": "other"}]})
backend = _mock_backend({"list-buckets": (True, stdout, "")})
v = ResourceVerifier(backend)
assert not v.resource_exists("s3", "my-bucket")
def test_list_fails(self) -> None:
backend = _mock_backend({"list-buckets": (False, "", "err")})
v = ResourceVerifier(backend)
assert not v.resource_exists("s3", "demo")
class TestResourceExistsDynamoDB:
def test_table_exists(self) -> None:
backend = _mock_backend({"describe-table": (True, "{}", "")})
v = ResourceVerifier(backend)
assert v.resource_exists("dynamodb", "orders")
def test_table_missing(self) -> None:
backend = _mock_backend({"describe-table": (False, "", "not found")})
v = ResourceVerifier(backend)
assert not v.resource_exists("dynamodb", "orders")
class TestResourceExistsLambda:
def test_function_exists(self) -> None:
backend = _mock_backend({"get-function": (True, "{}", "")})
v = ResourceVerifier(backend)
assert v.resource_exists("lambda", "processor")
def test_function_missing(self) -> None:
backend = _mock_backend({"get-function": (False, "", "not found")})
v = ResourceVerifier(backend)
assert not v.resource_exists("lambda", "processor")
class TestResourceExistsSQS:
def test_queue_exists(self) -> None:
backend = _mock_backend({"get-queue-url": (True, "http://...", "")})
v = ResourceVerifier(backend)
assert v.resource_exists("sqs", "my-queue")
def test_queue_missing(self) -> None:
backend = _mock_backend({"get-queue-url": (False, "", "not found")})
v = ResourceVerifier(backend)
assert not v.resource_exists("sqs", "my-queue")
class TestResourceExistsSNS:
def test_topic_exists(self) -> None:
stdout = json.dumps(
{"Topics": [{"TopicArn": "arn:aws:sns:us-east-1:000000000000:alerts"}]}
)
backend = _mock_backend({"list-topics": (True, stdout, "")})
v = ResourceVerifier(backend)
assert v.resource_exists("sns", "alerts")
def test_topic_missing(self) -> None:
stdout = json.dumps(
{"Topics": [{"TopicArn": "arn:aws:sns:us-east-1:000000000000:other"}]}
)
backend = _mock_backend({"list-topics": (True, stdout, "")})
v = ResourceVerifier(backend)
assert not v.resource_exists("sns", "alerts")
class TestResourceExistsIAM:
def test_role_exists(self) -> None:
backend = _mock_backend({"get-role": (True, "{}", "")})
v = ResourceVerifier(backend)
assert v.resource_exists("iam", "my-role")
def test_user_exists(self) -> None:
backend = _mock_backend(
{"get-role": (False, "", ""), "get-user": (True, "{}", "")}
)
v = ResourceVerifier(backend)
assert v.resource_exists("iam", "deploy-bot")
def test_policy_exists(self) -> None:
stdout = json.dumps({"Policies": [{"PolicyName": "my-policy"}]})
backend = _mock_backend(
{
"get-role": (False, "", ""),
"get-user": (False, "", ""),
"list-policies": (True, stdout, ""),
}
)
v = ResourceVerifier(backend)
assert v.resource_exists("iam", "my-policy")
def test_iam_not_found(self) -> None:
backend = _mock_backend(
{
"get-role": (False, "", ""),
"get-user": (False, "", ""),
"list-policies": (True, json.dumps({"Policies": []}), ""),
}
)
v = ResourceVerifier(backend)
assert not v.resource_exists("iam", "ghost")
class TestResourceExistsSecretsManager:
def test_secret_exists(self) -> None:
backend = _mock_backend({"describe-secret": (True, "{}", "")})
v = ResourceVerifier(backend)
assert v.resource_exists("secretsmanager", "db-creds")
def test_secret_missing(self) -> None:
backend = _mock_backend({"describe-secret": (False, "", "not found")})
v = ResourceVerifier(backend)
assert not v.resource_exists("secretsmanager", "db-creds")
class TestResourceExistsApiGateway:
def test_api_exists(self) -> None:
stdout = json.dumps({"items": [{"name": "my-api"}]})
backend = _mock_backend({"get-rest-apis": (True, stdout, "")})
v = ResourceVerifier(backend)
assert v.resource_exists("apigateway", "my-api")
def test_api_missing(self) -> None:
stdout = json.dumps({"items": []})
backend = _mock_backend({"get-rest-apis": (True, stdout, "")})
v = ResourceVerifier(backend)
assert not v.resource_exists("apigateway", "my-api")
class TestResourceExistsECS:
def test_cluster_exists_active(self) -> None:
stdout = json.dumps({"clusters": [{"clusterName": "prod", "status": "ACTIVE"}]})
backend = _mock_backend({"describe-clusters": (True, stdout, "")})
v = ResourceVerifier(backend)
assert v.resource_exists("ecs", "prod")
def test_cluster_inactive(self) -> None:
stdout = json.dumps(
{"clusters": [{"clusterName": "prod", "status": "INACTIVE"}]}
)
backend = _mock_backend({"describe-clusters": (True, stdout, "")})
v = ResourceVerifier(backend)
assert not v.resource_exists("ecs", "prod")
def test_cluster_not_found(self) -> None:
backend = _mock_backend({"describe-clusters": (False, "", "")})
v = ResourceVerifier(backend)
assert not v.resource_exists("ecs", "prod")
class TestResourceExistsRDS:
def test_instance_exists(self) -> None:
backend = _mock_backend({"describe-db-instances": (True, "{}", "")})
v = ResourceVerifier(backend)
assert v.resource_exists("rds", "my-db")
def test_instance_missing(self) -> None:
backend = _mock_backend({"describe-db-instances": (False, "", "not found")})
v = ResourceVerifier(backend)
assert not v.resource_exists("rds", "my-db")
class TestResourceExistsElastiCache:
def test_cluster_exists(self) -> None:
backend = _mock_backend({"describe-cache-clusters": (True, "{}", "")})
v = ResourceVerifier(backend)
assert v.resource_exists("elasticache", "session-cache")
class TestResourceExistsRoute53:
def test_zone_exists(self) -> None:
stdout = json.dumps({"HostedZones": [{"Name": "example.com."}]})
backend = _mock_backend({"list-hosted-zones": (True, stdout, "")})
v = ResourceVerifier(backend)
assert v.resource_exists("route53", "example.com")
def test_zone_trailing_dot_normalized(self) -> None:
stdout = json.dumps({"HostedZones": [{"Name": "example.com."}]})
backend = _mock_backend({"list-hosted-zones": (True, stdout, "")})
v = ResourceVerifier(backend)
assert v.resource_exists("route53", "example.com.")
class TestResourceExistsELBv2:
def test_lb_exists(self) -> None:
stdout = json.dumps({"LoadBalancers": [{"LoadBalancerName": "web-alb"}]})
backend = _mock_backend({"describe-load-balancers": (True, stdout, "")})
v = ResourceVerifier(backend)
assert v.resource_exists("elbv2", "web-alb")
def test_lb_missing(self) -> None:
backend = _mock_backend({"describe-load-balancers": (False, "", "not found")})
v = ResourceVerifier(backend)
assert not v.resource_exists("elbv2", "web-alb")
class TestResourceExistsEFS:
def test_fs_by_creation_token(self) -> None:
stdout = json.dumps(
{"FileSystems": [{"CreationToken": "app-storage", "Tags": []}]}
)
backend = _mock_backend({"describe-file-systems": (True, stdout, "")})
v = ResourceVerifier(backend)
assert v.resource_exists("efs", "app-storage")
def test_fs_by_tag(self) -> None:
stdout = json.dumps(
{
"FileSystems": [
{
"CreationToken": "token-123",
"Tags": [{"Key": "Name", "Value": "shared-data"}],
}
]
}
)
backend = _mock_backend({"describe-file-systems": (True, stdout, "")})
v = ResourceVerifier(backend)
assert v.resource_exists("efs", "shared-data")
def test_fs_missing(self) -> None:
stdout = json.dumps({"FileSystems": []})
backend = _mock_backend({"describe-file-systems": (True, stdout, "")})
v = ResourceVerifier(backend)
assert not v.resource_exists("efs", "nonexistent")
class TestResourceExistsCognito:
def test_pool_exists(self) -> None:
stdout = json.dumps({"UserPools": [{"Name": "customer-auth"}]})
backend = _mock_backend({"list-user-pools": (True, stdout, "")})
v = ResourceVerifier(backend)
assert v.resource_exists("cognito-idp", "customer-auth")
def test_pool_missing(self) -> None:
stdout = json.dumps({"UserPools": []})
backend = _mock_backend({"list-user-pools": (True, stdout, "")})
v = ResourceVerifier(backend)
assert not v.resource_exists("cognito-idp", "customer-auth")
class TestResourceExistsSSM:
def test_param_exists(self) -> None:
backend = _mock_backend({"get-parameter": (True, "{}", "")})
v = ResourceVerifier(backend)
assert v.resource_exists("ssm", "/app/config")
class TestResourceExistsEventBridge:
def test_rule_exists(self) -> None:
backend = _mock_backend({"describe-rule": (True, "{}", "")})
v = ResourceVerifier(backend)
assert v.resource_exists("events", "nightly-etl")
class TestResourceExistsApiGatewayV2:
def test_api_exists(self) -> None:
stdout = json.dumps({"Items": [{"Name": "products-api"}]})
backend = _mock_backend({"get-apis": (True, stdout, "")})
v = ResourceVerifier(backend)
assert v.resource_exists("apigatewayv2", "products-api")
def test_api_missing(self) -> None:
stdout = json.dumps({"Items": []})
backend = _mock_backend({"get-apis": (True, stdout, "")})
v = ResourceVerifier(backend)
assert not v.resource_exists("apigatewayv2", "products-api")
class TestResourceExistsCloudFormation:
def test_stack_exists(self) -> None:
backend = _mock_backend({"describe-stacks": (True, "{}", "")})
v = ResourceVerifier(backend)
assert v.resource_exists("cloudformation", "vpc-stack")
class TestResourceExistsGlue:
def test_database_exists(self) -> None:
backend = _mock_backend({"get-database": (True, "{}", "")})
v = ResourceVerifier(backend)
assert v.resource_exists("glue", "analytics-db")
class TestResourceExistsEBS:
def test_volume_exists(self) -> None:
stdout = json.dumps({"Volumes": [{"VolumeId": "vol-123"}]})
backend = _mock_backend({"describe-volumes": (True, stdout, "")})
v = ResourceVerifier(backend)
assert v.resource_exists("ebs", "data-volume")
def test_no_volumes(self) -> None:
stdout = json.dumps({"Volumes": []})
backend = _mock_backend({"describe-volumes": (True, stdout, "")})
v = ResourceVerifier(backend)
assert not v.resource_exists("ebs", "data-volume")
class TestResourceExistsFirehose:
def test_stream_exists(self) -> None:
backend = _mock_backend({"describe-delivery-stream": (True, "{}", "")})
v = ResourceVerifier(backend)
assert v.resource_exists("firehose", "event-stream")
class TestResourceExistsUnknownService:
def test_unknown_service(self) -> None:
backend = _mock_backend({})
v = ResourceVerifier(backend)
assert not v.resource_exists("unknown-service", "name")
class TestResourceExistsInvalidJson:
def test_s3_bad_json(self) -> None:
backend = _mock_backend({"list-buckets": (True, "not-json", "")})
v = ResourceVerifier(backend)
assert not v.resource_exists("s3", "demo")
def test_sns_bad_json(self) -> None:
backend = _mock_backend({"list-topics": (True, "{bad", "")})
v = ResourceVerifier(backend)
assert not v.resource_exists("sns", "alerts")
|