22
22
from __future__ import print_function
23
23
from __future__ import unicode_literals
24
24
25
- import unittest
26
- import six
27
- from tests .compat import mock
25
+ import logging
28
26
29
- import pkg_resources
27
+ import pytest
30
28
31
29
from airflow .www_rbac import app as application
30
+ from tests .compat import mock
32
31
33
32
34
- class PluginsTestRBAC ( unittest . TestCase ):
35
- def setUp (self ):
33
+ class TestPluginsRBAC ( object ):
34
+ def setup_method (self , method ):
36
35
self .app , self .appbuilder = application .create_app (testing = True )
37
36
38
37
def test_flaskappbuilder_views (self ):
@@ -41,18 +40,18 @@ def test_flaskappbuilder_views(self):
41
40
plugin_views = [view for view in self .appbuilder .baseviews
42
41
if view .blueprint .name == appbuilder_class_name ]
43
42
44
- self . assertTrue ( len (plugin_views ) == 1 )
43
+ assert len (plugin_views ) == 1
45
44
46
45
# view should have a menu item matching category of v_appbuilder_package
47
46
links = [menu_item for menu_item in self .appbuilder .menu .menu
48
47
if menu_item .name == v_appbuilder_package ['category' ]]
49
48
50
- self . assertTrue ( len (links ) == 1 )
49
+ assert len (links ) == 1
51
50
52
51
# menu link should also have a link matching the name of the package.
53
52
link = links [0 ]
54
- self . assertEqual ( link .name , v_appbuilder_package ['category' ])
55
- self . assertEqual ( link .childs [0 ].name , v_appbuilder_package ['name' ])
53
+ assert link .name == v_appbuilder_package ['category' ]
54
+ assert link .childs [0 ].name == v_appbuilder_package ['name' ]
56
55
57
56
def test_flaskappbuilder_menu_links (self ):
58
57
from tests .plugins .test_plugin import appbuilder_mitem
@@ -61,40 +60,45 @@ def test_flaskappbuilder_menu_links(self):
61
60
links = [menu_item for menu_item in self .appbuilder .menu .menu
62
61
if menu_item .name == appbuilder_mitem ['category' ]]
63
62
64
- self . assertTrue ( len (links ) == 1 )
63
+ assert len (links ) == 1
65
64
66
65
# menu link should also have a link matching the name of the package.
67
66
link = links [0 ]
68
- self . assertEqual ( link .name , appbuilder_mitem ['category' ])
69
- self . assertEqual ( link .childs [0 ].name , appbuilder_mitem ['name' ])
67
+ assert link .name == appbuilder_mitem ['category' ]
68
+ assert link .childs [0 ].name == appbuilder_mitem ['name' ]
70
69
71
70
def test_app_blueprints (self ):
72
71
from tests .plugins .test_plugin import bp
73
72
74
73
# Blueprint should be present in the app
75
- self . assertTrue ( 'test_plugin' in self .app .blueprints )
76
- self .assertEqual ( self . app .blueprints ['test_plugin' ].name , bp .name )
74
+ assert 'test_plugin' in self .app .blueprints
75
+ assert self .app .blueprints ['test_plugin' ].name == bp .name
77
76
78
- @unittest .skipIf (six .PY2 , 'self.assertLogs not available for Python 2' )
79
- @mock .patch ('pkg_resources.iter_entry_points' )
80
- def test_entrypoint_plugin_errors_dont_raise_exceptions (self , mock_ep_plugins ):
77
+ @pytest .mark .quarantined
78
+ def test_entrypoint_plugin_errors_dont_raise_exceptions (self , caplog ):
81
79
"""
82
80
Test that Airflow does not raise an Error if there is any Exception because of the
83
81
Plugin.
84
82
"""
85
- from airflow .plugins_manager import load_entrypoint_plugins , import_errors
83
+ from airflow .plugins_manager import import_errors , load_entrypoint_plugins , entry_points_with_dist
84
+
85
+ mock_dist = mock .Mock ()
86
86
87
87
mock_entrypoint = mock .Mock ()
88
88
mock_entrypoint .name = 'test-entrypoint'
89
+ mock_entrypoint .group = 'airflow.plugins'
89
90
mock_entrypoint .module_name = 'test.plugins.test_plugins_manager'
90
- mock_entrypoint .load .side_effect = Exception ('Version Conflict' )
91
- mock_ep_plugins .return_value = [mock_entrypoint ]
91
+ mock_entrypoint .load .side_effect = ImportError ('my_fake_module not found' )
92
+ mock_dist .entry_points = [mock_entrypoint ]
93
+
94
+ with mock .patch ('importlib_metadata.distributions' , return_value = [mock_dist ]), caplog .at_level (
95
+ logging .ERROR , logger = 'airflow.plugins_manager'
96
+ ):
97
+ load_entrypoint_plugins (entry_points_with_dist ('airflow.plugins' ), [])
92
98
93
- with self .assertLogs ("airflow.plugins_manager" , level = "ERROR" ) as log_output :
94
- load_entrypoint_plugins (pkg_resources .iter_entry_points ('airflow.plugins' ), [])
95
- received_logs = log_output .output [0 ]
99
+ received_logs = caplog .text
96
100
# Assert Traceback is shown too
97
101
assert "Traceback (most recent call last):" in received_logs
98
- assert "Version Conflict " in received_logs
102
+ assert "my_fake_module not found " in received_logs
99
103
assert "Failed to import plugin test-entrypoint" in received_logs
100
- assert (' test.plugins.test_plugins_manager' , 'Version Conflict' ) in import_errors .items ()
104
+ assert (" test.plugins.test_plugins_manager" , "my_fake_module not found" ) in import_errors .items ()
0 commit comments