def test_creating_redis_connection_open_file_raises_IOError(self): """An `IOError` exception must be raised if there was error while opening the password file.""" open.side_effect = IOError('Error opening the file') with patch('rq_exporter.utils.Redis') as Redis: with self.assertRaises(IOError): get_redis_connection(password_file='/path/to/redis_pass') Redis.from_url.assert_not_called() open.assert_called_with('/path/to/redis_pass', 'r') Redis.assert_not_called()
def test_creating_redis_connection_from_url(self): """When the `url` argument is passed the connection must be created with `Redis.from_url`.""" with patch('rq_exporter.utils.Redis') as Redis: connection = get_redis_connection(url='redis://') Redis.from_url.assert_called_with('redis://') open.assert_not_called() Redis.assert_not_called() self.assertEqual(connection, Redis.from_url.return_value)
def test_creating_redis_connection_with_sentinel(self): """When the `sentinel` argument is set the connection must be created from the sentinel.""" with patch('rq_exporter.utils.Sentinel') as Sentinel: connection = get_redis_connection(sentinel='127.0.0.1', sentinel_port='26379', sentinel_master='mymaster') Sentinel().master_for.assert_called_with('mymaster', db='0', socket_timeout=1) open.assert_not_called() self.assertEqual(connection, Sentinel().master_for.return_value)
def test_creating_redis_connection_without_url(self): """When the `url` argument is not set the connection must be created from the other options.""" with patch('rq_exporter.utils.Redis') as Redis: connection = get_redis_connection(host='redis_host', port='6363', db='1') Redis.from_url.assert_not_called() open.assert_not_called() Redis.assert_called_with(host='redis_host', port='6363', db='1', password=None) self.assertEqual(connection, Redis.return_value)
def test_creating_redis_connection_with_password(self): """The `password` argument must be used if `password_file` was not passed.""" with patch('rq_exporter.utils.Redis') as Redis: connection = get_redis_connection(host='redis_host', port='6379', db='0', password='******') Redis.from_url.assert_not_called() open.assert_not_called() Redis.assert_called_with(host='redis_host', port='6379', db='0', password='******') self.assertEqual(connection, Redis.return_value)
def test_creating_redis_connection_with_password_from_file(self): """The password must be set from the `password_file` argument if it was passed.""" with patch('rq_exporter.utils.Redis') as Redis: connection = get_redis_connection( host='redis_host', port='6379', db='0', password='******', password_file='/path/to/redis_pass') Redis.from_url.assert_not_called() open.assert_called_with('/path/to/redis_pass', 'r') Redis.assert_called_with(host='redis_host', port='6379', db='0', password='******') self.assertEqual(connection, Redis.return_value)