Skip to content

Commit 6b47a8b

Browse files
authored
Merge pull request #125 from ChannelFinder/alias_test
Move an IOC to another host test
2 parents 098f74f + 1820ad6 commit 6b47a8b

File tree

8 files changed

+114
-35
lines changed

8 files changed

+114
-35
lines changed

.github/workflows/server.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: recceiver
33

44
on:
55
push:
6-
branches: [ "master" ]
6+
branches: [ "*" ]
77
paths:
88
- server/**
99
pull_request:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: test-removeinfotag
1+
name: test-bash-ioc
22
include:
33
- cf-compose.yml
44
services:

server/docker/test-multi-recc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ services:
3636
restart: true
3737
environment:
3838
- IOCSH_NAME=IOC1-2
39-
hostname: ioc1-1
39+
hostname: ioc1-2
4040
recc2:
4141
extends:
4242
recc1

server/docker/test-single-ioc.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: test-single-ioc
2+
include:
3+
- cf-compose.yml
4+
services:
5+
recc1:
6+
extends:
7+
file: ../compose.yml
8+
service: recc
9+
depends_on:
10+
cf:
11+
condition: service_healthy
12+
restart: true
13+
hostname: recc1
14+
networks:
15+
- net-1-recc-1
16+
- net-2-cf
17+
ioc1-1:
18+
extends:
19+
file: ../../client/ioc-compose.yml
20+
service: ioc1
21+
depends_on:
22+
recc1:
23+
condition: service_healthy
24+
restart: true
25+
environment:
26+
- IOCSH_NAME=IOC1-1
27+
hostname: ioc1-1
28+
networks:
29+
- net-1-recc-1
30+
31+
networks:
32+
net-1-recc-1:
33+
driver: bridge

server/tests/docker.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import time
23
from pathlib import Path
34
from typing import Optional
45

@@ -10,7 +11,7 @@
1011
LOG: logging.Logger = logging.getLogger(__name__)
1112

1213

13-
def test_compose(compose_file=Path("docker") / Path("test-multi-recc.yml")) -> DockerCompose:
14+
def test_compose(compose_file: Path) -> DockerCompose:
1415
current_path = Path(__file__).parent.resolve()
1516

1617
return DockerCompose(
@@ -30,17 +31,24 @@ def fetch_containers_and_log_logs(compose: DockerCompose) -> None:
3031
LOG.debug(log.decode("utf-8"))
3132

3233

33-
@pytest.fixture(scope="class")
34-
def setup_compose():
35-
LOG.info("Setup test environment")
36-
compose = test_compose()
37-
compose.start()
38-
yield compose
39-
LOG.info("Teardown test environment")
40-
LOG.info("Stopping docker compose")
41-
if LOG.level <= logging.DEBUG:
42-
fetch_containers_and_log_logs(compose)
43-
compose.stop()
34+
class ComposeFixtureFactory:
35+
def __init__(self, compose_file: Path) -> None:
36+
self.compose_file = compose_file
37+
38+
def return_fixture(self):
39+
@pytest.fixture(scope="class")
40+
def setup_compose() -> DockerCompose:
41+
LOG.info("Setup test environment")
42+
compose = test_compose(self.compose_file)
43+
compose.start()
44+
yield compose
45+
LOG.info("Teardown test environment")
46+
LOG.info("Stopping docker compose")
47+
if LOG.level <= logging.DEBUG:
48+
fetch_containers_and_log_logs(compose)
49+
compose.stop()
50+
51+
return setup_compose
4452

4553

4654
def restart_container(compose: DockerCompose, host_name: str) -> str:
@@ -65,3 +73,27 @@ def start_container(
6573
if container_id:
6674
docker_client = DockerClient()
6775
docker_client.containers.get(container_id).start()
76+
77+
78+
def clone_container(
79+
compose: DockerCompose,
80+
new_host_name: str,
81+
host_name: Optional[str] = None,
82+
container_id: Optional[str] = None,
83+
sleep_time=10,
84+
) -> str:
85+
container_id = container_id or compose.get_container(host_name).ID
86+
if not container_id:
87+
raise Exception("Container not found")
88+
89+
docker_client = DockerClient()
90+
container = docker_client.containers.get(container_id)
91+
image = container.image
92+
networks = container.attrs["NetworkSettings"]["Networks"].keys()
93+
container.stop()
94+
time.sleep(sleep_time)
95+
container.remove()
96+
docker_client.containers.run(
97+
image, detach=True, environment={"IOC_NAME": host_name}, hostname=new_host_name, network=list(networks)[0]
98+
)
99+
return container_id
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
import threading
33
from pathlib import Path
44

5-
import pytest
65
from testcontainers.compose import DockerCompose
76

87
from docker import DockerClient
98
from docker.models.containers import Container
109

1110
from .client_checks import INACTIVE_PROPERTY, check_channel_property, create_client_and_wait, wait_for_sync
12-
from .docker import fetch_containers_and_log_logs, test_compose # noqa: F401
11+
from .docker import ComposeFixtureFactory
1312

1413
LOG: logging.Logger = logging.getLogger(__name__)
1514

@@ -19,18 +18,7 @@
1918
encoding="utf-8",
2019
)
2120

22-
23-
@pytest.fixture(scope="class")
24-
def setup_compose():
25-
LOG.info("Setup remove test environment")
26-
compose = test_compose(Path("docker") / Path("test-remove-infotag.yml"))
27-
compose.start()
28-
yield compose
29-
LOG.info("Teardown test environment")
30-
LOG.info("Stopping docker compose")
31-
if LOG.level <= logging.DEBUG:
32-
fetch_containers_and_log_logs(compose)
33-
compose.stop()
21+
setup_compose = ComposeFixtureFactory(Path("docker") / Path("test-bash-ioc.yml")).return_fixture()
3422

3523

3624
def docker_exec_new_command(container: Container, command: str):
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import logging
2+
from pathlib import Path
23

34
import pytest
45
from channelfinder import ChannelFinderClient
6+
from testcontainers.compose import DockerCompose
57

68
from .client_checks import create_client_and_wait
7-
from .docker import setup_compose # noqa: F401
9+
from .docker import ComposeFixtureFactory
810

911
LOG: logging.Logger = logging.getLogger(__name__)
1012

@@ -13,13 +15,15 @@
1315
# 4 iocs, 6 channels per ioc (2 reccaster.db, 2 somerecords.db, 2 aliases in somerecords.db)
1416
EXPECTED_DEFAULT_CHANNEL_COUNT = 32
1517

18+
setup_compose = ComposeFixtureFactory(Path("docker") / Path("test-multi-recc.yml")).return_fixture()
19+
1620

1721
@pytest.fixture(scope="class")
18-
def cf_client(setup_compose): # noqa: F811
22+
def cf_client(setup_compose: DockerCompose): # noqa: F811
1923
return create_client_and_wait(setup_compose, EXPECTED_DEFAULT_CHANNEL_COUNT)
2024

2125

22-
class TestE2E:
26+
class TestMultipleRecceiver:
2327
def test_number_of_channels_and_channel_name(self, cf_client: ChannelFinderClient) -> None:
2428
channels = cf_client.find(name="*")
2529
assert len(channels) == EXPECTED_DEFAULT_CHANNEL_COUNT
Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import time
3+
from pathlib import Path
34

45
import pytest
56
from channelfinder import ChannelFinderClient
@@ -12,17 +13,25 @@
1213
create_client_and_wait,
1314
wait_for_sync,
1415
)
15-
from .docker import restart_container, setup_compose, shutdown_container, start_container # noqa: F401
16+
from .docker import (
17+
ComposeFixtureFactory,
18+
clone_container,
19+
restart_container,
20+
shutdown_container,
21+
start_container,
22+
)
1623

1724
PROPERTIES_TO_MATCH = ["pvStatus", "recordType", "recordDesc", "alias", "hostName", "iocName", "recceiverID"]
1825

1926
LOG: logging.Logger = logging.getLogger(__name__)
2027

21-
EXPECTED_DEFAULT_CHANNEL_COUNT = 32
28+
EXPECTED_DEFAULT_CHANNEL_COUNT = 8
29+
30+
setup_compose = ComposeFixtureFactory(Path("docker") / Path("test-single-ioc.yml")).return_fixture()
2231

2332

2433
@pytest.fixture(scope="class")
25-
def cf_client(setup_compose): # noqa: F811
34+
def cf_client(setup_compose: DockerCompose) -> ChannelFinderClient: # noqa: F811
2635
return create_client_and_wait(setup_compose, expected_channel_count=EXPECTED_DEFAULT_CHANNEL_COUNT)
2736

2837

@@ -104,3 +113,16 @@ def test_status_property_works_between_cf_down(
104113
)
105114
channels_inactive = cf_client.find(property=[("iocName", "IOC1-1")])
106115
assert all(INACTIVE_PROPERTY in ch["properties"] for ch in channels_inactive)
116+
117+
118+
class TestMoveIocHost:
119+
def test_move_ioc_host(
120+
self,
121+
setup_compose: DockerCompose, # noqa: F811
122+
cf_client: ChannelFinderClient,
123+
) -> None:
124+
channels_begin = cf_client.find(name="*")
125+
clone_container(setup_compose, "ioc1-1-new", host_name="ioc1-1")
126+
wait_for_sync(cf_client, lambda cf_client: check_channel_property(cf_client, "IOC1-1:Msg-I"))
127+
channels_end = cf_client.find(name="*")
128+
assert len(channels_begin) == len(channels_end)

0 commit comments

Comments
 (0)