Skip to content

Commit

Permalink
[Storage] Migrate test_queue_service_stats.py to test proxy (#27286)
Browse files Browse the repository at this point in the history
* JSON is invalid

* Wip

* Done
  • Loading branch information
vincenttran-msft authored Nov 4, 2022
1 parent 653d77c commit ae5480c
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 220 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"Entries": [
{
"RequestUri": "https://storagename-secondary.queue.core.windows.net/?restype=service\u0026comp=stats",
"RequestMethod": "GET",
"RequestHeaders": {
"Accept": "application/xml",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"User-Agent": "azsdk-python-storage-queue/12.5.1 Python/3.10.2 (Windows-10-10.0.19044-SP0)",
"x-ms-date": "Thu, 03 Nov 2022 23:07:59 GMT",
"x-ms-version": "2020-08-04"
},
"RequestBody": null,
"StatusCode": 200,
"ResponseHeaders": {
"Cache-Control": "no-cache",
"Content-Type": "application/xml",
"Date": "Thu, 03 Nov 2022 23:07:59 GMT",
"Server": [
"Windows-Azure-Queue/1.0",
"Microsoft-HTTPAPI/2.0"
],
"Transfer-Encoding": "chunked",
"x-ms-version": "2020-08-04"
},
"ResponseBody": "\uFEFF\u003C?xml version=\u00221.0\u0022 encoding=\u0022utf-8\u0022?\u003E\u003CStorageServiceStats\u003E\u003CGeoReplication\u003E\u003CStatus\u003Elive\u003C/Status\u003E\u003CLastSyncTime\u003EThu, 03 Nov 2022 23:03:09 GMT\u003C/LastSyncTime\u003E\u003C/GeoReplication\u003E\u003C/StorageServiceStats\u003E"
}
],
"Variables": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"Entries": [
{
"RequestUri": "https://storagename-secondary.queue.core.windows.net/?restype=service\u0026comp=stats",
"RequestMethod": "GET",
"RequestHeaders": {
"Accept": "application/xml",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"User-Agent": "azsdk-python-storage-queue/12.5.1 Python/3.10.2 (Windows-10-10.0.19044-SP0)",
"x-ms-date": "Thu, 03 Nov 2022 23:08:20 GMT",
"x-ms-version": "2020-08-04"
},
"RequestBody": null,
"StatusCode": 200,
"ResponseHeaders": {
"Cache-Control": "no-cache",
"Content-Type": "application/xml",
"Date": "Thu, 03 Nov 2022 23:08:21 GMT",
"Server": [
"Windows-Azure-Queue/1.0",
"Microsoft-HTTPAPI/2.0"
],
"Transfer-Encoding": "chunked",
"x-ms-version": "2020-08-04"
},
"ResponseBody": "\uFEFF\u003C?xml version=\u00221.0\u0022 encoding=\u0022utf-8\u0022?\u003E\u003CStorageServiceStats\u003E\u003CGeoReplication\u003E\u003CStatus\u003Eunavailable\u003C/Status\u003E\u003CLastSyncTime\u003E\u003C/LastSyncTime\u003E\u003C/GeoReplication\u003E\u003C/StorageServiceStats\u003E"
}
],
"Variables": {}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"Entries": [
{
"RequestUri": "https://storagename-secondary.queue.core.windows.net/?restype=service\u0026comp=stats",
"RequestMethod": "GET",
"RequestHeaders": {
"Accept": "application/xml",
"Accept-Encoding": "gzip, deflate",
"User-Agent": "azsdk-python-storage-queue/12.5.1 Python/3.10.2 (Windows-10-10.0.19044-SP0)",
"x-ms-date": "Thu, 03 Nov 2022 23:18:09 GMT",
"x-ms-version": "2021-02-12"
},
"RequestBody": null,
"StatusCode": 200,
"ResponseHeaders": {
"Cache-Control": "no-cache",
"Content-Type": "application/xml",
"Date": "Thu, 03 Nov 2022 23:18:10 GMT",
"Server": [
"Windows-Azure-Queue/1.0",
"Microsoft-HTTPAPI/2.0"
],
"Transfer-Encoding": "chunked",
"x-ms-version": "2021-02-12"
},
"ResponseBody": "\uFEFF\u003C?xml version=\u00221.0\u0022 encoding=\u0022utf-8\u0022?\u003E\u003CStorageServiceStats\u003E\u003CGeoReplication\u003E\u003CStatus\u003Elive\u003C/Status\u003E\u003CLastSyncTime\u003EThu, 03 Nov 2022 23:15:20 GMT\u003C/LastSyncTime\u003E\u003C/GeoReplication\u003E\u003C/StorageServiceStats\u003E"
}
],
"Variables": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"Entries": [
{
"RequestUri": "https://storagename-secondary.queue.core.windows.net/?restype=service\u0026comp=stats",
"RequestMethod": "GET",
"RequestHeaders": {
"Accept": "application/xml",
"Accept-Encoding": "gzip, deflate",
"User-Agent": "azsdk-python-storage-queue/12.5.1 Python/3.10.2 (Windows-10-10.0.19044-SP0)",
"x-ms-date": "Thu, 03 Nov 2022 23:18:43 GMT",
"x-ms-version": "2021-02-12"
},
"RequestBody": null,
"StatusCode": 200,
"ResponseHeaders": {
"Cache-Control": "no-cache",
"Content-Type": "application/xml",
"Date": "Thu, 03 Nov 2022 23:18:44 GMT",
"Server": [
"Windows-Azure-Queue/1.0",
"Microsoft-HTTPAPI/2.0"
],
"Transfer-Encoding": "chunked",
"x-ms-version": "2021-02-12"
},
"ResponseBody": "\uFEFF\u003C?xml version=\u00221.0\u0022 encoding=\u0022utf-8\u0022?\u003E\u003CStorageServiceStats\u003E\u003CGeoReplication\u003E\u003CStatus\u003Eunavailable\u003C/Status\u003E\u003CLastSyncTime\u003E\u003C/LastSyncTime\u003E\u003C/GeoReplication\u003E\u003C/StorageServiceStats\u003E"
}
],
"Variables": {}
}

This file was deleted.

This file was deleted.

59 changes: 28 additions & 31 deletions sdk/storage/azure-storage-queue/tests/test_queue_service_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,59 @@
# --------------------------------------------------------------------------
import unittest

import pytest
from azure.storage.queue import QueueServiceClient
from settings.testcase import QueuePreparer
from devtools_testutils.storage import StorageTestCase

SERVICE_UNAVAILABLE_RESP_BODY = '<?xml version="1.0" encoding="utf-8"?><StorageServiceStats><GeoReplication><Status' \
'>unavailable</Status><LastSyncTime></LastSyncTime></GeoReplication' \
'></StorageServiceStats> '

SERVICE_LIVE_RESP_BODY = '<?xml version="1.0" encoding="utf-8"?><StorageServiceStats><GeoReplication><Status' \
'>live</Status><LastSyncTime>Wed, 19 Jan 2021 22:28:43 GMT</LastSyncTime></GeoReplication' \
'></StorageServiceStats> '
from devtools_testutils import recorded_by_proxy
from devtools_testutils.storage import StorageRecordedTestCase
from settings.testcase import QueuePreparer

# --Test Class -----------------------------------------------------------------
class QueueServiceStatsTest(StorageTestCase):
class TestQueueServiceStats(StorageRecordedTestCase):

# --Helpers-----------------------------------------------------------------
def _assert_stats_default(self, stats):
self.assertIsNotNone(stats)
self.assertIsNotNone(stats['geo_replication'])
assert stats is not None
assert stats['geo_replication'] is not None

self.assertEqual(stats['geo_replication']['status'], 'live')
self.assertIsNotNone(stats['geo_replication']['last_sync_time'])
assert stats['geo_replication']['status'] == 'live'
assert stats['geo_replication']['last_sync_time'] is not None

def _assert_stats_unavailable(self, stats):
self.assertIsNotNone(stats)
self.assertIsNotNone(stats['geo_replication'])

self.assertEqual(stats['geo_replication']['status'], 'unavailable')
self.assertIsNone(stats['geo_replication']['last_sync_time'])
assert stats is not None
assert stats['geo_replication'] is not None

@staticmethod
def override_response_body_with_unavailable_status(response):
response.http_response.text = lambda encoding=None: SERVICE_UNAVAILABLE_RESP_BODY

@staticmethod
def override_response_body_with_live_status(response):
response.http_response.text = lambda encoding=None: SERVICE_LIVE_RESP_BODY
assert stats['geo_replication']['status'] == 'unavailable'
assert stats['geo_replication']['last_sync_time'] is None

# --Test cases per service ---------------------------------------

@pytest.mark.playback_test_only
@QueuePreparer()
def test_queue_service_stats_f(self, storage_account_name, storage_account_key):
@recorded_by_proxy
def test_queue_service_stats(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")

# Arrange
qsc = self.create_storage_client(QueueServiceClient, self.account_url(storage_account_name, "queue"), storage_account_key)

# Act
stats = qsc.get_service_stats(raw_response_hook=self.override_response_body_with_live_status)
stats = qsc.get_service_stats()
# Assert
self._assert_stats_default(stats)

@pytest.mark.playback_test_only
@QueuePreparer()
def test_queue_service_stats_when_unavailable(self, storage_account_name, storage_account_key):
@recorded_by_proxy
def test_queue_service_stats_when_unavailable(self, **kwargs):
storage_account_name = kwargs.pop("storage_account_name")
storage_account_key = kwargs.pop("storage_account_key")

# Arrange
qsc = self.create_storage_client(QueueServiceClient, self.account_url(storage_account_name, "queue"), storage_account_key)

# Act
stats = qsc.get_service_stats(raw_response_hook=self.override_response_body_with_unavailable_status)
stats = qsc.get_service_stats()

# Assert
self._assert_stats_unavailable(stats)
Expand Down
Loading

0 comments on commit ae5480c

Please sign in to comment.