Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sumo 233810 fix unit testing automation #69

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions EventHubs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
requests==2.20.0
azure-mgmt-resource==1.2.2
azure-mgmt-eventhub==1.2.0
requests==2.31.0
azure-monitor-query==1.2.0
azure-identity==1.15.0
azure-core==1.29.6
azure-mgmt-resource==23.0.1
azure-mgmt-eventhub==11.0.0
azure-servicebus==0.21.1
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
azure-cosmosdb-table==1.0.2
azure-mgmt-storage==1.0.0

azure-mgmt-storage==21.1.0
2 changes: 1 addition & 1 deletion EventHubs/src/azuredeploy_metrics.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
{ "name": "WEBSITE_NODE_DEFAULT_VERSION", "value": "~18"},
{ "name": "Project", "value": "EventHubs/target/metrics_build/" },
{ "name": "AzureWebJobsStorage", "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', parameters('storageAccounts_sumometapplogs_name'), ';AccountKey=', listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccounts_sumometapplogs_name')),'2022-09-01').keys[0].value,';EndpointSuffix=', parameters('storageEndpointSuffix'))]" },
{ "name": "APPLICATIONINSIGHTS_CONNECTION_STRING", "value": "[reference(concat('microsoft.insights/components/', parameters('appInsightsName')), '2015-05-01').ConnectionString]" },
{ "name": "APPLICATIONINSIGHTS_CONNECTION_STRING", "value": "[concat('microsoft.insights/components/', parameters('appInsightsName'), '2015-05-01').ConnectionString]" },
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
{ "name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING", "value" : "[concat('DefaultEndpointsProtocol=https;AccountName=', parameters('storageAccounts_sumometapplogs_name'), ';AccountKey=', listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccounts_sumometapplogs_name')),'2022-09-01').keys[0].value,';EndpointSuffix=', parameters('storageEndpointSuffix'))]"},
{ "name": "WEBSITE_CONTENTSHARE", "value": "[toLower(parameters('sites_SumoMetricsFunctionApp_name'))]" },
{ "name": "FUNCTION_APP_EDIT_MODE", "value": "readwrite"},
Expand Down
73 changes: 42 additions & 31 deletions EventHubs/tests/baseeventhubtest.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
import os
import sys
sys.path.insert(0, '../../test_utils')
from basetest import BaseTest
import json
import time
from time import sleep
from requests import Session
from datetime import datetime
from azure.servicebus import ServiceBusService
from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.eventhub import EventHubManagementClient
from azure.mgmt.storage import StorageManagementClient
from azure.cosmosdb.table.tableservice import TableService
from azure.servicebus import ServiceBusService

sys.path.insert(0, '../../test_utils')
from basetest import BaseTest

class BaseEventHubTest(BaseTest):

def setUp(self):
self.create_credentials()
self.resource_client = ResourceManagementClient(self.credentials,
self.subscription_id)
self.azure_credential = DefaultAzureCredential()
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
self.subscription_id = os.getenv("AZURE_SUBSCRIPTION_ID")
self.resource_client = ResourceManagementClient(self.azure_credential, self.subscription_id)
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
try:
self.sumo_endpoint_url = os.environ["SumoEndpointURL"]
except KeyError:
raise Exception("SumoEndpointURL environment variables are not set")

self.repo_name, self.branch_name = self.get_git_info()

def tearDown(self):
if self.resource_group_exists(self.RESOURCE_GROUP_NAME):
self.delete_resource_group()
print("RESOURCE_GROUP_NAME...:",self.RESOURCE_GROUP_NAME)
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
#self.delete_resource_group()

def get_resource_name(self, resprefix, restype):
for item in self.resource_client.resources.list_by_resource_group(self.RESOURCE_GROUP_NAME):
Expand All @@ -49,39 +51,48 @@ def wait_for_table_results(self, query):
sleep(15)
max_retries -= 1

def get_table_service(self):
storage_client = StorageManagementClient(self.credentials,
self.subscription_id)
STORAGE_ACCOUNT_NAME = self.get_resource_name(self.STORAGE_ACCOUNT_NAME, "Microsoft.Storage/storageAccounts")
storage_keys = storage_client.storage_accounts.list_keys(
self.RESOURCE_GROUP_NAME, STORAGE_ACCOUNT_NAME)
acckey = storage_keys.keys[0].value
table_service = TableService(account_name=STORAGE_ACCOUNT_NAME,
account_key=acckey)
return table_service

def insert_mock_logs_in_EventHub(self, filename):
print("Inserting fake logs in EventHub")

himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
defaultauthorule_name = "RootManageSharedAccessKey"
namespace_name = self.get_resource_name(self.event_hub_namespace_prefix, "Microsoft.EventHub/namespaces")
eventhub_client = EventHubManagementClient(self.azure_credential, self.subscription_id)
eventhub_keys = eventhub_client.namespaces.list_keys(self.RESOURCE_GROUP_NAME, namespace_name, defaultauthorule_name)

sbs = ServiceBusService(
namespace_name,
shared_access_key_name=defaultauthorule_name,
shared_access_key_value=eventhub_keys.primary_key,
request_session=Session()
)

mock_logs = json.load(open(filename))
# print("inserting %s" % (mock_logs))
sbs.send_event(self.eventhub_name, json.dumps(mock_logs))
print("Event inserted")

def insert_mock_metrics_in_EventHub(self, filename):
print("Inserting fake logs in EventHub")
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved

defaultauthorule_name = "RootManageSharedAccessKey"

eventhub_client = EventHubManagementClient(self.credentials,
self.subscription_id)

ehkeys = eventhub_client.namespaces.list_keys(
self.RESOURCE_GROUP_NAME, namespace_name, defaultauthorule_name)

namespace_name = self.get_resource_name(self.event_hub_namespace_prefix, "Microsoft.EventHub/namespaces")
eventhub_client = EventHubManagementClient(self.azure_credential, self.subscription_id)
eventhub_keys = eventhub_client.namespaces.list_keys(self.RESOURCE_GROUP_NAME, namespace_name, defaultauthorule_name)

sbs = ServiceBusService(
namespace_name,
shared_access_key_name=defaultauthorule_name,
shared_access_key_value=ehkeys.primary_key,
shared_access_key_value=eventhub_keys.primary_key,
request_session=Session()
)

mock_logs = json.load(open(filename))
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
print("inserting %s" % (mock_logs))
sbs.send_event(self.eventhub_name, json.dumps(mock_logs))
mock_logs = json.dumps(mock_logs)
mock_logs = mock_logs.replace("2018-03-07T14:23:51.991Z", datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ"))
mock_logs = mock_logs.replace("C088DC46", "%d-%s" % (1, str(int(time.time()))))

# print("inserting %s" % (mock_logs))
sbs.send_event(self.eventhub_name, mock_logs)
print("Event inserted")

def _parse_template(self):
Expand Down
169 changes: 136 additions & 33 deletions EventHubs/tests/test_eventhub_metrics.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,151 @@
import sys
import unittest
import datetime
from io import StringIO
from datetime import timedelta
from baseeventhubtest import BaseEventHubTest


from azure.monitor.query import LogsQueryClient, LogsQueryStatus
class TestEventHubMetrics(BaseEventHubTest):

def setUp(self):
super(TestEventHubMetrics, self).setUp()
self.RESOURCE_GROUP_NAME = "TestEventHubMetrics-%s" % (
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
datetime.datetime.now().strftime("%d-%m-%y-%H-%M-%S"))
self.function_name_prefix = "EventHubs_Metrics"
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
self.STORAGE_ACCOUNT_NAME = "sumometlogs"
self.template_name = 'azuredeploy_metrics.json'
self.log_table_name = "AzureWebJobsHostLogs%d%02d" % (
datetime.datetime.now().year, datetime.datetime.now().month)
self.RESOURCE_GROUP_NAME = "azure_metric_unittest"
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
self.namespace_location = "eastus"
# self.function_name_prefix = "EventHubs_Metrics"
# self.STORAGE_ACCOUNT_NAME = "sumometlogs"
self.template_name = "azuredeploy_metrics.json"
self.event_hub_namespace_prefix = "SumoMetricsNamespace"
self.eventhub_name = 'insights-metrics-pt1m'

self.eventhub_name = "insights-metrics-pt1m"
super(TestEventHubMetrics, self).setUp()

def test_pipeline(self):

self.create_resource_group()
self.deploy_template()
# self.deploy_template()
print("Testing Stack Creation")
self.assertTrue(self.resource_group_exists(self.RESOURCE_GROUP_NAME))
self.table_service = self.get_table_service()
self.insert_mock_logs_in_EventHub('metrics_fixtures.json')
self.check_error_logs()
self.assertTrue(self.resource_group_exists(self.RESOURCE_GROUP_NAME))
# total resource 8 or 9
self.insert_mock_metrics_in_EventHub('metrics_fixtures.json')
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
self.check_success_log()
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
self.check_error_log()
self.check_warning_log()

def check_success_log(self):
# Save the original stdout for later comparison
original_stdout = sys.stdout
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
sys.stdout = StringIO()

try:
client = LogsQueryClient(self.azure_credential)

query = '''union
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
*,
app('sumometricsappinsights6dbfe7sr3obyi').traces
| where operation_Id == "69f9a532d7cb04dd3ba6254c3d682458"'''

response = client.query_workspace('800e2f15-1fa6-4c5a-a3f5-db8f647ee8a1', query, timespan=timedelta(days=1))
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved

if response.status == LogsQueryStatus.PARTIAL:
error = response.partial_error
data = response.partial_data
elif response.status == LogsQueryStatus.SUCCESS:
data = response.tables
for table in data:
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
for col in table.columns:
print(col + " ", end="")
for row in table.rows:
for item in row:
print(item, end="")
print("\n")
except Exception as e:
print("An unexpected error occurred during the test:")
print("Exception", e)

# Capture the output
captured_output = sys.stdout.getvalue()

# Assertions
self.assertIn('Sent all metric data to Sumo. Exit now.', captured_output)

# Reset redirect.
sys.stdout = original_stdout

def check_error_log(self):
# Save the original stdout for later comparison
original_stdout = sys.stdout
himanshu219 marked this conversation as resolved.
Show resolved Hide resolved
sys.stdout = StringIO()

try:
client = LogsQueryClient(self.azure_credential)

query = '''union
*,
app('sumometricsappinsights6dbfe7sr3obyi').traces
| where operation_Id == "69f9a532d7cb04dd3ba6254c3d682458"'''

response = client.query_workspace('800e2f15-1fa6-4c5a-a3f5-db8f647ee8a1', query, timespan=timedelta(days=1))

if response.status == LogsQueryStatus.PARTIAL:
error = response.partial_error
data = response.partial_data
elif response.status == LogsQueryStatus.SUCCESS:
data = response.tables
for table in data:
for col in table.columns:
print(col + " ", end="")
for row in table.rows:
for item in row:
print(item, end="")
print("\n")
except Exception as e:
print("An unexpected error occurred during the test:")
print("Exception", e)

# Capture the output
captured_output = sys.stdout.getvalue()

# Aassertions
self.assertIn('"LogLevel":"Error"', captured_output)

# Reset redirect.
sys.stdout = original_stdout

def check_warning_log(self):
# Save the original stdout for later comparison
original_stdout = sys.stdout
sys.stdout = StringIO()

def check_error_logs(self):
print("sleeping 1min for function execution")
query = "PartitionKey eq 'R2'"
self.wait_for_table_results(query)
self.assertTrue(self.get_row_count(query) > 0)
try:
client = LogsQueryClient(self.azure_credential)

query = '''union
*,
app('sumometricsappinsights6dbfe7sr3obyi').traces
| where operation_Id == "69f9a532d7cb04dd3ba6254c3d682458"'''

response = client.query_workspace('800e2f15-1fa6-4c5a-a3f5-db8f647ee8a1', query, timespan=timedelta(days=1))

if response.status == LogsQueryStatus.PARTIAL:
error = response.partial_error
data = response.partial_data
elif response.status == LogsQueryStatus.SUCCESS:
data = response.tables
for table in data:
for col in table.columns:
print(col + " ", end="")
for row in table.rows:
for item in row:
print(item, end="")
print("\n")
except Exception as e:
print("An unexpected error occurred during the test:")
print("Exception", e)

rows = self.table_service.query_entities(
self.log_table_name, filter=query)
# Capture the output
captured_output = sys.stdout.getvalue()

haserr = False
for row in rows.items:
print("LogRow: ", row["FunctionName"], row["HasError"])
if row["FunctionName"].startswith(self.function_name_prefix) and row["HasError"]:
haserr = True
# Aassertions
self.assertIn('"LogLevel":"Warning"', captured_output)

self.assertTrue(not haserr)
# Reset redirect.
sys.stdout = original_stdout

if __name__ == '__main__':
unittest.main()
unittest.main()
Loading