|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -from unittest import mock |
| 15 | +from importlib.metadata import PackageNotFoundError |
| 16 | +from unittest import TestCase, mock |
| 17 | +from unittest.mock import call, patch |
16 | 18 |
|
17 | 19 | import cassandra.cluster |
18 | 20 | from wrapt import BoundFunctionWrapper |
19 | 21 |
|
20 | 22 | import opentelemetry.instrumentation.cassandra |
21 | 23 | from opentelemetry import trace as trace_api |
22 | 24 | from opentelemetry.instrumentation.cassandra import CassandraInstrumentor |
| 25 | +from opentelemetry.instrumentation.cassandra.package import ( |
| 26 | + _instruments_cassandra_driver, |
| 27 | + _instruments_scylla_driver, |
| 28 | +) |
23 | 29 | from opentelemetry.sdk import resources |
24 | 30 | from opentelemetry.test.test_base import TestBase |
25 | 31 | from opentelemetry.trace import SpanKind |
@@ -137,3 +143,95 @@ def test_instrument_connection_no_op_tracer_provider( |
137 | 143 |
|
138 | 144 | spans_list = self.memory_exporter.get_finished_spans() |
139 | 145 | self.assertEqual(len(spans_list), 0) |
| 146 | + |
| 147 | + |
| 148 | +class TestCassandraInstrumentationDependencies(TestCase): |
| 149 | + @patch("opentelemetry.instrumentation.cassandra.distribution") |
| 150 | + def test_instrumentation_dependencies_cassandra_driver_installed( |
| 151 | + self, mock_distribution |
| 152 | + ) -> None: |
| 153 | + instrumentation = CassandraInstrumentor() |
| 154 | + |
| 155 | + def _distribution(name): |
| 156 | + if name == "cassandra-driver": |
| 157 | + return None |
| 158 | + raise PackageNotFoundError |
| 159 | + |
| 160 | + mock_distribution.side_effect = _distribution |
| 161 | + package_to_instrument = instrumentation.instrumentation_dependencies() |
| 162 | + self.assertEqual(mock_distribution.call_count, 1) |
| 163 | + self.assertEqual( |
| 164 | + mock_distribution.mock_calls, |
| 165 | + [ |
| 166 | + call("cassandra-driver"), |
| 167 | + ], |
| 168 | + ) |
| 169 | + self.assertEqual( |
| 170 | + package_to_instrument, (_instruments_cassandra_driver,) |
| 171 | + ) |
| 172 | + |
| 173 | + @patch("opentelemetry.instrumentation.cassandra.distribution") |
| 174 | + def test_instrumentation_dependencies_scylla_driver_installed( |
| 175 | + self, mock_distribution |
| 176 | + ) -> None: |
| 177 | + instrumentation = CassandraInstrumentor() |
| 178 | + |
| 179 | + def _distribution(name): |
| 180 | + if name == "scylla-driver": |
| 181 | + return None |
| 182 | + raise PackageNotFoundError |
| 183 | + |
| 184 | + mock_distribution.side_effect = _distribution |
| 185 | + package_to_instrument = instrumentation.instrumentation_dependencies() |
| 186 | + self.assertEqual(mock_distribution.call_count, 2) |
| 187 | + self.assertEqual( |
| 188 | + mock_distribution.mock_calls, |
| 189 | + [ |
| 190 | + call("cassandra-driver"), |
| 191 | + call("scylla-driver"), |
| 192 | + ], |
| 193 | + ) |
| 194 | + self.assertEqual(package_to_instrument, (_instruments_scylla_driver,)) |
| 195 | + |
| 196 | + @patch("opentelemetry.instrumentation.cassandra.distribution") |
| 197 | + def test_instrumentation_dependencies_both_installed( |
| 198 | + self, mock_distribution |
| 199 | + ) -> None: |
| 200 | + instrumentation = CassandraInstrumentor() |
| 201 | + |
| 202 | + def _distribution(name): |
| 203 | + return None |
| 204 | + |
| 205 | + mock_distribution.side_effect = _distribution |
| 206 | + package_to_instrument = instrumentation.instrumentation_dependencies() |
| 207 | + self.assertEqual(mock_distribution.call_count, 1) |
| 208 | + self.assertEqual( |
| 209 | + mock_distribution.mock_calls, [call("cassandra-driver")] |
| 210 | + ) |
| 211 | + self.assertEqual( |
| 212 | + package_to_instrument, (_instruments_cassandra_driver,) |
| 213 | + ) |
| 214 | + |
| 215 | + @patch("opentelemetry.instrumentation.cassandra.distribution") |
| 216 | + def test_instrumentation_dependencies_none_installed( |
| 217 | + self, mock_distribution |
| 218 | + ) -> None: |
| 219 | + instrumentation = CassandraInstrumentor() |
| 220 | + |
| 221 | + def _distribution(name): |
| 222 | + raise PackageNotFoundError |
| 223 | + |
| 224 | + mock_distribution.side_effect = _distribution |
| 225 | + package_to_instrument = instrumentation.instrumentation_dependencies() |
| 226 | + self.assertEqual(mock_distribution.call_count, 2) |
| 227 | + self.assertEqual( |
| 228 | + mock_distribution.mock_calls, |
| 229 | + [ |
| 230 | + call("cassandra-driver"), |
| 231 | + call("scylla-driver"), |
| 232 | + ], |
| 233 | + ) |
| 234 | + self.assertEqual( |
| 235 | + package_to_instrument, |
| 236 | + (_instruments_cassandra_driver, _instruments_scylla_driver), |
| 237 | + ) |
0 commit comments