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
|
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License in the project root for
# license information.
# --------------------------------------------------------------------------
import os
from json import loads
from unittest.mock import patch
from azure.monitor.opentelemetry._diagnostics.status_logger import AzureStatusLogger, _get_status_logger_file_name
TEST_MACHINE_NAME = "TEST_MACHINE_NAME"
TEST_PID = 321
TEST_OPERATION = "TEST_OPERATION"
TEST_OPERATION = "TEST_OPERATION"
TEST_SITE_NAME = "TEST_SITE_NAME"
TEST_CUSTOMER_IKEY = "TEST_CUSTOMER_IKEY"
TEST_EXTENSION_VERSION = "TEST_EXTENSION_VERSION"
TEST_VERSION = "TEST_VERSION"
TEST_SUBSCRIPTION_ID = "TEST_SUBSCRIPTION_ID"
MESSAGE1 = "MESSAGE1"
MESSAGE2 = "MESSAGE2"
def set_up(file_path, is_diagnostics_enabled=True):
patch(
"azure.monitor.opentelemetry._diagnostics.status_logger._STATUS_LOG_PATH",
file_path,
).start()
patch(
"azure.monitor.opentelemetry._diagnostics.status_logger._STATUS_LOG_PATH",
os.path.dirname(file_path),
).start()
patch(
"azure.monitor.opentelemetry._diagnostics.status_logger._get_status_logger_file_name",
return_value=os.path.basename(file_path),
).start()
patch(
"azure.monitor.opentelemetry._diagnostics.status_logger._get_customer_ikey_from_env_var",
return_value=TEST_CUSTOMER_IKEY,
).start()
patch(
"azure.monitor.opentelemetry._diagnostics.status_logger._EXTENSION_VERSION",
TEST_EXTENSION_VERSION,
).start()
patch(
"azure.monitor.opentelemetry._diagnostics.status_logger.VERSION",
TEST_VERSION,
).start()
patch(
"azure.monitor.opentelemetry._diagnostics.status_logger._is_diagnostics_enabled",
return_value=is_diagnostics_enabled,
).start()
patch(
"azure.monitor.opentelemetry._diagnostics.status_logger.getpid",
return_value=TEST_PID,
).start()
patch(
"azure.monitor.opentelemetry._diagnostics.status_logger._MACHINE_NAME",
TEST_MACHINE_NAME,
).start()
def check_file_for_messages(agent_initialized_successfully, file_path, reason=None, sdk_present=None):
with open(file_path, "r") as f:
f.seek(0)
json = loads(f.readline())
assert json["AgentInitializedSuccessfully"] == agent_initialized_successfully
assert json["AppType"] == "python"
assert json["MachineName"] == TEST_MACHINE_NAME
assert json["PID"] == TEST_PID
assert json["SdkVersion"] == TEST_VERSION
assert json["Ikey"] == TEST_CUSTOMER_IKEY
assert json["ExtensionVersion"] == TEST_EXTENSION_VERSION
if reason:
assert json["Reason"] == reason
else:
assert "Reason" not in json
if sdk_present:
assert json["SDKPresent"] == sdk_present
else:
assert "SDKPresent" not in json
assert not f.read()
def check_file_is_empty(file_path):
with open(file_path, "r") as f:
f.seek(0)
assert not f.read()
class TestStatusLogger:
def test_log_status_success(self, temp_file_path):
set_up(temp_file_path, is_diagnostics_enabled=True)
AzureStatusLogger.log_status(False, MESSAGE1)
AzureStatusLogger.log_status(True, MESSAGE2)
check_file_for_messages(True, temp_file_path, MESSAGE2)
def test_log_status_failed_initialization(self, temp_file_path):
set_up(temp_file_path, is_diagnostics_enabled=True)
AzureStatusLogger.log_status(True, MESSAGE1)
AzureStatusLogger.log_status(False, MESSAGE2)
check_file_for_messages(False, temp_file_path, MESSAGE2)
def test_log_status_no_reason(self, temp_file_path):
set_up(temp_file_path, is_diagnostics_enabled=True)
AzureStatusLogger.log_status(False, MESSAGE1)
AzureStatusLogger.log_status(True)
check_file_for_messages(True, temp_file_path)
def test_log_status_sdk_present(self, temp_file_path):
set_up(temp_file_path, is_diagnostics_enabled=True)
AzureStatusLogger.log_status(True, reason=MESSAGE1)
AzureStatusLogger.log_status(False, reason=MESSAGE2, sdk_present=True)
check_file_for_messages(
agent_initialized_successfully=False, file_path=temp_file_path, reason=MESSAGE2, sdk_present=True
)
def test_disabled_log_status_success(self, temp_file_path):
set_up(temp_file_path, is_diagnostics_enabled=False)
AzureStatusLogger.log_status(False, MESSAGE1)
AzureStatusLogger.log_status(True, MESSAGE2)
check_file_is_empty(temp_file_path)
def test_disabled_log_status_failed_initialization(self, temp_file_path):
set_up(temp_file_path, is_diagnostics_enabled=False)
AzureStatusLogger.log_status(True, MESSAGE1)
AzureStatusLogger.log_status(False, MESSAGE2)
check_file_is_empty(temp_file_path)
def test_disabled_log_status_no_reason(self, temp_file_path):
set_up(temp_file_path, is_diagnostics_enabled=False)
AzureStatusLogger.log_status(False, MESSAGE1)
AzureStatusLogger.log_status(True)
check_file_is_empty(temp_file_path)
def test_disabled_log_status_sdk_present(self, temp_file_path):
set_up(temp_file_path, is_diagnostics_enabled=False)
AzureStatusLogger.log_status(True, reason=MESSAGE1)
AzureStatusLogger.log_status(False, reason=MESSAGE2, sdk_present=True)
check_file_is_empty(temp_file_path)
@patch(
"azure.monitor.opentelemetry._diagnostics.status_logger._MACHINE_NAME",
TEST_MACHINE_NAME,
)
def test_get_status_logger_file_name(self):
assert _get_status_logger_file_name(TEST_PID) == f"status_{TEST_MACHINE_NAME}_{TEST_PID}.json"
|