|
3 | 3 | from pathlib import Path |
4 | 4 | from typing import Optional |
5 | 5 |
|
| 6 | +from channelfinder import ChannelFinderClient |
6 | 7 | from testcontainers.compose import DockerCompose |
7 | 8 |
|
8 | 9 | from docker import DockerClient |
9 | 10 | from docker.models.containers import Container |
10 | 11 |
|
11 | 12 | from .client_checks import ( |
| 13 | + BASE_IOC_CHANNEL_COUNT, |
12 | 14 | DEFAULT_CHANNEL_NAME, |
13 | 15 | INACTIVE_PROPERTY, |
14 | 16 | check_channel_property, |
@@ -46,43 +48,108 @@ def stream_logs(exec_result, cmd: str): |
46 | 48 | log_thread.start() |
47 | 49 |
|
48 | 50 |
|
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: |
51 | 81 | """ |
52 | | - Test that the setup in the docker compose creates channels in channelfinder |
| 82 | + Test that removing an infotag from a record works |
53 | 83 | """ |
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") |
59 | 87 | 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) |
61 | 89 |
|
62 | 90 | # Check ioc1-1 has ai:test with info tag "archive" |
63 | 91 | 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": []} |
65 | 94 |
|
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 | + ) |
68 | 98 |
|
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") |
70 | 101 |
|
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" |
76 | 107 | ) |
77 | | - docker_ioc.start() |
78 | 108 |
|
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 |
81 | 109 |
|
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") |
85 | 152 |
|
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) |
0 commit comments