1
1
# Copyright (c) Microsoft Corporation. All rights reserved.
2
2
# Licensed under the MIT License.
3
- import importlib .util
4
3
import os
5
4
import sys
5
+ import importlib .util
6
6
import unittest
7
7
from unittest .mock import patch
8
8
9
9
from azure_functions_worker import testutils
10
+ from azure_functions_worker .utils .common import is_python_version
10
11
from azure_functions_worker .utils .dependency import DependencyManager
11
12
12
13
@@ -228,7 +229,7 @@ def test_add_to_sys_path_no_duplication(self):
228
229
229
230
def test_add_to_sys_path_import_module (self ):
230
231
DependencyManager ._add_to_sys_path (self ._customer_deps_path , True )
231
- import common_module # NoQA
232
+ import common_module # NoQA
232
233
self .assertEqual (
233
234
common_module .package_location ,
234
235
os .path .join (self ._customer_deps_path , 'common_module' )
@@ -239,7 +240,7 @@ def test_add_to_sys_path_import_namespace_path(self):
239
240
into sys.path
240
241
"""
241
242
DependencyManager ._add_to_sys_path (self ._customer_deps_path , True )
242
- import common_namespace # NoQA
243
+ import common_namespace # NoQA
243
244
self .assertEqual (len (common_namespace .__path__ ), 1 )
244
245
self .assertEqual (
245
246
common_namespace .__path__ [0 ],
@@ -516,7 +517,7 @@ def test_clear_path_importer_cache_and_modules_retain_namespace(self):
516
517
sys .path .insert (0 , self ._worker_deps_path )
517
518
518
519
# Ensure new import is from _worker_deps_path
519
- import common_module as worker_mod # NoQA
520
+ import common_module as worker_mod # NoQA
520
521
self .assertIn ('common_module' , sys .modules )
521
522
self .assertEqual (
522
523
worker_mod .package_location ,
@@ -554,6 +555,41 @@ def test_use_worker_dependencies_disable(self):
554
555
with self .assertRaises (ImportError ):
555
556
import common_module # NoQA
556
557
558
+ @unittest .skipUnless (
559
+ sys .version_info .major == 3 and sys .version_info .minor != 10 ,
560
+ 'Test only available for Python 3.6, 3.7, 3.8 or 3.9'
561
+ )
562
+ def test_use_worker_dependencies_default_python_36_37_38_39 (self ):
563
+ # Feature should be disabled in Python 3.6, 3.7, 3.8 and 3.9
564
+ # Setup paths
565
+ DependencyManager .worker_deps_path = self ._worker_deps_path
566
+ DependencyManager .cx_deps_path = self ._customer_deps_path
567
+ DependencyManager .cx_working_dir = self ._customer_func_path
568
+
569
+ # The common_module cannot be imported since feature is disabled
570
+ DependencyManager .use_worker_dependencies ()
571
+ with self .assertRaises (ImportError ):
572
+ import common_module # NoQA
573
+
574
+ @unittest .skipUnless (
575
+ sys .version_info .major == 3 and sys .version_info .minor == 10 ,
576
+ 'Test only available for Python 3.10'
577
+ )
578
+ def test_use_worker_dependencies_default_python_310 (self ):
579
+ # Feature should be enabled in Python 3.10 by default
580
+ # Setup paths
581
+ DependencyManager .worker_deps_path = self ._worker_deps_path
582
+ DependencyManager .cx_deps_path = self ._customer_deps_path
583
+ DependencyManager .cx_working_dir = self ._customer_func_path
584
+
585
+ # Ensure the common_module is imported from _worker_deps_path
586
+ DependencyManager .use_worker_dependencies ()
587
+ import common_module # NoQA
588
+ self .assertEqual (
589
+ common_module .package_location ,
590
+ os .path .join (self ._worker_deps_path , 'common_module' )
591
+ )
592
+
557
593
def test_prioritize_customer_dependencies (self ):
558
594
# Setup app settings
559
595
os .environ ['PYTHON_ISOLATE_WORKER_DEPENDENCIES' ] = 'true'
@@ -592,51 +628,54 @@ def test_prioritize_customer_dependencies_disable(self):
592
628
with self .assertRaises (ImportError ):
593
629
import common_module # NoQA
594
630
595
- def test_prioritize_customer_dependencies_from_working_directory (self ):
596
- self ._initialize_scenario ()
597
-
631
+ @unittest .skipIf (is_python_version ('3.10' ),
632
+ 'Test not available for python 3.10' )
633
+ def test_prioritize_customer_dependencies_default_python_36_37_38_39 (self ):
634
+ # Feature should be disabled in Python 3.6, 3.7, 3.8 and 3.9
598
635
# Setup paths
599
636
DependencyManager .worker_deps_path = self ._worker_deps_path
600
637
DependencyManager .cx_deps_path = self ._customer_deps_path
601
638
DependencyManager .cx_working_dir = self ._customer_func_path
602
639
603
- # Ensure the func_specific_module is imported from _customer_func_path
640
+ # Ensure the common_module is imported from _customer_deps_path
604
641
DependencyManager .prioritize_customer_dependencies ()
605
- import func_specific_module # NoQA
606
- self .assertEqual (
607
- func_specific_module .package_location ,
608
- os .path .join (self ._customer_func_path , 'func_specific_module' )
609
- )
642
+ with self .assertRaises (ImportError ):
643
+ import common_module # NoQA
610
644
611
- def test_reload_customer_libraries_dependency_isolation_true (self ):
612
- os .environ ['PYTHON_ISOLATE_WORKER_DEPENDENCIES' ] = 'true'
645
+ @unittest .skipUnless (
646
+ sys .version_info .major == 3 and sys .version_info .minor == 10 ,
647
+ 'Test only available for Python 3.10'
648
+ )
649
+ def test_prioritize_customer_dependencies_default_python_310 (self ):
650
+ # Feature should be enabled in Python 3.10 by default
613
651
# Setup paths
614
652
DependencyManager .worker_deps_path = self ._worker_deps_path
615
653
DependencyManager .cx_deps_path = self ._customer_deps_path
616
654
DependencyManager .cx_working_dir = self ._customer_func_path
617
655
618
- DependencyManager .reload_customer_libraries (self ._customer_deps_path )
656
+ # Ensure the common_module is imported from _customer_deps_path
657
+ DependencyManager .prioritize_customer_dependencies ()
619
658
import common_module # NoQA
620
659
self .assertEqual (
621
660
common_module .package_location ,
622
- os .path .join (self ._customer_deps_path , 'common_module' ))
661
+ os .path .join (self ._customer_deps_path , 'common_module' )
662
+ )
663
+
664
+ def test_prioritize_customer_dependencies_from_working_directory (self ):
665
+ self ._initialize_scenario ()
623
666
624
- def test_reload_customer_libraries_dependency_isolation_false (self ):
625
- os .environ ['PYTHON_ISOLATE_WORKER_DEPENDENCIES' ] = 'false'
626
667
# Setup paths
627
668
DependencyManager .worker_deps_path = self ._worker_deps_path
628
669
DependencyManager .cx_deps_path = self ._customer_deps_path
629
670
DependencyManager .cx_working_dir = self ._customer_func_path
630
671
631
- DependencyManager ._add_to_sys_path (self ._worker_deps_path , True )
632
- import azure .functions # NoQA
633
-
634
- DependencyManager ._add_to_sys_path (self ._customer_deps_path , True )
635
- DependencyManager .reload_customer_libraries (self ._customer_deps_path )
636
- # Checking if azure.functions gets reloaded
637
- self .assertIn (
638
- os .path .join (self ._customer_deps_path , 'azure' , 'functions' ),
639
- sys .modules ['azure.functions' ].__path__ )
672
+ # Ensure the func_specific_module is imported from _customer_func_path
673
+ DependencyManager .prioritize_customer_dependencies ()
674
+ import func_specific_module # NoQA
675
+ self .assertEqual (
676
+ func_specific_module .package_location ,
677
+ os .path .join (self ._customer_func_path , 'func_specific_module' )
678
+ )
640
679
641
680
def test_remove_module_cache (self ):
642
681
# First import the common_module and create a sys.modules cache
0 commit comments