Mercurial > hg > orthanc-tests
comparison NewTests/MaxStorage/test_max_storage_pg.py @ 624:543e372d2265
added a PG max storage test
author | Alain Mazy <am@osimis.io> |
---|---|
date | Wed, 14 Feb 2024 11:25:40 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
623:3bb4b2589030 | 624:543e372d2265 |
---|---|
1 import unittest | |
2 import time | |
3 import os | |
4 import threading | |
5 from helpers import OrthancTestCase, Helpers | |
6 | |
7 from orthanc_api_client import OrthancApiClient, ChangeType | |
8 from orthanc_api_client.exceptions import HttpError | |
9 from orthanc_api_client import helpers as OrthancHelpers | |
10 | |
11 from orthanc_tools import OrthancTestDbPopulator | |
12 | |
13 import pathlib | |
14 import subprocess | |
15 import glob | |
16 here = pathlib.Path(__file__).parent.resolve() | |
17 | |
18 | |
19 class TestMaxStoragePG(OrthancTestCase): | |
20 | |
21 @classmethod | |
22 def terminate(cls): | |
23 | |
24 if Helpers.is_docker(): | |
25 subprocess.run(["docker", "rm", "-f", "pg-server"]) | |
26 else: | |
27 cls.pg_service_process.terminate() | |
28 | |
29 | |
30 @classmethod | |
31 def prepare(cls): | |
32 test_name = "MaxStoragePG" | |
33 cls._storage_name = "max-storage-pg" | |
34 network_name = "max-storage-pg" | |
35 | |
36 print(f'-------------- preparing {test_name} tests') | |
37 | |
38 cls.clear_storage(storage_name=cls._storage_name) | |
39 | |
40 pg_hostname = "localhost" | |
41 if Helpers.is_docker(): | |
42 pg_hostname = "pg-server" | |
43 cls.create_docker_network(network_name) | |
44 | |
45 config = { | |
46 "PostgreSQL" : { | |
47 "EnableStorage": False, | |
48 "EnableIndex": True, | |
49 "Host": pg_hostname, | |
50 "Port": 5432, | |
51 "Database": "postgres", | |
52 "Username": "postgres", | |
53 "Password": "postgres", | |
54 "IndexConnectionsCount": 10, | |
55 "MaximumConnectionRetries" : 2000, | |
56 "ConnectionRetryInterval" : 5, | |
57 "TransactionMode": "ReadCommitted", | |
58 #"TransactionMode": "Serializable", | |
59 "EnableVerboseLogs": True | |
60 }, | |
61 "AuthenticationEnabled": False, | |
62 "OverwriteInstances": True, | |
63 "MaximumStorageSize": 1, | |
64 "MaximumStorageMode": "Recycle" | |
65 # "MaximumPatientCount": 1, | |
66 # "MaximumStorageMode": "Reject" | |
67 } | |
68 | |
69 config_path = cls.generate_configuration( | |
70 config_name=f"{test_name}", | |
71 storage_name=cls._storage_name, | |
72 config=config, | |
73 plugins=Helpers.plugins | |
74 ) | |
75 | |
76 # launch the docker PG server | |
77 print('--------------- launching PostgreSQL server ------------------') | |
78 | |
79 pg_cmd = [ | |
80 "docker", "run", "--rm", | |
81 "-p", "5432:5432", | |
82 "--name", "pg-server", | |
83 "--env", "POSTGRES_HOST_AUTH_METHOD=trust" | |
84 ] | |
85 | |
86 if Helpers.is_docker(): | |
87 pg_cmd.extend(["--network", network_name]) | |
88 pg_cmd.append("postgres:15") | |
89 | |
90 cls.pg_service_process = subprocess.Popen(pg_cmd) | |
91 time.sleep(5) | |
92 | |
93 if Helpers.break_after_preparation: | |
94 print(f"++++ It is now time to start your Orthanc under tests with configuration file '{config_path}' +++++") | |
95 input("Press Enter to continue") | |
96 else: | |
97 cls.launch_orthanc_under_tests( | |
98 config_name=f"{test_name}", | |
99 storage_name=cls._storage_name, | |
100 config=config, | |
101 plugins=Helpers.plugins, | |
102 docker_network=network_name | |
103 ) | |
104 | |
105 cls.o = OrthancApiClient(cls.o._root_url) | |
106 cls.o.wait_started() | |
107 cls.o.delete_all_content() | |
108 | |
109 | |
110 def test_upload(self): | |
111 self.o.delete_all_content() | |
112 self.clear_storage(storage_name=self._storage_name) | |
113 | |
114 uploaded_instances_ids = [] | |
115 counter = 0 | |
116 # upload 10 images of 500x500, since the MaximumStorageSize is 1MB, only 2 of them should remain in the storage | |
117 for i in range(0, 10): | |
118 counter += 1 | |
119 dicom_file = OrthancHelpers.generate_test_dicom_file(width=500, height=500, | |
120 tags = { | |
121 "PatientID" : f"{i}", | |
122 "StudyInstanceUID" : f"{i}", | |
123 "SeriesInstanceUID" : f"{i}.{counter%10}" | |
124 }) | |
125 try: | |
126 uploaded_instances_ids.extend(self.o.upload(dicom_file)) | |
127 except HttpError as er: | |
128 if er.http_status_code == 507: | |
129 pass # ignore | |
130 | |
131 # some instances have been discarded | |
132 self.assertLess(len(self.o.instances.get_all_ids()), 10) | |
133 self.assertLess(len(self.o.patients.get_all_ids()), 10) | |
134 self.assertLess(self.o.get_statistics().total_disk_size, 1*1024*1024) |