File: test_utils.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (189 lines) | stat: -rw-r--r-- 6,477 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License in the project root for
# license information.
# --------------------------------------------------------------------------

from importlib import reload
from os import environ
from unittest import TestCase
from unittest.mock import patch

from azure.monitor.opentelemetry import _utils
from azure.monitor.opentelemetry.exporter._constants import _AKS_ARM_NAMESPACE_ID

TEST_VALUE = "TEST_VALUE"
TEST_IKEY = "1234abcd-ab12-34cd-ab12-a23456abcdef"
TEST_CONN_STR = f"InstrumentationKey={TEST_IKEY};IngestionEndpoint=https://centralus-2.in.applicationinsights.azure.com/;LiveEndpoint=https://centralus.livediagnostics.monitor.azure.com/"


def clear_env_var(env_var):
    if env_var in environ:
        del environ[env_var]


class TestUtils(TestCase):
    @patch.dict(
        "os.environ",
        {"ApplicationInsightsAgent_EXTENSION_VERSION": TEST_VALUE},
    )
    def test_extension_version(self):
        reload(_utils)
        self.assertEqual(_utils._EXTENSION_VERSION, TEST_VALUE)

    def test_extension_version_default(self):
        clear_env_var("ApplicationInsightsAgent_EXTENSION_VERSION")
        reload(_utils)
        self.assertEqual(_utils._EXTENSION_VERSION, "disabled")

    @patch.dict("os.environ", {"APPLICATIONINSIGHTS_CONNECTION_STRING": TEST_CONN_STR})
    def test_ikey(self):
        reload(_utils)
        self.assertEqual(_utils._get_customer_ikey_from_env_var(), TEST_IKEY)

    def test_ikey_defaults(self):
        clear_env_var("APPLICATIONINSIGHTS_CONNECTION_STRING")
        reload(_utils)
        self.assertEqual(_utils._get_customer_ikey_from_env_var(), "unknown")

    @patch(
        "azure.monitor.opentelemetry.exporter._utils._is_attach_enabled",
        return_value=True,
    )
    @patch(
        "azure.monitor.opentelemetry.exporter._utils._is_on_app_service",
        return_value=True,
    )
    @patch(
        "azure.monitor.opentelemetry.exporter._utils._is_on_aks",
        return_value=False,
    )
    @patch(
        "azure.monitor.opentelemetry.exporter._utils._is_on_functions",
        return_value=False,
    )
    def test_diagnostics_app_service_attach(
        self, attach_mock, app_service_mock, aks_mock, functions_mock
    ):
        reload(_utils)
        self.assertTrue(_utils._is_diagnostics_enabled())

    @patch(
        "azure.monitor.opentelemetry.exporter._utils._is_attach_enabled",
        return_value=True,
    )
    @patch(
        "azure.monitor.opentelemetry.exporter._utils._is_on_app_service",
        return_value=False,
    )
    @patch(
        "azure.monitor.opentelemetry.exporter._utils._is_on_aks",
        return_value=True,
    )
    @patch(
        "azure.monitor.opentelemetry.exporter._utils._is_on_functions",
        return_value=False,
    )
    def test_diagnostics_aks_attach(
        self, attach_mock, app_service_mock, aks_mock, functions_mock
    ):
        reload(_utils)
        self.assertTrue(_utils._is_diagnostics_enabled())

    @patch(
        "azure.monitor.opentelemetry.exporter._utils._is_attach_enabled",
        return_value=True,
    )
    # Functions have the WEBSITE_SITE_NAME environment variable.
    # This causes them to appear as App Service resources
    @patch(
        "azure.monitor.opentelemetry.exporter._utils._is_on_app_service",
        return_value=True,
    )
    @patch(
        "azure.monitor.opentelemetry.exporter._utils._is_on_aks",
        return_value=False,
    )
    @patch(
        "azure.monitor.opentelemetry.exporter._utils._is_on_functions",
        return_value=True,
    )
    def test_diagnostics_functions_attach(
        self, attach_mock, app_service_mock, aks_mock, functions_mock
    ):
        reload(_utils)
        # Functions attach does not currently enable diagnostics
        self.assertFalse(_utils._is_diagnostics_enabled())

    @patch(
        "azure.monitor.opentelemetry.exporter._utils._is_attach_enabled",
        return_value=False,
    )
    def test_diagnostics_disabled(self, attach_mock):
        reload(_utils)
        self.assertFalse(_utils._is_diagnostics_enabled())

    @patch(
        "azure.monitor.opentelemetry._utils.platform.system",
        return_value="Linux",
    )
    def test_log_path_linux(self, mock_system):
        self.assertEqual(_utils._get_log_path(), "/var/log/applicationinsights")

    @patch(
        "azure.monitor.opentelemetry._utils.platform.system",
        return_value="Linux",
    )
    def test_status_log_path_linux(self, mock_system):
        self.assertEqual(
            _utils._get_log_path(status_log_path=True),
            "/var/log/applicationinsights",
        )

    @patch(
        "azure.monitor.opentelemetry._utils.platform.system",
        return_value="Windows",
    )
    @patch("pathlib.Path.home", return_value="\\HOME\\DIR")
    def test_log_path_windows(self, mock_system, mock_home):
        self.assertEqual(
            _utils._get_log_path(),
            "\\HOME\\DIR\\LogFiles\\ApplicationInsights",
        )

    @patch(
        "azure.monitor.opentelemetry._utils.platform.system",
        return_value="Windows",
    )
    @patch("pathlib.Path.home", return_value="\\HOME\\DIR")
    def test_status_log_path_windows(self, mock_system, mock_home):
        self.assertEqual(
            _utils._get_log_path(status_log_path=True),
            "\\HOME\\DIR\\LogFiles\\ApplicationInsights\\status",
        )

    @patch(
        "azure.monitor.opentelemetry._utils.platform.system",
        return_value="Window",
    )
    def test_log_path_other(self, mock_platform):
        self.assertIsNone(_utils._get_log_path())

    @patch(
        "azure.monitor.opentelemetry._utils.platform.system",
        return_value="linux",
    )
    def test_status_log_path_other(self, mock_platform):
        self.assertIsNone(_utils._get_log_path(status_log_path=True))

    @patch.dict("os.environ", {"key": "value"})
    def test_env_var_or_default(self):
        self.assertEqual(_utils._env_var_or_default("key"), "value")

    @patch.dict("os.environ", {})
    def test_env_var_or_default_empty(self):
        self.assertEqual(_utils._env_var_or_default("key"), "")

    @patch.dict("os.environ", {})
    def test_env_var_or_default_empty_with_defaults(self):
        self.assertEqual(_utils._env_var_or_default("key", default_val="value"), "value")