1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
# -*- encoding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import logging
import operator
import re
import secrets
from io import BytesIO
from unittest.mock import patch
import requests
import odoo
from odoo.modules.registry import Registry
from odoo.tests.common import BaseCase, HttpCase, tagged
from odoo.tools import config
class TestDatabaseManager(HttpCase):
def test_database_manager(self):
if not config['list_db']:
return
res = self.url_open('/web/database/manager')
self.assertEqual(res.status_code, 200)
# check that basic existing db actions are present
self.assertIn('.o_database_backup', res.text)
self.assertIn('.o_database_duplicate', res.text)
self.assertIn('.o_database_delete', res.text)
# check that basic db actions are present
self.assertIn('.o_database_create', res.text)
self.assertIn('.o_database_restore', res.text)
@tagged('-at_install', 'post_install', '-standard', 'database_operations')
class TestDatabaseOperations(BaseCase):
def setUp(self):
self.password = secrets.token_hex()
# monkey-patch password verification
self.verify_admin_password_patcher = patch(
'odoo.tools.config.verify_admin_password', self.password.__eq__,
)
self.startPatcher(self.verify_admin_password_patcher)
self.db_name = config['db_name']
self.assertTrue(self.db_name)
# monkey-patch db-filter
self.addCleanup(operator.setitem, config, 'dbfilter', config['dbfilter'])
config['dbfilter'] = self.db_name + '.*'
self.base_databases = self.list_dbs_filtered()
self.session = requests.Session()
self.session.get(self.url('/web/database/manager'))
def tearDown(self):
self.assertEqual(
self.list_dbs_filtered(),
self.base_databases,
'No database should have been created or removed at the end of this test',
)
def list_dbs_filtered(self):
return set(db for db in odoo.service.db.list_dbs(True) if re.match(config['dbfilter'], db))
def url(self, path):
return HttpCase.base_url() + path
def assertDbs(self, dbs):
self.assertEqual(self.list_dbs_filtered() - self.base_databases, set(dbs))
def url_open_drop(self, dbname):
res = self.session.post(self.url('/web/database/drop'), data={
'master_pwd': self.password,
'name': dbname,
}, allow_redirects=False)
res.raise_for_status()
return res
def test_database_creation(self):
# check verify_admin_password patch
self.assertTrue(odoo.tools.config.verify_admin_password(self.password))
# create a database
test_db_name = self.db_name + '-test-database-creation'
self.assertNotIn(test_db_name, self.list_dbs_filtered())
res = self.session.post(self.url('/web/database/create'), data={
'master_pwd': self.password,
'name': test_db_name,
'login': 'admin',
'password': 'admin',
'lang': 'en_US',
'phone': '',
}, allow_redirects=False)
self.assertEqual(res.status_code, 303)
self.assertIn('/odoo', res.headers['Location'])
self.assertDbs([test_db_name])
# delete the created database
res = self.url_open_drop(test_db_name)
self.assertEqual(res.status_code, 303)
self.assertIn('/web/database/manager', res.headers['Location'])
self.assertDbs([])
def test_database_duplicate(self):
# duplicate this database
test_db_name = self.db_name + '-test-database-duplicate'
self.assertNotIn(test_db_name, self.list_dbs_filtered())
res = self.session.post(self.url('/web/database/duplicate'), data={
'master_pwd': self.password,
'name': self.db_name,
'new_name': test_db_name,
}, allow_redirects=False)
self.assertEqual(res.status_code, 303)
self.assertIn('/web/database/manager', res.headers['Location'])
self.assertDbs([test_db_name])
# delete the created database
res = self.url_open_drop(test_db_name)
self.assertIn('/web/database/manager', res.headers['Location'])
self.assertDbs([])
def test_database_restore(self):
test_db_name = self.db_name + '-test-database-restore'
self.assertNotIn(test_db_name, self.list_dbs_filtered())
# backup the current database inside a temporary zip file
res = self.session.post(
self.url('/web/database/backup'),
data={
'master_pwd': self.password,
'name': self.db_name,
},
allow_redirects=False,
stream=True,
)
res.raise_for_status()
datetime_pattern = r'\d\d\d\d-\d\d-\d\d_\d\d-\d\d-\d\d'
self.assertRegex(
res.headers.get('Content-Disposition'),
fr"attachment; filename\*=UTF-8''{self.db_name}_{datetime_pattern}\.zip"
)
backup_file = BytesIO()
backup_file.write(res.content)
self.assertGreater(backup_file.tell(), 0, "The backup seems corrupted")
# upload the backup under a new name (create a duplicate)
with self.subTest(DEFAULT_MAX_CONTENT_LENGTH=None), \
patch.object(odoo.http, 'DEFAULT_MAX_CONTENT_LENGTH', None):
backup_file.seek(0)
self.session.post(
self.url('/web/database/restore'),
data={
'master_pwd': self.password,
'name': test_db_name,
'copy': True,
},
files={
'backup_file': backup_file,
},
allow_redirects=False
).raise_for_status()
self.assertDbs([test_db_name])
self.url_open_drop(test_db_name)
# upload the backup again, this time simulating that the file is
# too large under the default size limit, the default size limit
# shouldn't apply to /web/database URLs
with self.subTest(DEFAULT_MAX_CONTENT_LENGTH=1024), \
patch.object(odoo.http, 'DEFAULT_MAX_CONTENT_LENGTH', 1024):
backup_file.seek(0)
self.session.post(
self.url('/web/database/restore'),
data={
'master_pwd': self.password,
'name': test_db_name,
'copy': True,
},
files={
'backup_file': backup_file,
},
allow_redirects=False
).raise_for_status()
self.assertDbs([test_db_name])
self.url_open_drop(test_db_name)
def test_database_http_registries(self):
# This test is about dropping a connection inside one worker and
# make sure that the other workers behave correctly.
#
# Setup
#
# duplicate this database
test_db_name = self.db_name + '-test-database-duplicate'
res = self.session.post(self.url('/web/database/duplicate'), data={
'master_pwd': self.password,
'name': self.db_name,
'new_name': test_db_name,
}, allow_redirects=False)
# get a registry and a cursor on that new database
registry = Registry(test_db_name)
cr = registry.cursor()
self.assertIn(test_db_name, Registry.registries)
# delete the created database but keep the cursor
with patch('odoo.sql_db.close_db') as close_db:
res = self.url_open_drop(test_db_name)
close_db.assert_called_once_with(test_db_name)
# simulate that some customers were connected to that dropped db
session_store = odoo.http.root.session_store
session = session_store.new()
session.update(odoo.http.get_default_session(), db=test_db_name)
session.context['lang'] = odoo.http.DEFAULT_LANG
self.session.cookies['session_id'] = session.sid
# make it possible to inject the registry back
patcher = patch.dict(Registry.registries.d, {test_db_name: registry})
registries = patcher.start()
self.addCleanup(patcher.stop)
#
# Tests
#
# The other worker doesn't have a registry in its LRU cache for
# that session database.
with self.subTest(msg="Registry.init() fails"):
session_store.save(session)
registries.pop('test_db_name', None)
with self.assertLogs('odoo.sql_db', logging.INFO) as capture:
res = self.session.get(self.url('/web/health'))
self.assertEqual(res.status_code, 200)
self.assertEqual(session_store.get(session.sid)['db'], None)
self.assertEqual(capture.output, [
"INFO:odoo.sql_db:Connection to the database failed",
])
# The other worker has a registry in its LRU cache for that
# session database. But it doesn't have a connection to the sql
# database.
with self.subTest(msg="Registry.cursor() fails"):
session_store.save(session)
registries[test_db_name] = registry
with self.assertLogs('odoo.sql_db', logging.INFO) as capture, \
patch.object(Registry, '__new__', return_value=registry):
res = self.session.get(self.url('/web/health'))
self.assertEqual(res.status_code, 200)
self.assertEqual(session_store.get(session.sid)['db'], None)
self.assertEqual(capture.output, [
"INFO:odoo.sql_db:Connection to the database failed",
])
# The other worker has a registry in its LRU cache for that
# session database. It also has a (now broken) connection to the
# sql database.
with self.subTest(msg="Registry.check_signaling() fails"):
session_store.save(session)
registries[test_db_name] = registry
with self.assertLogs('odoo.sql_db', logging.ERROR) as capture, \
patch.object(Registry, '__new__', return_value=registry), \
patch.object(Registry, 'cursor', return_value=cr):
res = self.session.get(self.url('/web/health'))
self.assertEqual(res.status_code, 200)
self.assertEqual(session_store.get(session.sid)['db'], None)
self.maxDiff = None
self.assertRegex(capture.output[0], (
r"^ERROR:odoo\.sql_db:bad query:(?s:.*?)"
r"ERROR: terminating connection due to administrator command\s+"
r"server closed the connection unexpectedly\s+"
r"This probably means the server terminated abnormally\s+"
r"before or while processing the request\.$"
))
|