You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2018/11/19 13:30:53 UTC

[GitHub] smentek commented on a change in pull request #4090: [AIRFLOW-3250] Fix for Redis Hook for not authorised connection calls

smentek commented on a change in pull request #4090: [AIRFLOW-3250] Fix for Redis Hook for not authorised connection calls
URL: https://github.com/apache/incubator-airflow/pull/4090#discussion_r234617782
 
 

 ##########
 File path: tests/contrib/hooks/test_redis_hook.py
 ##########
 @@ -19,32 +19,116 @@
 
 
 import unittest
-from mock import patch
-
+from mock import patch, MagicMock
 from airflow import configuration
 from airflow.contrib.hooks.redis_hook import RedisHook
 
 
 class TestRedisHook(unittest.TestCase):
+
     def setUp(self):
         configuration.load_test_config()
 
-    def test_get_conn(self):
+    @patch('airflow.contrib.hooks.redis_hook.StrictRedis')
+    @patch('airflow.contrib.hooks.redis_hook.RedisHook.get_connection')
+    def test_get_conn(self, redis_hook_get_connection_mock, StrictRedisMock):
+        HOST = 'localhost'
+        PORT = 6379
+        PASSWORD = 's3cret!'
+        DB = 0
+
+        extra_dejson_mock = MagicMock()
+        extra_dejson_mock.get.return_value = DB
+        connection_parameters = MagicMock()
+        connection_parameters.configure_mock(
+            host=HOST,
+            port=PORT,
+            password=PASSWORD,
+            extra_dejson=extra_dejson_mock)
+        redis_hook_get_connection_mock.return_value = connection_parameters
+
+        hook = RedisHook(redis_conn_id='redis_default')
+        self.assertEqual(hook.redis, None)
+
+        self.assertEqual(hook.host, None, 'host initialised as None.')
+        self.assertEqual(hook.port, None, 'port initialised as None.')
+        self.assertEqual(hook.password, None, 'password initialised as None.')
+        self.assertEqual(hook.db, None, 'db initialised as None.')
+
+        self.assertIs(hook.get_conn(), hook.get_conn(), 'Connection initialized only if None.')
+
+        StrictRedisMock.assert_called_once_with(
+            host=HOST,
+            port=PORT,
+            password=PASSWORD,
+            db=DB)
+
+    @patch('airflow.contrib.hooks.redis_hook.StrictRedis')
+    @patch('airflow.contrib.hooks.redis_hook.RedisHook.get_connection')
+    def test_get_conn_password_stays_none(self, redis_hook_get_connection_mock, StrictRedisMock):
+        HOST = 'localhost'
+        PORT = 6379
+        PASSWORD = 'None'
+        DB = 0
+
+        extra_dejson_mock = MagicMock()
+        extra_dejson_mock.get.return_value = DB
+        connection_parameters = MagicMock()
+        connection_parameters.configure_mock(
+            host=HOST,
+            port=PORT,
+            password=PASSWORD,
+            extra_dejson=extra_dejson_mock)
+        redis_hook_get_connection_mock.return_value = connection_parameters
+
+        hook = RedisHook(redis_conn_id='redis_default')
+        hook.get_conn()
+        self.assertEqual(hook.password, None)
+
+    @patch('airflow.contrib.hooks.redis_hook.RedisHook.get_connection')
+    def test_real_ping(self, redis_hook_get_connection_mock):
+        HOST = 'redis'
+        PORT = 6379
+        PASSWORD = 'None'
+        DB = 0
+
+        extra_dejson_mock = MagicMock()
+        extra_dejson_mock.get.return_value = DB
+        connection_parameters = MagicMock()
+        connection_parameters.configure_mock(
+            host=HOST,
+            port=PORT,
+            password=PASSWORD,
+            extra_dejson=extra_dejson_mock)
+        redis_hook_get_connection_mock.return_value = connection_parameters
+
         hook = RedisHook(redis_conn_id='redis_default')
-        self.assertEqual(hook.client, None)
-        self.assertEqual(
-            repr(hook.get_conn()),
-            (
-                'StrictRedis<ConnectionPool'
-                '<Connection<host=localhost,port=6379,db=0>>>'
-            )
-        )
-
-    @patch("airflow.contrib.hooks.redis_hook.RedisHook.get_conn")
-    def test_first_conn_instantiation(self, get_conn):
+        redis = hook.get_conn()
+
+        self.assertTrue(redis.ping(), 'Connection to Redis with PING works.')
+
+    @patch('airflow.contrib.hooks.redis_hook.RedisHook.get_connection')
 
 Review comment:
   Done.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services