alexellis commented on a change in pull request #4267: [AIRFLOW-3411]  create 
openfaas hook
URL: https://github.com/apache/airflow/pull/4267#discussion_r322746524
 
 

 ##########
 File path: tests/contrib/hooks/test_openfaas_hook.py
 ##########
 @@ -0,0 +1,132 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+import unittest
+import requests_mock
+from airflow.models import Connection
+from airflow.contrib.hooks.openfaas_hook import OpenFaasHook
+from airflow.hooks.base_hook import BaseHook
+from airflow import configuration, AirflowException
+
+try:
+    from unittest import mock
+except ImportError:
+    try:
+        import mock
+    except ImportError:
+        mock = None
+
+FUNCTION_NAME = "function_name"
+
+
+class TestOpenFaasHook(unittest.TestCase):
+    GET_FUNCTION = "/system/function/"
+    INVOKE_ASYNC_FUNCTION = "/async-function/"
+    DEPLOY_FUNCTION = "/system/functions"
+    UPDATE_FUNCTION = "/system/functions"
+
+    def setUp(self):
+        configuration.load_test_config()
+        self.hook = OpenFaasHook(function_name=FUNCTION_NAME)
+        self.mock_response = {'ans': 'a'}
+
+    @mock.patch.object(BaseHook, 'get_connection')
+    @requests_mock.mock()
+    def test_is_function_exist_false(self, mock_get_connection, m):
+        m.get("http://open-faas.io"; + self.GET_FUNCTION + FUNCTION_NAME,
+              json=self.mock_response, status_code=404)
+        mock_connection = Connection(host="http://open-faas.io";)
+
+        mock_get_connection.return_value = mock_connection
+        does_function_exist = self.hook.does_function_exist()
+        self.assertFalse(does_function_exist)
+
+    @mock.patch.object(BaseHook, 'get_connection')
+    @requests_mock.mock()
+    def test_is_function_exist_true(self, mock_get_connection, m):
+        m.get("http://open-faas.io"; + self.GET_FUNCTION + FUNCTION_NAME,
+              json=self.mock_response, status_code=202)
+        mock_connection = Connection(host="http://open-faas.io";)
+
+        mock_get_connection.return_value = mock_connection
+        does_function_exist = self.hook.does_function_exist()
+        self.assertTrue(does_function_exist)
+
+    @mock.patch.object(BaseHook, 'get_connection')
+    @requests_mock.mock()
+    def test_update_function_true(self, mock_get_connection, m):
+        m.put("http://open-faas.io"; + self.UPDATE_FUNCTION, 
json=self.mock_response, status_code=202)
+        mock_connection = Connection(host="http://open-faas.io";)
+
+        mock_get_connection.return_value = mock_connection
+        update_function_ans = self.hook.update_function({})
+        self.assertEqual(update_function_ans, None)
+
+    @mock.patch.object(BaseHook, 'get_connection')
+    @requests_mock.mock()
+    def test_update_function_false(self, mock_get_connection, m):
+        m.put("http://open-faas.io"; + self.UPDATE_FUNCTION, 
json=self.mock_response, status_code=400)
+        mock_connection = Connection(host="http://open-faas.io";)
+        mock_get_connection.return_value = mock_connection
+
+        with self.assertRaises(AirflowException) as context:
+            self.hook.update_function({})
+        self.assertIn('failed to update ' + FUNCTION_NAME, 
str(context.exception))
+
+    @mock.patch.object(BaseHook, 'get_connection')
+    @requests_mock.mock()
+    def test_invoke_async_function_false(self, mock_get_connection, m):
+        m.post("http://open-faas.io"; + self.INVOKE_ASYNC_FUNCTION + 
FUNCTION_NAME, json=self.mock_response,
+               status_code=400)
+        mock_connection = Connection(host="http://open-faas.io";)
+        mock_get_connection.return_value = mock_connection
+
+        with self.assertRaises(AirflowException) as context:
+            self.hook.invoke_async_function({})
+        self.assertIn('failed to invoke function', str(context.exception))
+
+    @mock.patch.object(BaseHook, 'get_connection')
+    @requests_mock.mock()
+    def test_invoke_async_function_true(self, mock_get_connection, m):
+        m.post("http://open-faas.io"; + self.INVOKE_ASYNC_FUNCTION + 
FUNCTION_NAME, json=self.mock_response,
+               status_code=202)
+        mock_connection = Connection(host="http://open-faas.io";)
 
 Review comment:
   Might be better to use a fictitious URL rather than one that looks similar 
to the official website?

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


With regards,
Apache Git Services

Reply via email to