Skip to content

Commit dde78fa

Browse files
jasiu86pgierasi
authored andcommitted
Reorder tests to minimalize opened containers
1 parent bca22cd commit dde78fa

23 files changed

+253
-381
lines changed

tests/functional/config.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#
1616

1717
import os
18-
import sys
1918

2019
from utils.helpers import get_int, get_bool
2120
from utils.parametrization import generate_test_object_name
@@ -117,6 +116,3 @@
117116
""" TT_SKIP_TEST_IF_HDDL_MTLS """
118117
skip_hddl_tests = get_bool("TT_SKIP_TEST_IF_HDDL_MTLS", "True")
119118
skip_hddl_tests = skip_hddl_tests and target_device == "HDDL"
120-
121-
""" USING_XDIST """
122-
using_xdist = ('-n' in sys.argv) or ('-c' in sys.argv)

tests/functional/conftest.py

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#
1616
import logging
1717
import os
18-
import pickle
1918
from collections import defaultdict
2019
from logging import FileHandler
2120

@@ -25,17 +24,16 @@
2524
from _pytest.outcomes import OutcomeException
2625

2726
from constants import MODEL_SERVICE, PREDICTION_SERVICE
28-
from utils.helpers import get_xdist_worker_nr, get_xdist_worker_count
29-
from utils.xdist_utils import OvmsCLoadScheduling
3027
from object_model.server import Server
28+
from utils.other import reorder_items_by_fixtures_used
3129
from utils.cleanup import clean_hanging_docker_resources, delete_test_directory, \
3230
get_containers_with_tests_suffix, get_docker_client
3331
from utils.logger import init_logger
3432
from tensorflow_serving.apis import prediction_service_pb2_grpc, \
3533
model_service_pb2_grpc # noqa
3634
from utils.files_operation import get_path_friendly_test_name
3735
from utils.parametrization import get_tests_suffix
38-
from config import test_dir, test_dir_cleanup, artifacts_dir, using_xdist
36+
from config import test_dir, test_dir_cleanup, artifacts_dir
3937

4038
logger = logging.getLogger(__name__)
4139

@@ -83,11 +81,6 @@ def pytest_configure():
8381
# Perform initial configuration.
8482
init_logger()
8583

86-
# if not os.environ.get("PYTEST_XDIST_WORKER", None):
87-
# copy_cached_models_to_test_dir()
88-
# copy_cached_resnet_models()
89-
90-
9184
init_conf_logger = logging.getLogger("init_conf")
9285

9386
container_names = get_containers_with_tests_suffix()
@@ -121,45 +114,7 @@ def pytest_unconfigure():
121114
@pytest.hookimpl(hookwrapper=True)
122115
def pytest_collection_modifyitems(session, config, items):
123116
yield
124-
items = OvmsCLoadScheduling.reorder_items_by_fixtures_used(session)
125-
126-
127-
#
128-
# @pytest.hookimpl(hookwrapper=True)
129-
# def pytest_collection_finish(session):
130-
# yield
131-
# # Collect all fixtures that starts Docker instance
132-
# # This map will keep fixture usages in
133-
#
134-
135-
136-
# if not using_xdist:
137-
# @pytest.hookimpl(hookwrapper=True)
138-
# def pytest_runtestloop(session):
139-
# # Override default runtestloop in order to sort test execution by used fixtures
140-
# # This operation will ensure that only required containers will run and container will be cleared after all usages.
141-
#
142-
# # Collect all fixtures that starts Docker instance
143-
# # This map will keep fixture usages in tests
144-
# server_fixtures_to_item = defaultdict(lambda: [])
145-
# for item in session.items:
146-
# item._server_fixtures = list(filter(lambda x: "start_server_" in x, item.fixturenames))
147-
# for fixture in item._server_fixtures:
148-
# server_fixtures_to_item[fixture].append(item)
149-
#
150-
# # Sort test items using required fixtures as key (group test by fixture)
151-
# sorted_items = sorted(session.items, key=lambda x: x._server_fixtures )
152-
#
153-
# for i, item in enumerate(sorted_items):
154-
# nextitem = sorted_items[i + 1] if i + 1 < len(sorted_items) else None
155-
# item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
156-
#
157-
# # Test finished: remove test item for all fixtures that was used
158-
# for fixture in item._server_fixtures:
159-
# server_fixtures_to_item[fixture].remove(item)
160-
# if len(server_fixtures_to_item[fixture]) == 0:
161-
# # No other tests will use this docker instance so we can close it.
162-
# Server.stop_by_fixture_name(fixture)
117+
items = reorder_items_by_fixtures_used(session)
163118

164119

165120
@pytest.hookimpl(hookwrapper=True)
@@ -197,14 +152,13 @@ def pytest_runtest_teardown(item):
197152
yield
198153
# Test finished: remove test item for all fixtures that was used
199154
for fixture in item._server_fixtures:
200-
if item in item.session._server_fixtures_to_item[fixture]:
201-
item.session._server_fixtures_to_item[fixture].remove(item)
202-
if len(item.session._server_fixtures_to_item[fixture]) == 0:
155+
if item in item.session._server_fixtures_to_tests[fixture]:
156+
item.session._server_fixtures_to_tests[fixture].remove(item)
157+
if len(item.session._server_fixtures_to_tests[fixture]) == 0:
203158
# No other tests will use this docker instance so we can close it.
204159
Server.stop_by_fixture_name(fixture)
205160

206161

207-
208162
def exception_catcher(when: str, outcome):
209163
if isinstance(outcome.excinfo, tuple):
210164
if len(outcome.excinfo) > 1 and isinstance(outcome.excinfo[1], OutcomeException):
@@ -241,8 +195,3 @@ def pytest_runtest_logfinish(nodeid, location):
241195
_root_logger.removeHandler(_root_logger._test_log_handler)
242196
yield
243197

244-
245-
def pytest_xdist_make_scheduler(config, log):
246-
scheduler = OvmsCLoadScheduling(config, log)
247-
log.debug("Created xdist scheduler")
248-
return scheduler

tests/functional/fixtures/server_detection_model_fixtures.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
from pathlib import Path
1716

1817
import pytest
1918

@@ -33,7 +32,7 @@ def start_server_face_detection_model_auto_shape(request):
3332
container_name_infix = "test-auto-shape"
3433
server = Server(request, start_server_command_args,
3534
container_name_infix, config.start_container_command,
36-
target_device=config.target_device, path_to_mount=Path(config.path_to_mount, __name__))
35+
target_device=config.target_device)
3736
return server.start()
3837

3938

@@ -49,7 +48,7 @@ def start_server_face_detection_model_named_shape(request):
4948
container_name_infix = "test-named-shape"
5049
server = Server(request, start_server_command_args,
5150
container_name_infix, config.start_container_command,
52-
target_device=config.target_device, path_to_mount=Path(config.path_to_mount, __name__))
51+
target_device=config.target_device)
5352
return server.start()
5453

5554

@@ -64,5 +63,5 @@ def start_server_face_detection_model_nonamed_shape(request):
6463
container_name_infix = "test-nonamed-shape"
6564
server = Server(request, start_server_command_args,
6665
container_name_infix, config.start_container_command,
67-
target_device=config.target_device, path_to_mount=Path(config.path_to_mount, __name__))
66+
target_device=config.target_device)
6867
return server.start()

tests/functional/fixtures/server_for_update_fixtures.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
from pathlib import Path
1716

1817
import pytest
1918
import shutil
@@ -38,7 +37,7 @@ def start_server_update_flow_latest(request):
3837
container_name_infix = "test-update-latest"
3938
server = Server(request, start_server_command_args,
4039
container_name_infix, config.start_container_command,
41-
target_device=config.target_device, path_to_mount=Path(config.path_to_mount, __name__))
40+
target_device=config.target_device)
4241
return server.start()
4342

4443

@@ -55,5 +54,5 @@ def start_server_update_flow_specific(request):
5554
container_name_infix = "test-update-specific"
5655
server = Server(request, start_server_command_args,
5756
container_name_infix, config.start_container_command,
58-
target_device=config.target_device, path_to_mount=Path(config.path_to_mount, __name__))
57+
target_device=config.target_device)
5958
return server.start()

tests/functional/fixtures/server_local_models_fixtures.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
import shutil
1818
import os
19-
from pathlib import Path
20-
2119
import pytest
2220

2321
import config
@@ -38,8 +36,7 @@ def start_server_single_model(request):
3836

3937
server = Server(request, start_server_command_args,
4038
container_name_infix, config.start_container_command,
41-
env_variables, target_device=config.target_device,
42-
path_to_mount=Path(config.path_to_mount, __name__))
39+
env_variables, target_device=config.target_device)
4340
return server.start()
4441

4542
@pytest.fixture(scope="session")
@@ -55,22 +52,25 @@ def start_server_single_model_onnx(request):
5552

5653
server = Server(request, start_server_command_args,
5754
container_name_infix, config.start_container_command,
58-
env_variables, target_device=config.target_device,
59-
path_to_mount=Path(config.path_to_mount, __name__))
55+
env_variables, target_device=config.target_device)
6056
return server.start()
6157

6258
@pytest.fixture(scope="session")
6359
def start_server_with_mapping(request):
64-
path_to_mount = Path(config.path_to_mount, __name__)
6560

66-
file_dst_path = path_to_mount + '/age_gender/1/mapping_config.json'
67-
Path(path_to_mount).mkdir(parents=True, exist_ok=True)
61+
def delete_mapping_file():
62+
if os.path.exists(file_dst_path):
63+
os.remove(file_dst_path)
64+
65+
request.addfinalizer(delete_mapping_file)
66+
67+
file_dst_path = config.path_to_mount + '/age_gender/1/mapping_config.json'
6868
shutil.copyfile('tests/functional/mapping_config.json', file_dst_path)
6969

7070
start_server_command_args = {"model_name": AgeGender.name,
7171
"model_path": AgeGender.model_path}
7272
container_name_infix = "test-2-out"
7373
server = Server(request, start_server_command_args,
7474
container_name_infix, config.start_container_command,
75-
target_device=config.target_device, path_to_mount=path_to_mount)
75+
target_device=config.target_device)
7676
return server.start()

tests/functional/fixtures/server_multi_model_fixtures.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#
1616

1717
import os
18-
from pathlib import Path
19-
2018
import pytest
2119

2220
import config
@@ -51,6 +49,5 @@ def start_server_multi_model(request, start_minio_server, get_minio_server_s3):
5149
"rest_workers": 2}
5250
container_name_infix = "test-multi"
5351
server = Server(request, start_server_command_args,
54-
container_name_infix, config.start_container_command, envs,
55-
path_to_mount=Path(config.path_to_mount, __name__))
52+
container_name_infix, config.start_container_command, envs)
5653
return server.start()

tests/functional/fixtures/server_remote_models_fixtures.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#
1616

1717
import os
18-
from pathlib import Path
1918

2019
import boto3
2120
import pytest
@@ -42,7 +41,7 @@ def start_server_single_model_from_gc(request):
4241
envs = ['https_proxy=' + os.getenv('https_proxy', "")]
4342
server = Server(request, start_server_command_args,
4443
container_name_infix, config.start_container_command, envs,
45-
target_device=config.target_device, path_to_mount=Path(config.path_to_mount, __name__))
44+
target_device=config.target_device)
4645
return server.start()
4746

4847

@@ -164,5 +163,5 @@ def start_server_single_model_from_minio(request, get_minio_server_s3):
164163
container_name_infix = "test-single-minio"
165164
server = Server(request, start_server_command_args,
166165
container_name_infix, config.start_container_command, envs,
167-
target_device=config.target_device, path_to_mount=Path(config.path_to_mount, __name__))
166+
target_device=config.target_device)
168167
return server.start()

tests/functional/fixtures/server_with_batching_fixtures.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
from pathlib import Path
1716

1817
import pytest
1918

@@ -29,7 +28,7 @@ def start_server_batch_model(request):
2928
container_name_infix = "test-batch"
3029
server = Server(request, start_server_command_args,
3130
container_name_infix, config.start_container_command,
32-
target_device=config.target_device, path_to_mount=Path(config.path_to_mount, __name__))
31+
target_device=config.target_device)
3332
return server.start()
3433

3534

@@ -41,7 +40,7 @@ def start_server_batch_model_2out(request):
4140
container_name_infix = "test-batch-2out"
4241
server = Server(request, start_server_command_args,
4342
container_name_infix, config.start_container_command,
44-
target_device=config.target_device, path_to_mount=Path(config.path_to_mount, __name__))
43+
target_device=config.target_device)
4544
return server.start()
4645

4746

@@ -54,7 +53,7 @@ def start_server_batch_model_auto(request):
5453
container_name_infix = "test-autobatch"
5554
server = Server(request, start_server_command_args,
5655
container_name_infix, config.start_container_command,
57-
target_device=config.target_device, path_to_mount=Path(config.path_to_mount, __name__))
56+
target_device=config.target_device)
5857
return server.start()
5958

6059

@@ -67,7 +66,7 @@ def start_server_batch_model_auto_2out(request):
6766
container_name_infix = "test-autobatch-2out"
6867
server = Server(request, start_server_command_args,
6968
container_name_infix, config.start_container_command,
70-
target_device=config.target_device, path_to_mount=Path(config.path_to_mount, __name__))
69+
target_device=config.target_device)
7170
return server.start()
7271

7372

@@ -80,7 +79,7 @@ def start_server_batch_model_bs4(request):
8079
container_name_infix = "test-batch4"
8180
server = Server(request, start_server_command_args,
8281
container_name_infix, config.start_container_command,
83-
target_device=config.target_device, path_to_mount=Path(config.path_to_mount, __name__))
82+
target_device=config.target_device)
8483
return server.start()
8584

8685

@@ -92,6 +91,5 @@ def start_server_batch_model_auto_bs4_2out(request):
9291
"batch_size": 4}
9392
container_name_infix = "test-batch4-2out"
9493
server = Server(request, start_server_command_args,
95-
container_name_infix, config.start_container_command,
96-
path_to_mount=Path(config.path_to_mount, __name__))
94+
container_name_infix, config.start_container_command)
9795
return server.start()

tests/functional/fixtures/server_with_version_policy_fixtures.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#
1616

1717
import os
18-
from pathlib import Path
19-
2018
import pytest
2119
import shutil
2220
from distutils.dir_util import copy_tree
@@ -36,8 +34,7 @@ def start_server_model_ver_policy(request):
3634
container_name_infix = "test-batch4-2out"
3735

3836
server = Server(request, start_server_command_args,
39-
container_name_infix, config.start_container_command,
40-
path_to_mount=Path(config.path_to_mount, __name__))
37+
container_name_infix, config.start_container_command)
4138
return server.start()
4239

4340

tests/functional/object_model/docker.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
1515
#
1616
import os
1717
import time
18-
from pathlib import Path
1918
from typing import List
2019

2120
from datetime import datetime
2221

2322
import docker
2423
from utils.files_operation import get_path_friendly_test_name
2524

26-
from config import target_device
2725
from retry.api import retry_call
2826

2927
import config
@@ -126,12 +124,6 @@ def _start(self):
126124
self.ensure_logs_contains()
127125
logger.info(f"Container started grpc_port:{self.grpc_port}\trest_port:{self.rest_port}")
128126
logger.debug(f"Container starting command args: {self.start_container_command}")
129-
130-
tt = list(map(lambda x: x.name, self.client.containers.list()))
131-
tt = list(map(lambda x: "_".join(x.split("_")[2:-3]), tt))
132-
133-
foo = 0
134-
135127
return self.container, {"grpc_port": self.grpc_port, "rest_port": self.rest_port}
136128

137129
def stop(self):
@@ -202,7 +194,7 @@ def save_container_logs_to_file(self, logs, dir_path: str = config.artifacts_dir
202194
if location:
203195
file_name = f"ovms_{get_path_friendly_test_name(location)}_{time_stamp}.log"
204196
else:
205-
file_name = f"ovms_{self.server.started_by_fixture}_{time_stamp}.log"
197+
file_name = f"ovms_{self.server.started_by_fixture.lstrip('start_')}_{time_stamp}.log"
206198
os.makedirs(dir_path, exist_ok=True)
207199
file_path = os.path.join(dir_path, file_name)
208200
with open(file_path, "w+") as text_file:

0 commit comments

Comments
 (0)