annotate NewTests/helpers.py @ 484:ddfabe1fbee1

new tests for DelayedDeletion plugin
author Alain Mazy <am@osimis.io>
date Wed, 22 Jun 2022 16:06:46 +0200
parents 45c3fe035fed
children 10a47656e34f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
1 import unittest
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
2 from orthanc_api_client import OrthancApiClient
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
3 import subprocess
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
4 import json
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
5 import time
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
6 import typing
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
7 import shutil
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
8 from threading import Thread
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
9
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
10
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
11 import pathlib
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
12 here = pathlib.Path(__file__).parent.resolve()
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
13
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
14
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
15 default_base_config = {
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
16 "AuthenticationEnabled": False,
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
17 "RemoteAccessAllowed": True
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
18 }
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
19
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
20 class Helpers:
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
21
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
22 orthanc_under_tests_hostname: str = 'localhost'
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
23 orthanc_under_tests_http_port: int = 8052
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
24 orthanc_under_tests_dicom_port: int = 4252
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
25 orthanc_under_tests_exe: str = None
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
26 orthanc_previous_version_exe: str = None
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
27 orthanc_under_tests_docker_image: str = None
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
28 skip_preparation: bool = False
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
29 break_after_preparation: bool = False
483
45c3fe035fed added tests for delayed_deletion
Alain Mazy <am@osimis.io>
parents: 475
diff changeset
30 break_before_preparation: bool = False
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
31 plugins: typing.List[str] = []
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
32
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
33 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
34 def get_orthanc_url(cls):
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
35 return f"http://{cls.orthanc_under_tests_hostname}:{cls.orthanc_under_tests_http_port}"
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
36
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
37 @classmethod
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
38 def is_docker(cls):
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
39 return cls.orthanc_under_tests_exe is None and cls.orthanc_under_tests_docker_image is not None
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
40
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
41 @classmethod
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
42 def is_exe(cls):
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
43 return cls.orthanc_under_tests_exe is not None and cls.orthanc_under_tests_docker_image is None
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
44
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
45 class OrthancTestCase(unittest.TestCase):
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
46
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
47 o: OrthancApiClient = None # the orthanc under tests api client
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
48 _orthanc_process = None
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
49 _orthanc_container_name = None
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
50 _orthanc_is_running = False
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
51 _orthanc_logger_thread = None
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
52
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
53 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
54 def setUpClass(cls):
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
55
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
56 cls.o = OrthancApiClient(Helpers.get_orthanc_url())
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
57 cls._prepare()
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
58
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
59 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
60 def tearDownClass(cls):
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
61 if not Helpers.break_after_preparation:
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
62 cls.kill_orthanc()
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
63
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
64 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
65 def prepare(cls):
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
66 pass # to override
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
67
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
68 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
69 def _prepare(cls):
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
70 if not Helpers.skip_preparation:
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
71 cls.prepare()
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
72
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
73 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
74 def get_storage_path(cls, storage_name: str):
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
75 return str(here / "storages" / f"{storage_name}")
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
76
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
77 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
78 def generate_configuration(cls, config_name: str, config: object, storage_name: str, plugins = []):
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
79
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
80 # add plugins and default storge directory
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
81 if plugins and len(plugins) > 0:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
82 config["Plugins"] = plugins
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
83
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
84 if Helpers.is_exe() and not "StorageDirectory" in config:
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
85 config["StorageDirectory"] = cls.get_storage_path(storage_name=storage_name)
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
86
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
87 if not "Name" in config:
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
88 config["Name"] = config_name
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
89
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
90 if not "HttpPort" in config:
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
91 config["HttpPort"] = Helpers.orthanc_under_tests_http_port
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
92
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
93 if not "DicomPort" in config:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
94 config["DicomPort"] = Helpers.orthanc_under_tests_dicom_port
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
95
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
96 # copy the values from the base config
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
97 for k, v in default_base_config.items():
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
98 if not k in config:
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
99 config[k] = v
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
100
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
101 # save to disk
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
102 path = str(here / "configurations" / f"{config_name}.json")
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
103 with open(path, "w") as f:
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
104 json.dump(config, f, indent=4)
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
105
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
106 return path
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
107
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
108 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
109 def clear_storage(cls, storage_name: str):
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
110 if Helpers.is_exe():
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
111 storage_path = cls.get_storage_path(storage_name=storage_name)
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
112 shutil.rmtree(storage_path, ignore_errors=True)
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
113 elif Helpers.is_docker():
475
fe37cf5e9c02 force docker volume rm
Alain Mazy <am@osimis.io>
parents: 474
diff changeset
114 subprocess.run(["docker", "volume", "rm", "-f", storage_name])
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
115
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
116 @classmethod
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
117 def launch_orthanc_to_prepare_db(cls, config_name: str = None, config: object = None, config_path: str = None, storage_name: str = None, plugins = []):
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
118 if config_name and storage_name and config:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
119 # generate the configuration file
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
120 config_path = cls.generate_configuration(
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
121 config_name=config_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
122 storage_name=storage_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
123 config=config,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
124 plugins=plugins
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
125 )
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
126 elif not config_path or not storage_name or not config_name:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
127 raise RuntimeError("Invalid configuration")
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
128
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
129 # run orthanc
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
130 if Helpers.orthanc_previous_version_exe:
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
131 cls.launch_orthanc_exe(
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
132 exe_path=Helpers.orthanc_previous_version_exe,
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
133 config_path=config_path
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
134 )
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
135 elif Helpers.orthanc_previous_version_docker_image:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
136 cls.launch_orthanc_docker(
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
137 docker_image=Helpers.orthanc_previous_version_docker_image,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
138 storage_name=storage_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
139 config_name=config_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
140 config_path=config_path
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
141 )
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
142 else:
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
143 raise RuntimeError("Invalid configuration, can not launch Orthanc")
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
144
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
145 @classmethod
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
146 def launch_orthanc_under_tests(cls, config_name: str = None, config: object = None, config_path: str = None, storage_name: str = None, plugins = []):
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
147 if config_name and storage_name and config:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
148 # generate the configuration file
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
149 config_path = cls.generate_configuration(
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
150 config_name=config_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
151 storage_name=storage_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
152 config=config,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
153 plugins=plugins
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
154 )
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
155 elif not config_path or not storage_name or not config_name:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
156 raise RuntimeError("Invalid configuration")
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
157
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
158 # run orthanc
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
159 if Helpers.orthanc_under_tests_exe:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
160 cls.launch_orthanc_exe(
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
161 exe_path=Helpers.orthanc_under_tests_exe,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
162 config_path=config_path
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
163 )
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
164 elif Helpers.orthanc_under_tests_docker_image:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
165 cls.launch_orthanc_docker(
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
166 docker_image=Helpers.orthanc_under_tests_docker_image,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
167 storage_name=storage_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
168 config_name=config_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
169 config_path=config_path
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
170 )
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
171 else:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
172 raise RuntimeError("Invalid configuration, can not launch Orthanc")
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
173
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
174 @classmethod
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
175 def launch_orthanc_exe(cls, exe_path: str, config_path: str):
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
176 cls._orthanc_process = subprocess.Popen(
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
177 [exe_path, "--verbose", config_path],
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
178 stdout=subprocess.PIPE,
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
179 stderr=subprocess.PIPE
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
180 )
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
181
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
182 cls.o.wait_started(10)
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
183 if not cls.o.is_alive():
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
184 output = cls.get_orthanc_process_output()
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
185 print("Orthanc output\n" + output)
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
186
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
187 raise RuntimeError(f"Orthanc failed to start '{exe_path}', conf = '{config_path}'. Check output above")
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
188
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
189 @classmethod
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
190 def launch_orthanc_docker(cls, docker_image: str, storage_name: str, config_path: str, config_name: str):
484
ddfabe1fbee1 new tests for DelayedDeletion plugin
Alain Mazy <am@osimis.io>
parents: 483
diff changeset
191 storage_path = cls.get_storage_path(storage_name=storage_name)
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
192
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
193 cmd = [
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
194 "docker", "run", "--rm",
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
195 "-e", "VERBOSE_ENABLED=true",
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
196 "-e", "VERBOSE_STARTUP=true",
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
197 "-v", f"{config_path}:/etc/orthanc/orthanc.json",
484
ddfabe1fbee1 new tests for DelayedDeletion plugin
Alain Mazy <am@osimis.io>
parents: 483
diff changeset
198 "-v", f"{storage_path}:/var/lib/orthanc/db/",
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
199 "--name", config_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
200 "-p", f"{Helpers.orthanc_under_tests_http_port}:{Helpers.orthanc_under_tests_http_port}",
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
201 "-p", f"{Helpers.orthanc_under_tests_dicom_port}:{Helpers.orthanc_under_tests_dicom_port}",
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
202 docker_image
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
203 ]
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
204 cls._orthanc_container_name = config_name
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
205 print("docker cmd line: " + " ".join(cmd))
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
206
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
207 cls._orthanc_process = subprocess.Popen(
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
208 cmd,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
209 stdout=subprocess.PIPE,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
210 stderr=subprocess.PIPE
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
211 )
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
212
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
213 cls.o.wait_started(10)
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
214 if not cls.o.is_alive():
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
215 output = cls.get_orthanc_process_output()
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
216 print("Orthanc output\n" + output)
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
217
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
218 raise RuntimeError(f"Orthanc failed to start Orthanc through Docker '{docker_image}', conf = '{config_path}'. Check output above")
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
219
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
220
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
221 @classmethod
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
222 def kill_orthanc(cls):
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
223 if Helpers.is_exe():
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
224 cls._orthanc_process.kill()
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
225 else:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
226 subprocess.run(["docker", "stop", cls._orthanc_container_name])
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
227 output = cls.get_orthanc_process_output()
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
228 print("Orthanc output\n" + output)
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
229 cls._orthanc_process = None
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
230
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
231 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
232 def get_orthanc_process_output(cls):
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
233 outputs = cls._orthanc_process.communicate()
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
234 output = ""
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
235 for o in outputs:
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
236 output += o.decode('utf-8')
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
237 return output