annotate NewTests/ReadOnly/test_readonly_pg.py @ 706:5137a2c89991 find-refactoring

fix
author Alain Mazy <am@orthanc.team>
date Fri, 04 Oct 2024 09:00:15 +0200
parents bdb9763d53ff
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
701
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
1 import unittest
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
2 import time
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
3 import os
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
4 import threading
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
5 from helpers import OrthancTestCase, Helpers
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
6
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
7 from orthanc_api_client import OrthancApiClient, ChangeType
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
8 from orthanc_api_client.exceptions import HttpError
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
9 from orthanc_api_client import helpers as OrthancHelpers
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
10
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
11 from orthanc_tools import OrthancTestDbPopulator
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
12
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
13 import pathlib
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
14 import subprocess
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
15 import glob
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
16 here = pathlib.Path(__file__).parent.resolve()
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
17
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
18
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
19 class TestReadOnlyPG(OrthancTestCase):
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
20
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
21 @classmethod
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
22 def terminate(cls):
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
23
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
24 if Helpers.is_docker():
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
25 subprocess.run(["docker", "rm", "-f", "pg-server"])
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
26 else:
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
27 cls.pg_service_process.terminate()
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
28
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
29
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
30 @classmethod
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
31 def prepare(cls):
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
32 test_name = "ReadOnlyPG"
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
33 cls._storage_name = "read-only-pg" #actually not used since we are using PG storage
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
34 network_name = "read-only-pg"
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
35
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
36 print(f'-------------- preparing {test_name} tests')
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
37
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
38 pg_hostname = "localhost"
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
39 if Helpers.is_docker():
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
40 pg_hostname = "pg-server"
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
41 cls.create_docker_network(network_name)
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
42
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
43 config = {
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
44 "PostgreSQL" : {
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
45 "EnableStorage": True,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
46 "EnableIndex": True,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
47 "Host": pg_hostname,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
48 "Port": 5432,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
49 "Database": "postgres",
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
50 "Username": "postgres",
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
51 "Password": "postgres",
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
52 "IndexConnectionsCount": 10,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
53 "MaximumConnectionRetries" : 20,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
54 "ConnectionRetryInterval" : 1,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
55 "TransactionMode": "ReadCommitted",
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
56 "EnableVerboseLogs": True
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
57 },
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
58 "AuthenticationEnabled": False,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
59 "OverwriteInstances": True,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
60 "ReadOnly": False, # disable for preparation
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
61 "DicomWeb": {
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
62 "EnableMetadataCache": False # disable for preparation
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
63 }
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
64 }
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
65
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
66 # launch the docker PG server
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
67 print('--------------- launching PostgreSQL server ------------------')
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
68
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
69 pg_cmd = [
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
70 "docker", "run", "--rm",
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
71 "-p", "5432:5432",
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
72 "--name", "pg-server",
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
73 "--env", "POSTGRES_HOST_AUTH_METHOD=trust"
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
74 ]
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
75
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
76 if Helpers.is_docker():
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
77 pg_cmd.extend(["--network", network_name])
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
78 pg_cmd.append("postgres:15")
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
79
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
80 cls.pg_service_process = subprocess.Popen(pg_cmd)
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
81 time.sleep(5)
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
82
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
83 print('--------------- launching Orthanc to prepare DB ------------------')
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
84 cls.launch_orthanc_to_prepare_db(
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
85 config_name=f"{test_name}",
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
86 storage_name=cls._storage_name,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
87 config=config,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
88 plugins=Helpers.plugins,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
89 docker_network=network_name
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
90 )
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
91
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
92 # upload a study
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
93 cls.uploaded_instances_ids = cls.o.upload_folder(here / "../../Database/Knix/Loc")
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
94 cls.one_instance_id = cls.uploaded_instances_ids[0]
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
95 cls.one_series_id = cls.o.instances.get_parent_series_id(cls.one_instance_id)
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
96 cls.one_study_id = cls.o.series.get_parent_study_id(cls.one_series_id)
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
97 cls.one_patient_id = cls.o.studies.get_parent_patient_id(cls.one_study_id)
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
98
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
99 cls.kill_orthanc()
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
100
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
101 print('--------------- stopped preparation Orthanc ------------------')
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
102
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
103 time.sleep(3)
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
104
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
105 # modify config for the readonly version
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
106 config["ReadOnly"] = True
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
107 config["DicomWeb"]["EnableMetadataCache"] = True
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
108
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
109 config_path = cls.generate_configuration(
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
110 config_name=f"{test_name}",
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
111 storage_name=cls._storage_name,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
112 config=config,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
113 plugins=Helpers.plugins
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
114 )
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
115
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
116 if Helpers.break_after_preparation:
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
117 print(f"++++ It is now time to start your Orthanc under tests with configuration file '{config_path}' +++++")
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
118 input("Press Enter to continue")
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
119 else:
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
120 cls.launch_orthanc_under_tests(
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
121 config_name=f"{test_name}",
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
122 storage_name=cls._storage_name,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
123 config=config,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
124 plugins=Helpers.plugins,
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
125 docker_network=network_name
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
126 )
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
127
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
128 cls.o = OrthancApiClient(cls.o._root_url)
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
129 cls.o.wait_started()
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
130
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
131
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
132 def test_write_methods_fail(self):
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
133 self.assertRaises(Exception, lambda: self.o.upload_folder(here / "../../Database/Knix/Loc"))
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
134 self.assertRaises(Exception, lambda: self.o.instances.delete(self.one_instance_id))
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
135 self.assertRaises(Exception, lambda: self.o.series.delete(self.one_series_id))
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
136 self.assertRaises(Exception, lambda: self.o.studies.delete(self.one_study_id))
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
137 self.assertRaises(Exception, lambda: self.o.patients.delete(self.one_patient_id))
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
138
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
139 tags = self.o.instances.get_tags(self.one_instance_id)
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
140
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
141
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
142
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
143 def test_read_methods_succeed(self):
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
144 # nothing should raise
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
145 tags = self.o.instances.get_tags(self.one_instance_id)
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
146
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
147 self.o.get_json(f"/dicom-web/studies/{tags['StudyInstanceUID']}/metadata")
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
148 self.o.get_json(f"/dicom-web/studies/{tags['StudyInstanceUID']}/series/{tags['SeriesInstanceUID']}/metadata")
bdb9763d53ff readonly tests
Alain Mazy <am@orthanc.team>
parents:
diff changeset
149 self.o.get_json(f"/statistics")