Skip to content

Commit 098f74f

Browse files
authored
Merge pull request #123 from ChannelFinder/cf_restart_test
Test restarting channelfinder works ok
2 parents e663296 + 8fc1c74 commit 098f74f

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

server/tests/docker.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
from pathlib import Path
3+
from typing import Optional
34

45
import pytest
56
from testcontainers.compose import DockerCompose
@@ -42,8 +43,25 @@ def setup_compose():
4243
compose.stop()
4344

4445

45-
def restart_container(compose: DockerCompose, host_name: str) -> None:
46+
def restart_container(compose: DockerCompose, host_name: str) -> str:
4647
container = compose.get_container(host_name)
4748
docker_client = DockerClient()
4849
docker_client.containers.get(container.ID).stop()
4950
docker_client.containers.get(container.ID).start()
51+
return container.ID
52+
53+
54+
def shutdown_container(compose: DockerCompose, host_name: str) -> str:
55+
container = compose.get_container(host_name)
56+
docker_client = DockerClient()
57+
docker_client.containers.get(container.ID).stop()
58+
return container.ID
59+
60+
61+
def start_container(
62+
compose: DockerCompose, host_name: Optional[str] = None, container_id: Optional[str] = None
63+
) -> None:
64+
container_id = container_id or compose.get_container(host_name).ID
65+
if container_id:
66+
docker_client = DockerClient()
67+
docker_client.containers.get(container_id).start()

server/tests/test_restart.py

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import logging
2+
import time
23

34
import pytest
45
from channelfinder import ChannelFinderClient
56
from testcontainers.compose import DockerCompose
67

7-
from .client_checks import channels_match, check_channel_property, create_client_and_wait, wait_for_sync
8-
from .docker import restart_container, setup_compose # noqa: F401
8+
from .client_checks import (
9+
INACTIVE_PROPERTY,
10+
channels_match,
11+
check_channel_property,
12+
create_client_and_wait,
13+
wait_for_sync,
14+
)
15+
from .docker import restart_container, setup_compose, shutdown_container, start_container # noqa: F401
916

1017
PROPERTIES_TO_MATCH = ["pvStatus", "recordType", "recordDesc", "alias", "hostName", "iocName", "recceiverID"]
1118

@@ -45,3 +52,55 @@ def test_manual_channels_same_after_restart(
4552
channels_end = cf_client.find(name="*")
4653
assert len(channels_begin) == len(channels_end)
4754
channels_match(channels_begin, channels_end, PROPERTIES_TO_MATCH + ["test_property"])
55+
56+
57+
def check_connection_active(cf_client: ChannelFinderClient) -> bool:
58+
try:
59+
cf_client.find(name="*")
60+
except Exception:
61+
return False
62+
return True
63+
64+
65+
class TestRestartChannelFinder:
66+
def test_status_property_works_after_cf_restart(
67+
self,
68+
setup_compose: DockerCompose, # noqa: F811
69+
cf_client: ChannelFinderClient,
70+
) -> None:
71+
# Arrange
72+
# Act
73+
restart_container(setup_compose, "cf")
74+
assert wait_for_sync(cf_client, check_connection_active)
75+
76+
# Assert
77+
shutdown_container(setup_compose, "ioc1-1")
78+
assert wait_for_sync(
79+
cf_client, lambda cf_client: check_channel_property(cf_client, "IOC1-1:Msg-I", INACTIVE_PROPERTY)
80+
)
81+
channels_inactive = cf_client.find(property=[("iocName", "IOC1-1")])
82+
assert all(INACTIVE_PROPERTY in ch["properties"] for ch in channels_inactive)
83+
84+
85+
class TestShutdownChannelFinder:
86+
def test_status_property_works_between_cf_down(
87+
self,
88+
setup_compose: DockerCompose, # noqa: F811
89+
cf_client: ChannelFinderClient,
90+
) -> None:
91+
# Arrange
92+
cf_container_id = shutdown_container(setup_compose, "cf")
93+
time.sleep(10) # Wait to ensure CF is down while IOC is down
94+
95+
# Act
96+
shutdown_container(setup_compose, "ioc1-1")
97+
time.sleep(10) # Wait to ensure CF is down while IOC is down
98+
start_container(setup_compose, container_id=cf_container_id)
99+
assert wait_for_sync(cf_client, check_connection_active)
100+
101+
# Assert
102+
assert wait_for_sync(
103+
cf_client, lambda cf_client: check_channel_property(cf_client, "IOC1-1:Msg-I", INACTIVE_PROPERTY)
104+
)
105+
channels_inactive = cf_client.find(property=[("iocName", "IOC1-1")])
106+
assert all(INACTIVE_PROPERTY in ch["properties"] for ch in channels_inactive)

0 commit comments

Comments
 (0)