Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix: wire 5 dropped state dicts into get_state/restore_state (#492)
Stephan Mielke2ef80b68d5b9
diff --git a/ministack/services/ecs.py b/ministack/services/ecs.pyindex c2f48cac..1c8eb0bf 100644--- a/ministack/services/ecs.py+++ b/ministack/services/ecs.py@@ -53,6 +53,12 @@ _tasks = AccountScopedDict()_tags = AccountScopedDict()_account_settings = AccountScopedDict()_capacity_providers = AccountScopedDict()+# `_attributes` was originally declared next to its handler block much+# further down the file. Moved up here so the import-time `load_state`+# block (which calls `restore_state` and references `_attributes`) sees+# it defined; otherwise warm-boot fires NameError, the surrounding+# try/except swallows it, and ALL ECS state silently fails to restore.+_attributes = AccountScopedDict()_docker = None@@ -108,6 +114,7 @@ def get_state():"tags": copy.deepcopy(_tags),"account_settings": copy.deepcopy(_account_settings),"capacity_providers": copy.deepcopy(_capacity_providers),+ "attributes": copy.deepcopy(_attributes),}# Save tasks but strip Docker container IDs.# Iterate _data directly to capture ALL accounts.@@ -131,6 +138,7 @@ def restore_state(data):_tags.update(data.get("tags", {}))_account_settings.update(data.get("account_settings", {}))_capacity_providers.update(data.get("capacity_providers", {}))+ _attributes.update(data.get("attributes", {}))from ministack.core.responses import AccountScopedDicttasks_data = data.get("tasks", {})if isinstance(tasks_data, AccountScopedDict):@@ -1494,10 +1502,10 @@ def _delete_account_setting(data):# ---------------------------------------------------------------------------# Attributes (PutAttributes / DeleteAttributes / ListAttributes)+# (state dict `_attributes` is declared at module top with the other+# state — see the comment there for why.)# ----------------------------------------------------------------------------_attributes = AccountScopedDict()-def _put_attributes(data):attrs = data.get("attributes", [])for attr in attrs:diff --git a/ministack/services/kinesis.py b/ministack/services/kinesis.pyindex 35a341d1..b2c23b20 100644--- a/ministack/services/kinesis.py+++ b/ministack/services/kinesis.py@@ -41,12 +41,16 @@ _sequence_lock = threading.Lock()# ── Persistence ────────────────────────────────────────────def get_state():- return {"streams": copy.deepcopy(_streams)}+ return {+ "streams": copy.deepcopy(_streams),+ "consumers": copy.deepcopy(_consumers),+ }def restore_state(data):if data:_streams.update(data.get("streams", {}))+ _consumers.update(data.get("consumers", {}))try:diff --git a/ministack/services/secretsmanager.py b/ministack/services/secretsmanager.pyindex e9bb5211..fe339689 100644--- a/ministack/services/secretsmanager.py+++ b/ministack/services/secretsmanager.py@@ -42,12 +42,16 @@ _resource_policies = AccountScopedDict()# ── Persistence ────────────────────────────────────────────def get_state():- return {"secrets": copy.deepcopy(_secrets)}+ return {+ "secrets": copy.deepcopy(_secrets),+ "resource_policies": copy.deepcopy(_resource_policies),+ }def restore_state(data):if data:_secrets.update(data.get("secrets", {}))+ _resource_policies.update(data.get("resource_policies", {}))try:diff --git a/ministack/services/sns.py b/ministack/services/sns.pyindex bbdc0d9e..6fc01e09 100644--- a/ministack/services/sns.py+++ b/ministack/services/sns.py@@ -58,13 +58,20 @@ _platform_endpoints = AccountScopedDict()# ── Persistence ────────────────────────────────────────────def get_state():- return {"topics": copy.deepcopy(_topics), "sub_arn_to_topic": copy.deepcopy(_sub_arn_to_topic)}+ return {+ "topics": copy.deepcopy(_topics),+ "sub_arn_to_topic": copy.deepcopy(_sub_arn_to_topic),+ "platform_applications": copy.deepcopy(_platform_applications),+ "platform_endpoints": copy.deepcopy(_platform_endpoints),+ }def restore_state(data):if data:_topics.update(data.get("topics", {}))_sub_arn_to_topic.update(data.get("sub_arn_to_topic", {}))+ _platform_applications.update(data.get("platform_applications", {}))+ _platform_endpoints.update(data.get("platform_endpoints", {}))try:diff --git a/tests/test_state_dict_persistence.py b/tests/test_state_dict_persistence.pynew file mode 100644index 00000000..281728e1--- /dev/null+++ b/tests/test_state_dict_persistence.py@@ -0,0 +1,214 @@+"""+Regression tests for "state dict dropped from get_state/restore_state" bugs.++Pattern: a service exposes an API that mutates an `AccountScopedDict`,+but the dict is missing from `get_state()` and/or `restore_state()`. With+PERSIST_STATE=1, every record stored via that API silently disappears on+the next restart.++This file covers five distinct state-dict persistence drops surfaced by+the persistence-symmetry audit:++ H-1 secretsmanager._resource_policies+ H-3 kinesis._consumers (enhanced fan-out)+ H-4 ecs._attributes (PutAttributes / ListAttributes)+ H-5 sns._platform_applications+ H-5 sns._platform_endpoints++Each test populates the dict, snapshots state via the public+`get_state()` / `restore_state()` contract, simulates a restart, and+asserts the record survived.+"""+import importlib++import pytest++from ministack.core import persistence+++def _module(mod_name):+ return importlib.import_module(f"ministack.services.{mod_name}")++[email protected](autouse=True)+def _enable_persistence(monkeypatch, tmp_path):+ """Force PERSIST_STATE on and point STATE_DIR at a tmp dir for the+ duration of each test so save_state / load_state actually write and+ read JSON files instead of short-circuiting."""+ monkeypatch.setattr(persistence, "PERSIST_STATE", True)+ monkeypatch.setattr(persistence, "STATE_DIR", str(tmp_path))+++def _round_trip(mod, svc_key):+ """Simulate a full warm-boot through the on-disk JSON path.++ Going through `save_state` / `load_state` (rather than calling+ `get_state` / `restore_state` directly in-memory) catches encoder+ / decoder regressions AND import-order bugs (a `restore_state`+ that references a globals-only symbol declared further down the+ module would NameError on real warm-boot but pass an in-memory+ test that already has the symbol bound)."""+ persistence.save_state(svc_key, mod.get_state())+ mod.reset()+ loaded = persistence.load_state(svc_key)+ assert loaded is not None, (+ f"persistence.load_state({svc_key!r}) returned None — state "+ "file was not written by save_state()."+ )+ mod.restore_state(loaded)+++# ── H-1: secretsmanager._resource_policies ─────────────────────────────++def test_secretsmanager_resource_policies_survive_warm_boot():+ """`PutResourcePolicy` writes to `_resource_policies`, but if that+ dict is missing from `get_state()` the policy is gone after restart.+ Terraform `aws_secretsmanager_secret_policy` would silently drop."""+ mod = _module("secretsmanager")+ mod.reset()+ arn = "arn:aws:secretsmanager:us-east-1:000000000000:secret:my-secret-AbCdEf"+ mod._resource_policies[arn] = '{"Version":"2012-10-17","Statement":[]}'++ _round_trip(mod, "secretsmanager")++ assert mod._resource_policies.get(arn) == '{"Version":"2012-10-17","Statement":[]}', (+ "Resource policy lost across get_state → restore_state — "+ "_resource_policies must be in both."+ )+ mod.reset()+++# ── H-3: kinesis._consumers ────────────────────────────────────────────++def test_kinesis_consumers_survive_warm_boot():+ """`RegisterStreamConsumer` writes to `_consumers`. Without+ persistence symmetry, every enhanced fan-out registration is lost on+ restart and `DescribeStreamConsumer` returns ResourceNotFoundException."""+ mod = _module("kinesis")+ mod.reset()+ consumer_arn = (+ "arn:aws:kinesis:us-east-1:000000000000:stream/my-stream/consumer/c1:123"+ )+ mod._consumers[consumer_arn] = {+ "ConsumerARN": consumer_arn,+ "ConsumerName": "c1",+ "ConsumerStatus": "ACTIVE",+ "StreamARN": "arn:aws:kinesis:us-east-1:000000000000:stream/my-stream",+ "ConsumerCreationTimestamp": 1700000000.0,+ }++ _round_trip(mod, "kinesis")++ assert consumer_arn in mod._consumers, (+ "Kinesis consumer lost across get_state → restore_state — "+ "_consumers must be in both."+ )+ mod.reset()+++# ── H-4: ecs._attributes ───────────────────────────────────────────────++def test_ecs_attributes_survive_warm_boot():+ """`PutAttributes` writes to `_attributes`. Lost on restart without+ persistence wiring."""+ mod = _module("ecs")+ mod.reset()+ mod._attributes["i-deadbeef:my-attr"] = {+ "name": "my-attr",+ "value": "v1",+ "targetType": "container-instance",+ "targetId": "i-deadbeef",+ }++ _round_trip(mod, "ecs")++ assert "i-deadbeef:my-attr" in mod._attributes, (+ "ECS attribute lost across get_state → restore_state — "+ "_attributes must be in both."+ )+ mod.reset()+++# ── H-5: sns._platform_applications + sns._platform_endpoints ─────────++def test_sns_platform_applications_survive_warm_boot():+ """`CreatePlatformApplication` writes to `_platform_applications`.+ Mobile push topology is lost on restart without persistence wiring."""+ mod = _module("sns")+ mod.reset()+ app_arn = "arn:aws:sns:us-east-1:000000000000:app/GCM/MyApp"+ mod._platform_applications[app_arn] = {+ "PlatformApplicationArn": app_arn,+ "Attributes": {"Platform": "GCM"},+ }++ _round_trip(mod, "sns")++ assert app_arn in mod._platform_applications, (+ "SNS platform application lost across get_state → restore_state — "+ "_platform_applications must be in both."+ )+ mod.reset()+++def test_sns_platform_endpoints_survive_warm_boot():+ """`CreatePlatformEndpoint` writes to `_platform_endpoints`."""+ mod = _module("sns")+ mod.reset()+ ep_arn = "arn:aws:sns:us-east-1:000000000000:endpoint/GCM/MyApp/abc"+ mod._platform_endpoints[ep_arn] = {+ "EndpointArn": ep_arn,+ "Token": "device-token-xyz",+ "Enabled": "true",+ }++ _round_trip(mod, "sns")++ assert ep_arn in mod._platform_endpoints, (+ "SNS platform endpoint lost across get_state → restore_state — "+ "_platform_endpoints must be in both."+ )+ mod.reset()+++# ── Import-order regression for the ECS NameError trap ───────────────++def test_ecs_module_reload_with_persisted_attributes_does_not_namerror():+ """Regression for the import-order trap: `restore_state()` runs at+ module import time (via the `try: load_state("ecs")` block at the+ bottom of services/ecs.py). If `_attributes` is declared AFTER that+ block, the restore call NameErrors and the surrounding try/except+ silently swallows it — wiping all ECS state on warm-boot.++ This test simulates a real warm-boot: write a populated `ecs.json`+ to STATE_DIR, then `importlib.reload()` the module so the load_state+ block runs against the file. If `_attributes` (or any other+ referenced symbol) is declared too late, the restored state will+ be missing because the entire restore_state body crashed."""+ mod = _module("ecs")+ mod.reset()+ arn = "arn:aws:ecs:us-east-1:000000000000:cluster/reload-canary"+ mod._clusters[arn] = {"clusterArn": arn, "status": "ACTIVE"}+ mod._attributes["i-canary:reload-attr"] = {+ "name": "reload-attr",+ "value": "v",+ "targetType": "container-instance",+ "targetId": "i-canary",+ }++ # Persist via the same path save_all uses on shutdown.+ persistence.save_state("ecs", mod.get_state())++ # Force a full reload so the module-level try/load_state/restore_state+ # block at the bottom of ecs.py executes against the on-disk JSON.+ importlib.reload(mod)++ assert arn in mod._clusters, (+ "Cluster lost after reload — likely NameError in restore_state "+ "swallowed by the try/except. Check that every referenced state "+ "dict (_attributes etc.) is declared BEFORE the load_state block."+ )+ assert "i-canary:reload-attr" in mod._attributes, (+ "ECS _attributes lost after reload — same root cause."+ )+ mod.reset()