Skip to content

Commit 3edd8d6

Browse files
committed
Azure: Adds unit test for publishing offer in preview
This commit adds a new unit test named `test_publish_live_when_state_is_preview` which aims to ensure that `cloudpub` is capable of publishing content to Azure when the offer is in the `preview` state with the desired SAS URI already assossiated with it. The expected behavior is to publish live when such events happens. Refers to SPSTRAT-583 Signed-off-by: Jonathan Gangi <jgangi@redhat.com>
1 parent b9986c7 commit 3edd8d6

4 files changed

Lines changed: 151 additions & 7 deletions

File tree

requirements-test.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pytest
44
pytest-cov
55
httmock
66
mypy
7+
requests-mock
78
sphinx
89
sphinx-autodoc-typehints
910
types-requests<=2.31.0.6

requirements-test.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,12 @@ requests==2.32.4 \
397397
# via
398398
# cloudpub (setup.py)
399399
# httmock
400+
# requests-mock
400401
# sphinx
402+
requests-mock==1.12.1 \
403+
--hash=sha256:b1e37054004cdd5e56c84454cc7df12b25f90f382159087f4b6915aaeef39563 \
404+
--hash=sha256:e9e12e333b525156e82a3c852f22016b9158220d2f47454de9cae8a77d371401
405+
# via -r requirements-test.in
401406
s3transfer==0.13.1 \
402407
--hash=sha256:a981aa7429be23fe6dfc13e80e4020057cbab622b08c0315288758d67cabc724 \
403408
--hash=sha256:c3fdba22ba1bd367922f27ec8032d6a1cf5f10c934fb5d68cf60fd5a23d936cf

tests/ms_azure/conftest.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
def token() -> Dict[str, str]:
3232
return {
3333
"token_type": "Bearer",
34-
"expires_in": "3599",
35-
"ext_expires_in": "3599",
36-
"expires_on": "1646935200", # 2022-03-10 15:00:00
37-
"not_before": "1646931600", # 2022-03-10 14:00:00
38-
"resource": "https://graph.microsoft.com/rp/product-ingestion",
39-
"access_token": "aBcDeFgHiJkLmNoPqRsTuVwXyZ",
34+
"expires_in": "9999",
35+
"ext_expires_in": "9999",
36+
"expires_on": "0",
37+
"not_before": "9999999999",
38+
"resource": "https://graph.microsoft.com/",
39+
"access_token": "faketoken",
4040
}
4141

4242

@@ -103,7 +103,7 @@ def errors() -> List[Dict[str, Any]]:
103103

104104

105105
@pytest.fixture
106-
def configure_success_response() -> Dict[str, Any]:
106+
def configure_running_response() -> Dict[str, Any]:
107107
return {
108108
"$schema": "https://schema",
109109
"jobId": "0a0a00aa-00a0-0a00-0a0a-00000a000a00",
@@ -115,6 +115,19 @@ def configure_success_response() -> Dict[str, Any]:
115115
}
116116

117117

118+
@pytest.fixture
119+
def configure_success_response() -> Dict[str, Any]:
120+
return {
121+
"$schema": "https://schema",
122+
"jobId": "0a0a00aa-00a0-0a00-0a0a-00000a000a00",
123+
"jobStatus": "completed",
124+
"jobResult": "succeeded",
125+
"jobStart": "2022-06-13T08:44:47.5235Z",
126+
"jobEnd": "0001-01-01T00:00:00",
127+
"errors": [],
128+
}
129+
130+
118131
@pytest.fixture
119132
def configure_failure_response() -> Dict[str, Any]:
120133
return {
@@ -155,6 +168,11 @@ def product_summary() -> Dict[str, Any]:
155168
}
156169

157170

171+
@pytest.fixture
172+
def products_list(product_summary) -> Dict[str, Any]:
173+
return {"value": [product_summary]}
174+
175+
158176
@pytest.fixture
159177
def customer_leads() -> Dict[str, str]:
160178
return {

tests/ms_azure/test_service.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from unittest import mock
66

77
import pytest
8+
import requests_mock
89
from _pytest.logging import LogCaptureFixture
910
from httmock import response
1011
from requests import Response
@@ -1502,3 +1503,122 @@ def test_publish_live_arm64_only(
15021503
]
15031504
mock_submit.assert_has_calls(submit_calls)
15041505
mock_ensure_publish.assert_called_once_with(product_obj.id)
1506+
1507+
def test_publish_live_when_state_is_preview(
1508+
self,
1509+
token: Dict[str, Any],
1510+
auth_dict: Dict[str, Any],
1511+
configure_running_response: Dict[str, Any],
1512+
configure_success_response: Dict[str, Any],
1513+
product: Dict[str, Any],
1514+
products_list: Dict[str, Any],
1515+
product_summary: Dict[str, Any],
1516+
submission: Dict[str, Any],
1517+
vmimage_source: Dict[str, Any],
1518+
metadata_azure_obj: mock.MagicMock,
1519+
caplog: pytest.LogCaptureFixture,
1520+
) -> None:
1521+
# Prepare testing data
1522+
metadata_azure_obj.keepdraft = False
1523+
metadata_azure_obj.destination = "example-product/plan-1"
1524+
# SAS URI should be already present during preview
1525+
metadata_azure_obj.image_path = vmimage_source["osDisk"]["uri"]
1526+
# Set the submission state to preview and create one for live after publishing
1527+
submission_preview = deepcopy(submission)
1528+
submission_preview.update({"target": {"targetType": "preview"}})
1529+
submission_live = deepcopy(submission)
1530+
submission_live.update({"target": {"targetType": "live"}})
1531+
# We want to have 2 kind of responses: one for the product still in preview
1532+
# and a final one when it gets live
1533+
submissions_inprog = {"value": [submission_preview]}
1534+
submissions_final = {"value": [submission_preview, submission_live]}
1535+
# We need to replace the existing "draft" submission of product's resources
1536+
# with the "preview" one to cause the test to run in an offer already in preview
1537+
filtered_resources = [
1538+
x for x in product.get("resources", []) if "submission" not in x.get("id", "")
1539+
]
1540+
filtered_resources.append(submission_preview)
1541+
product["resources"] = filtered_resources
1542+
# Constants
1543+
login_url = "https://login.microsoftonline.com/foo/oauth2/token"
1544+
base_url = "https://graph.microsoft.com/rp/product-ingestion"
1545+
product_id = str(product_summary['id']).split("/")[-1]
1546+
1547+
# Test
1548+
with caplog.at_level(logging.INFO):
1549+
with requests_mock.Mocker() as m:
1550+
m.post(login_url, json=token)
1551+
m.get(f"{base_url}/product", json=products_list)
1552+
m.get(f"{base_url}/resource-tree/product/{product_id}", json=product)
1553+
m.post(f"{base_url}/configure", json=configure_running_response)
1554+
m.get(
1555+
f"{base_url}/configure/{configure_success_response['jobId']}/status",
1556+
json=configure_success_response,
1557+
)
1558+
m.get(
1559+
f"{base_url}/submission/{product_id}",
1560+
[
1561+
{"json": submissions_inprog}, # ensure_can_publish call "preview"
1562+
{"json": submissions_inprog}, # ensure_can_publish call "live"
1563+
{"json": submissions_inprog}, # _is_submission_in_preview call
1564+
{"json": submissions_inprog}, # submit_to_status check prev_state call
1565+
{"json": submissions_final}, # submit_to_status validation after configure
1566+
],
1567+
)
1568+
azure_svc = AzureService(auth_dict)
1569+
azure_svc.publish(metadata=metadata_azure_obj)
1570+
# Present messages
1571+
assert 'Requesting the products list.' in caplog.text
1572+
assert (
1573+
'Requesting the product ID "ffffffff-ffff-ffff-ffff-ffffffffffff" with state "preview".'
1574+
in caplog.text
1575+
)
1576+
assert (
1577+
'Preparing to associate the image "https://uri.test.com" with the plan "plan-1" from product "example-product"' # noqa: E501
1578+
in caplog.text
1579+
)
1580+
assert 'Retrieving the technical config for "example-product/plan-1".' in caplog.text
1581+
assert (
1582+
'Creating the VMImageResource with SAS for image: "https://uri.test.com"' in caplog.text
1583+
)
1584+
assert (
1585+
'The destination "example-product/plan-1" already contains the SAS URI: "https://uri.test.com".' # noqa: E501
1586+
in caplog.text
1587+
)
1588+
assert 'Publishing the new changes for "example-product" on plan "plan-1"' in caplog.text
1589+
assert (
1590+
'Requesting the product ID "ffffffff-ffff-ffff-ffff-ffffffffffff" with state "preview".'
1591+
in caplog.text
1592+
)
1593+
assert (
1594+
'Ensuring no other publishing jobs are in progress for "ffffffff-ffff-ffff-ffff-ffffffffffff"' # noqa: E501
1595+
in caplog.text
1596+
)
1597+
assert (
1598+
'Looking up for submission in state "preview" for "ffffffff-ffff-ffff-ffff-ffffffffffff"' # noqa: E501
1599+
in caplog.text
1600+
)
1601+
assert (
1602+
'Looking up for submission in state "live" for "ffffffff-ffff-ffff-ffff-ffffffffffff"'
1603+
in caplog.text
1604+
)
1605+
assert 'The product "example-product" is already set to preview' in caplog.text
1606+
assert (
1607+
'Submitting the status of "ffffffff-ffff-ffff-ffff-ffffffffffff" to "live"'
1608+
in caplog.text
1609+
)
1610+
assert (
1611+
'Finished publishing the image "https://uri.test.com" to "example-product/plan-1"\n'
1612+
in caplog.text
1613+
)
1614+
1615+
# Absent messages
1616+
assert (
1617+
'Scanning the disk versions from "example-product/plan-1" for the image "https://uri.test.com"' # noqa: E501
1618+
not in caplog.text
1619+
)
1620+
assert 'The DiskVersion doesn\'t exist, creating one from scratch.' not in caplog.text
1621+
assert 'Updating SKUs for "example-product/plan-1".' not in caplog.text
1622+
assert (
1623+
'Updating the technical configuration for "example-product/plan-1".' not in caplog.text
1624+
)

0 commit comments

Comments
 (0)