1919from unittest .mock import call , patch
2020
2121from opentelemetry .instrumentation import bootstrap
22- from opentelemetry .instrumentation .bootstrap_gen import libraries
22+ from opentelemetry .instrumentation .bootstrap_gen import (
23+ default_instrumentations ,
24+ libraries ,
25+ )
2326
2427
2528def sample_packages (packages , rate ):
@@ -56,15 +59,15 @@ def setUpClass(cls):
5659 "opentelemetry.instrumentation.bootstrap._pip_check" ,
5760 )
5861
59- cls .pkg_patcher .start ()
60- cls .mock_pip_install = cls .pip_install_patcher .start ()
61- cls .mock_pip_check = cls .pip_check_patcher .start ()
62+ def setUp (self ):
63+ super ().setUp ()
64+ self .mock_pip_check = self .pip_check_patcher .start ()
65+ self .mock_pip_install = self .pip_install_patcher .start ()
6266
63- @classmethod
64- def tearDownClass (cls ):
65- cls .pip_check_patcher .start ()
66- cls .pip_install_patcher .start ()
67- cls .pkg_patcher .stop ()
67+ def tearDown (self ):
68+ super ().tearDown ()
69+ self .pip_check_patcher .stop ()
70+ self .pip_install_patcher .stop ()
6871
6972 @patch ("sys.argv" , ["bootstrap" , "-a" , "pipenv" ])
7073 def test_run_unknown_cmd (self ):
@@ -73,18 +76,44 @@ def test_run_unknown_cmd(self):
7376
7477 @patch ("sys.argv" , ["bootstrap" , "-a" , "requirements" ])
7578 def test_run_cmd_print (self ):
79+ self .pkg_patcher .start ()
7680 with patch ("sys.stdout" , new = StringIO ()) as fake_out :
7781 bootstrap .run ()
7882 self .assertEqual (
7983 fake_out .getvalue (),
8084 "\n " .join (self .installed_libraries ) + "\n " ,
8185 )
86+ self .pkg_patcher .stop ()
8287
8388 @patch ("sys.argv" , ["bootstrap" , "-a" , "install" ])
8489 def test_run_cmd_install (self ):
90+ self .pkg_patcher .start ()
8591 bootstrap .run ()
8692 self .mock_pip_install .assert_has_calls (
8793 [call (i ) for i in self .installed_libraries ],
8894 any_order = True ,
8995 )
90- self .assertEqual (self .mock_pip_check .call_count , 1 )
96+ self .mock_pip_check .assert_called_once ()
97+ self .pkg_patcher .stop ()
98+
99+ @patch ("sys.argv" , ["bootstrap" , "-a" , "install" ])
100+ def test_can_override_available_libraries (self ):
101+ bootstrap .run (libraries = [])
102+ self .mock_pip_install .assert_has_calls (
103+ [call (i ) for i in default_instrumentations ],
104+ any_order = True ,
105+ )
106+ self .mock_pip_check .assert_called_once ()
107+
108+ @patch ("sys.argv" , ["bootstrap" , "-a" , "install" ])
109+ def test_can_override_available_default_instrumentations (self ):
110+ with patch (
111+ "opentelemetry.instrumentation.bootstrap._is_installed" ,
112+ return_value = True ,
113+ ):
114+ bootstrap .run (default_instrumentations = [])
115+ self .mock_pip_install .assert_has_calls (
116+ [call (i ) for i in self .installed_libraries ],
117+ any_order = True ,
118+ )
119+ self .mock_pip_check .assert_called_once ()
0 commit comments