|
5 | 5 | from unittest import mock |
6 | 6 |
|
7 | 7 | import pytest |
| 8 | +import requests_mock |
8 | 9 | from _pytest.logging import LogCaptureFixture |
9 | 10 | from httmock import response |
10 | 11 | from requests import Response |
@@ -1502,3 +1503,122 @@ def test_publish_live_arm64_only( |
1502 | 1503 | ] |
1503 | 1504 | mock_submit.assert_has_calls(submit_calls) |
1504 | 1505 | 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