19
19
from unittest .mock import call , patch
20
20
21
21
from 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
+ )
23
26
24
27
25
28
def sample_packages (packages , rate ):
@@ -56,15 +59,15 @@ def setUpClass(cls):
56
59
"opentelemetry.instrumentation.bootstrap._pip_check" ,
57
60
)
58
61
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 ()
62
66
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 ()
68
71
69
72
@patch ("sys.argv" , ["bootstrap" , "-a" , "pipenv" ])
70
73
def test_run_unknown_cmd (self ):
@@ -73,18 +76,44 @@ def test_run_unknown_cmd(self):
73
76
74
77
@patch ("sys.argv" , ["bootstrap" , "-a" , "requirements" ])
75
78
def test_run_cmd_print (self ):
79
+ self .pkg_patcher .start ()
76
80
with patch ("sys.stdout" , new = StringIO ()) as fake_out :
77
81
bootstrap .run ()
78
82
self .assertEqual (
79
83
fake_out .getvalue (),
80
84
"\n " .join (self .installed_libraries ) + "\n " ,
81
85
)
86
+ self .pkg_patcher .stop ()
82
87
83
88
@patch ("sys.argv" , ["bootstrap" , "-a" , "install" ])
84
89
def test_run_cmd_install (self ):
90
+ self .pkg_patcher .start ()
85
91
bootstrap .run ()
86
92
self .mock_pip_install .assert_has_calls (
87
93
[call (i ) for i in self .installed_libraries ],
88
94
any_order = True ,
89
95
)
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