|
39 | 39 |
|
40 | 40 |
|
41 | 41 | class FakePortainer: |
42 | | - def __init__(self, stack_status, container_sequences): |
| 42 | + def __init__(self, stack_status, container_sequences, stack_status_sequence=None): |
43 | 43 | self.stack_status = stack_status |
| 44 | + self.stack_status_sequence = list(stack_status_sequence or []) |
44 | 45 | self.container_sequences = list(container_sequences) |
45 | 46 | self.up_calls = [] |
46 | 47 | self.start_calls = [] |
| 48 | + self.stop_calls = [] |
47 | 49 |
|
48 | 50 | def get_stack_by_name(self, app_id, endpoint_id): |
49 | | - return {"Id": 9, "Name": app_id, "Status": self.stack_status} |
| 51 | + if self.stack_status_sequence: |
| 52 | + status = self.stack_status_sequence.pop(0) |
| 53 | + else: |
| 54 | + status = self.stack_status |
| 55 | + return {"Id": 9, "Name": app_id, "Status": status} |
| 56 | + |
| 57 | + def stop_stack(self, app_id, endpoint_id): |
| 58 | + self.stop_calls.append((app_id, endpoint_id)) |
50 | 59 |
|
51 | 60 | def up_stack(self, stack_id, endpoint_id): |
52 | 61 | self.up_calls.append((stack_id, endpoint_id)) |
@@ -115,6 +124,34 @@ def test_restore_start_uses_start_stack_for_active_stack(monkeypatch): |
115 | 124 | assert portainer.up_calls == [] |
116 | 125 |
|
117 | 126 |
|
| 127 | +def test_restore_uses_pre_stop_stack_state_for_active_stack(monkeypatch): |
| 128 | + manager = _build_manager() |
| 129 | + portainer = FakePortainer( |
| 130 | + stack_status=1, |
| 131 | + stack_status_sequence=[1], |
| 132 | + container_sequences=[[{"Names": ["/wordpress_demo"], "State": "running"}]], |
| 133 | + ) |
| 134 | + |
| 135 | + monkeypatch.setattr(manager, '_check_repository', lambda: True) |
| 136 | + monkeypatch.setattr(manager, 'list_snapshots', lambda app_id: [{"id": "snap-1", "short_id": "snap-1"}]) |
| 137 | + monkeypatch.setattr(manager, '_run_restic_container', lambda command, extra_volumes: '{"message_type":"summary"}') |
| 138 | + monkeypatch.setattr(manager, '_ensure_restored_app_running', lambda *args, **kwargs: None) |
| 139 | + monkeypatch.setattr(back_manager_module, 'AppManger', lambda: types.SimpleNamespace( |
| 140 | + get_app_by_id=lambda app_id: types.SimpleNamespace( |
| 141 | + endpointId=1, |
| 142 | + volumes=[{"Mountpoint": "/var/lib/docker/volumes/wordpress_demo/_data", "Name": "wordpress_demo"}], |
| 143 | + ) |
| 144 | + )) |
| 145 | + monkeypatch.setattr(back_manager_module, 'PortainerManager', lambda: portainer) |
| 146 | + monkeypatch.setattr(manager, '_resolve_host_path', lambda path: path) |
| 147 | + |
| 148 | + manager.restore_backup('wordpress_demo', 'snap-1') |
| 149 | + |
| 150 | + assert portainer.stop_calls == [('wordpress_demo', 1)] |
| 151 | + assert portainer.start_calls == [('wordpress_demo', 1)] |
| 152 | + assert portainer.up_calls == [] |
| 153 | + |
| 154 | + |
118 | 155 | def test_restore_validation_rejects_only_exited_runtime_containers(monkeypatch): |
119 | 156 | manager = _build_manager() |
120 | 157 | portainer = FakePortainer( |
|
0 commit comments