Uploaded image for project: 'Subscription Watch'
  1. Subscription Watch
  2. SWATCH-3426

Fix flaky test test_verify_contract_missing_purchase

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Undefined Undefined
    • 2025-04-16 - API
    • None
    • None
    • 5
    • False
    • Hide

      None

      Show
      None
    • False

      Test:

      application = <iqe.base.application.Application object at 0x7f1f429ae630>
      product_id = 'ansible-aap-managed', billing_provider = 'azure'
      tally_db_session = <sqlalchemy.orm.session.Session object at 0x7f1f1c1ef5c0>
      
          @pytest.mark.ephemeral
          @pytest.mark.ephemeral_only
          @pytest.mark.parametrize("product_id", ["ansible-aap-managed"])
          @pytest.mark.parametrize("billing_provider", ["azure"])
          def test_verify_contract_missing_purchase(
              application, product_id, billing_provider, tally_db_session
          ):
              """Verify contract with missing sku is not created
              metadata:
                  assignee: tmcknigh
                  negative: false
                  importance: critical
                  requirements: payg_tally
                  test_steps:
                      1. Create contract with a missing purchase.
                      3. Sync contract.
                      4. Verify contract was not created
                  expected_results:
                      1. Contract should not be saved
              """
              tally_db_session.execute("truncate table contracts cascade;")
              tally_db_session.execute("truncate table subscription cascade;")
              metric_dict = create_metrics(product_id)
              subscription_id = "61412512"
              original_start_date_timestamp = today() - datetime.timedelta(days=30)
              original_end_date_timestamp = today() + datetime.timedelta(days=5)
              payload = application.rhsm_subscriptions.generate_contract_payload(
                  product_id=product_id,
                  billing_provider=billing_provider,
                  billing_account_id="0fa44b14-ef8b-4e9e-8358-1e4230250d73",
                  subscription_number="123456789",
                  subscription_id=subscription_id,
                  start_date=datetime_to_iso8601_format(original_start_date_timestamp),
                  end_date=datetime_to_iso8601_format(original_end_date_timestamp),
                  vendor_product_code="offerPlanId",
                  **metric_dict,
              )
          
              # Remove purchase from payload
              payload["partner_entitlement"]["purchase"] = {}
          
              partner_entitlement_response = create_partner_api_response(payload)
              application.rhsm_subscriptions.create_partner_api_wiremock(partner_entitlement_response)
              logging.info(f"Creating contract with payload : {partner_entitlement_response}, ")
              subscription_search_mock_response = (
                  subscription_service_response.get_json_data_from_entitlement_payload(
                      payload,
                      application.user.identity.org_id,
                      original_start_date_timestamp,
                      original_end_date_timestamp,
                  )
              )
              application.rhsm_subscriptions.create_subscription_search_api_wiremock(
                  subscription_search_mock_response
              )
          
              # Create contract
              # We are expecting to receive a 500 status code
              # so we add the expect_negative to skip the ok.status check
              response = application.rhsm_subscriptions.sync_contracts(expect_negative=True)
      >       assert response["status"] == "500", f"Expected 500 Bad Request Received: {response}"
      E       TypeError: 'Response' object is not subscriptable
      
      /iqe_venv/lib/python3.12/site-packages/iqe_rhsm_subscriptions/tests/component/swatch_contracts/test_contracts_errors.py:142: TypeError
      

      Output:

      --------------------------------- Captured Log ---------------------------------
      2025-03-28 06:54:01 INFO Using random seed value for random generation: 8329
      2025-03-28 06:54:01 INFO select version()
      2025-03-28 06:54:01 INFO [raw sql] {}
      2025-03-28 06:54:01 INFO select current_schema()
      2025-03-28 06:54:01 INFO [raw sql] {}
      2025-03-28 06:54:01 INFO show standard_conforming_strings
      2025-03-28 06:54:01 INFO [raw sql] {}
      2025-03-28 06:54:01 INFO BEGIN (implicit)
      2025-03-28 06:54:01 INFO truncate table contracts cascade;
      2025-03-28 06:54:01 INFO [generated in 0.00027s] {}
      2025-03-28 06:54:01 INFO truncate table subscription cascade;
      2025-03-28 06:54:01 INFO [generated in 0.00029s] {}
      2025-03-28 06:54:01 INFO SKUs for ansible-aap-managed : ['MCT4701']
      2025-03-28 06:54:01 INFO Contract payload created:{'partner_entitlement': {'rhAccountId': '18939572', 'sourcePartner': 'azure_marketplace', 'entitlementDates': {'startDate': '2025-02-26T06:54:01.441455Z', 'endDate': '2025-04-02T06:54:01.441464Z'}, 'rhEntitlements': [{'subscriptionNumber': '123456789', 'sku': 'MCT4701'}], 'purchase': {'contracts': [{'planId': 'plan_id_1234', 'dimensions': [{'name': 'Instance-hours', 'value': '1'}, {'name': 'Managed-nodes', 'value': '1'}]}], 'azureResourceId': 'azure_record_id_123', 'vendorProductCode': 'offerPlanId'}, 'partnerIdentities': {'azureSubscriptionId': '0fa44b14-ef8b-4e9e-8358-1e4230250d73'}}, 'subscription_id': '61412512'}
      2025-03-28 06:54:01 INFO Creating contract with payload : {'content': [{'rhAccountId': '18939572', 'sourcePartner': 'azure_marketplace', 'entitlementDates': {'startDate': '2025-02-26T06:54:01.441455Z', 'endDate': '2025-04-02T06:54:01.441464Z'}, 'rhEntitlements': [{'subscriptionNumber': '123456789', 'sku': 'MCT4701'}], 'purchase': {}, 'partnerIdentities': {'azureSubscriptionId': '0fa44b14-ef8b-4e9e-8358-1e4230250d73'}}]}, 
      2025-03-28 06:54:01 INFO Subscription Search Wiremock Endpoint Enabled
      2025-03-28 06:54:01 INFO POST request response url: http://swatch-contracts-service:8000/api/swatch-contracts/internal/rpc/sync/contracts/18939572?is_pre_cleanup=False, status_code: 400
      2025-03-28 06:54:01 INFO Trace log using : 00-ae2903f2f1b2ce19eabbb2793ab3a87e-fdfc2de8e9ba9665-01  
      2025-03-28 06:54:01 INFO Not valid JSON output. Returning plain text message.
      2025-03-28 06:54:01 INFO Contracts synced for OrgID: 18939572
      2025-03-28 06:54:01 INFO ROLLBACK
      --------------------------------- Captured Out ---------------------------------
      2025-03-28 06:54:01,393 INFO sqlalchemy.engine.Engine select version()
      2025-03-28 06:54:01,393 INFO sqlalchemy.engine.Engine [raw sql] {}
      2025-03-28 06:54:01,394 INFO sqlalchemy.engine.Engine select current_schema()
      2025-03-28 06:54:01,394 INFO sqlalchemy.engine.Engine [raw sql] {}
      2025-03-28 06:54:01,395 INFO sqlalchemy.engine.Engine show standard_conforming_strings
      2025-03-28 06:54:01,395 INFO sqlalchemy.engine.Engine [raw sql] {}
      2025-03-28 06:54:01,396 INFO sqlalchemy.engine.Engine BEGIN (implicit)
      2025-03-28 06:54:01,399 INFO sqlalchemy.engine.Engine truncate table contracts cascade;
      2025-03-28 06:54:01,399 INFO sqlalchemy.engine.Engine [generated in 0.00027s] {}
      2025-03-28 06:54:01,418 INFO sqlalchemy.engine.Engine truncate table subscription cascade;
      2025-03-28 06:54:01,418 INFO sqlalchemy.engine.Engine [generated in 0.00029s] {}
      2025-03-28 06:54:01,842 INFO sqlalchemy.engine.Engine ROLLBACK
      

      The problem is that the service returns 404 Not Found because the entity does not exist yet. The following test execution works fine.

              rhn-engineering-wpoteat William Poteat
              jcarvaja@redhat.com Jose Carvajal Hilario
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: