|
1 | | -import pathlib |
2 | 1 | import time |
3 | | -from docker import DockerClient |
4 | | -from testcontainers.compose import DockerCompose |
| 2 | +import pytest |
5 | 3 |
|
6 | 4 | from channelfinder import ChannelFinderClient |
7 | 5 | import logging |
| 6 | +from .docker import setup_compose # noqa: F401 |
8 | 7 |
|
9 | 8 | logging.basicConfig( |
10 | 9 | level=logging.DEBUG, |
|
15 | 14 | LOG: logging.Logger = logging.getLogger(__name__) |
16 | 15 |
|
17 | 16 | MAX_WAIT_SECONDS = 180 |
| 17 | +TIME_PERIOD_INCREMENT = 2 |
| 18 | + |
| 19 | +# Number of channels expected in the default setup |
| 20 | +# 4 iocs, 6 channels per ioc (2 reccaster.db, 2 somerecords.db, 2 aliases in somerecords.db) |
18 | 21 | EXPECTED_DEFAULT_CHANNELS = 24 |
19 | 22 |
|
20 | 23 |
|
21 | | -def fullSetupDockerCompose() -> DockerCompose: |
22 | | - current_path = pathlib.Path(__file__).parent.resolve() |
| 24 | +def check_channel_count(cf_client, name="*"): |
| 25 | + channels = cf_client.find(name=name) |
| 26 | + LOG.debug("Found %s channels", len(channels)) |
| 27 | + return len(channels) == EXPECTED_DEFAULT_CHANNELS |
| 28 | + |
| 29 | + |
| 30 | +def wait_for_sync(cf_client, check): |
| 31 | + time_period_to_wait_seconds = 1 |
| 32 | + total_seconds_waited = 0 |
| 33 | + while total_seconds_waited < MAX_WAIT_SECONDS: |
| 34 | + if check(cf_client): |
| 35 | + break |
| 36 | + time.sleep(time_period_to_wait_seconds) |
| 37 | + total_seconds_waited += time_period_to_wait_seconds |
| 38 | + time_period_to_wait_seconds += TIME_PERIOD_INCREMENT |
| 39 | + |
| 40 | + |
| 41 | +def create_client_and_wait(compose): |
| 42 | + LOG.info("Waiting for channels to sync") |
| 43 | + cf_client = create_client_from_compose(compose) |
| 44 | + wait_for_sync(cf_client, lambda cf_client: check_channel_count(cf_client)) |
| 45 | + return cf_client |
| 46 | + |
23 | 47 |
|
24 | | - return DockerCompose( |
25 | | - str(current_path.parent.resolve()), |
26 | | - compose_file_name=str( |
27 | | - current_path.parent.joinpath("test-compose.yml").resolve() |
28 | | - ), |
29 | | - build=True, |
30 | | - ) |
| 48 | +def create_client_from_compose(compose): |
| 49 | + cf_host, cf_port = compose.get_service_host_and_port("cf") |
| 50 | + cf_url = f"http://{cf_host if cf_host else 'localhost'}:{cf_port}/ChannelFinder" |
| 51 | + # wait for channels to sync |
| 52 | + LOG.info("CF URL: %s", cf_url) |
| 53 | + cf_client = ChannelFinderClient(BaseURL=cf_url) |
| 54 | + return cf_client |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture(scope="class") |
| 58 | +def cf_client(setup_compose): # noqa: F811 |
| 59 | + return create_client_and_wait(setup_compose) |
31 | 60 |
|
32 | 61 |
|
33 | 62 | class TestE2E: |
34 | | - compose: DockerCompose |
35 | | - |
36 | | - def setup_method(self) -> None: |
37 | | - """Setup the test environment""" |
38 | | - LOG.info("Setting up test") |
39 | | - self.compose = fullSetupDockerCompose() |
40 | | - self.compose.start() |
41 | | - |
42 | | - def teardown_method(self) -> None: |
43 | | - """Teardown the test environment""" |
44 | | - |
45 | | - LOG.info("Tearing down test") |
46 | | - if self.compose: |
47 | | - LOG.info("Stopping docker compose") |
48 | | - if LOG.level <= logging.DEBUG: |
49 | | - docker_client = DockerClient() |
50 | | - conts = { |
51 | | - container.ID: container |
52 | | - for container in self.compose.get_containers() |
53 | | - } |
54 | | - for cont_id, cont in conts.items(): |
55 | | - log = docker_client.containers.get(cont_id).logs() |
56 | | - LOG.debug("Info for container %s", cont) |
57 | | - LOG.debug("Logs for container %s", cont.Name) |
58 | | - LOG.debug(log.decode("utf-8")) |
59 | | - self.compose.stop() |
60 | | - |
61 | | - def test_smoke(self) -> None: |
62 | | - """ |
63 | | - Test that the setup in the docker compose creates channels in channelfinder |
64 | | - """ |
65 | | - LOG.info("Waiting for channels to sync") |
66 | | - cf_host, cf_port = self.compose.get_service_host_and_port("cf") |
67 | | - cf_url = f"http://{cf_host if cf_host else 'localhost'}:{cf_port}/ChannelFinder" |
68 | | - # wait for channels to sync |
69 | | - LOG.info("CF URL: %s", cf_url) |
70 | | - cf_client = ChannelFinderClient(BaseURL=cf_url) |
71 | | - self.wait_for_sync(cf_client) |
| 63 | + def test_number_of_channels_and_channel_name(self, cf_client) -> None: |
72 | 64 | channels = cf_client.find(name="*") |
73 | 65 | assert len(channels) == EXPECTED_DEFAULT_CHANNELS |
74 | 66 | assert channels[0]["name"] == "IOC1-1:Msg-I" |
75 | 67 |
|
76 | | - def wait_for_sync(self, cf_client): |
77 | | - seconds_to_wait = 1 |
78 | | - total_seconds_waited = 0 |
79 | | - while total_seconds_waited < MAX_WAIT_SECONDS: |
80 | | - try: |
81 | | - channels = cf_client.find(name="*") |
82 | | - LOG.info( |
83 | | - "Found %s in %s seconds", |
84 | | - len(channels), |
85 | | - total_seconds_waited, |
86 | | - ) |
87 | | - if len(channels) == EXPECTED_DEFAULT_CHANNELS: |
88 | | - break |
89 | | - except Exception as e: |
90 | | - LOG.error(e) |
91 | | - time.sleep(seconds_to_wait) |
92 | | - total_seconds_waited += seconds_to_wait |
93 | | - seconds_to_wait += 2 |
| 68 | + # Smoke Test Default Properties |
| 69 | + def test_number_of_aliases_and_alais_property(self, cf_client) -> None: |
| 70 | + channels = cf_client.find(property=[("alias", "*")]) |
| 71 | + assert len(channels) == 8 |
| 72 | + assert channels[0]["name"] == "IOC1-1:lix1" |
| 73 | + assert { |
| 74 | + "name": "alias", |
| 75 | + "value": "IOC1-1:li", |
| 76 | + "owner": "admin", |
| 77 | + "channels": [], |
| 78 | + } in channels[0]["properties"] |
| 79 | + |
| 80 | + def test_number_of_recordDesc_and_property(self, cf_client) -> None: |
| 81 | + channels = cf_client.find(property=[("recordDesc", "*")]) |
| 82 | + assert len(channels) == 4 |
| 83 | + assert { |
| 84 | + "name": "recordDesc", |
| 85 | + "value": "testdesc", |
| 86 | + "owner": "admin", |
| 87 | + "channels": [], |
| 88 | + } in channels[0]["properties"] |
| 89 | + |
| 90 | + def test_number_of_recordType_and_property(self, cf_client) -> None: |
| 91 | + channels = cf_client.find(property=[("recordType", "*")]) |
| 92 | + assert len(channels) == EXPECTED_DEFAULT_CHANNELS |
| 93 | + assert { |
| 94 | + "name": "recordType", |
| 95 | + "value": "stringin", |
| 96 | + "owner": "admin", |
| 97 | + "channels": [], |
| 98 | + } in channels[0]["properties"] |
0 commit comments