annotate NewTests/helpers.py @ 577:0649a19df194

new tests for auth-service
author Alain Mazy <am@osimis.io>
date Fri, 08 Sep 2023 12:03:50 +0200
parents 80ba6f1d521c
children 6753d96dd71f
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
490
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
5 import os
473
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
490
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
20
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
21 class Helpers:
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
22
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
23 orthanc_under_tests_hostname: str = 'localhost'
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
24 orthanc_under_tests_http_port: int = 8052
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
25 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
26 orthanc_under_tests_exe: str = None
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
27 orthanc_previous_version_exe: str = None
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
28 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
29 skip_preparation: bool = False
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
30 break_after_preparation: bool = False
483
45c3fe035fed added tests for delayed_deletion
Alain Mazy <am@osimis.io>
parents: 475
diff changeset
31 break_before_preparation: bool = False
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
32 plugins: typing.List[str] = []
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
33
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
34 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
35 def get_orthanc_url(cls):
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
36 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
37
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
38 @classmethod
490
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
39 def get_orthanc_ip(cls):
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
40 return cls.orthanc_under_tests_hostname
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
41
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
42 @classmethod
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
43 def get_orthanc_dicom_port(cls):
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
44 return cls.orthanc_under_tests_dicom_port
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
45
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
46 @classmethod
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
47 def is_docker(cls):
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
48 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
49
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
50 @classmethod
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
51 def is_exe(cls):
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
52 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
53
490
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
54 @classmethod
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
55 def find_executable(cls, name):
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
56 p = os.path.join('/usr/local/bin', name)
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
57 if os.path.isfile(p):
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
58 return p
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
59
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
60 p = os.path.join('/usr/local/sbin', name)
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
61 if os.path.isfile(p):
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
62 return p
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
63
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
64 return name
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
65
10a47656e34f new tests for MaximumStorageMode
Alain Mazy <am@osimis.io>
parents: 484
diff changeset
66
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
67 class OrthancTestCase(unittest.TestCase):
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
68
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
69 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
70 _orthanc_process = None
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
71 _orthanc_container_name = None
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
72 _orthanc_is_running = False
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
73 _orthanc_logger_thread = None
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
74
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
75 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
76 def setUpClass(cls):
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
77
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
78 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
79 cls._prepare()
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
80
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
81 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
82 def tearDownClass(cls):
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
83 if not Helpers.break_after_preparation:
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
84 cls.kill_orthanc()
577
0649a19df194 new tests for auth-service
Alain Mazy <am@osimis.io>
parents: 576
diff changeset
85 cls.terminate()
473
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 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
88 def prepare(cls):
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
89 pass # to override
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
90
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
91 @classmethod
576
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents: 490
diff changeset
92 def terminate(cls):
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents: 490
diff changeset
93 pass # to override
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents: 490
diff changeset
94
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents: 490
diff changeset
95 @classmethod
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
96 def _prepare(cls):
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
97 if not Helpers.skip_preparation:
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
98 cls.prepare()
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
99
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
100 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
101 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
102 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
103
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
104 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
105 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
106
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
107 # add plugins and default storge directory
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
108 if plugins and len(plugins) > 0:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
109 config["Plugins"] = plugins
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
110
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
111 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
112 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
113
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
114 if not "Name" in config:
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
115 config["Name"] = config_name
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
116
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
117 if not "HttpPort" in config:
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
118 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
119
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
120 if not "DicomPort" in config:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
121 config["DicomPort"] = Helpers.orthanc_under_tests_dicom_port
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
122
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
123 # 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
124 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
125 if not k in config:
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
126 config[k] = v
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
127
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
128 # save to disk
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
129 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
130 with open(path, "w") as f:
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
131 json.dump(config, f, indent=4)
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
132
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
133 return path
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
134
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
135 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
136 def clear_storage(cls, storage_name: str):
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
137 if Helpers.is_exe():
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
138 storage_path = cls.get_storage_path(storage_name=storage_name)
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
139 shutil.rmtree(storage_path, ignore_errors=True)
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
140 elif Helpers.is_docker():
475
fe37cf5e9c02 force docker volume rm
Alain Mazy <am@osimis.io>
parents: 474
diff changeset
141 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
142
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
143 @classmethod
577
0649a19df194 new tests for auth-service
Alain Mazy <am@osimis.io>
parents: 576
diff changeset
144 def create_docker_network(cls, network: str):
0649a19df194 new tests for auth-service
Alain Mazy <am@osimis.io>
parents: 576
diff changeset
145 if Helpers.is_docker():
0649a19df194 new tests for auth-service
Alain Mazy <am@osimis.io>
parents: 576
diff changeset
146 subprocess.run(["docker", "network", "rm", network]) # ignore error
0649a19df194 new tests for auth-service
Alain Mazy <am@osimis.io>
parents: 576
diff changeset
147 subprocess.run(["docker", "network", "create", network])
0649a19df194 new tests for auth-service
Alain Mazy <am@osimis.io>
parents: 576
diff changeset
148
0649a19df194 new tests for auth-service
Alain Mazy <am@osimis.io>
parents: 576
diff changeset
149 @classmethod
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
150 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
151 if config_name and storage_name and config:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
152 # generate the configuration file
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
153 config_path = cls.generate_configuration(
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
154 config_name=config_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
155 storage_name=storage_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
156 config=config,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
157 plugins=plugins
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
158 )
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
159 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
160 raise RuntimeError("Invalid configuration")
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
161
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
162 # run orthanc
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
163 if Helpers.orthanc_previous_version_exe:
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
164 cls.launch_orthanc_exe(
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
165 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
166 config_path=config_path
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
167 )
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
168 elif Helpers.orthanc_previous_version_docker_image:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
169 cls.launch_orthanc_docker(
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
170 docker_image=Helpers.orthanc_previous_version_docker_image,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
171 storage_name=storage_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
172 config_name=config_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
173 config_path=config_path
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
174 )
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
175 else:
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
176 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
177
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
178 @classmethod
577
0649a19df194 new tests for auth-service
Alain Mazy <am@osimis.io>
parents: 576
diff changeset
179 def launch_orthanc_under_tests(cls, config_name: str = None, config: object = None, config_path: str = None, storage_name: str = None, plugins = [], docker_network: str = None):
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
180 if config_name and storage_name and config:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
181 # generate the configuration file
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
182 config_path = cls.generate_configuration(
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
183 config_name=config_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
184 storage_name=storage_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
185 config=config,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
186 plugins=plugins
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
187 )
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
188 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
189 raise RuntimeError("Invalid configuration")
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
190
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
191 # run orthanc
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
192 if Helpers.orthanc_under_tests_exe:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
193 cls.launch_orthanc_exe(
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
194 exe_path=Helpers.orthanc_under_tests_exe,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
195 config_path=config_path
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
196 )
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
197 elif Helpers.orthanc_under_tests_docker_image:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
198 cls.launch_orthanc_docker(
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
199 docker_image=Helpers.orthanc_under_tests_docker_image,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
200 storage_name=storage_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
201 config_name=config_name,
577
0649a19df194 new tests for auth-service
Alain Mazy <am@osimis.io>
parents: 576
diff changeset
202 config_path=config_path,
0649a19df194 new tests for auth-service
Alain Mazy <am@osimis.io>
parents: 576
diff changeset
203 network=docker_network
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
204 )
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
205 else:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
206 raise RuntimeError("Invalid configuration, can not launch Orthanc")
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
207
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
208 @classmethod
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
209 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
210 cls._orthanc_process = subprocess.Popen(
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
211 [exe_path, "--verbose", config_path],
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
212 stdout=subprocess.PIPE,
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
213 stderr=subprocess.PIPE
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
214 )
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
215
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
216 cls.o.wait_started(10)
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
217 if not cls.o.is_alive():
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
218 output = cls.get_orthanc_process_output()
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
219 print("Orthanc output\n" + output)
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
220
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
221 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
222
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
223 @classmethod
577
0649a19df194 new tests for auth-service
Alain Mazy <am@osimis.io>
parents: 576
diff changeset
224 def launch_orthanc_docker(cls, docker_image: str, storage_name: str, config_path: str, config_name: str, network: str = None):
484
ddfabe1fbee1 new tests for DelayedDeletion plugin
Alain Mazy <am@osimis.io>
parents: 483
diff changeset
225 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
226
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
227 cmd = [
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
228 "docker", "run", "--rm",
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
229 "-e", "VERBOSE_ENABLED=true",
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
230 "-e", "VERBOSE_STARTUP=true",
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
231 "-v", f"{config_path}:/etc/orthanc/orthanc.json",
484
ddfabe1fbee1 new tests for DelayedDeletion plugin
Alain Mazy <am@osimis.io>
parents: 483
diff changeset
232 "-v", f"{storage_path}:/var/lib/orthanc/db/",
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
233 "--name", config_name,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
234 "-p", f"{Helpers.orthanc_under_tests_http_port}:{Helpers.orthanc_under_tests_http_port}",
577
0649a19df194 new tests for auth-service
Alain Mazy <am@osimis.io>
parents: 576
diff changeset
235 "-p", f"{Helpers.orthanc_under_tests_dicom_port}:{Helpers.orthanc_under_tests_dicom_port}"
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
236 ]
577
0649a19df194 new tests for auth-service
Alain Mazy <am@osimis.io>
parents: 576
diff changeset
237 if network:
0649a19df194 new tests for auth-service
Alain Mazy <am@osimis.io>
parents: 576
diff changeset
238 cmd.extend(["--network", network])
0649a19df194 new tests for auth-service
Alain Mazy <am@osimis.io>
parents: 576
diff changeset
239 cmd.append(docker_image)
0649a19df194 new tests for auth-service
Alain Mazy <am@osimis.io>
parents: 576
diff changeset
240
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
241 cls._orthanc_container_name = config_name
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
242 print("docker cmd line: " + " ".join(cmd))
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
243
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
244 cls._orthanc_process = subprocess.Popen(
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
245 cmd,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
246 stdout=subprocess.PIPE,
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
247 stderr=subprocess.PIPE
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
248 )
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
249
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
250 cls.o.wait_started(10)
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
251 if not cls.o.is_alive():
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
252 output = cls.get_orthanc_process_output()
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
253 print("Orthanc output\n" + output)
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
254
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
255 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
256
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
257
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
258 @classmethod
473
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
259 def kill_orthanc(cls):
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
260 if Helpers.is_exe():
576
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents: 490
diff changeset
261 if cls._orthanc_process:
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents: 490
diff changeset
262 cls._orthanc_process.kill()
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents: 490
diff changeset
263 else:
80ba6f1d521c new tests for authorization plugin (native only)
Alain Mazy <am@osimis.io>
parents: 490
diff changeset
264 return
474
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
265 else:
6917a26881ed NewTests working with Docker
Alain Mazy <am@osimis.io>
parents: 473
diff changeset
266 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
267 output = cls.get_orthanc_process_output()
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
268 print("Orthanc output\n" + output)
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
269 cls._orthanc_process = None
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
270
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
271 @classmethod
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
272 def get_orthanc_process_output(cls):
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
273 outputs = cls._orthanc_process.communicate()
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
274 output = ""
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
275 for o in outputs:
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
276 output += o.decode('utf-8')
4ee85b016a40 added NewTests framework - only the Housekeeper tests right now
Alain Mazy <am@osimis.io>
parents:
diff changeset
277 return output