Skip to content

Commit 466a55d

Browse files
authored
Merge pull request #126 from jacomago/remove-things-tests
Remove things tests
2 parents fa0dcf9 + 638e6fb commit 466a55d

9 files changed

+122
-36
lines changed

server/tests/client_checks.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
TIME_PERIOD_INCREMENT = 2
1414
DEFAULT_CHANNEL_NAME = "IOC1-1:ai:test"
1515

16+
BASE_ALIAS_COUNT = 1
17+
BASE_RECORD_COUNT = 1
18+
BASE_IOC_CHANNEL_COUNT = BASE_ALIAS_COUNT + BASE_RECORD_COUNT
19+
1620

1721
def channel_match(channel0, channel1, properties_to_match: list[str]):
1822
assert channel0["name"] == channel1["name"]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
record(ai, "$(P)ai:test") {
3-
alias("$(P)ai:test:alias")
43
info("test", "testing")
4+
info("archive", "MONITOR@1")
55
field(DESC, "testdesc")
66
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
record(ai, "$(P)ai:test") {
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
record(ai, "$(P)ai:test") {
3+
}
4+
5+
record(ai, "$(P)ai:test-2") {
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
record(ai, "$(P)ai:test") {
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
record(ai, "$(P)ai:test") {
3+
info("archive", "testing")
4+
}

server/tests/test_bash_ioc.py

Lines changed: 94 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
from pathlib import Path
44
from typing import Optional
55

6+
from channelfinder import ChannelFinderClient
67
from testcontainers.compose import DockerCompose
78

89
from docker import DockerClient
910
from docker.models.containers import Container
1011

1112
from .client_checks import (
13+
BASE_IOC_CHANNEL_COUNT,
1214
DEFAULT_CHANNEL_NAME,
1315
INACTIVE_PROPERTY,
1416
check_channel_property,
@@ -46,43 +48,108 @@ def stream_logs(exec_result, cmd: str):
4648
log_thread.start()
4749

4850

49-
class TestRemoveProperty:
50-
def test_remove_property(self, setup_compose: DockerCompose) -> None: # noqa: F811
51+
def start_ioc(setup_compose: DockerCompose, db_file: Optional[str] = None) -> Container:
52+
ioc_container = setup_compose.get_container("ioc1-1")
53+
docker_client = DockerClient()
54+
docker_ioc = docker_client.containers.get(ioc_container.ID)
55+
docker_exec_new_command(docker_ioc, "./demo /ioc/st.cmd", env={"DB_FILE": db_file} if db_file else None)
56+
return docker_ioc
57+
58+
59+
def restart_ioc(
60+
ioc_container: Container, cf_client: ChannelFinderClient, channel_name: str, new_db_file: str
61+
) -> Container:
62+
ioc_container.stop()
63+
LOG.info("Waiting for channels to go inactive")
64+
assert wait_for_sync(
65+
cf_client,
66+
lambda cf_client: check_channel_property(cf_client, name=channel_name, prop=INACTIVE_PROPERTY),
67+
)
68+
ioc_container.start()
69+
70+
docker_exec_new_command(ioc_container, "./demo /ioc/st.cmd", env={"DB_FILE": new_db_file})
71+
# Detach by not waiting for the thread to finish
72+
73+
LOG.debug("ioc1-1 restart")
74+
assert wait_for_sync(cf_client, lambda cf_client: check_channel_property(cf_client, name=channel_name)), (
75+
"ioc1-1 failed to restart and sync"
76+
)
77+
78+
79+
class TestRemoveInfoTag:
80+
def test_remove_infotag(self, setup_compose: DockerCompose) -> None:
5181
"""
52-
Test that the setup in the docker compose creates channels in channelfinder
82+
Test that removing an infotag from a record works
5383
"""
54-
ioc_container = setup_compose.get_container("ioc1-1")
55-
docker_client = DockerClient()
56-
docker_ioc = docker_client.containers.get(ioc_container.ID)
57-
docker_exec_new_command(docker_ioc, "./demo /ioc/st.cmd")
58-
84+
test_channel_count = 1
85+
# Arrange
86+
docker_ioc = start_ioc(setup_compose, db_file="test_remove_infotag_before.db")
5987
LOG.info("Waiting for channels to sync")
60-
cf_client = create_client_and_wait(setup_compose, expected_channel_count=2)
88+
cf_client = create_client_and_wait(setup_compose, expected_channel_count=test_channel_count)
6189

6290
# Check ioc1-1 has ai:test with info tag "archive"
6391
LOG.debug('Checking ioc1-1 has ai:test with info tag "archive"')
64-
channel = cf_client.find(name=DEFAULT_CHANNEL_NAME)
92+
channels = cf_client.find(name=DEFAULT_CHANNEL_NAME)
93+
TEST_INFO_TAG = {"name": "archive", "owner": "admin", "value": "testing", "channels": []}
6594

66-
def get_len_archive_properties(channel):
67-
return len([prop for prop in channel[0]["properties"] if prop["name"] == "archive"])
95+
assert any(TEST_INFO_TAG in ch["properties"] for ch in channels), (
96+
"Info tag 'archive' not found in channel before removal"
97+
)
6898

69-
assert get_len_archive_properties(channel) == 1
99+
# Act
100+
restart_ioc(docker_ioc, cf_client, DEFAULT_CHANNEL_NAME, "test_remove_infotag_after.db")
70101

71-
docker_ioc.stop()
72-
LOG.info("Waiting for channels to go inactive")
73-
assert wait_for_sync(
74-
cf_client,
75-
lambda cf_client: check_channel_property(cf_client, name=DEFAULT_CHANNEL_NAME, prop=INACTIVE_PROPERTY),
102+
# Assert
103+
channels = cf_client.find(name=DEFAULT_CHANNEL_NAME)
104+
LOG.debug("archive channels: %s", channels)
105+
assert all(TEST_INFO_TAG not in ch["properties"] for ch in channels), (
106+
"Info tag 'archive' still found in channel after removal"
76107
)
77-
docker_ioc.start()
78108

79-
docker_exec_new_command(docker_ioc, "./demo /ioc/st.cmd", env={"DB_FILE": "test_remove_infotag.db"})
80-
# Detach by not waiting for the thread to finish
81109

82-
LOG.debug("ioc1-1 restart")
83-
assert wait_for_sync(cf_client, lambda cf_client: check_channel_property(cf_client, name=DEFAULT_CHANNEL_NAME))
84-
LOG.debug("ioc1-1 has restarted and synced")
110+
class TestRemoveChannel:
111+
def test_remove_channel(self, setup_compose: DockerCompose) -> None: # noqa: F811
112+
"""
113+
Test that removing a channel works correctly.
114+
"""
115+
# Arrange
116+
docker_ioc = start_ioc(setup_compose, db_file="test_remove_channel_before.db")
117+
LOG.info("Waiting for channels to sync")
118+
cf_client = create_client_and_wait(setup_compose, expected_channel_count=2)
119+
120+
# Check ioc1-1 has base channel
121+
LOG.debug("Checking ioc1-1 has both channels before removal")
122+
check_channel_property(cf_client, name=DEFAULT_CHANNEL_NAME)
123+
second_channel_name = f"{DEFAULT_CHANNEL_NAME}-2"
124+
check_channel_property(cf_client, name=second_channel_name)
125+
126+
# Act
127+
restart_ioc(docker_ioc, cf_client, DEFAULT_CHANNEL_NAME, "test_remove_channel_after.db")
128+
129+
# Assert
130+
check_channel_property(cf_client, name=second_channel_name, prop=INACTIVE_PROPERTY)
131+
check_channel_property(cf_client, name=DEFAULT_CHANNEL_NAME)
132+
133+
134+
class TestRemoveAlias:
135+
def test_remove_alias(self, setup_compose: DockerCompose) -> None: # noqa: F811
136+
"""
137+
Test that removing an alias works correctly.
138+
"""
139+
# Arrange
140+
docker_ioc = start_ioc(setup_compose)
141+
LOG.info("Waiting for channels to sync")
142+
cf_client = create_client_and_wait(setup_compose, expected_channel_count=BASE_IOC_CHANNEL_COUNT)
143+
144+
# Check before alias status
145+
LOG.debug('Checking ioc1-1 has ai:base_pv3 has an Active alias"')
146+
channel_alias_name = f"{DEFAULT_CHANNEL_NAME}:alias"
147+
check_channel_property(cf_client, name=DEFAULT_CHANNEL_NAME)
148+
check_channel_property(cf_client, name=channel_alias_name)
149+
150+
# Act
151+
restart_ioc(docker_ioc, cf_client, DEFAULT_CHANNEL_NAME, "test_remove_alias_after.db")
85152

86-
channel = cf_client.find(name=DEFAULT_CHANNEL_NAME)
87-
LOG.debug("archive channel: %s", channel)
88-
assert get_len_archive_properties(channel) == 0
153+
# Assert
154+
check_channel_property(cf_client, name=DEFAULT_CHANNEL_NAME)
155+
check_channel_property(cf_client, name=channel_alias_name, prop=INACTIVE_PROPERTY)

server/tests/test_multiple_recceiver.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
from channelfinder import ChannelFinderClient
66
from testcontainers.compose import DockerCompose
77

8-
from .client_checks import DEFAULT_CHANNEL_NAME, create_client_and_wait
8+
from .client_checks import BASE_ALIAS_COUNT, BASE_IOC_CHANNEL_COUNT, DEFAULT_CHANNEL_NAME, create_client_and_wait
99
from .docker import ComposeFixtureFactory
1010

1111
LOG: logging.Logger = logging.getLogger(__name__)
1212

1313
RECSYNC_RESTART_DELAY = 30
1414
# Number of channels expected in the default setup
15-
# 4 iocs, 1 channel 1 alias in archive.db
16-
EXPECTED_DEFAULT_CHANNEL_COUNT = 4 * 2
15+
IOC_COUNT = 4
16+
EXPECTED_DEFAULT_CHANNEL_COUNT = IOC_COUNT * BASE_IOC_CHANNEL_COUNT
1717

1818
setup_compose = ComposeFixtureFactory(Path("tests") / "docker" / "test-multi-recc.yml").return_fixture()
1919

@@ -32,7 +32,7 @@ def test_number_of_channels_and_channel_name(self, cf_client: ChannelFinderClien
3232
# Smoke Test Default Properties
3333
def test_number_of_aliases_and_alais_property(self, cf_client: ChannelFinderClient) -> None:
3434
channels = cf_client.find(property=[("alias", "*")])
35-
assert len(channels) == 4
35+
assert len(channels) == IOC_COUNT * BASE_ALIAS_COUNT
3636
assert channels[0]["name"] == DEFAULT_CHANNEL_NAME + ":alias"
3737
assert {
3838
"name": "alias",
@@ -43,7 +43,7 @@ def test_number_of_aliases_and_alais_property(self, cf_client: ChannelFinderClie
4343

4444
def test_number_of_recordDesc_and_property(self, cf_client: ChannelFinderClient) -> None:
4545
channels = cf_client.find(property=[("recordDesc", "*")])
46-
assert len(channels) == 8
46+
assert len(channels) == EXPECTED_DEFAULT_CHANNEL_COUNT
4747
assert {
4848
"name": "recordDesc",
4949
"value": "testdesc",

server/tests/test_single_ioc.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from testcontainers.compose import DockerCompose
88

99
from .client_checks import (
10+
BASE_IOC_CHANNEL_COUNT,
1011
DEFAULT_CHANNEL_NAME,
1112
INACTIVE_PROPERTY,
1213
channels_match,
@@ -26,14 +27,12 @@
2627

2728
LOG: logging.Logger = logging.getLogger(__name__)
2829

29-
EXPECTED_DEFAULT_CHANNEL_COUNT = 2
30-
3130
setup_compose = ComposeFixtureFactory(Path("tests") / "docker" / "test-single-ioc.yml").return_fixture()
3231

3332

3433
@pytest.fixture(scope="class")
3534
def cf_client(setup_compose: DockerCompose) -> ChannelFinderClient: # noqa: F811
36-
return create_client_and_wait(setup_compose, expected_channel_count=EXPECTED_DEFAULT_CHANNEL_COUNT)
35+
return create_client_and_wait(setup_compose, expected_channel_count=BASE_IOC_CHANNEL_COUNT)
3736

3837

3938
class TestRestartIOC:

0 commit comments

Comments
 (0)