|
2 | 2 | # See LICENSE file for licensing details. |
3 | 3 |
|
4 | 4 | import re |
5 | | -from unittest.mock import patch |
| 5 | +from unittest.mock import Mock, patch, sentinel |
6 | 6 |
|
7 | 7 | from constants import POSTGRESQL_SNAP_NAME |
8 | | -from utils import new_password, snap_refreshed |
| 8 | +from utils import _remove_stale_otel_sdk_packages, new_password, snap_refreshed |
9 | 9 |
|
10 | 10 |
|
11 | 11 | def test_new_password(): |
@@ -34,3 +34,57 @@ def test_snap_refreshed(): |
34 | 34 | ): |
35 | 35 | assert snap_refreshed("100") is False |
36 | 36 | assert snap_refreshed("200") is False |
| 37 | + |
| 38 | + |
| 39 | +def test_remove_stale_otel_sdk_packages(): |
| 40 | + with ( |
| 41 | + patch("utils.os.getenv", return_value=None) as _getenv, |
| 42 | + patch("utils.shutil") as _shutil, |
| 43 | + patch("utils.distributions") as _distributions, |
| 44 | + ): |
| 45 | + other_dist = Mock() |
| 46 | + other_dist._normalized_name = "test" |
| 47 | + otel_dist = Mock() |
| 48 | + otel_dist._normalized_name = "opentelemetry_test" |
| 49 | + stale_otel_dist = Mock() |
| 50 | + stale_otel_dist._normalized_name = "opentelemetry_test" |
| 51 | + stale_otel_dist.files = [] |
| 52 | + stale_otel_dist._path = sentinel.path |
| 53 | + |
| 54 | + # Not called if not upgrade hook |
| 55 | + _remove_stale_otel_sdk_packages() |
| 56 | + |
| 57 | + _distributions.assert_not_called() |
| 58 | + _shutil.rmtree.assert_not_called() |
| 59 | + _distributions.reset_mock() |
| 60 | + _shutil.rmtree.reset_mock() |
| 61 | + |
| 62 | + # don't execute on Juju 3 |
| 63 | + _getenv.side_effect = ["3.0.0", "hooks/upgrade-charm"] |
| 64 | + _remove_stale_otel_sdk_packages() |
| 65 | + |
| 66 | + _distributions.assert_not_called() |
| 67 | + _shutil.rmtree.assert_not_called() |
| 68 | + _distributions.reset_mock() |
| 69 | + _shutil.rmtree.reset_mock() |
| 70 | + |
| 71 | + # Upgrade hook, nothing to remove |
| 72 | + _getenv.side_effect = ["2.9.53", "hooks/upgrade-charm"] |
| 73 | + _distributions.return_value = [other_dist, otel_dist] |
| 74 | + |
| 75 | + _remove_stale_otel_sdk_packages() |
| 76 | + |
| 77 | + _distributions.assert_called_once_with() |
| 78 | + _shutil.rmtree.assert_not_called() |
| 79 | + _distributions.reset_mock() |
| 80 | + _shutil.rmtree.reset_mock() |
| 81 | + |
| 82 | + # Upgrade hook, duplicate otel packages |
| 83 | + _getenv.side_effect = ["2.9.53", "hooks/upgrade-charm"] |
| 84 | + _distributions.return_value = [other_dist, otel_dist, stale_otel_dist] |
| 85 | + _remove_stale_otel_sdk_packages() |
| 86 | + |
| 87 | + _distributions.assert_called_once_with() |
| 88 | + _shutil.rmtree.assert_called_once_with(sentinel.path) |
| 89 | + _distributions.reset_mock() |
| 90 | + _shutil.rmtree.reset_mock() |
0 commit comments