Skip to content

Commit

Permalink
Merge pull request #4141 from StackStorm/better-st2api-logs
Browse files Browse the repository at this point in the history
Log successful connections to mongo
  • Loading branch information
blag authored May 29, 2018
2 parents 23e2d2f + 0622ddf commit e961ed5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
4 changes: 4 additions & 0 deletions st2common/st2common/models/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def _db_connect(db_name, db_host, db_port, username=None, password=None,
port=db_port, tz_aware=True,
username=username, password=password,
**ssl_kwargs)

LOG.info('Successfully connected to database "%s" @ "%s" as user "%s".' % (
db_name, host_string, str(username_string)))

return connection


Expand Down
35 changes: 31 additions & 4 deletions st2common/tests/unit/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ def test_db_setup_connecting_info_logging(self, mock_log):
actual_message = mock_log.info.call_args_list[0][0][0]
self.assertEqual(expected_message, actual_message)

# Check for helpful error messages if the connection is successful
expected_log_message = ('Successfully connected to database "st2" @ "localhost:27017" as '
'user "user_st2".')
actual_log_message = mock_log.info.call_args_list[1][0][0]
self.assertEqual(expected_log_message, actual_log_message)

# 2. Password provided as part of uri string (single host)
db_host = 'mongodb://user_st22:[email protected]:5555'
username = None
Expand All @@ -78,9 +84,14 @@ def test_db_setup_connecting_info_logging(self, mock_log):
password=password)

expected_message = 'Connecting to database "st2" @ "127.0.0.2:5555" as user "user_st22".'
actual_message = mock_log.info.call_args_list[1][0][0]
actual_message = mock_log.info.call_args_list[2][0][0]
self.assertEqual(expected_message, actual_message)

expected_log_message = ('Successfully connected to database "st2" @ "127.0.0.2:5555" as '
'user "user_st22".')
actual_log_message = mock_log.info.call_args_list[3][0][0]
self.assertEqual(expected_log_message, actual_log_message)

# 3. Password provided as part of uri string (single host) - username
# provided as argument has precedence
db_host = 'mongodb://user_st210:[email protected]:5555'
Expand All @@ -90,9 +101,14 @@ def test_db_setup_connecting_info_logging(self, mock_log):
password=password)

expected_message = 'Connecting to database "st2" @ "127.0.0.2:5555" as user "user_st23".'
actual_message = mock_log.info.call_args_list[2][0][0]
actual_message = mock_log.info.call_args_list[4][0][0]
self.assertEqual(expected_message, actual_message)

expected_log_message = ('Successfully connected to database "st2" @ "127.0.0.2:5555" as '
'user "user_st23".')
actual_log_message = mock_log.info.call_args_list[5][0][0]
self.assertEqual(expected_log_message, actual_log_message)

# 4. Just host provided in the url string
db_host = 'mongodb://127.0.0.2:5555'
username = 'user_st24'
Expand All @@ -101,9 +117,14 @@ def test_db_setup_connecting_info_logging(self, mock_log):
password=password)

expected_message = 'Connecting to database "st2" @ "127.0.0.2:5555" as user "user_st24".'
actual_message = mock_log.info.call_args_list[3][0][0]
actual_message = mock_log.info.call_args_list[6][0][0]
self.assertEqual(expected_message, actual_message)

expected_log_message = ('Successfully connected to database "st2" @ "127.0.0.2:5555" as '
'user "user_st24".')
actual_log_message = mock_log.info.call_args_list[7][0][0]
self.assertEqual(expected_log_message, actual_log_message)

# 5. Multiple hosts specified as part of connection uri
db_host = 'mongodb://user6:pass6@host1,host2,host3'
username = None
Expand All @@ -113,9 +134,15 @@ def test_db_setup_connecting_info_logging(self, mock_log):

expected_message = ('Connecting to database "st2" @ "host1:27017,host2:27017,host3:27017 '
'(replica set)" as user "user6".')
actual_message = mock_log.info.call_args_list[4][0][0]
actual_message = mock_log.info.call_args_list[8][0][0]
self.assertEqual(expected_message, actual_message)

expected_log_message = ('Successfully connected to database "st2" @ '
'"host1:27017,host2:27017,host3:27017 '
'(replica set)" as user "user6".')
actual_log_message = mock_log.info.call_args_list[9][0][0]
self.assertEqual(expected_log_message, actual_log_message)


class DbCleanupTest(DbTestCase):

Expand Down

0 comments on commit e961ed5

Please sign in to comment.