blob: b7bda226be61accb05aa3442880a138ccee7bd82 [file] [log] [blame]
Don Garrett580717f2015-07-24 14:11:22 -07001#!/usr/bin/python
2#
Prashanth B0e960282014-05-13 19:38:28 -07003# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import mock
8import unittest
9
10import common
11
12from autotest_lib.database import database_connection
13from autotest_lib.frontend import setup_django_environment
Jakob Juelich7bef8412014-10-14 19:11:54 -070014from autotest_lib.frontend.afe import readonly_connection
Fang Dengf08814a2015-08-03 18:12:18 +000015from autotest_lib.server import utils as server_utils
Prashanth B0e960282014-05-13 19:38:28 -070016from autotest_lib.scheduler import scheduler_lib
17from django.db import utils as django_utils
18
19
20class ConnectionManagerTests(unittest.TestCase):
21 """Connection manager unittests."""
22
23 def setUp(self):
24 self.connection_manager = None
Jakob Juelich7bef8412014-10-14 19:11:54 -070025 readonly_connection.set_globally_disabled = mock.MagicMock()
Prashanth B0e960282014-05-13 19:38:28 -070026 setup_django_environment.enable_autocommit = mock.MagicMock()
Fang Dengf08814a2015-08-03 18:12:18 +000027 server_utils.Singleton._instances = {}
Prashanth B0e960282014-05-13 19:38:28 -070028
29
30 def tearDown(self):
Jakob Juelich7bef8412014-10-14 19:11:54 -070031 readonly_connection.set_globally_disabled.reset_mock()
Prashanth B0e960282014-05-13 19:38:28 -070032 setup_django_environment.enable_autocommit.reset_mock()
33
34
35 def testConnectionDisconnect(self):
36 """Test connection and disconnecting from the database."""
37 # Test that the connection manager only opens a connection once.
38 connection_manager = scheduler_lib.ConnectionManager()
39 connection_manager.open_connection = mock.MagicMock()
40 connection = connection_manager.get_connection()
41 connection_manager.open_connection.assert_called_once_with()
42 connection_manager.open_connection.reset_mock()
43 connection = connection_manager.get_connection()
44 self.assertTrue(
45 connection_manager.open_connection.call_count == 0)
46 connection_manager.open_connection.reset_mock()
47
48 # Test that del on the connection manager closes the connection
49 connection_manager.disconnect = mock.MagicMock()
50 connection_manager.__del__()
51 connection_manager.disconnect.assert_called_once_with()
52
53
54 def testConnectionReconnect(self):
55 """Test that retries don't destroy the connection."""
56 database_connection._DjangoBackend.execute = mock.MagicMock()
57 database_connection._DjangoBackend.execute.side_effect = (
58 django_utils.DatabaseError('Database Error'))
59 connection_manager = scheduler_lib.ConnectionManager()
60 connection = connection_manager.get_connection()
61 self.assertRaises(django_utils.DatabaseError,
62 connection.execute, *('', None, True))
63 self.assertTrue(
64 database_connection._DjangoBackend.execute.call_count == 2)
65 database_connection._DjangoBackend.execute.reset_mock()
66 self.assertTrue(connection_manager.db_connection ==
67 connection_manager.get_connection())
68
69
70 def testConnectionManagerSingleton(self):
71 """Test that the singleton works as expected."""
72 # Confirm that instantiating the class applies global db settings.
73 connection_manager = scheduler_lib.ConnectionManager()
Jakob Juelich7bef8412014-10-14 19:11:54 -070074 readonly_connection.set_globally_disabled.assert_called_once_with(True)
Prashanth B0e960282014-05-13 19:38:28 -070075 setup_django_environment.enable_autocommit.assert_called_once_with()
76
Jakob Juelich7bef8412014-10-14 19:11:54 -070077 readonly_connection.set_globally_disabled.reset_mock()
Prashanth B0e960282014-05-13 19:38:28 -070078 setup_django_environment.enable_autocommit.reset_mock()
79
80 # Confirm that instantiating another connection manager doesn't change
81 # the database settings, and in fact, returns the original manager.
82 connection_manager_2 = scheduler_lib.ConnectionManager()
83 self.assertTrue(connection_manager == connection_manager_2)
84 self.assertTrue(
Jakob Juelich7bef8412014-10-14 19:11:54 -070085 readonly_connection.set_globally_disabled.call_count == 0)
Prashanth B0e960282014-05-13 19:38:28 -070086 self.assertTrue(
87 setup_django_environment.enable_autocommit.call_count == 0)
88
89 # Confirm that we don't open the connection when the class is
90 # instantiated.
91 self.assertTrue(connection_manager.db_connection is None)
Don Garrett580717f2015-07-24 14:11:22 -070092
93
94if __name__ == '__main__':
95 unittest.main()